Mercurial > touhou
comparison 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 |
comparison
equal
deleted
inserted
replaced
93:d167280a82fc | 94:ca571697ec83 |
---|---|
17 from pytouhou.utils.helpers import read_string, get_logger | 17 from pytouhou.utils.helpers import read_string, get_logger |
18 | 18 |
19 logger = get_logger(__name__) | 19 logger = get_logger(__name__) |
20 | 20 |
21 | 21 |
22 class Object(object): | 22 class Model(object): |
23 def __init__(self): | 23 def __init__(self): |
24 self.header = (b'\x00') * 28 #TODO | 24 self.unknown = 0 |
25 self.bounding_box = (0., 0., 0., | |
26 0., 0., 0.) | |
25 self.quads = [] | 27 self.quads = [] |
26 | 28 |
27 | 29 |
28 | 30 |
29 class Stage(object): | 31 class Stage(object): |
30 def __init__(self, num): | 32 def __init__(self, num): |
31 self.num = num | 33 self.num = num |
32 self.name = '' | 34 self.name = '' |
33 self.bgms = (('', ''), ('', ''), ('', '')) | 35 self.bgms = (('', ''), ('', ''), ('', '')) |
34 self.objects = [] | 36 self.models = [] |
35 self.object_instances = [] | 37 self.object_instances = [] |
36 self.script = [] | 38 self.script = [] |
37 | 39 |
38 | 40 |
39 @classmethod | 41 @classmethod |
40 def read(cls, file, num): | 42 def read(cls, file, num): |
41 stage = Stage(num) | 43 stage = Stage(num) |
42 | 44 |
43 nb_objects, nb_faces = unpack('<HH', file.read(4)) | 45 nb_models, nb_faces = unpack('<HH', file.read(4)) |
44 object_instances_offset, script_offset = unpack('<II', file.read(8)) | 46 object_instances_offset, script_offset = unpack('<II', file.read(8)) |
45 if file.read(4) != b'\x00\x00\x00\x00': | 47 if file.read(4) != b'\x00\x00\x00\x00': |
46 raise Exception #TODO | 48 raise Exception #TODO |
47 | 49 |
48 stage.name = read_string(file, 128, 'shift-jis') | 50 stage.name = read_string(file, 128, 'shift-jis') |
57 bgm_c_path = read_string(file, 128, 'ascii') | 59 bgm_c_path = read_string(file, 128, 'ascii') |
58 bgm_d_path = read_string(file, 128, 'ascii') | 60 bgm_d_path = read_string(file, 128, 'ascii') |
59 | 61 |
60 stage.bgms = [(bgm_a, bgm_a_path), (bgm_b, bgm_b_path), (bgm_c, bgm_c_path), (bgm_d, bgm_d_path)] #TODO: handle ' ' | 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 ' ' |
61 | 63 |
62 # Read object definitions | 64 # Read model definitions |
63 offsets = unpack('<%s' % ('I' * nb_objects), file.read(4 * nb_objects)) | 65 offsets = unpack('<%s' % ('I' * nb_models), file.read(4 * nb_models)) |
64 for offset in offsets: | 66 for offset in offsets: |
65 obj = Object() | 67 model = Model() |
66 obj.header = file.read(28) #TODO: this has to be reversed! | 68 id_, unknown, x, y, z, width, height, depth = unpack('<HHffffff', file.read(28)) |
69 model.unknown = unknown | |
70 model.bounding_box = x, y, z, width, height, depth #TODO: check | |
67 while True: | 71 while True: |
68 unknown, size = unpack('<HH', file.read(4)) | 72 unknown, size = unpack('<HH', file.read(4)) |
69 if unknown == 0xffff: | 73 if unknown == 0xffff: |
70 break | 74 break |
71 if size != 0x1c: | 75 if size != 0x1c: |
72 raise Exception #TODO | 76 raise Exception #TODO |
73 script_index, _padding, x, y, z, width, height = unpack('<HHfffff', file.read(24)) | 77 script_index, _padding, x, y, z, width, height = unpack('<HHfffff', file.read(24)) |
74 #TODO: store script_index, x, y, z, width and height | 78 #TODO: store script_index, x, y, z, width and height |
75 obj.quads.append((script_index, x, y, z, width, height)) | 79 model.quads.append((script_index, x, y, z, width, height)) |
76 stage.objects.append(obj) | 80 stage.models.append(model) |
77 | 81 |
78 | 82 |
79 # Read object usages | 83 # Read object usages |
80 file.seek(object_instances_offset) | 84 file.seek(object_instances_offset) |
81 while True: | 85 while True: |
82 obj_id, unknown, x, y, z = unpack('<HHfff', file.read(16)) | 86 obj_id, unknown, x, y, z = unpack('<HHfff', file.read(16)) |
83 if (obj_id, unknown) == (0xffff, 0xffff): | 87 if (obj_id, unknown) == (0xffff, 0xffff): |
84 break | 88 break |
85 if unknown != 256: | 89 if unknown != 256: |
86 raise Exception #TODO | 90 raise Exception #TODO |
87 stage.object_instances.append((stage.objects[obj_id], x, y, z)) | 91 stage.object_instances.append((obj_id, x, y, z)) |
88 | 92 |
89 | 93 |
90 # Read other funny things (script) | 94 # Read other funny things (script) |
91 file.seek(script_offset) | 95 file.seek(script_offset) |
92 while True: | 96 while True: |