To read a binary file in python we need a basic code:
import struct
binfile = open('FILENAME', 'rb')
try:
bin = binfile.read()
finally:
binfile.close()
Where bin variable contains content of the whole file. On IESDP we have
IE File Formats where each file type has full description. We can read part of the file using given OFFSET -
print bin[OFFSET:OFFSET+SIZE]IF the data is binary - we have to convert it.
-
resref, char array - direct acces, non binary
-
dword, strref - convert with struct
print struct.unpack('i', bin[0x0014:0x0014+4])
-
word - convert with struct
print struct.unpack('h', bin[0x0080:0x0080+2])
-
byte, unsigned byte, char - convert with struct
print struct.unpack('b', bin[0x000b:0x000b+1])
note 1: To write binary data from a given text data use struct.pack with the same format (i, h, b).
note 2: struct.unpack returns a tuple. To acces a value:
a = struct.unpack('i', tekst[0x0014:0x0014+4])
print a[0]
Each tuple element has a number (from zero). This code will work everywhere python works. Should also work on jython (python in java) and IronPython (python in .NET). If someone wants to make a GUI app - on windows
py2exe can help make an *exe from the Application, and for GUI libs: python tkinter is good for Mac and Windows, looks terible on Linux/Unix. pyGTK+ - good for all 3 OS. pyQT - easy, good for Unix/Linux and Mac, on Windows - from PyQT4 (in development)
For compressed biffs there is zlib module
CRE example:
import struct
plik = open('beheld01.cre', 'rb')
try:
tekst = plik.read()
finally:
plik.close()
print 'File Signature: ' + tekst[0x0000:0x0000+4]
print 'Version: ' + tekst[0x0004:0x0004+4]
print 'Long Creature name: ' + str(struct.unpack('i', tekst[0x0008:0x0008+4])[0])
print 'Short Creature name: ' + str(struct.unpack('i', tekst[0x000c:0x000c+4])[0])
print 'Creature flags: ' + str(struct.unpack('i', tekst[0x0010:0x0010+4])[0])
print 'XP for killing: ' + str(struct.unpack('i', tekst[0x0014:0x0014+4])[0])
print 'CRE Level: ' + str(struct.unpack('i', tekst[0x0018:0x0018+4])[0])
print 'Current HP: ' + str(struct.unpack('h', tekst[0x0024:0x0024+2])[0])