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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
import sys, struct, re
f = open(sys.argv[1], 'rb')
rlyt = f.read()
f.close()
from ethyl import formats
for (name, definition) in formats.items():
definition = re.sub(re.compile('//.*$', re.M), '', definition)
definition = definition.strip()
definition = re.sub('\n[\n ]+', '\n', definition)
df = [[j.strip() for j in i.strip().split()] for i in definition.split('\n')]
formats[name] = df
def pr_dict(data, start=''):
for a in sorted(data.keys()):
b = data[a]
print '%s%s: %s' % (start, str(a), hex(b) if type(b) in (int, long) else repr(b))
types = {
'u8': 'B',
'u16': 'H',
'u32': 'I',
'u64': 'Q',
's8': 'b',
's16': 'h',
's32': 'i',
's64': 'q',
'float': 'f',
'f32': 'f',
'double': 'd',
'f64': 'd',
'bool': 'c',
'char': 'c',
}
def parse_var(pos, typ, chunk):
if not types.has_key(typ):
raise Exception('What\'s a %s?' % typ)
fmt = '>' + types[typ]
size = struct.calcsize(fmt)
return (pos + size), struct.unpack(fmt, chunk[pos:pos + size])[0]
def nullterm(str_plus):
z = str_plus.find('\0')
if z != -1:
return str_plus[:z]
else:
return str_plus
def parse_data(chunk, definition, prefix=None, start=0):
df = formats[definition][:]
#print df, repr(definition)
print df
etc = df[-1][0][:3] == 'etc'
etcn = etc and df.pop() == ['etc!']
pos = start
ret = {}
for typ, name in df:
m = re.match('^([^\[]+)\[([a-zA-Z0-9]+)\]$', name)
if m:
name = m.group(1)
size = eval(m.group(2))
if typ == 'char':
var = chunk[pos:pos+size].rstrip(chr(0))
pos += size
else:
var = []
for n in xrange(size):
pos, varn = parse_var(pos, typ, chunk)
var.append(varn)
else:
pos, var = parse_var(pos, typ, chunk)
if ret.has_key(name):
try:
ret[name].append(var)
except AttributeError:
ret[name] = [ret[name], var]
else:
ret[name] = var
#print hex(pos)
if pos > len(chunk):
raise Exception('too much!')
if pos < len(chunk):
if etc and not etcn:
ret['etc'] = repr(chunk[pos:])
elif not etc:
raise Exception('Incomplete handling...')
if prefix is not None:
ret2 = {}
for (k, v) in ret.items():
ret2[prefix + '.' + k] = v
ret = ret2
return (ret, pos) if etcn else ret
def iff_to_chunks(z):
pos = 0
chunks = []
while pos < len(z):
cc = z[pos:pos+4]
length, = struct.unpack('>I', z[pos+4:pos+8])
#print cc
#print hex(length)
data = z[pos+8:pos+length]
pos += length
chunks.append((cc, data))
return chunks
assert rlyt[0:8] == 'RLYT\xfe\xff\x00\x08'
offs, = struct.unpack('>h', rlyt[0xc:0xe])
ch = iff_to_chunks(rlyt[offs:])
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')
if typ == 'txt1':
vars2, pos = parse_data(chunk[pos:], 'text', '~text')
txt = chunk[vars2['~text.name_offs'] - 8:]
vars2['~text.text'] = unicode(txt, 'utf_16_be').rstrip('\x00')
vars.update(vars2)
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', 'mat1'):
things = {'txl1': 'textures', 'fnl1': 'files', 'mat1': 'materials'}[typ]
vars, pos = parse_data(chunk, 'numoffs')
vars[things] = []
pos += vars['offs']
del vars['offs'] # unneeded
for n in xrange(vars['num']):
if typ == 'mat1':
offset, = struct.unpack('>I', chunk[pos:pos+4])
pos += 4
else:
offset, unk = struct.unpack('>II', chunk[pos:pos+8])
pos += 8
name = nullterm(chunk[offset - 8:])
vars[things].append(name if typ == 'mat1' else {'name': name, 'unk': unk})
if typ == 'mat1':
materials = vars['materials']
elif typ in ('grs1', 'pas1'):
indent += 1
elif typ in ('gre1', 'pae1'):
indent -= 1
else:
print repr(chunk)
if vars: pr_dict(vars, ' ' * indent + ' ')
#print hex(len(ch))
|