annotate pytouhou/formats/std.py @ 609:23b9418e4b2f

Remove generic usage of GL_DRAW_FRAMEBUFFER which was introduced in GL 3.0 with framebuffer_blit.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 21 Dec 2014 18:10:23 +0100
parents 70e2ed71b09c
children d1f0bb0b7a17
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))
377
70e2ed71b09c Add meaningful exceptions in format parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
84 object_instances_offset, script_offset, zero = unpack('<III', file.read(12))
70e2ed71b09c Add meaningful exceptions in format parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
85 assert zero == 0
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
87 stage.name = read_string(file, 128, 'shift_jis')
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
88
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
89 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
90 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
91 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
92 bgm_d = read_string(file, 128, 'shift_jis')
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
93
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
94 bgm_a_path = read_string(file, 128, 'ascii')
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
95 bgm_b_path = read_string(file, 128, 'ascii')
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
96 bgm_c_path = read_string(file, 128, 'ascii')
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
97 bgm_d_path = read_string(file, 128, 'ascii')
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
98
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 204
diff changeset
99 stage.bgms = [None if bgm[0] == u' ' else bgm
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 204
diff changeset
100 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
101
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
102 # Read model definitions
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
103 offsets = unpack('<%s' % ('I' * nb_models), file.read(4 * nb_models))
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
104 for offset in offsets:
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
105 model = Model()
111
340fcda8e64a Fix a few, minor things
Thibaut Girka <thib@sitedethib.com>
parents: 110
diff changeset
106 file.seek(offset)
204
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
107
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
108 # Read model header
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
109 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
110 model.unknown = unknown
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
111 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
112
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
113 # Read model quads
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
114 while True:
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
115 unknown, size = unpack('<HH', file.read(4))
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
116 if unknown == 0xffff:
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
117 break
377
70e2ed71b09c Add meaningful exceptions in format parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
118 assert size == 0x1c
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
119 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
120 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
121 stage.models.append(model)
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
122
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
123
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
124 # Read object usages
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
125 file.seek(object_instances_offset)
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
126 while True:
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
127 obj_id, unknown, x, y, z = unpack('<HHfff', file.read(16))
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
128 if (obj_id, unknown) == (0xffff, 0xffff):
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
129 break
377
70e2ed71b09c Add meaningful exceptions in format parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
130 assert unknown == 256 #TODO: really?
94
ca571697ec83 Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents: 58
diff changeset
131 stage.object_instances.append((obj_id, x, y, z))
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
132
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
133
377
70e2ed71b09c Add meaningful exceptions in format parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
134 # Read the script
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
135 file.seek(script_offset)
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
136 while True:
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
137 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
138 if (frame, opcode, size) == (0xffffffff, 0xffff, 0xffff):
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
139 break
377
70e2ed71b09c Add meaningful exceptions in format parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
140 assert size == 0x0c
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
141 data = file.read(size)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
142 if opcode in cls._instructions:
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
143 args = unpack('<%s' % cls._instructions[opcode][0], data)
13
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 1
diff changeset
144 else:
58bc264aba38 Refactor
Thibaut Girka <thib@sitedethib.com>
parents: 1
diff changeset
145 args = (data,)
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
146 logger.warn('unknown opcode %d', opcode)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
147 stage.script.append((frame, opcode, args))
0
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
148
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
149 return stage
6b2c7af2384c Hello Gensokyo _o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
150
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
151
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
152 def write(self, file):
204
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
153 """Write to a Stage Definition file."""
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
154 model_offsets = []
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
155 second_section_offset = 0
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
156 third_section_offset = 0
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 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
159
204
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
160 # 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
161 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
162 file.write(pack('<II', 0, 0))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
163 file.write(pack('<I', 0))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
164 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
165 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
166 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
167 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
168 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
169 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
170
204
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
171 # Write first section (models)
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
172 for i, model in enumerate(self.models):
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
173 model_offsets.append(file.tell())
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
174 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
175 for quad in model.quads:
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
176 file.write(pack('<HH', 0x00, 0x1c))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
177 file.write(pack('<Hxxfffff', *quad))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
178 file.write(pack('<HH', 0xffff, 4))
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
179
204
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
180 # Write second section (object instances)
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
181 second_section_offset = file.tell()
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
182 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
183 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
184 file.write(b'\xff' * 16)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
185
204
88361534c77e Add some documentation (argh, so much left to document!)
Thibaut Girka <thib@sitedethib.com>
parents: 118
diff changeset
186 # Write third section (script)
110
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
187 third_section_offset = file.tell()
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
188 for frame, opcode, args in self.script:
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
189 size = calcsize(self._instructions[opcode][0])
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
190 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
191 file.write(b'\xff' * 20)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
192
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
193 # Fix offsets
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
194 file.seek(4)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
195 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
196 file.seek(16+128+128*2*4)
3ac41b966fed Add writing support to pytouhou.formats.std!
Thibaut Girka <thib@sitedethib.com>
parents: 97
diff changeset
197 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
198