Changes between Initial Version and Version 1 of reading_baldurs_gate_binary_files


Ignore:
Timestamp:
08/18/2006 05:18:57 PM (13 years ago)
Author:
riklaunim@…
Comment:

Page Creation

Legend:

Unmodified
Added
Removed
Modified
  • reading_baldurs_gate_binary_files

    v1 v1  
     1Python has “batteries included” and as we all know Django is written in this language. Python has struct module (http://docs.python.org/lib/module-struct.html) which can do many interesting things.
     2
     3If you played in Baldurs Gate, Icewind Dale or Planescape Torment then this mini tutorial may be quite interesting for you – we will read data from a binary file – Baldurs Gate II CHR file containing data abut players character. Before we continue check out:
     4
     5[http://forums.pocketplane.net/index.php/topic,21741.0.html pocketplane.net forums] – How to read data from infinity engine files
     6
     7[http://iesdp.gibberlings3.net/ieformats/ieformats.htm IE File Formats list] – specification of Infinity Engine files, we will use CHR v2
     8
     9
     10'''Requirements''': you need a working django, can be development server. We will create one view and one template.
     11
     12- Create a view and hook it up in the urls.py:
     13{{{
     14from django.shortcuts import render_to_response
     15import struct
     16
     17def view_char(request):
     18        file = open('media/Mag.chr', 'rb')
     19        try:
     20                text = file.read()
     21        finally:
     22                file.close()
     23        print text[0x0000:0x0000+4]
     24        print text[0x0004:0x0004+4]
     25        print text[0x0008:0x0008+32]
     26        return render_to_response('char.html')
     27}}}
     28Where '''('media/Mag.chr')''' is a path to the CHR file. Create '''char.html''' template with some pointless text and open the view in the browser. In the terminal where the development server is running you should see:
     29{{{
     30CHR
     31V2.0
     32Daria
     33}}}
     34We have just read the basic data, and reading more isn't any problem (only problem is to specify the locations). Looking at CHR V2 and CRE V1 specification we can read more data:
     35{{{
     36from django.shortcuts import render_to_response
     37import struct
     38
     39def view_char(request):
     40        file = open('media/Mag.chr', 'rb')
     41        try:
     42                text = file.read()
     43        finally:
     44                file.close()
     45        char = {}
     46        if str(text[0x0000:0x0000+4]) == 'CHR ':
     47                char['name'] = str(text[0x0008:0x0008+32])
     48                a = struct.unpack('i', text[0x0028:0x0028+4]) # offset to CRE "file"
     49                a = 0x0000+a[0]
     50                if str(text[a:a+4]) == 'CRE ' and str(text[a+4:a+8]) == 'V1.0':
     51                        char['xp'] = str(struct.unpack('i', text[a+0x0018:a+0x0018+4])[0])
     52                        char['hp'] = str(struct.unpack('h', text[a+0x0024:a+0x0024+2])[0])
     53                        char['pic'] = str(text[a+0x0034:a+0x0034+8])
     54                        char['str'] = str(struct.unpack('b', text[a+0x0238:a+0x0238+1])[0]) #str
     55                        char['int'] = str(struct.unpack('b', text[a+0x023a:a+0x023a+1])[0]) #int
     56                        char['wis'] = str(struct.unpack('b', text[a+0x023b:a+0x023b+1])[0]) #wis
     57                        char['dex'] = str(struct.unpack('b', text[a+0x023c:a+0x023c+1])[0]) #dex
     58                        char['con'] = str(struct.unpack('b', text[a+0x023d:a+0x023d+1])[0]) #con
     59                        char['char'] = str(struct.unpack('b', text[a+0x023e:a+0x023e+1])[0]) #char
     60        # better error checking/reporting would be nice
     61        return render_to_response('char.html', {'char': char})
     62}}}
     63And the template:
     64{{{
     65<h1>{{ char.name }}</h1>
     66<b>Hit Points</b>: {{ char.hp }}<br>
     67<b>Experience Points</b>: {{ char.xp }}<br>
     68<b>Strenght</b>: {{ char.str }}<br>
     69<b>Dexterity</b>: {{ char.dex }}<br>
     70<b>Constitution</b>: {{ char.com }}<br>
     71<b>Inteligence</b>: {{ char.int }}<br>
     72<b>Wisdom</b>: {{ char.wis }}<br>
     73<b>Charisma</b>: {{ char.char }}
     74}}}
     75
     76
     77'''The results''':
     78
     79- [http://www.fotosik.pl/pokaz_obrazek/1caaa78b57139425.html Screenshot 1]
     80
     81- [http://www.fotosik.pl/pokaz_obrazek/d83c09db39cc1a70.html Screenshot 2]
     82
     83You can read and write to any binary file with python – you just need the specification which shows where the data is located and what type it is :) Note that there aren't available specifications for most proprietary file formats.
Back to Top