Posted by: Riklaunim
« on: August 18, 2006, 05:29:49 PM »If someone is interested: Reading Infinity Engine files in Django - a python web framework
import struct
import zlib
# we need a SAV file in the same dir
plik = open('baldur.sav', 'rb')
try:
tekst = plik.read()
finally:
plik.close()
a = 0x0004 + 4
# decompress everything until end of file
while a < len(tekst):
lenght = struct.unpack('i', tekst[a:a+4])[0]
print 'Lenght of filename: ' + str(lenght)
# get the filename - it has various lenght
filename = str(tekst[a+4:a+16])
# split into 2 parts using dot as splitter
file_name = filename.split('.')
#get the extension+rubbish
ext = file_name[1]
# kill the rubbish, read only 3 chars :)
file_name = file_name[0] +'.'+ ext[0:3]
print 'Filename: ' + file_name
decomp_size = struct.unpack('i', tekst[a+0x0004+lenght:a+0x0004+lenght+4])[0]
print 'Decompressed size: ' + str(decomp_size)
comp_size = struct.unpack('i', tekst[a+0x0008+lenght:a+0x0008+lenght+4])[0]
print 'Compressed size: ' + str(comp_size)
if decomp_size != 0:
code = tekst[a+0x000c+lenght:a+0x000c+lenght+comp_size]
open(file_name, 'wb').write(zlib.decompress(code))
print 'Saved Raw data as ' + file_name
a = a+0x000c+lenght+comp_size
print "----------------------------"
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]print struct.unpack('i', bin[0x0014:0x0014+4])
- word - convert with structprint struct.unpack('h', bin[0x0080:0x0080+2])
- byte, unsigned byte, char - convert with structprint struct.unpack('b', bin[0x000b:0x000b+1])
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) 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])