0
|
1 from struct import pack, unpack
|
|
2 from pytouhou.utils.helpers import read_string
|
|
3
|
|
4
|
|
5
|
|
6 class Object(object):
|
|
7 def __init__(self):
|
|
8 self.header = (b'\x00') * 28 #TODO
|
|
9 self.quads = []
|
|
10
|
|
11
|
|
12
|
|
13 class Stage(object):
|
|
14 def __init__(self):
|
|
15 self.name = ''
|
|
16 self.bgms = (('', ''), ('', ''), ('', ''))
|
|
17 self.objects = []
|
|
18 self.object_instances = []
|
|
19 self.script = []
|
|
20
|
|
21
|
|
22 @classmethod
|
|
23 def read(cls, file):
|
|
24 stage = Stage()
|
|
25
|
|
26 nb_objects, nb_faces = unpack('<HH', file.read(4))
|
|
27 object_instances_offset, script_offset = unpack('<II', file.read(8))
|
|
28 if file.read(4) != b'\x00\x00\x00\x00':
|
|
29 raise Exception #TODO
|
|
30
|
|
31 stage.name = read_string(file, 128, 'shift-jis')
|
|
32
|
|
33 bgm_a = read_string(file, 128, 'shift-jis')
|
|
34 bgm_b = read_string(file, 128, 'shift-jis')
|
|
35 bgm_c = read_string(file, 128, 'shift-jis')
|
|
36 bgm_d = read_string(file, 128, 'shift-jis')
|
|
37
|
|
38 bgm_a_path = read_string(file, 128, 'ascii')
|
|
39 bgm_b_path = read_string(file, 128, 'ascii')
|
|
40 bgm_c_path = read_string(file, 128, 'ascii')
|
|
41 bgm_d_path = read_string(file, 128, 'ascii')
|
|
42
|
|
43 stage.bgms = [(bgm_a, bgm_a_path), (bgm_b, bgm_b_path), (bgm_c, bgm_c_path), (bgm_d, bgm_d_path)] #TODO: handle ' '
|
|
44
|
|
45 # Read object definitions
|
|
46 offsets = unpack('<%s' % ('I' * nb_objects), file.read(4 * nb_objects))
|
|
47 for offset in offsets:
|
|
48 obj = Object()
|
|
49 obj.header = file.read(28) #TODO: this has to be reversed!
|
|
50 while True:
|
|
51 unknown, size = unpack('<HH', file.read(4))
|
|
52 if unknown == 0xffff:
|
|
53 break
|
|
54 if size != 0x1c:
|
|
55 raise Exception #TODO
|
|
56 script_index, _padding, x, y, z, width, height = unpack('<HHfffff', file.read(24))
|
|
57 #TODO: store script_index, x, y, z, width and height
|
|
58 obj.quads.append((script_index, x, y, z, width, height))
|
|
59 stage.objects.append(obj)
|
|
60
|
|
61
|
|
62 # Read object usages
|
|
63 file.seek(object_instances_offset)
|
|
64 while True:
|
|
65 obj_id, unknown, x, y, z = unpack('<HHfff', file.read(16))
|
|
66 if (obj_id, unknown) == (0xffff, 0xffff):
|
|
67 break
|
|
68 if unknown != 256:
|
|
69 raise Exception #TODO
|
|
70 stage.object_instances.append((stage.objects[obj_id], x, y, z))
|
|
71
|
|
72
|
|
73 # Read other funny things (script)
|
|
74 file.seek(script_offset)
|
|
75 while True:
|
|
76 frame, message_type, size = unpack('<IHH', file.read(8))
|
|
77 if (frame, message_type, size) == (0xffffffff, 0xffff, 0xffff):
|
|
78 break
|
|
79 if size != 0x0c:
|
|
80 raise Exception #TODO
|
|
81 data = file.read(12)
|
|
82 #TODO: do something useful with this
|
|
83
|
|
84 return stage
|
|
85
|