-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathContainer.py
338 lines (308 loc) · 10.2 KB
/
Container.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import os
import struct
from typing import TYPE_CHECKING, List
import zipfile
from worlds.Files import APContainer
import py_randomprime
from .MetroidPrimeInterface import GAMES, HUD_MESSAGE_DURATION
if TYPE_CHECKING:
from ppc_asm.assembler.ppc import GeneralRegister # type: ignore
class MetroidPrimeContainer(APContainer):
game: str = "Metroid Prime"
def __init__(
self,
config_json: str,
options_json: str,
outfile_name: str,
output_directory: str,
player=None,
player_name: str = "",
server: str = "",
):
self.config_json = config_json
self.config_path = "config.json"
self.options_path = "options.json"
self.options_json = options_json
container_path = os.path.join(output_directory, outfile_name + ".apmp1")
super().__init__(container_path, player, player_name, server)
def write_contents(self, opened_zipfile: zipfile.ZipFile) -> None:
opened_zipfile.writestr(self.config_path, self.config_json)
opened_zipfile.writestr(self.options_path, self.options_json)
super().write_contents(opened_zipfile)
def add(
output_register: "GeneralRegister",
input_register1: "GeneralRegister",
input_register2: "GeneralRegister",
):
"""
output_register = input_register1 + input_register2
"""
from ppc_asm.assembler.ppc import Instruction
return Instruction.compose(
(
(31, 6, False), # Opcode for add
(output_register.number, 5, False),
(input_register1.number, 5, False),
(input_register2.number, 5, False),
(266, 10, False), # Function code for add
(0, 1, False), # Rc bit
)
)
def slw(
output_register: "GeneralRegister",
input_register: "GeneralRegister",
shift_amount_register: "GeneralRegister",
):
"""
output_register = input_register << shift_amount_register
"""
from ppc_asm.assembler.ppc import Instruction
return Instruction.compose(
(
(31, 6, False), # Opcode for slw
(input_register.number, 5, False),
(output_register.number, 5, False),
(shift_amount_register.number, 5, False),
(24, 10, False), # Function code for slw
(0, 1, False), # Rc bit
)
)
def slwi(
output_register: "GeneralRegister", input_register: "GeneralRegister", literal: int
):
"""
output_register = input_register << shift_amount_register
"""
from ppc_asm.assembler.ppc import Instruction
return Instruction.compose(
(
(21, 6, False), # Opcode for slwi
(input_register.number, 5, False),
(output_register.number, 5, False),
(literal, 5, False),
(0, 5, False),
(31 - literal, 5, False),
(0, 1, False), # Rc bit
)
)
def construct_hook_patch(game_version: str, progressive_beams: bool) -> List[int]:
from ppc_asm.assembler.ppc import (
addi,
bl,
li,
lwz,
r1,
r3,
r4,
r5,
r6,
r31,
stw,
cmpwi,
bne,
mtspr,
blr,
lmw,
r0,
LR,
stwu,
mfspr,
or_,
lbz,
stmw,
stb,
lis,
r7,
r9,
nop,
ori,
GeneralRegister,
)
from ppc_asm import assembler
symbols = py_randomprime.symbols_for_version(game_version)
# UpdateHintState is 0x1BC in length, 111 instructions
num_preserved_registers = 2
num_required_instructions = 111
instruction_size = 4
block_size = 32
patch_stack_length = 0x30 + (num_preserved_registers * instruction_size)
instructions: List = [
stwu(r1, -(patch_stack_length - instruction_size), r1),
mfspr(r0, LR),
stw(r0, patch_stack_length, r1),
stmw(
GeneralRegister(block_size - num_preserved_registers),
patch_stack_length
- instruction_size
- num_preserved_registers * instruction_size,
r1,
),
or_(r31, r3, r3),
# Check if trigger is set
lis(
r6, GAMES[game_version]["HUD_TRIGGER_ADDRESS"] >> 16
), # Load upper 16 bits of address
ori(
r6, r6, GAMES[game_version]["HUD_TRIGGER_ADDRESS"] & 0xFFFF
), # Load lower 16 bits of address
lbz(r5, 0, r6),
cmpwi(r5, 1),
bne("early_return_hud"),
# If trigger is set then reset it to 0
li(r5, 0),
stb(r5, 0, r6),
# Prep function arguments
lis(
r5, struct.unpack("<I", struct.pack("<f", HUD_MESSAGE_DURATION))[0] >> 16
), # Float duration to show message
li(r6, 0x0),
li(r7, 0x1),
li(r9, 0x9),
stw(r5, 0x10, r1),
stb(r7, 0x14, r1),
stb(r6, 0x15, r1),
stb(r6, 0x16, r1),
stb(r7, 0x17, r1),
stw(r9, 0x18, r1),
addi(r3, r1, 0x1C),
lis(
r4, GAMES[game_version]["HUD_MESSAGE_ADDRESS"] >> 16
), # Load upper 16 bits of message address
ori(
r4, r4, GAMES[game_version]["HUD_MESSAGE_ADDRESS"] & 0xFFFF
), # Load lower 16 bits of message address
bl(symbols["wstring_l__4rstlFPCw"]),
addi(r4, r1, 0x10),
# Call function
bl(symbols["DisplayHudMemo__9CSamusHudFRC7wstringRC12SHudMemoInfo"]),
nop().with_label("early_return_hud"),
# Progressive Beam Patch
*construct_progressive_beam_patch(game_version, progressive_beams),
# Early return
lmw(
GeneralRegister(block_size - num_preserved_registers),
patch_stack_length
- instruction_size
- num_preserved_registers * instruction_size,
r1,
).with_label("early_return_beam"),
lwz(r0, patch_stack_length, r1),
mtspr(LR, r0),
addi(r1, r1, patch_stack_length - instruction_size),
blr(),
]
# Fill remaining instructions with nops
while len(instructions) < num_required_instructions:
instructions.append(nop())
if len(instructions) > num_required_instructions:
raise Exception(
f"Patch function is too long: {len(instructions)}/{num_required_instructions}"
)
return list(
assembler.assemble_instructions(
symbols["UpdateHintState__13CStateManagerFf"], instructions, symbols=symbols
)
)
def _load_player_state_to_r6(game_version: str) -> List[int]:
from ppc_asm.assembler.ppc import lis, ori, lwz, r6, r5
cstate_manager_global = GAMES[game_version]["cstate_manager_global"]
return [
# Get the player state address
lis(
r6, cstate_manager_global >> 16
), # Load upper 16 bits of cstate_manager_global
ori(
r6, r6, cstate_manager_global & 0xFFFF
), # Load lower 16 bits of cstate_manager_global
lwz(
r6, 0x8B8, r6
), # Load the player state address from cstate_manager_global + 0x8B8
# : Dereference the player state address pointer to get the actual player state address
lwz(r6, 0, r6), # Load the player state address from the pointer stored in r6
]
def construct_progressive_beam_patch(
game_version: str, progressive_beams: bool
) -> List[int]:
from ppc_asm.assembler.ppc import (
addi,
bl,
b,
li,
lwz,
r1,
r3,
r4,
r5,
r6,
r8,
r10,
r31,
stw,
cmpwi,
bgt,
mtspr,
blr,
lmw,
r0,
LR,
stwu,
mfspr,
or_,
stmw,
stw,
lis,
r7,
r9,
nop,
ori,
GeneralRegister,
Instruction,
)
def add(
output_register: GeneralRegister,
input_register1: GeneralRegister,
input_register2: GeneralRegister,
):
"""
output_register = input_register1 + input_register2
"""
return Instruction.compose(
(
(31, 6, False), # Opcode for add
(output_register.number, 5, False),
(input_register1.number, 5, False),
(input_register2.number, 5, False),
(266, 10, False), # Function code for add
(0, 1, False), # Rc bit
)
)
if not progressive_beams:
return []
cstate_manager_global = GAMES[game_version]["cstate_manager_global"]
charge_beam_offset = 0x7C
instructions: List = [
# Step 0: Get the player state address
*_load_player_state_to_r6(game_version),
# Step 1: Get the current beam from the player state
lwz(r5, 0x8, r6), # Load the current beam value
# Step 2: Read the value at the beam address in CPlayerState::PowerUps[41]
slwi(r5, r5, 3), # Multiply by 8
addi(r7, r6, 0x2C), # Add powerups array offset + 4 (to get item capacity)
add(r7, r7, r5), # Add power beam offset
lwz(r8, 0, r7), # Load the value at the progressive beam address
# Step 3: Check the value and set the appropriate address
cmpwi(r8, 1),
bgt("activate_charge_beam"),
# If value is 0, set the byte at player state address + charge_beam_offset to 0
li(r9, 0),
b("set_charge_beam"),
# If value is 1, set the byte at player state address + charge_beam_offset to 1
li(r9, 1).with_label("activate_charge_beam"),
# Set charge beam state
addi(r10, r6, charge_beam_offset).with_label(
"set_charge_beam"
), # Calculate player state address + charge_beam_offset
stw(r9, 0, r10), # Store 1 at the calculated address
b("early_return_beam"),
]
return instructions