annotate pytouhou/formats/std.py @ 370:74471afbac37

Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 27 Jul 2012 18:43:48 +0200
parents 61adb5453e46
children 70e2ed71b09c
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
204
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
15 """Stage Definition (STD) files handling.
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
16
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
17 This module provides classes for handling the Stage Definition file format.
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
18 The STD file format is a format used in Touhou 6: EoSD to describe non-gameplay
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
19 aspects of a stage: its name, its music, 3D models composing its background,
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
20 and various scripted events such as camera movement.
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
21 """
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
22
52
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 20
diff changeset
23
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
24 from struct import pack, unpack, calcsize
58
3da4de9decd0 Use logging module
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
25 from pytouhou.utils.helpers import read_string, get_logger
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26
58
3da4de9decd0 Use logging module
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
27 logger = get_logger(__name__)
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
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
30 class Model(object):
111
340fcda8e64a Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents: 110
diff changeset
31 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
32 self.unknown = 0
111
340fcda8e64a Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents: 110
diff changeset
33 self.bounding_box = bounding_box or (0., 0., 0.,
340fcda8e64a Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents: 110
diff changeset
34 0., 0., 0.)
340fcda8e64a Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents: 110
diff changeset
35 self.quads = quads or []
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39 class Stage(object):
204
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
40 """Handle Touhou 6 Stage Definition files.
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
41
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
42 Stage Definition files are structured files describing non-gameplay aspects
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
43 aspects of a stage. They are split in a header an 3 additional sections.
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
44
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
45 The header contains the name of the stage, the background musics (BGM) used,
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
46 as well as the number of quads and objects composing the background.
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
47 The first section describes the models composing the background, whereas
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
48 the second section dictates how they are used.
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
49 The last section describes the changes to the camera, fog, and other things.
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
50
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
51 Instance variables:
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
52 name -- name of the stage
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
53 bgms -- list of (name, path) describing the different background musics used
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
54 models -- list of Model objects
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
55 object_instances -- list of instances of the aforementioned models
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
56 script -- stage script (camera, fog, etc.)
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
57 """
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
58
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
59 _instructions = {0: ('fff', 'set_viewpos'),
111
340fcda8e64a Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents: 110
diff changeset
60 1: ('BBBxff', 'set_fog'),
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
61 2: ('fff', 'set_viewpos2'),
118
c596a1a69402 Fix std parsing
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
62 3: ('Ixxxxxxxx', 'start_interpolating_viewpos2'),
c596a1a69402 Fix std parsing
Thibaut Girka <thib@sitedethib.com>
parents: 111
diff changeset
63 4: ('Ixxxxxxxx', 'start_interpolating_fog')}
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
64
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 94
diff changeset
65 def __init__(self):
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66 self.name = ''
111
340fcda8e64a Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents: 110
diff changeset
67 self.bgms = (('', ''), ('', ''), ('', ''), ('', ''))
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
68 self.models = []
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69 self.object_instances = []
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70 self.script = []
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
73 @classmethod
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 94
diff changeset
74 def read(cls, file):
204
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
75 """Read a Stage Definition file.
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
76
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
77 Raise an exception if the file is invalid.
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
78 Return a STD instance otherwise.
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
79 """
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
80
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents: 94
diff changeset
81 stage = Stage()
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
83 nb_models, nb_faces = unpack('<HH', file.read(4))
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
84 object_instances_offset, script_offset = unpack('<II', file.read(8))
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
85 if file.read(4) != b'\x00\x00\x00\x00':
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86 raise Exception #TODO
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
88 stage.name = read_string(file, 128, 'shift_jis')
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
89
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
90 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
91 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
92 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
93 bgm_d = read_string(file, 128, 'shift_jis')
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
94
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
95 bgm_a_path = read_string(file, 128, 'ascii')
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
96 bgm_b_path = read_string(file, 128, 'ascii')
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
97 bgm_c_path = read_string(file, 128, 'ascii')
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
98 bgm_d_path = read_string(file, 128, 'ascii')
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
99
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 204
diff changeset
100 stage.bgms = [None if bgm[0] == u' ' else bgm
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 204
diff changeset
101 for bgm in ((bgm_a, bgm_a_path), (bgm_b, bgm_b_path), (bgm_c, bgm_c_path), (bgm_d, bgm_d_path))]
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
102
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
103 # Read model definitions
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
104 offsets = unpack('<%s' % ('I' * nb_models), file.read(4 * nb_models))
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
105 for offset in offsets:
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
106 model = Model()
111
340fcda8e64a Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents: 110
diff changeset
107 file.seek(offset)
204
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
108
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
109 # Read model header
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
110 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
111 model.unknown = unknown
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
112 model.bounding_box = x, y, z, width, height, depth #TODO: check
204
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
113
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
114 # Read model quads
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
115 while True:
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
116 unknown, size = unpack('<HH', file.read(4))
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
117 if unknown == 0xffff:
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
118 break
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
119 if size != 0x1c:
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
120 raise Exception #TODO
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
121 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
122 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
123 stage.models.append(model)
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
124
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
125
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
126 # Read object usages
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
127 file.seek(object_instances_offset)
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
128 while True:
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
129 obj_id, unknown, x, y, z = unpack('<HHfff', file.read(16))
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
130 if (obj_id, unknown) == (0xffff, 0xffff):
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
131 break
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
132 if unknown != 256:
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
133 raise Exception #TODO
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
134 stage.object_instances.append((obj_id, x, y, z))
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
135
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
136
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
137 # Read other funny things (script)
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
138 file.seek(script_offset)
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
139 while True:
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
140 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
141 if (frame, opcode, size) == (0xffffffff, 0xffff, 0xffff):
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
142 break
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
143 if size != 0x0c:
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
144 raise Exception #TODO
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
145 data = file.read(size)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
146 if opcode in cls._instructions:
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
147 args = unpack('<%s' % cls._instructions[opcode][0], data)
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 1
diff changeset
148 else:
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 1
diff changeset
149 args = (data,)
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
150 logger.warn('unknown opcode %d', opcode)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
151 stage.script.append((frame, opcode, args))
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
152
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
153 return stage
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
154
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
155
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
156 def write(self, file):
204
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
157 """Write to a Stage Definition file."""
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
158 model_offsets = []
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
159 second_section_offset = 0
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
160 third_section_offset = 0
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
161
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
162 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
163
204
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
164 # Write header (offsets, number of quads, name and background musics)
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
165 file.write(pack('<HH', len(self.models), nb_faces))
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
166 file.write(pack('<II', 0, 0))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
167 file.write(pack('<I', 0))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
168 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
169 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
170 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
171 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
172 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
173 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
174
204
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
175 # Write first section (models)
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
176 for i, model in enumerate(self.models):
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
177 model_offsets.append(file.tell())
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
178 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
179 for quad in model.quads:
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
180 file.write(pack('<HH', 0x00, 0x1c))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
181 file.write(pack('<Hxxfffff', *quad))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
182 file.write(pack('<HH', 0xffff, 4))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
183
204
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
184 # Write second section (object instances)
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
185 second_section_offset = file.tell()
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
186 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
187 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
188 file.write(b'\xff' * 16)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
189
204
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
190 # Write third section (script)
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
191 third_section_offset = file.tell()
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
192 for frame, opcode, args in self.script:
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
193 size = calcsize(self._instructions[opcode][0])
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
194 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
195 file.write(b'\xff' * 20)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
196
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
197 # Fix offsets
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
198 file.seek(4)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
199 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
200 file.seek(16+128+128*2*4)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
201 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
202