Question

I have a save game file that I am trying to parse out all of the characters attributes by reading the file using hex offsets. I'm able to get all strings out properly since that plain text but I am having issues with parsing the binary portions that I am working with.

I'm pretty sure that I'm reading in the right data but when I unpack the string I am getting unexpected (incorrect) output

The file I'm working with is www.retro-gaming-world.com/SAVE.DAT

import struct
infile = open('SAVE.DAT','rb')
try:
    buff = infile.read()
finally:
    infile.close

infile.seek(0x00,0)
print "Save Signature: " + infile.read(0x18)
print "Save Version: " + str(struct.unpack('>i',buff[0x18:0x18+4])[0])
infile.seek(0x1C,0)
print "The letter R: " + infile.read(0x01)
infile.seek(0x1D,0)
print "Character Name: " + infile.read(0x20)
infile.seek(0x3D,0)
print "Save Game Name: " + infile.read(0x1E)
print "Save game day: " + str(struct.unpack('>i',buff[0x5B:0x5B+4])[0])
print "Save game month: " + str(struct.unpack('>i',buff[0x5D:0x5D+4])[0])
print "Save game year: " + str(struct.unpack('>i', buff[0x5F:0x5F+4])[0])

I'm having two different issues, either the wrong data is returned or when I try to unpack some of the fields I get an error that the string isn't long enough, I can read in more but the day month and year are only 2 and 4 bytes respectively and are integers, I'm not sure I'm going about this the right way, I believe I'm fetching the right fields but think I am unpacking or handling the data incorrectly some where if not completely.

version should return 0100 day should return 21 month should return 09 year should return 2013

What exactly am I getting wrong hrere, is there another way or a better way to go about parsing the fields from the binary?

Was it helpful?

Solution

The error is, that although the values are of integer type, they only have the length of 2, being an unsigned short in C. Thus, you have to read them as

struct.unpack('>H',buff[0x5B:0x5B+2])[0])

and so on. signed or unsigned does not seem to make a difference here. If available, check the documentation of the save file, it should be denoted there which is appropriate. If not, good luck trying (itertools can be helpful).

For more details of types, check the table on the Python documentation for structs

As a big fan of Fallout 1 and 2 I do wish you good luck and lots of success with the project (-;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top