Files
Neutron/tools/binary/gadget_controls.py
T

138 lines
7.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""Read-only gadget panel contracts from retained REA and guarded EXE spans."""
from __future__ import annotations
import argparse
import hashlib
import json
from pathlib import Path
import struct
from checkpoint_script_state import EXE_SHA256, find_case, read_va
from pe_inspect import PE
BODIES = (
('menu_keys', 0x425870, 0x425B20, 'd06e437c3b858d187a094306891333ce735445c412e35ef8ccb33c37d8ce7735'),
('menu_close', 0x425B20, 0x425C32, '8e80202c37b31235630e097297395c3c196959dfa11fdc5582cf6acc7b7a74c4'),
('menu_open', 0x425EF0, 0x426027, '472c5356cc85c9d3e79f053e63114fb03d7b81d79b51a653fc56c7ae1fee1008'),
('menu_tick', 0x427370, 0x427F6F, 'c0a95b31c9d2c0563c132fd2a8b04fd1710c2b77a17e0746722b407909f10b45'),
('hit', 0x403A30, 0x403BCE, 'fd20747d496832e9582deea8daa1c41c1400e9de35d07656ca622a61614ed2ce'),
('initialize', 0x404DE0, 0x405F70, 'd3505b89b02f229976a47566c1e1a5897489fc52bc21b55ee234b1c552c2057c'),
('select', 0x428D50, 0x4298E8, 'e86e6ad89c80b6d1656c3f4697d8f2cb06f79c4d2bc1c403f5449ad391aec9ff'),
('render', 0x402F60, 0x4032C0, '74653924b0988d391478ef1f3cbf96eda8cda0283b5665e22f295b41b9abd462'),
('description_init', 0x4018B4, 0x401A37, '1debbfc78bf08053a878fff43c54c7d38362920fa87d8393f02d86144369d5d7'),
)
# Slices are protected by the full EXE identity and the applicable spans above.
# Retain offsets/bytes for individual review rather than inferred signatures.
SITES = (
('tab_byte', 0x4258EB, 5), ('space_byte', 0x4258F8, 5),
('shared_key_latch', 0x425A7C, 6), ('open_flag_before_toggle', 0x425A99, 5),
('selected_mode_test', 0x425AAE, 8), ('cleanup_before_unequip', 0x425ABC, 6),
('unequip_global', 0x425AC2, 9), ('open_mode_zero', 0x425AE1, 4),
('open_mode_call', 0x425AF1, 6), ('open_game_pause', 0x425F8D, 6),
('open_flag_write', 0x425F9D, 7), ('open_physics_disable', 0x425FE9, 8),
('open_sound_gate_false', 0x426004, 7), ('close_flag_false', 0x425B4C, 6),
('close_game_resume', 0x425BAB, 6), ('close_sound_gate_true', 0x425BFE, 7),
('close_physics_enable', 0x425C05, 8), ('hit_state_skip_zero', 0x403A69, 9),
('hit_state_skip_four', 0x403A72, 10), ('hit_x_lower_strict', 0x403AD1, 9),
('hit_y_lower_strict', 0x403B04, 5), ('hit_state_three', 0x403B2E, 6),
('hit_return_last', 0x403BBF, 5), ('practice_scene500', 0x427794, 7),
('practice_vr_range', 0x4277B5, 20), ('practice_old_state_two', 0x4277DF, 6),
('practice_disarm_three', 0x4277E5, 11), ('practice_arm_two', 0x4277FC, 11),
('return_slot10', 0x427866, 10), ('select_close_before_cleanup', 0x4278DB, 6),
('select_cleanup', 0x4278E5, 6), ('select_from_menu4c8', 0x4278F3, 16),
('select_body_enable_after', 0x42791C, 7), ('select_ui_prelude', 0x428D8F, 15),
('select_table', 0x428DCD, 19), ('default_device_practice_state', 0x428DE0, 19),
('default_device_practice_load', 0x428E26, 44),
('initial_practice_return_state1', 0x4223BE, 20),
('escape_closes_open_panel', 0x42AB6D, 19),
('initial_display_highlight3', 0x401F93, 10),
)
VTABLES = (
(0x4A3BDC, 0x184, 0x425870), (0x4A3BDC, 0x1AC, 0x427370),
(0x4A3BDC, 0x1F8, 0x425EF0), (0x4A3BDC, 0x1FC, 0x425B20),
(0x4A3BDC, 0x174, 0x428D50), (0x48D414, 0x4A4, 0x403A30),
(0x48D414, 0x4A8, 0x403A10), (0x48D414, 0x4B0, 0x405FB0),
(0x48D414, 0x498, 0x406240), (0x48D414, 0x4C4, 0x406590),
)
TILES = ((12, 182), (158, 182), (12, 327), (157, 327),
(342, 182), (487, 182), (342, 327), (487, 327))
IDLE_CANVASES = (52, 46, 47, 48, 49, 50, 51, 45)
DESCRIPTION_CANVASES = (109, 115, 111, 113, 116, 114, 118, 110)
PRACTICE = (
(0, 0x428FCA, 0x4EF650, 'vr01.gam'), (1, 0x4296E8, 0x4EF58C, 'vr02.gam'),
(2, 0x428F51, 0x4EF678, 'vr03.gam'), (3, 0x428E41, 0x4EF6A0, 'vr04.gam'),
(4, 0x429348, 0x4EF628, 'vr05.gam'), (5, 0x42943D, 0x4EF5D8, 'vr06.gam'),
(6, 0x4297CD, 0x4EF564, 'vr07.gam'), (7, 0x4298B2, 0x4EF53C, 'vr08.gam'),
)
def collect(original: Path | str):
pe = PE(find_case(Path(original), 'Neutron.exe'))
digest = hashlib.sha256(pe.data).hexdigest()
if digest != EXE_SHA256:
raise ValueError('Unexpected original EXE identity')
bodies = []
for name, start, end, expected in BODIES:
actual = hashlib.sha256(read_va(pe, start, end - start)).hexdigest()
if actual != expected:
raise ValueError(f'Changed source span {name}')
bodies.append(dict(name=name, va=hex(start), file_offset=hex(pe.offset(start - pe.base)),
length=end-start, sha256=actual))
sites = [dict(name=name, va=hex(va), file_offset=hex(pe.offset(va - pe.base)),
bytes=read_va(pe, va, length).hex()) for name, va, length in SITES]
tables = []
for base, slot, expected in VTABLES:
target = struct.unpack('<I', read_va(pe, base + slot, 4))[0]
if target != expected:
raise ValueError(f'Changed virtual {base:x}+{slot:x}')
tables.append(dict(table=hex(base), slot=hex(slot), target=hex(target)))
practice = []
for item, call, string, name in PRACTICE:
if read_va(pe, call, 5) != b'\x68' + struct.pack('<I', string) or read_va(pe, string, len(name)+1) != name.encode()+b'\0':
raise ValueError('Changed practice destination')
practice.append(dict(item=item, push_va=hex(call), push_offset=hex(pe.offset(call-pe.base)),
string_va=hex(string), level=name))
return dict(sha256=digest, bodies=bodies, sites=sites, vtables=tables, practice=practice,
selection_table=[hex(v) for v in struct.unpack('<10I', read_va(pe, 0x4298E8, 40))],
tiles=[dict(item=i, x=x, y=y, hover_canvas=11+i, idle_canvas=IDLE_CANVASES[i],
description_canvas=DESCRIPTION_CANVASES[i]) for i, (x, y) in enumerate(TILES)])
def tab_action(panel_open: bool, equipped_mode: int, latched: bool = False):
"""Bounded425870 branch; caller/game/other-key preconditions are separate."""
if latched:
return 'none'
if panel_open:
return 'close'
return 'unequip' if equipped_mode != -1 else 'open_gadgets'
def hit_gadgets(states, x: float, y: float):
if len(states) != 8:
raise ValueError('Eight group0 gadget states required')
result = None
for item, ((left, top), state) in enumerate(zip(TILES, states)):
if state not in (0, 4) and left < x < left + 141 and top < y < top + 141:
result = item
return result
def practice_toggle(current: int, level: str, scene_task: int):
if scene_task >= 500 or 'VR01' <= level <= 'VR08':
return current
return 3 if current == 2 else 2
def default_device_selection(practice_state: int):
"""After ordinary cleanup, item3 keeps source mode−1; UI highlight is3."""
return dict(highlight=3, equipped_mode=-1,
practice_level='VR04' if practice_state == 2 else None,
practice_state=1 if practice_state == 2 else practice_state)
if __name__ == '__main__':
cli = argparse.ArgumentParser(description=__doc__)
cli.add_argument('original', type=Path)
print(json.dumps(collect(cli.parse_args().original), indent=2))