aboutsummaryrefslogtreecommitdiff
path: root/parse-brlyt.py
blob: d79b38279e685ee808dc90e587e04e9646681ffc (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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')
            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.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, 'material', start = offset - 8)
            #vars['materials'].append(mat)
            flags = mat['flags']
            
            assert mpos == 0x40 + offset - 8
        
            
            #print hex(struct.unpack('>I', chunk[mpos:mpos+4])[0])
            
            # I believe material is:
            # 0x40, followed by
            # 4 * flags[28-31], followed by
            
            
            mat['ua1'], mpos = get_array(chunk, mpos, bit_extract(flags, 28, 31), 4)
            
            # 0x14 * flags[24-27], followed by
            
            mat['ua2'], mpos = get_array(chunk, mpos, bit_extract(flags, 24, 27), 0x14)
            
            # 4*flags[20-23], followed by
            
            mat['ua3'], mpos = get_array(chunk, mpos, bit_extract(flags, 20, 23), 4)
            
            # 4 * flags[6]
            
            mat['ua4'], mpos = get_opt(chunk, mpos, bit_extract(flags, 6), 4)
            
            # 4 * flags[4]
            
            mat['ua5'], mpos = get_opt(chunk, mpos, bit_extract(flags, 4), 4)
            
            # 4 * flags[19]
            
            mat['ua6'], mpos = get_opt(chunk, mpos, bit_extract(flags, 19), 4)
            
            # 0x14 * flags[17-18]
            
            mat['ua7'], mpos = get_array(chunk, mpos, bit_extract(flags, 17, 18), 0x14)
            
            # 4 * flags[14-16]
            
            mat['ua8'], mpos = get_array(chunk, mpos, bit_extract(flags, 14, 16), 4)
            
            # 0x10 * flags[9-13]
            
            mat['ua8'], mpos = get_array(chunk, mpos, bit_extract(flags, 9, 13), 0x10)
            
            # 4 * flags[8], these are bytes btw
            
            mat['uaa'], mpos = get_opt(chunk, mpos, bit_extract(flags, 8), 4)
            
            # 4 * flags[7]
            
            mat['uab'], mpos = get_opt(chunk, mpos, bit_extract(flags, 7), 4)

            if n < vars['num'] - 1:
                next_offset, = struct.unpack('>I', chunk[pos+4:pos+8])
                if next_offset - 8 != mpos:
                    mat['~_insane'] = next_offset - 8 - mpos # Extra shit we didn't parse :(

            vars['materials'].append(mat)
            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))