Files
Neutron/tools/binary/camera_handoff.py
T

133 lines
6.0 KiB
Python

#!/usr/bin/env python3
"""Read-only shared viewport/default/follow camera handoff evidence."""
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 = (
('default_already_jimmy', 0x447D54, 'a1e499500083c40c3bc6753f'),
('default_restore_raw_control', 0x447E27, 'ff9018010000'),
('default_local_snap', 0x447E2F, '680000fac3680000fa43'),
('default_transform_local', 0x447E42, 'ff9284030000'),
('default_current_viewport', 0x447E54, '8b0d509a5000'),
('default_current_eye_x', 0x447E5E, 'd94144'),
('default_current_eye_y', 0x447E61, 'd94148'),
('default_distance_sqrt', 0x447E95, 'd9fa'),
('default_strict_threshold', 0x447E9D, 'd81dace54800dfe0f6c441751e'),
('default_snap_x', 0x447EAA, 'd95944'),
('default_snap_y', 0x447EB6, 'd95848'),
('default_snap_z', 0x447EC3, 'd9594c'),
('walking_capture_shared_viewport', 0x438EF7, 'a1509a5000'),
('walking_previous_x', 0x438F03, 'd94044d99d14070000'),
('walking_previous_y', 0x438F17, 'd94148d99d18070000'),
('walking_previous_z', 0x438F26, 'd9424cd99d1c070000'),
('walking_ray_callback', 0x439714, 'ff966c010000'),
('walking_delta_previous_x', 0x439722, 'd8a514070000'),
('walking_delta_previous_y', 0x43972C, 'd8a518070000'),
('walking_delta_previous_z', 0x439742, 'd8a51c070000'),
('walking_normal_x_gain', 0x4397B1, 'd88d54080000'),
('walking_normal_y_gain', 0x4397C2, 'd98558080000dc05d02a4a00'),
('walking_normal_z_gain', 0x4397E7, 'd88d5c080000'),
('walking_add_shared_x', 0x439811, 'd800'),
('walking_add_shared_y', 0x43981D, 'd84004d95804'),
('walking_add_shared_z', 0x43982A, 'd84008d95808'),
('walking_aim_from_previous_x', 0x439834, 'd8a514070000'),
('walking_aim_from_previous_y', 0x43984D, 'd8a518070000'),
('walking_aim_from_previous_z', 0x43985B, 'd8a51c070000'),
('walking_angles_from_vector', 0x439865, 'ff1508d14800'),
('walking_angle_wrap', 0x4398A3, '25ff3f0000'),
('airborne_capture_shared_viewport', 0x439E33, 'a1509a5000'),
('airborne_previous_x', 0x439E40, 'd94044d95c243c'),
('airborne_previous_y', 0x439E47, 'd94048d95c2440'),
('airborne_previous_z', 0x439E4E, 'd9404c'),
('airborne_delta_previous_x', 0x439EF1, 'd864243c'),
('airborne_delta_previous_y', 0x439EF9, 'd8642440'),
('airborne_delta_previous_z', 0x439F0B, 'd8642444'),
('airborne_add_shared_x', 0x43A07D, 'd84044'),
('airborne_add_shared_y', 0x43A08F, 'd84004d95804'),
('airborne_add_shared_z', 0x43A099, 'd84008d95808'),
('airborne_aim_previous_x', 0x43A0A3, 'd8642444'),
('airborne_aim_previous_y', 0x43A0BA, 'd8642448'),
('airborne_aim_previous_z', 0x43A0C6, 'd864244c'),
('airborne_angles_from_vector', 0x43A0CE, 'ff1508d14800'),
('mca_shared_eye_x', 0x431677, 'a1509a5000d95844'),
('mca_shared_eye_y', 0x431683, '8b0d509a5000'),
('mca_shared_eye_z', 0x431694, '8b15509a5000'),
('camera_setup_eye_y', 0x437742, 'c781cc07000000004843'),
('camera_setup_eye_z', 0x437763, 'c781d00700000000afc3'),
)
TABLES = (
('Jimmy walking input/follow', 0x4A3BDC, 0x124, 0x438BC0),
('Jimmy airborne input/follow', 0x4A3BDC, 0x128, 0x439900),
('Jimmy camera setup', 0x4A3BDC, 0x170, 0x437740),
('Jimmy static ray callback', 0x4A3BDC, 0x16C, 0x43B820),
)
@dataclass(frozen=True)
class CameraProjection:
eye: tuple[float, float, float]
direction: tuple[float, float, float]
def default_handoff(view: CameraProjection, local_snap_world, already_jimmy: bool):
"""Bounded route projection of DEFAULT; orientation is not rewritten here."""
distance = math.sqrt(sum((a-b)**2 for a, b in zip(view.eye, local_snap_world, strict=True)))
if not already_jimmy and distance > 1000:
return replace(view, eye=tuple(local_snap_world))
return view
def normal_follow(view: CameraProjection, desired_eye, look_point, dt, ray_gain=1):
"""Walking normal-branch interpretation only, not source/x87 execution.
Desired-eye construction/special fields are intentionally supplied by caller;
static ray behavior is separately recovered. Aim uses the pre-update eye.
"""
gains = (1.6, 1.2, 1.6)
eye = tuple(old+(desired-old)*gain*ray_gain*dt
for old, desired, gain in zip(view.eye, desired_eye, gains, strict=True))
direction = tuple(look-old for look, old in zip(look_point, view.eye, strict=True))
return CameraProjection(eye, direction)
def collect(root):
pe = PE(find_case(Path(root), 'Neutron.exe'))
if hashlib.sha256(pe.data).hexdigest() != EXE_SHA256:
raise ValueError('Unsupported original EXE identity')
sites = []
for name, va, expected in SITES:
actual = read_va(pe, va, len(bytes.fromhex(expected))).hex()
if actual != expected:
raise ValueError(f'{name} changed 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, expected in TABLES:
target, = struct.unpack('<I', read_va(pe, table+slot, 4))
if target != expected:
raise ValueError(f'{name} callback changed')
tables.append(dict(name=name, table=hex(table), slot=hex(slot), target=hex(target),
file_offset=hex(pe.offset(table+slot-pe.base))))
threshold, = struct.unpack('<f', read_va(pe, 0x48E5AC, 4))
if threshold != 1000:
raise ValueError('DEFAULT threshold changed')
return dict(sha256=EXE_SHA256, sites=sites, tables=tables,
threshold=dict(va='0x48e5ac', file_offset=hex(pe.offset(0x48E5AC-pe.base)), value=threshold),
scope='Source-byte evidence plus bounded state projection; no original runtime execution.')
if __name__ == '__main__':
cli = argparse.ArgumentParser(description=__doc__)
cli.add_argument('original', type=Path)
print(json.dumps(collect(cli.parse_args().original), indent=2))