Files
Neutron/tools/binary/ceiling_wrapper.py

171 lines
7.7 KiB
Python

#!/usr/bin/env python3
"""Read-only Jimmy ceiling-wrapper evidence and bounded state projection."""
from __future__ import annotations
import argparse
from dataclasses import dataclass, replace
import hashlib
import json
import math
from pathlib import Path
import struct
from checkpoint_script_state import EXE_SHA256, find_case, read_va
from pe_inspect import PE
SITES = (
('wrapper_velocity_before_ceiling', 0x424BE0, 'ff92bc020000d94004d81d14d94800'),
('wrapper_only_strictly_upward', 0x424BF1, 'f6c441750c'),
('wrapper_ceiling_virtual', 0x424BFC, 'ff92a4010000'),
('wrapper_distance_virtual', 0x424C08, 'ff90a8010000'),
('wrapper_movement_after_queries', 0x424C34, 'e8072d0100'),
('ceiling_logical_root', 0x42519B, 'ff9010030000'),
('ceiling_one_sided', 0x4251A3, '6a0083ec10'),
('ceiling_root_y_plus150', 0x4251B8, 'd9442438d805343e4a00'),
('ceiling_ray_dispatch', 0x4251FC, 'e80f700500'),
('ceiling_hit_required', 0x425204, '84c00f84c7000000'),
('ceiling_live_velocity_recheck', 0x425215, 'ff92bc020000d94004d81d14d94800'),
('ceiling_recheck_strictly_upward', 0x425226, 'f6c4410f85a4000000'),
('ceiling_negative_velocity_times_double06', 0x42523E, 'd94004d9e0dc0df8e54800'),
('ceiling_minimum_downward85_compare', 0x42524D, 'd81df0e54800'),
('ceiling_minimum_downward85_write', 0x425255, 'f6c4417508c744240c0000aac2'),
('ceiling_write_velocity_xyz', 0x42528E, 'ff93c4020000'),
('ceiling_optional_sound_gate', 0x425294, '66833d88054f0000742b'),
('ceiling_sound_elapsed_compare', 0x42529E, 'd9875c1e0000d81d50d94800'),
('ceiling_sound47', 0x4252B1, '6a006a2f6affe8c4360300'),
('ceiling_sound_elapsed_reset', 0x4252BF, 'c7875c1e000000000000'),
('ceiling_cancellation_virtual', 0x4252CD, 'ff92e0010000'),
('cancellation_only_mode1', 0x425123, 'b801000000663986c4070000753c'),
('cancellation_jump_zero_mode1', 0x425139, '66c786e80700000000668986c4070000'),
('cancellation_fall_request', 0x425134, '68e8f34e00'),
('cancellation_owned_effect', 0x42514F, '8b86f807000085c0740a'),
('cancellation_effect_zero', 0x425159, 'c780c006000000000000'),
('cancellation_cleanup_virtual', 0x425167, 'ff9040010000'),
('cleanup_base_noop', 0x429913, 'e858900400'),
('cleanup_class_flags', 0x429918, 'c686641e000000c786681e000000000000'),
('cleanup_saved_sound', 0x429941, '8b863c0a000083f8ff7415'),
('cleanup_saved_sound_stop', 0x42994C, '6a0050e8acf00200'),
('cleanup_saved_sound_invalid', 0x429957, 'c7863c0a0000ffffffff'),
('distance_global_bypass', 0x4252E3, 'a010824f00'),
('distance_root_minus2500', 0x425335, 'd9442438d825e80c4a00'),
('distance_nearest_ray', 0x425379, 'e862730500'),
('distance_shadow_pointer_required', 0x4253AC, '8b8f2c0a000085c9'),
('distance_cache_store', 0x42547C, 'd99f6c080000'),
)
TABLES = (
('complete_ceiling_query', 0x4A3BDC, 0x1A4, 0x425170),
('complete_distance_query', 0x4A3BDC, 0x1A8, 0x4252E0),
('complete_ceiling_cancel', 0x4A3BDC, 0x1E0, 0x425120),
('complete_animation_request', 0x4A3BDC, 0xE0, 0x40DD90),
('complete_gadget_stop', 0x4A3BDC, 0x140, 0x429910),
('interface_velocity_get', 0x4A373C, 0x2BC, 0x471F10),
('interface_velocity_set', 0x4A373C, 0x2C4, 0x471F40),
('interface_root_get', 0x4A373C, 0x310, 0x472300),
)
CONSTANTS = (
('root_upward_span', 0x4A3E34, 'f', 150.0),
('bounce_coefficient', 0x48E5F8, 'd', 0.6),
('maximum_vertical_response', 0x48E5F0, 'f', -85.0),
('upward_threshold', 0x48D914, 'f', 0.0),
('sound_elapsed_threshold', 0x48D950, 'f', 5.0),
('shadow_downward_span', 0x4A0CE8, 'f', 2500.0),
)
BODY_HASHES = (
('ceiling_cancellation', 0x425120, 0x4F,
'170f31c1f88d09d9f9d4a5cfa69c2d34a77a477ca63bcbf5efe4d0409f6c61d2'),
('ceiling_query_response', 0x425170, 0x16C,
'8a2555793945f3b0eacecfefd8ffcd838c9820250b109d6120bf0a86ad32c153'),
('shadow_distance_refresh', 0x4252E0, 0x1AB,
'28482ee15afc15af43f344656b7262b1d61d68c667ebf5125bfb20b20328dc7d'),
('derived_cleanup', 0x429910, 0x53,
'eca3f8629cdc54c87fe4cb4e7b1f272099d10009a230711d2c1cbcf5fc37d382'),
)
@dataclass(frozen=True)
class CeilingState:
velocity: tuple[float, float, float]
mode: int
jump: int
fire_remaining: float | None
hold: float = 0
extra_acceleration: float = 0
queued_impulse: float = 0
def upward_segment(root):
"""425170 root to root+150Y, not a radius/capsule-top segment."""
if len(root) != 3 or not all(math.isfinite(v) for v in root):
raise ValueError('Finite XYZ root required')
return tuple(root), (root[0], root[1] + 150, root[2])
def ceiling_projection(state, hit):
"""Bounded response/cancel projection, not original execution or full wrapper.
hit is supplied by the separately recovered 47C210 static predicate. No
ownership input exists. Class sound/gadget cleanup callbacks are events only;
Python is not an x87 arithmetic oracle.
"""
if len(state.velocity) != 3 or not all(math.isfinite(v) for v in state.velocity):
raise ValueError('Finite velocity required')
if state.velocity[1] <= 0:
return state, ()
if not hit:
return state, ('query47C210',)
# Final store is float32; coefficient is the original stored double.
reflected = struct.unpack('<f', struct.pack('<f', min(-state.velocity[1] * 0.6, -85.0)))[0]
state = replace(state, velocity=(state.velocity[0], reflected, state.velocity[2]))
events = ('query47C210', 'velocity2C4', 'cancel1E0')
if state.mode == 1:
state = replace(state, jump=0,
fire_remaining=0 if state.fire_remaining is not None else None)
events += ('FALL', 'cleanup140')
return state, events
def collect(root):
pe = PE(find_case(Path(root), 'Neutron.exe'))
if hashlib.sha256(pe.data).hexdigest() != EXE_SHA256:
raise ValueError('Unsupported original EXE')
sites = []
for name, va, expected in SITES:
actual = read_va(pe, va, len(bytes.fromhex(expected))).hex()
if actual != expected:
raise ValueError(f'Changed instruction {name} at {va:#x}: {actual}')
sites.append(dict(name=name, va=hex(va), file_offset=hex(pe.offset(va-pe.base)), bytes=actual))
tables = []
for name, table, slot, target in TABLES:
if struct.unpack('<I', read_va(pe, table+slot, 4))[0] != target:
raise ValueError(f'Changed virtual target {name}')
tables.append(dict(name=name, table=hex(table), slot=hex(slot), target=hex(target),
file_offset=hex(pe.offset(table+slot-pe.base))))
constants = []
for name, va, fmt, expected in CONSTANTS:
value = struct.unpack('<'+fmt, read_va(pe, va, struct.calcsize(fmt)))[0]
if value != expected:
raise ValueError(f'Changed constant {name}')
constants.append(dict(name=name, va=hex(va), file_offset=hex(pe.offset(va-pe.base)),
format=fmt, value=value))
bodies = []
for name, va, length, expected in BODY_HASHES:
actual = hashlib.sha256(read_va(pe, va, length)).hexdigest()
if actual != expected:
raise ValueError(f'Changed body {name}')
bodies.append(dict(name=name, va=hex(va), file_offset=hex(pe.offset(va-pe.base)),
length=length, sha256=actual))
if read_va(pe, 0x4EF3E8, 5) != b'FALL\0':
raise ValueError('Changed FALL animation identity')
return dict(sha256=EXE_SHA256, sites=sites, tables=tables, constants=constants, bodies=bodies,
scope='Static source bytes and explicit projection; no original runtime or complete solver.')
if __name__ == '__main__':
cli = argparse.ArgumentParser(description=__doc__)
cli.add_argument('original', type=Path)
print(json.dumps(collect(cli.parse_args().original), indent=2))