Mercurial > touhou
annotate pytouhou/formats/std.py @ 94:ca571697ec83
Various minor optimisations and refactoring
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sun, 04 Sep 2011 20:04:00 +0200 |
parents | 3da4de9decd0 |
children | ac2e5e1c2c3c |
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 |
0 | 16 from struct import pack, unpack |
58 | 17 from pytouhou.utils.helpers import read_string, get_logger |
0 | 18 |
58 | 19 logger = get_logger(__name__) |
0 | 20 |
21 | |
94
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
58
diff
changeset
|
22 class Model(object): |
0 | 23 def __init__(self): |
94
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
58
diff
changeset
|
24 self.unknown = 0 |
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
58
diff
changeset
|
25 self.bounding_box = (0., 0., 0., |
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
58
diff
changeset
|
26 0., 0., 0.) |
0 | 27 self.quads = [] |
28 | |
29 | |
30 | |
31 class Stage(object): | |
15 | 32 def __init__(self, num): |
33 self.num = num | |
0 | 34 self.name = '' |
35 self.bgms = (('', ''), ('', ''), ('', '')) | |
94
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
58
diff
changeset
|
36 self.models = [] |
0 | 37 self.object_instances = [] |
38 self.script = [] | |
39 | |
40 | |
41 @classmethod | |
15 | 42 def read(cls, file, num): |
43 stage = Stage(num) | |
0 | 44 |
94
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
58
diff
changeset
|
45 nb_models, nb_faces = unpack('<HH', file.read(4)) |
0 | 46 object_instances_offset, script_offset = unpack('<II', file.read(8)) |
47 if file.read(4) != b'\x00\x00\x00\x00': | |
48 raise Exception #TODO | |
49 | |
50 stage.name = read_string(file, 128, 'shift-jis') | |
51 | |
52 bgm_a = read_string(file, 128, 'shift-jis') | |
53 bgm_b = read_string(file, 128, 'shift-jis') | |
54 bgm_c = read_string(file, 128, 'shift-jis') | |
55 bgm_d = read_string(file, 128, 'shift-jis') | |
56 | |
57 bgm_a_path = read_string(file, 128, 'ascii') | |
58 bgm_b_path = read_string(file, 128, 'ascii') | |
59 bgm_c_path = read_string(file, 128, 'ascii') | |
60 bgm_d_path = read_string(file, 128, 'ascii') | |
61 | |
62 stage.bgms = [(bgm_a, bgm_a_path), (bgm_b, bgm_b_path), (bgm_c, bgm_c_path), (bgm_d, bgm_d_path)] #TODO: handle ' ' | |
63 | |
94
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
58
diff
changeset
|
64 # Read model definitions |
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
58
diff
changeset
|
65 offsets = unpack('<%s' % ('I' * nb_models), file.read(4 * nb_models)) |
0 | 66 for offset in offsets: |
94
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
58
diff
changeset
|
67 model = Model() |
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
58
diff
changeset
|
68 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
|
69 model.unknown = unknown |
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
58
diff
changeset
|
70 model.bounding_box = x, y, z, width, height, depth #TODO: check |
0 | 71 while True: |
72 unknown, size = unpack('<HH', file.read(4)) | |
73 if unknown == 0xffff: | |
74 break | |
75 if size != 0x1c: | |
76 raise Exception #TODO | |
77 script_index, _padding, x, y, z, width, height = unpack('<HHfffff', file.read(24)) | |
78 #TODO: store script_index, x, y, z, width and height | |
94
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
58
diff
changeset
|
79 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
|
80 stage.models.append(model) |
0 | 81 |
82 | |
83 # Read object usages | |
84 file.seek(object_instances_offset) | |
85 while True: | |
86 obj_id, unknown, x, y, z = unpack('<HHfff', file.read(16)) | |
87 if (obj_id, unknown) == (0xffff, 0xffff): | |
88 break | |
89 if unknown != 256: | |
90 raise Exception #TODO | |
94
ca571697ec83
Various minor optimisations and refactoring
Thibaut Girka <thib@sitedethib.com>
parents:
58
diff
changeset
|
91 stage.object_instances.append((obj_id, x, y, z)) |
0 | 92 |
93 | |
94 # Read other funny things (script) | |
95 file.seek(script_offset) | |
96 while True: | |
97 frame, message_type, size = unpack('<IHH', file.read(8)) | |
98 if (frame, message_type, size) == (0xffffffff, 0xffff, 0xffff): | |
99 break | |
100 if size != 0x0c: | |
101 raise Exception #TODO | |
102 data = file.read(12) | |
13 | 103 #TODO: maybe add a name somewhere |
104 if message_type == 0: # ViewPos | |
105 args = unpack('<fff', data) | |
106 elif message_type == 1: # Color | |
107 args = unpack('<BBBBff', data) | |
108 elif message_type == 2: # ViewPos2 | |
20
6ebf9539c077
Handle more enemies types and movements
Thibaut Girka <thib@sitedethib.com>
parents:
15
diff
changeset
|
109 args = unpack('<fff', data) |
13 | 110 elif message_type == 3: # StartInterpolatingViewPos2 |
111 args = tuple(unpack('<III', data)[:1]) | |
112 elif message_type == 4: # StartInterpolatingFog | |
113 args = tuple(unpack('<III', data)[:1]) | |
114 else: | |
115 args = (data,) | |
58 | 116 logger.warn('unknown opcode %d (data: %r)', message_type, data) |
13 | 117 stage.script.append((frame, message_type, args)) |
0 | 118 |
119 return stage | |
120 |