File seeking DLL issue, not functioning correctly.

Hello,

I recently moved from using the rather primitive Game Maker tool, and I'm trying to port some of my old works into C++. I have a little experience in C++, since I've made one very simple project before.

Right now, I'm attempting the conversion of scripts for seeking files(from bigger files) of an old game, and extracting them into separate, smaller files. -- it's a thing I put together for a image editing program, so I'd like the speed of C++ for this. But, it doesn't seem to work.

It's built for use with a DLL, since I still plan on using Game Maker for some stuff. That's why I'm using a 'double' instead of something that would probably be more efficient... Any pointers in the right direction = appreciated.

The problem is, it doesn't actually write a file properly... It'll write bytes to a file, but not correctly... Let us say I have a 4BPP image, and it's 8.03KBs, it'll only write half of that to the new file, and it won't be correct...

By the way, warning -- I'm not a very tidy coder. I tried to comment what I understand the best I could, but since I'm still kinda new to C++, and my knowledge is slim...

- Non API -
1
2
3
4
5
6
7
8
inline char writeByte(FILE* File,char Byte)
{
    return putc(Byte, File);
}
inline char readByte(FILE* File)
{
    return getc(File);
}


- API -
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#define export extern "C" __declspec (dllexport)

export double _KFTIMRip(char File[], char Target[])
{
	FILE *Source, *Source2, *Path;
	int byteCheck1, byteCheck2, byteCheck3, byteCheck4, byteCheck5, byteCheck6; //init some ints
	int fileStart, fileEnd, fileSize, fileCount, i, Byte; //init some more ints

	//Makes sure we're not ripping thin air, send 0 back, for error handling if we are...
	if(File == NULL)
	{
		return 0;
	}
	Source = fopen(File, "rb"); //Opens the file we want to seek...

	//Start a while look, which goes through the bytes of the file...
	while(!feof(Source))
	{
		byteCheck1 = readByte(Source);
		byteCheck2 = readByte(Source);
		byteCheck3 = readByte(Source);
		byteCheck4 = readByte(Source);
		byteCheck5 = readByte(Source);
		byteCheck6 = readByte(Source);

		//Provisional file checking...
		if(byteCheck1 == byteCheck4 && byteCheck2 == byteCheck5 && byteCheck3 == byteCheck6)
		{
			if(byteCheck3*4 == byteCheck6 && byteCheck2*4 == byteCheck3)
			{
				if(byteCheck1 != 0 && byteCheck2 != 0 && byteCheck3 != 0 && byteCheck4 != 0 && byteCheck5 != 0 && byteCheck6 != 0) // Make sure they're not padding bytes... 
				{//And now we've got a KFTim!
					if(fileStart == 0) //Check if the file start is equal to 0, if it is, set it to something else.
					{
						fileStart = ftell(Source)-64; //sets it to the start of the file, we know a KFTim header is 64 bytes long, so we minus that from the current pos.
					}
					if(fileEnd == 0) //Check if the file end is equal to 0, if it is, set it to something else.
					{
						fileEnd = (fileStart+64) + ((byteCheck5*4)*(byteCheck6)); //Sets it to: current position + header, + imagewidth*imageheight.
					}
					fileSize = (fileEnd - fileStart); //Gets the files size
					fileCount++; //add one to the file counter
					fseek(Source,(size_t) fileStart, SEEK_SET); //Set the file position
					
					//Full speed ahead!
					Source2 = fopen(Target, "wb"); //Opens the file we're writing.
					while(i < fileEnd) //loops, until i is = or > fileEnd (end of file we want to rip)
					{
						Byte = readByte(Source); //reads a byte
						writeByte(Source2 ,Byte); //writes the byte we just read to the new file.
						i++; //adds 1 to i,
					}
					fclose(Source2); //Closes the file we were writing too.
					fileStart = 0; //Sets the file start back to 0
					fileEnd = 0; //Sets the fileEnd back to 0
				}
			}
		}
	}
	fclose(Source); //Closes the file we were checking
	return (double) fileCount; //Returns the amount of files it found.
} 


I had a feeling it was to do with my checking methods, because in Game Maker I was reading shorts, longs AND bytes, instead of just single characters from the binary for the checks (the byteCheck1,2,3 etc part)..

I'd also like your opinions and criticism on my code, I know it can't be that great since I rushed it in about 15 minutes. But I like those since it helps me get an idea on what I'm doing right or wrong... Probably doing a lot more wrong than I am right.
Last edited on
Topic archived. No new replies allowed.