aboutsummaryrefslogtreecommitdiff
path: root/parse-brlyt.py
blob: b69eba3eb27448473cd669523aa94828aa6c5257 (plain) (blame)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import sys, struct, re
from ethyl import *

f = open(sys.argv[1], 'rb')
rlyt = f.read()
f.close()

assert rlyt[0:8] == 'RLYT\xfe\xff\x00\x08'
offs, nel = struct.unpack('>hh', rlyt[0xc:0x10])
ch = iff_to_chunks(rlyt[offs:])

assert len(ch) == nel

indent = 0
for typ, chunk in ch:
    print '    ' * indent + typ + ' ' + hex(len(chunk))
    vars = None
    if typ in ('pic1', 'pan1', 'bnd1', 'wnd1', 'txt1'):  
        vars, pos = parse_data(chunk, 'pane')
        assert chunk[:pos] == unparse_data(vars, 'pane') # for testing
        if typ == 'txt1':
            vars2, pos = parse_data(chunk[pos:], 'text', '~text')
            os = vars2['~text.name_offs'] - 8
            txt = chunk[os:os + vars2['~text.len2']]
            vars2['~text.text'] = unicode(txt, 'utf_16_be').rstrip('\x00')
            vars2['~text.material'] = materials[vars2['~text.mat_off']]
            vars.update(vars2)
            #print vars['~text.cnt2'], len(vars2['~text.text']), len(txt), repr(txt)
        elif typ == 'pic1':
            vars2, pos = parse_data(chunk, 'pic', prefix='~pic', start=pos)
            vars2['~pic.material'] = materials[vars2['~pic.mat_off']]
            vars2['~pic.texcoords'] = []
            for n in xrange(vars2['~pic.num_texcoords']):
                vars2['~pic.texcoords'].append(struct.unpack('>ffffffff', chunk[pos:pos+0x20]))
                pos += 0x20
            assert pos == len(chunk)
            vars.update(vars2)
    elif typ == 'lyt1':
        vars = parse_data(chunk, 'lytheader')
    elif typ == 'grp1':
        vars, pos = parse_data(chunk, 'group')
        vars['subs'] = []
        for n in xrange(vars['numsubs']):
            vars['subs'].append(nullterm(chunk[pos:pos+16]))
            pos += 16
    elif typ in ('txl1', 'fnl1'):
        things = {'txl1': 'textures', 'fnl1': 'files'}[typ]
        vars, pos = parse_data(chunk, 'numoffs')
        vars[things] = []
        pos += vars['offs']
        bpos = pos
        del vars['offs'] # unneeded
        for n in xrange(vars['num']):
            offset, unk = struct.unpack('>II', chunk[pos:pos+8])
            name = nullterm(chunk[offset + bpos:])
            vars[things].append({'name': name, 'unk': unk})
            pos += 8
    elif typ == 'mat1':
        vars, pos = parse_data(chunk, 'numoffs')
        vars['materials'] = []
        pos += vars['offs']
        bpos = pos
        del vars['offs']
        for n in xrange(vars['num']):
            offset, = struct.unpack('>I', chunk[pos:pos+4])
            mat, mpos = parse_data(chunk[offset - 8:], 'material')
            #vars['materials'].append(mat)
            flags = mat['flags']
            
            offs1 = bit_extract(flags, 17, 18) * 0x14
            offs2 = (bit_extract(flags, 28, 31) * 4) + 0x40
            offs3 = offs2 + bit_extract(flags, 24, 27) * 0x14 + bit_extract(flags, 20, 23) * 4
            offs4 = bit_extract(flags, 8, 11) * 4 * 0x14
            print map(hex, [offs1, offs2, offs3, offs4])
            
            pr_dict(mat, '             ')
            print
            pos += 4
        
    elif typ in ('grs1', 'pas1'):
        indent += 1
    elif typ in ('gre1', 'pae1'):
        indent -= 1
    else:
        print 'Unhandled type %s' % typ
        print repr(chunk)
    if vars: pr_dict(vars, '    ' * indent + '    ')
#print hex(len(ch))