Files
Neutron/tools/binary/checkpoint_character_reset.py
T

141 lines
6.5 KiB
Python

#!/usr/bin/env python3
"""Read-only retained-character initialization and PostLoad evidence."""
from __future__ import annotations
import argparse
import hashlib
import json
from pathlib import Path
import struct
import sys
sys.path.insert(0, str(Path(__file__).resolve().parent))
from checkpoint_script_state import EXE_SHA256, find_case, read_va, rel_call_target
from pe_inspect import PE
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'levels'))
import gam
SITES = (
('player_init_base', 0x437896, 'e8550f0200'),
('player_init_speed_zero', 0x4378A1, '898608060000'),
('player_init_turn_zero', 0x4378A7, '898614060000'),
('player_init_618_zero', 0x4378AD, '898618060000'),
('player_init_euler_getter', 0x4378B8, 'ff9028030000'),
('player_init_yaw_capture', 0x4378BE, '8b500489961c060000'),
('euler_getter_y', 0x4723FC, '8b9128020000'),
('postload_jump_zero', 0x42378B, '6689bd28070000'),
('postload_normal_radius60', 0x4236A3, '6800007042'),
('postload_normal_support_factor', 0x4236B3, '6885eba13f'),
('postload_animation_pause_disable', 0x42370A, 'ff901c010000'),
('stage_sound_stop', 0x43B7FB, 'ff9248010000'),
('sound_stop_only_handle', 0x429A19, 'c786440a0000ffffffff'),
('fire_constructor_preservation', 0x421E51, 'e8eaf00400'),
('fire_constructor_owner_zero', 0x421E62, '899ebc060000'),
('fire_constructor_timer_zero', 0x421E68, '899ec0060000'),
('fire_owner_assignment', 0x437525, '8981bc060000'),
('fire_grounded_owner_clear', 0x421FE9, 'c7860006000000000000'),
('fire_timer_tick', 0x421FF3, 'd98600060000d8642418'),
('player_constructor_mode_zero', 0x43748A, '66899dc4070000'),
('goddard_init_base_jump', 0x41E6A0, 'e90b98feff'),
('goddard_init_authored_state', 0x407EBC, '8b867c080000898608060000'),
('goddard_init_waypoint_clear', 0x407EC8, 'c7860008000000000000'),
('goddard_postload_base', 0x41E593, 'e8e8aefeff'),
('goddard_reuse_callback', 0x423CE2, 'ff9074010000'),
('inventory_disable_compare_one', 0x40389E, '66833801751166c7000400'),
('inventory_enable_compare_four', 0x4038AA, '66833804750566c7000100'),
('tutorial_level_lower_bound', 0x423E52, '3d31305256'),
('tutorial_level_upper_bound', 0x423E5D, '3d38305256'),
('tutorial_mode_flag', 0x423E6A, 'c6853809000001'),
('tutorial_objective_hidden', 0x423EC6, 'c70588814f000000a041'),
)
VTABLES = (
('Jimmy Init', 0x4A373C, 0x28, 0x437890),
('Jimmy PostLoad', 0x4A373C, 0x40C, 0x423610),
('Jimmy Euler getter', 0x4A373C, 0x328, 0x4723F0),
('Jimmy transition stop sound', 0x4A3BDC, 0x148, 0x429A00),
('Jimmy animation pause', 0x4A3BDC, 0x11C, 0x40D350),
('Fire Init', 0x4A3160, 0x28, 0x4587F0),
('Fire PostLoad', 0x4A3160, 0x40C, 0x40E7B0),
('Goddard Init', 0x49F480, 0x28, 0x41E6A0),
('Goddard PostLoad', 0x49F480, 0x40C, 0x41E590),
('Shadow Init', 0x4B37B8, 0x28, 0x4587F0),
('Shadow PostLoad', 0x4B37B8, 0x40C, 0x43F810),
)
GADGET_INDICES = (7, 3, 0, 5, 1, 6, 2, 4)
ENABLE_CALLS = (0x423DEF, 0x423DFA, 0x423E05, 0x423E10,
0x423E1B, 0x423E26, 0x423E34, 0x423E3F)
DISABLE_CALLS = (0x423E71, 0x423E7C, 0x423E87, 0x423E92,
0x423E9D, 0x423EA8, 0x423EB6, 0x423EC1)
def gadget_postload(states, tutorial):
"""Common PostLoad prefix only:403870 preserves values other than1/4.
Later level tails override this result; see retained_return_cleanup.py.
This function does not model final scene availability.
"""
values = list(states)
if len(values) < 8:
raise ValueError('Need all eight group0 gadget entries')
for index in GADGET_INDICES:
if values[index] == 4:
values[index] = 1
if tutorial:
for index in GADGET_INDICES:
if values[index] == 1:
values[index] = 4
return values
def collect(root: Path):
pe = PE(find_case(root, 'Neutron.exe'))
digest = hashlib.sha256(pe.data).hexdigest()
sites = []
for name, va, expected in SITES:
actual = read_va(pe, va, len(bytes.fromhex(expected))).hex()
item = dict(name=name, va=hex(va), file_offset=hex(pe.offset(va-pe.base)),
expected=expected, actual=actual, matches=actual == expected)
if actual.startswith('e8'):
item['direct_call_target'] = hex(rel_call_target(pe, va))
sites.append(item)
tables = []
for name, table, slot, expected in VTABLES:
actual = struct.unpack('<I', read_va(pe, table + slot, 4))[0]
tables.append(dict(name=name, table=hex(table), slot=hex(slot),
file_offset=hex(pe.offset(table+slot-pe.base)),
target=hex(actual), matches=actual == expected))
calls = []
for disabled, addresses in ((0, ENABLE_CALLS), (1, DISABLE_CALLS)):
for index, va in zip(GADGET_INDICES, addresses, strict=True):
target = rel_call_target(pe, va)
# Each site is preceded by push bool; push item; push group0.
expected = bytes((0x6A, disabled, 0x6A, index, 0x6A, 0))
# First tutorial call inserts its mode flag write after the pushes.
argument_start = va - (13 if va == 0x423E71 else 6)
actual = read_va(pe, argument_start, 6)
calls.append(dict(va=hex(va), file_offset=hex(pe.offset(va-pe.base)),
group=0, index=index, disabled=disabled,
target=hex(target), matches=actual == expected and target == 0x403870))
route = []
for relative in ('gam/level1b.gam', 'gam/VR04.gam', 'gam/level1.gam'):
path = find_case(root, relative)
level = gam.parse(path.read_bytes())
jimmy = next(e for e in level.entities if e.kind == '3JIM')
route.append(dict(file=relative, code=level.code, offset=hex(jimmy.offset),
sha256=hashlib.sha256(path.read_bytes()).hexdigest(),
player_properties=sorted(jimmy.values())))
return dict(known_binary=digest == EXE_SHA256, sha256=digest,
sites=sites, vtables=tables, gadget_calls=calls, route=route,
scope='Static source bytes/data only; no original runtime execution.')
if __name__ == '__main__':
cli = argparse.ArgumentParser(description=__doc__)
cli.add_argument('original', type=Path)
result = collect(cli.parse_args().original)
print(json.dumps(result, indent=2))
if not result['known_binary'] or not all(x['matches'] for key in ('sites', 'vtables', 'gadget_calls') for x in result[key]):
raise SystemExit(1)