annotate pytouhou/formats/anm0.py @ 38:cb5b27011044

Small refactoring and proper anm0's instruction 5 handling
author Thibaut Girka <thib@sitedethib.com>
date Sun, 14 Aug 2011 18:08:45 +0200
parents 9027692abd79
children 93c8dc2de923
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 from struct import pack, unpack
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 from pytouhou.utils.helpers import read_string
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4 #TODO: refactor/clean up
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 class Animations(object):
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
8 def __init__(self):
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9 self.size = (0, 0)
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10 self.first_name = None
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 self.secondary_name = None
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 self.sprites = {}
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 self.scripts = {}
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 @classmethod
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17 def read(cls, file):
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18 nb_sprites, nb_scripts, zero1 = unpack('<III', file.read(12))
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 width, height, format, zero2 = unpack('<IIII', file.read(16))
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20 first_name_offset, unused, secondary_name_offset = unpack('<III', file.read(12))
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 version, unknown1, thtxoffset, hasdata, nextoffset = unpack('<IIIII', file.read(20))
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22 if version != 0:
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23 raise Exception #TODO
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 file.read(4) #TODO
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 sprite_offsets = [unpack('<I', file.read(4))[0] for i in range(nb_sprites)]
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27 script_offsets = [unpack('<II', file.read(8)) for i in range(nb_scripts)]
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29 anm = Animations()
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 anm.size = (width, height)
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 # Names
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34 if first_name_offset:
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35 file.seek(first_name_offset)
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 anm.first_name = read_string(file, 32, 'ascii') #TODO: 32, really?
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37 if secondary_name_offset:
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 file.seek(secondary_name_offset)
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39 anm.secondary_name = read_string(file, 32, 'ascii') #TODO: 32, really?
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42 # Sprites
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 file.seek(64)
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 anm.sprites = {}
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 for offset in sprite_offsets:
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 file.seek(offset)
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 idx, x, y, width, height = unpack('<Iffff', file.read(20))
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48 anm.sprites[idx] = x, y, width, height
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51 # Scripts
38
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
52 anm.scripts = {}
2
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53 for i, offset in script_offsets:
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54 anm.scripts[i] = []
38
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
55 instruction_offsets = []
2
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56 file.seek(offset)
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57 while True:
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
58 #TODO
38
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
59 instruction_offsets.append(file.tell() - offset)
2
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60 time, instr_type, length = unpack('<HBB', file.read(4))
35
9027692abd79 Fix anm0 parsing
Thibaut Girka <thib@sitedethib.com>
parents: 16
diff changeset
61 data = file.read(length)
38
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
62 if instr_type == 1: # set_sprite
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
63 args = unpack('<I', data)
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
64 elif instr_type == 2: # set_scale
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
65 args = unpack('<ff', data)
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
66 elif instr_type == 3: # set_alpha
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
67 args = unpack('<I', data)
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
68 elif instr_type == 5: # jump
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
69 # Translate offset to instruction index
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
70 args = (instruction_offsets.index(unpack('<I', data)[0]),)
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
71 elif instr_type == 9: # set_3d_rotation
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
72 args = unpack('<fff', data)
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
73 elif instr_type == 10: # set_3d_rotation_speed
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
74 args = unpack('<fff', data)
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
75 else:
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
76 args = (data,)
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
77 anm.scripts[i].append((time, instr_type, args))
2
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
78 if instr_type == 0:
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
79 break
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
80 #TODO
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 2
diff changeset
82 return anm
2
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83