annotate pytouhou/formats/std.py @ 136:d3005ebe797a

Fix MSG parsing, use the offsets instead of trying to relate them to the actual data.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 23 Sep 2011 10:05:20 -0700
parents c596a1a69402
children 88361534c77e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
1 # -*- encoding: utf-8 -*-
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
2 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
4 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
6 ## it under the terms of the GNU General Public License as published
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
7 ## by the Free Software Foundation; version 3 only.
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
8 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
9 ## This program is distributed in the hope that it will be useful,
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
12 ## GNU General Public License for more details.
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
13 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
14
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
15
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
16 from struct import pack, unpack, calcsize
58
3da4de9decd0 Use logging module
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
17 from pytouhou.utils.helpers import read_string, get_logger
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18
58
3da4de9decd0 Use logging module
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
19 logger = get_logger(__name__)
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
22 class Model(object):
111
340fcda8e64a Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents: 110
diff changeset
23 def __init__(self, unknown=0, bounding_box=None, quads=None):
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
24 self.unknown = 0
111
340fcda8e64a Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents: 110
diff changeset
25 self.bounding_box = bounding_box or (0., 0., 0.,
340fcda8e64a Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents: 110
diff changeset
26 0., 0., 0.)
340fcda8e64a Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents: 110
diff changeset
27 self.quads = quads or []
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 class Stage(object):
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
32 _instructions = {0: ('fff', 'set_viewpos'),
111
340fcda8e64a Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents: 110
diff changeset
33 1: ('BBBxff', 'set_fog'),
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
34 2: ('fff', 'set_viewpos2'),
118
c596a1a69402 Fix std parsing
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
35 3: ('Ixxxxxxxx', 'start_interpolating_viewpos2'),
c596a1a69402 Fix std parsing
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
36 4: ('Ixxxxxxxx', 'start_interpolating_fog')}
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
37
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 94
diff changeset
38 def __init__(self):
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39 self.name = ''
111
340fcda8e64a Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents: 110
diff changeset
40 self.bgms = (('', ''), ('', ''), ('', ''), ('', ''))
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
41 self.models = []
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42 self.object_instances = []
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 self.script = []
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 @classmethod
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 94
diff changeset
47 def read(cls, file):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 94
diff changeset
48 stage = Stage()
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
50 nb_models, nb_faces = unpack('<HH', file.read(4))
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51 object_instances_offset, script_offset = unpack('<II', file.read(8))
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52 if file.read(4) != b'\x00\x00\x00\x00':
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53 raise Exception #TODO
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
55 stage.name = read_string(file, 128, 'shift_jis')
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
57 bgm_a = read_string(file, 128, 'shift_jis')
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
58 bgm_b = read_string(file, 128, 'shift_jis')
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
59 bgm_c = read_string(file, 128, 'shift_jis')
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
60 bgm_d = read_string(file, 128, 'shift_jis')
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
62 bgm_a_path = read_string(file, 128, 'ascii')
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63 bgm_b_path = read_string(file, 128, 'ascii')
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64 bgm_c_path = read_string(file, 128, 'ascii')
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 bgm_d_path = read_string(file, 128, 'ascii')
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67 stage.bgms = [(bgm_a, bgm_a_path), (bgm_b, bgm_b_path), (bgm_c, bgm_c_path), (bgm_d, bgm_d_path)] #TODO: handle ' '
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
69 # Read model definitions
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
70 offsets = unpack('<%s' % ('I' * nb_models), file.read(4 * nb_models))
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71 for offset in offsets:
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
72 model = Model()
111
340fcda8e64a Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents: 110
diff changeset
73 file.seek(offset)
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
74 id_, unknown, x, y, z, width, height, depth = unpack('<HHffffff', file.read(28))
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
75 model.unknown = unknown
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
76 model.bounding_box = x, y, z, width, height, depth #TODO: check
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
77 while True:
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
78 unknown, size = unpack('<HH', file.read(4))
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
79 if unknown == 0xffff:
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
80 break
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81 if size != 0x1c:
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 raise Exception #TODO
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
83 script_index, x, y, z, width, height = unpack('<Hxxfffff', file.read(24))
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
84 model.quads.append((script_index, x, y, z, width, height))
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
85 stage.models.append(model)
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
88 # Read object usages
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
89 file.seek(object_instances_offset)
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
90 while True:
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
91 obj_id, unknown, x, y, z = unpack('<HHfff', file.read(16))
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
92 if (obj_id, unknown) == (0xffff, 0xffff):
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
93 break
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
94 if unknown != 256:
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
95 raise Exception #TODO
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
96 stage.object_instances.append((obj_id, x, y, z))
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
97
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
98
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
99 # Read other funny things (script)
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
100 file.seek(script_offset)
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
101 while True:
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
102 frame, opcode, size = unpack('<IHH', file.read(8))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
103 if (frame, opcode, size) == (0xffffffff, 0xffff, 0xffff):
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
104 break
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
105 if size != 0x0c:
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
106 raise Exception #TODO
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
107 data = file.read(size)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
108 if opcode in cls._instructions:
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
109 args = unpack('<%s' % cls._instructions[opcode][0], data)
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 1
diff changeset
110 else:
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 1
diff changeset
111 args = (data,)
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
112 logger.warn('unknown opcode %d', opcode)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
113 stage.script.append((frame, opcode, args))
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
114
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
115 return stage
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
116
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
117
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
118 def write(self, file):
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
119 model_offsets = []
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
120 second_section_offset = 0
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
121 third_section_offset = 0
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
122
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
123 nb_faces = sum(len(model.quads) for model in self.models)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
124
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
125 # Write header
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
126 file.write(pack('<HH', len(self.models), nb_faces)) #TODO: nb_faces
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
127 file.write(pack('<II', 0, 0))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
128 file.write(pack('<I', 0))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
129 file.write(pack('<128s', self.name.encode('shift_jis')))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
130 for bgm_name, bgm_path in self.bgms:
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
131 file.write(pack('<128s', bgm_name.encode('shift_jis')))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
132 for bgm_name, bgm_path in self.bgms:
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
133 file.write(pack('<128s', bgm_path.encode('ascii')))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
134 file.write(b'\x00\x00\x00\x00' * len(self.models))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
135
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
136 # Write first section
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
137 for i, model in enumerate(self.models):
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
138 model_offsets.append(file.tell())
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
139 file.write(pack('<HHffffff', i, model.unknown, *model.bounding_box))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
140 for quad in model.quads:
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
141 file.write(pack('<HH', 0x00, 0x1c))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
142 file.write(pack('<Hxxfffff', *quad))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
143 file.write(pack('<HH', 0xffff, 4))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
144
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
145 # Write second section
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
146 second_section_offset = file.tell()
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
147 for obj_id, x, y, z in self.object_instances:
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
148 file.write(pack('<HHfff', obj_id, 256, x, y, z))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
149 file.write(b'\xff' * 16)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
150
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
151 # Write third section
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
152 third_section_offset = file.tell()
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
153 for frame, opcode, args in self.script:
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
154 size = calcsize(self._instructions[opcode][0])
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
155 file.write(pack('<IHH%s' % self._instructions[opcode][0], frame, opcode, size, *args))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
156 file.write(b'\xff' * 20)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
157
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
158 # Fix offsets
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
159 file.seek(4)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
160 file.write(pack('<II', second_section_offset, third_section_offset))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
161 file.seek(16+128+128*2*4)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
162 file.write(pack('<%sI' % len(self.models), *model_offsets))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
163