annotate pytouhou/formats/exe.py @ 316:f0be7ea62330

Fix a bug with ECL instruction 96, and fix overall ECL handling. The issue with instruction 96 was about death callbacks, being executed on the caller of instruction 96 instead of the dying enemies. This was introduced by changeset 5930b33a0370. Additionnaly, ECL processes are now an attribute of the Enemy, and death/timeout conditions are checked right after the ECL frame, even if the ECL script has already ended, just like in the original game.
author Thibaut Girka <thib@sitedethib.com>
date Thu, 29 Mar 2012 21:18:35 +0200
parents 5492472963b0
children 2674c789e0c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 ##
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 ## Copyright (C) 2011 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 ##
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 ## This program is free software; you can redistribute it and/or modify
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 ## it under the terms of the GNU General Public License as published
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 ## by the Free Software Foundation; version 3 only.
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 ##
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 ## This program is distributed in the hope that it will be useful,
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 ## GNU General Public License for more details.
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 ##
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15
235
e59bd7979ddc Do a little cleanup, and fix PCB SHT usage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 230
diff changeset
16 from copy import copy
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 from struct import Struct, unpack
235
e59bd7979ddc Do a little cleanup, and fix PCB SHT usage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 230
diff changeset
18
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 from pytouhou.utils.pe import PEFile
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 from pytouhou.utils.helpers import get_logger
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 logger = get_logger(__name__)
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 SQ2 = 2. ** 0.5 / 2.
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28
298
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 261
diff changeset
29 class InvalidExeException(Exception):
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 261
diff changeset
30 pass
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 261
diff changeset
31
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 261
diff changeset
32
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 class Shot(object):
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 def __init__(self):
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 self.interval = 0
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 self.delay = 0
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 self.pos = (0., 0.)
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 self.hitbox = (0., 0.)
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 self.angle = 0.
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 self.speed = 0.
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 self.damage = 0
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 self.orb = 0
230
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
43 self.type = 0
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 self.sprite = 0
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 self.unknown1 = None
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 class SHT(object):
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 def __init__(self):
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 #self.unknown1 = None
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 #self.bombs = 0.
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 #self.unknown2 = None
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 self.hitbox = 4.
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 self.graze_hitbox = 42.
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 self.autocollection_speed = 8.
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 self.item_hitbox = 38.
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 # No percentage_of_cherry_loss_on_die
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 self.point_of_collection = 128 #TODO: find the real default.
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 self.horizontal_vertical_speed = 0.
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 self.horizontal_vertical_focused_speed = 0.
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 self.diagonal_speed = 0.
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 self.diagonal_focused_speed = 0.
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 self.shots = {}
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 @classmethod
230
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
67 def find_character_defs(cls, pe_file):
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
68 """Generator returning the possible VA of character definition blocks.
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
69
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
70 Based on knowledge of the structure, it tries to find valid definition blocks
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
71 without embedding any copyrighted material or hard-coded offsets that would
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
72 only be useful for a specific build of the game.
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
73 """
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
74
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 format = Struct('<4f2I')
230
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
76 data_section = [section for section in pe_file.sections
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
77 if section.Name.startswith('.data')][0]
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
78 text_section = [section for section in pe_file.sections
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
79 if section.Name.startswith('.text')][0]
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 data_va = pe_file.image_base + data_section.VirtualAddress
230
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
81 data_size = data_section.SizeOfRawData
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 text_va = pe_file.image_base + text_section.VirtualAddress
230
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
83 text_size = text_section.SizeOfRawData
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84
230
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
85 # Search the whole data segment for 4 successive character definitions
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
86 for addr in xrange(data_va, data_va + data_size, 4):
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 for character_id in xrange(4):
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 pe_file.seek_to_va(addr + character_id * 24)
230
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
89 (speed1, speed2, speed3, speed4,
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
90 ptr1, ptr2) = format.unpack(pe_file.file.read(format.size))
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91
230
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
92 # Check whether the character's speed make sense,
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
93 # and whether the function pointers point to valid addresses
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
94 if not (all(0. < x < 10. for x in (speed1, speed2, speed3, speed4))
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 and speed2 <= speed1
230
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
96 and 0 <= ptr1 - text_va < text_size - 8
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
97 and 0 <= ptr2 - text_va < text_size - 8):
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 break
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99
230
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
100 # So far, this character definition seems to be valid.
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
101 # Now, make sure the shoot function wrappers pass valid addresses
261
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
102
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
103 # Search for the “push” instruction
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
104 for i in xrange(20):
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
105 # Find the “push” instruction
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
106 pe_file.seek_to_va(ptr1 + i)
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
107 instr1, shtptr1 = unpack('<BI', pe_file.file.read(5))
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
108 pe_file.seek_to_va(ptr2 + i)
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
109 instr2, shtptr2 = unpack('<BI', pe_file.file.read(5))
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
110 if instr1 == 0x68 and instr2 == 0x68 and (0 <= shtptr1 - data_va < data_size - 12
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
111 and 0 <= shtptr2 - data_va < data_size - 12):
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
112 # It is unlikely this character record is *not* valid, but
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
113 # just to be sure, let's check the first SHT definition.
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
114 pe_file.seek_to_va(shtptr1)
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
115 nb_shots, power, shotsptr = unpack('<III', pe_file.file.read(12))
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
116 if (0 < nb_shots <= 1000
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
117 and 0 <= power < 1000
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
118 and 0 <= shotsptr - data_va < data_size - 36*nb_shots):
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
119 break
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
120 # Check if everything is fine...
230
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
121 if not (0 <= shtptr1 - data_va < data_size - 12
261
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
122 and 0 <= shtptr2 - data_va < data_size - 12
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
123 and 0 < nb_shots <= 1000
230
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
124 and 0 <= power < 1000
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
125 and 0 <= shotsptr - data_va < data_size - 36*nb_shots):
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 break
230
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
127
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
128 else:
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
129 # XXX: Obscure python feature! This only gets executed if the
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
130 # XXX: loop ended without a break statement.
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
131 # In our case, it's only executed if all the 4 character
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
132 # definitions are considered valid.
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 yield addr
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
136 @classmethod
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137 def read(cls, file):
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
138 pe_file = PEFile(file)
261
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
139 data_section = [section for section in pe_file.sections
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
140 if section.Name.startswith('.data')][0]
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
141 data_va = pe_file.image_base + data_section.VirtualAddress
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
142 data_size = data_section.SizeOfRawData
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143
298
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 261
diff changeset
144 possible_character_records = list(cls.find_character_defs(pe_file))
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 261
diff changeset
145 if not possible_character_records:
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 261
diff changeset
146 raise InvalidExeException
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 261
diff changeset
147
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 261
diff changeset
148 character_records_va = possible_character_records[0]
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150 characters = []
235
e59bd7979ddc Do a little cleanup, and fix PCB SHT usage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 230
diff changeset
151 shots_offsets = {}
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152 for character in xrange(4):
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153 sht = cls()
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 pe_file.seek_to_va(character_records_va + 6*4*character)
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
156
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157 data = unpack('<4f2I', file.read(6*4))
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158 (speed, speed_focused, speed_unknown1, speed_unknown2,
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
159 shots_func_offset, shots_func_offset_focused) = data
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
160
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
161 sht.horizontal_vertical_speed = speed
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
162 sht.horizontal_vertical_focused_speed = speed_focused
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
163 sht.diagonal_speed = speed * SQ2
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
164 sht.diagonal_focused_speed = speed_focused * SQ2
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
165
235
e59bd7979ddc Do a little cleanup, and fix PCB SHT usage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 230
diff changeset
166 # Characters might have different shot types whether they are
e59bd7979ddc Do a little cleanup, and fix PCB SHT usage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 230
diff changeset
167 # focused or not, but properties read earlier apply to both modes.
e59bd7979ddc Do a little cleanup, and fix PCB SHT usage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 230
diff changeset
168 focused_sht = copy(sht)
e59bd7979ddc Do a little cleanup, and fix PCB SHT usage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 230
diff changeset
169 characters.append((sht, focused_sht))
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
170
235
e59bd7979ddc Do a little cleanup, and fix PCB SHT usage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 230
diff changeset
171 for sht, func_offset in ((sht, shots_func_offset), (focused_sht, shots_func_offset_focused)):
261
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
172 # Search for the “push” instruction
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
173 for i in xrange(20):
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
174 # Find the “push” instruction
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
175 pe_file.seek_to_va(func_offset + i)
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
176 instr, offset = unpack('<BI', file.read(5))
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
177 if instr == 0x68 and 0 <= offset - data_va < data_size - 12:
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
178 pe_file.seek_to_va(offset)
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
179 nb_shots, power, shotsptr = unpack('<III', pe_file.file.read(12))
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
180 if (0 < nb_shots <= 1000
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
181 and 0 <= power < 1000
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
182 and 0 <= shotsptr - data_va < data_size - 36*nb_shots):
2876c267be00 Add heuristics to extract character data from the demo .exe.
Thibaut Girka <thib@sitedethib.com>
parents: 236
diff changeset
183 break
235
e59bd7979ddc Do a little cleanup, and fix PCB SHT usage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 230
diff changeset
184 if offset not in shots_offsets:
e59bd7979ddc Do a little cleanup, and fix PCB SHT usage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 230
diff changeset
185 shots_offsets[offset] = []
e59bd7979ddc Do a little cleanup, and fix PCB SHT usage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 230
diff changeset
186 shots_offsets[offset].append(sht)
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
187
235
e59bd7979ddc Do a little cleanup, and fix PCB SHT usage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 230
diff changeset
188 for shots_offset, shts in shots_offsets.iteritems():
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
189 pe_file.seek_to_va(shots_offset)
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
190
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
191 level_count = 9
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
192 levels = []
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
193 for i in xrange(level_count):
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
194 shots_count, power, offset = unpack('<III', file.read(3*4))
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
195 levels.append((shots_count, power, offset))
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
196
235
e59bd7979ddc Do a little cleanup, and fix PCB SHT usage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 230
diff changeset
197 shots = {}
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
198
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
199 for shots_count, power, offset in levels:
235
e59bd7979ddc Do a little cleanup, and fix PCB SHT usage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 230
diff changeset
200 shots[power] = []
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
201 pe_file.seek_to_va(offset)
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
202
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
203 for i in xrange(shots_count):
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
204 shot = Shot()
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
205
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
206 data = unpack('<HH6fHBBhh', file.read(36))
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
207 (shot.interval, shot.delay, x, y, hitbox_x, hitbox_y,
230
1c24a6d93c1b Improve find_character_defs, clean up a bit, and disable attack types 2 and 3 for now
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
208 shot.angle, shot.speed, shot.damage, shot.orb, shot.type,
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
209 shot.sprite, shot.unknown1) = data
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
210
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
211 shot.pos = (x, y)
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
212 shot.hitbox = (hitbox_x, hitbox_y)
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
213
235
e59bd7979ddc Do a little cleanup, and fix PCB SHT usage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 230
diff changeset
214 shots[power].append(shot)
e59bd7979ddc Do a little cleanup, and fix PCB SHT usage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 230
diff changeset
215
e59bd7979ddc Do a little cleanup, and fix PCB SHT usage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 230
diff changeset
216 for sht in shts:
e59bd7979ddc Do a little cleanup, and fix PCB SHT usage.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 230
diff changeset
217 sht.shots = shots
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
218
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
219
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
220 return characters
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
221