-
Notifications
You must be signed in to change notification settings - Fork 3
/
scenebin.py
66 lines (58 loc) · 1.77 KB
/
scenebin.py
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
import struct
from struct import unpack
import sys, io, pathlib
from warnings import warn
from common import calcKeyCode
def readString(fin):
length, = unpack('>H', fin.read(2))
return fin.read(length).decode('shift-jis')
def stylecolor(c):
r, g, b = c[:3]
if (r*r + g*g + b*b) < 48768:
stylecode = 48
else:
stylecode = 38
mr = min(r, 255)
mg = min(g, 255)
mb = min(b, 255)
s = "\x1b[%d;2;%d;%d;%dm#%02X%02X%02X\x1b[0m"%(stylecode, mr, mg, mb, r, g, b)
for x in c[3:]:
s += " %02X"%x
return s
def readsection(fin):
sectionlength, namehash = unpack('>IH', fin.read(6))
#print("len", sectionlength, "hash", namehash)
#print("at", fin.tell())
name = readString(fin)
assert namehash == calcKeyCode(name), (hex(namehash), name)
#print("Found a", name)
if name in classes.registeredObjectClasses:
#print("Constructing a", classes.registeredObjectClasses[name])
o = classes.registeredObjectClasses[name]()
else:
warn("Unknown class {}".format(name))
o = classes.TNameRef()
o.namehash = namehash
o.name = name
assert name.isidentifier()
x = io.BytesIO(fin.read(sectionlength-8-len(name)))
#print("Reading")
try:
o.read(x)
except struct.error as e:
warn("Couldn't load a {}: {}".format(name, e))
o.extra = x.read()
#print("Got", o)
return o
import classes
if __name__ == "__main__":
def printobj(o, i=0):
try: print(' '*i, o)
except Exception as e: print(' '*i, e)
if o.extra:
print(' '*(i+1), o.extra.hex())
if hasattr(o, "objects"):
for o2 in o.objects:
printobj(o2, i+1)
o = readsection(open(sys.argv[1], 'rb'))
printobj(o)