annotate pytouhou/formats/anm0.py @ 69:a142e57218a0

Refactor. Move VMs to pytouhou.vm.
author Thibaut Girka <thib@sitedethib.com>
date Sat, 27 Aug 2011 10:58:54 +0200
parents 694f25881d0f
children a03d7a94b997
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: 41
diff changeset
1 # -*- encoding: utf-8 -*-
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 41
diff changeset
2 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 41
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: 41
diff changeset
4 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 41
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: 41
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: 41
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: 41
diff changeset
8 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 41
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: 41
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: 41
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: 41
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: 41
diff changeset
13 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 41
diff changeset
14
2
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15 from struct import pack, unpack
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
16 from pytouhou.utils.helpers import read_string, get_logger
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
17
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
18
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
19 logger = get_logger(__name__)
2
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 #TODO: refactor/clean up
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 class Animations(object):
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
25 _instructions = {0: ('', 'delete'),
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
26 1: ('I', 'set_sprite'),
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
27 2: ('ff', 'set_scale'),
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
28 3: ('I', 'set_alpha'),
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
29 4: ('BBBx', 'set_color'),
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
30 5: ('I', 'jump'),
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
31 7: ('', 'toggle_mirrored'),
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
32 9: ('fff', 'set_3d_rotations'),
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
33 10: ('fff', 'set_3d_rotations_speed'),
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
34 11: ('ff', 'set_scale_speed'),
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
35 12: ('ii', 'fade'),
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
36 15: ('', 'keep_still'),
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
37 16: ('i', 'set_random_sprite'),
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
38 23: ('', 'set_corner_relative_placement'),
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
39 27: ('f', 'shift_texture_x'),
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
40 28: ('f', 'shift_texture_y'),
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
41 30: ('ffi', 'scale_in')}
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
42
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
43
2
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 def __init__(self):
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 self.size = (0, 0)
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 self.first_name = None
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 self.secondary_name = None
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48 self.sprites = {}
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49 self.scripts = {}
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
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52 @classmethod
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53 def read(cls, file):
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54 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
55 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
56 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
57 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
58 if version != 0:
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59 raise Exception #TODO
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60 file.read(4) #TODO
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
62 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
63 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
64
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 anm = Animations()
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67 anm.size = (width, height)
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69 # Names
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70 if first_name_offset:
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71 file.seek(first_name_offset)
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72 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
73 if secondary_name_offset:
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74 file.seek(secondary_name_offset)
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
75 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
76
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
77
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
78 # Sprites
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
79 file.seek(64)
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
80 anm.sprites = {}
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81 for offset in sprite_offsets:
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 file.seek(offset)
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83 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
84 anm.sprites[idx] = x, y, width, height
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
85
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87 # Scripts
38
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
88 anm.scripts = {}
2
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
89 for i, offset in script_offsets:
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
90 anm.scripts[i] = []
38
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
91 instruction_offsets = []
2
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
92 file.seek(offset)
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
93 while True:
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
94 #TODO
38
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
95 instruction_offsets.append(file.tell() - offset)
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
96 time, opcode, length = unpack('<HBB', file.read(4))
35
9027692abd79 Fix anm0 parsing
Thibaut Girka <thib@sitedethib.com>
parents: 16
diff changeset
97 data = file.read(length)
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
98 if opcode in cls._instructions:
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
99 args = unpack('<%s' % cls._instructions[opcode][0], data)
38
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
100 else:
cb5b27011044 Small refactoring and proper anm0's instruction 5 handling
Thibaut Girka <thib@sitedethib.com>
parents: 35
diff changeset
101 args = (data,)
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
102 logger.warn('unknown opcode %d', opcode)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
103
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
104 anm.scripts[i].append((time, opcode, args))
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
105 if opcode == 0:
2
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
106 break
69
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
107
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
108 # Translate offsets to instruction pointers
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
109 for instr_offset, (j, instr) in zip(instruction_offsets, enumerate(anm.scripts[i])):
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
110 time, opcode, args = instr
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
111 if opcode == 5:
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
112 args = (instruction_offsets.index(args[0]),)
a142e57218a0 Refactor. Move VMs to pytouhou.vm.
Thibaut Girka <thib@sitedethib.com>
parents: 57
diff changeset
113 anm.scripts[i][j] = time, opcode, args
2
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
114 #TODO
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
115
16
66ce9bb440ac Refactor in order to support multiple textures
Thibaut Girka <thib@sitedethib.com>
parents: 2
diff changeset
116 return anm
2
057cb96907e3 Add preliminay support for EoSD's ANM format
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
117