Files
Neutron/tools/binary/collision_grid_traversal.py

152 lines
6.6 KiB
Python

#!/usr/bin/env python3
"""Read-only source grid/dispatcher anchors and bounded ordering fixtures."""
from __future__ import annotations
import argparse
from dataclasses import dataclass
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
BODIES = (
('static_dispatcher', 0x47B4B0, 0x47B889, 'da4ca56149636d5fc5059403a0ef81a0007c96972e3c12f2a8dedfa7568dd160'),
('register_cell', 0x479800, 0x47991C, '078ce274b0135b7fb28ada2e1fb171817683d1435a96916e22253466956b0044'),
('polygon_filter', 0x4797A0, 0x4797FD, 'a654906ad9c89a4744e78eee2a1745e59f9978ed4d28948d18e2a14abba2f63a'),
('corner_accessor', 0x477F10, 0x477F5F, 'b5460f170b2315a462700d585b0626321186977a7bc192ede831a1e1ba60677c'),
)
SITES = (
('delta_magnitude', 0x47B4BB, 'ff1504d14800'),
('force_static_processing_field', 0x47B4C9, '8a812c04000084c07515'),
('epsilon_threshold', 0x47B4D7, 'd81d405e4d00dfe0f6c4010f8599030000'),
('dispatcher_call_statistic', 0x47B4E8, '8b15785d6900'),
('candidate_x', 0x47B507, 'd803d95c2434'),
('candidate_y', 0x47B511, 'd84304d95c2438'),
('candidate_z', 0x47B51C, 'd84308d95c243c'),
('radius_getter', 0x47B523, 'ff9060020000'),
('expansion_radius_plus_magnitude', 0x47B529, 'd9542420d844241c'),
('x_upper_uses_max', 0x47B59B, '663bc78944241c7f04897c241c'),
('z_upper_uses_min', 0x47B61B, '663bf8897c2418ddd87c0489442418'),
('wall_grid_cell', 0x47B697, '8b048d249b5000'),
('per_node_collision_gate', 0x47B6B2, '803de5694f0000'),
('allow_lower_edges', 0x47B6F4, 'c705745d690001000000'),
('forward_edges_flag', 0x47B6FE, '8b442470'),
('call_polygon_wrapper', 0x47B72E, 'e87d040000'),
('clear_lower_edges', 0x47B742, 'c705745d690000000000'),
('next_fifo_node', 0x47B74C, '8b6d00'),
('increment_z', 0x47B76A, '4241663b542418'),
('increment_x', 0x47B77F, '4681c704010000'),
('retry_flag_branch', 0x47B799, '8a44241384c00f8597000000'),
('publish_candidate_x', 0x47B7B1, '8903'),
('publish_candidate_yz', 0x47B7B7, '894b04895308'),
('last_accepted_polygon', 0x47B7C0, '8b0d4c5d6900'),
('last_normal_threshold', 0x47B7E3, 'd81d48314a00'),
('normal_callback374', 0x47B80F, 'ff9774030000'),
('normal_callback370', 0x47B82E, 'ff9770030000'),
('retry_delta_x', 0x47B83C, 'd9442434d823'),
('retry_call_same_dispatcher', 0x47B877, '5350e832fcffff'),
('wrapper_stores_last_polygon_and_retry', 0x47BD65, '89354c5d6900c60101'),
('physics_clear_last_polygon', 0x47A038, '891d4c5d6900'),
('physics_face_distance130000', 0x47A03E, 'c7055c5d690000e8fd47'),
('physics_edge_distance_radius', 0x47A062, 'd91de0fb5c00'),
('dynamic_face_distance_minus1', 0x47AB4B, 'c7055c5d6900000080bf'),
('dynamic_reclip_edges_false', 0x47B30B, '6a00'),
('dynamic_reclip_dispatch', 0x47B364, 'e847010000'),
('registration_checkpoint_filter', 0x4791C9, 'e8f2020000'),
('registration_game_filter', 0x4791DB, 'e8c0050000'),
('registration_polygon_order', 0x479479, '83c530'),
('game_polygon_consumer', 0x4797EC, 'ff9234010000'),
('cell_duplicate_polygon_only', 0x47985E, '8b55008b78083b17'),
('cell_fifo_append', 0x47989F, '8958048947048b48048901'),
('wall_normal_strict', 0x4798BD, 'd81d74d94800'),
('corner_records_stride20', 0x477F1C, '8d04808b5124'),
('shape_vertices_pointer', 0x477F29, '8b5168c1e00403c2'),
)
SCALE = struct.unpack('<f', struct.pack('<f', .002))[0]
EPSILON = struct.unpack('<f', bytes.fromhex('00000034'))[0]
def cell(value):
value = math.trunc(value*SCALE+130)
return (value+32768) % 65536 - 32768
def traversal_bounds(previous, delta, radius, magnitude):
"""Source bounds with supplied quick_magnitude result; x87 boundaries unprobed."""
extent = radius+magnitude
candidate = tuple(a+b for a, b in zip(previous, delta, strict=True))
x0, x1 = cell(previous[0]-extent), cell(candidate[0]-extent)
x2, x3 = cell(previous[0]+extent), cell(candidate[0]+extent)
z0, z1 = cell(previous[2]-extent), cell(candidate[2]-extent)
z2, z3 = cell(previous[2]+extent), cell(candidate[2]+extent)
return min(x0,x1), max(x2,x3), min(z0,z1), min(z2,z3)
def cells(bounds):
x0, x1, z0, z1 = bounds
return [(x,z) for x in range(max(0,x0), min(259,x1)+1)
for z in range(max(0,z0), min(259,z1)+1)]
def visits(grid, bounds):
"""Frozen-cell order, including repeated registrations in different cells."""
return [(xy, polygon) for xy in cells(bounds) for polygon in grid.get(xy, ())]
def append_registration(grid, xy, polygon, shape):
"""479800 compares polygon identity alone within one cell, then appends FIFO."""
registered = grid.setdefault(xy, [])
if any(item[0] == polygon for item in registered):
return False
registered.append((polygon, shape))
return True
@dataclass
class NearestProjection:
face: float
edge: float
last_polygon: int | None = None
def scan_projection(grid, bounds, candidate, nearest, visitor):
"""One dispatcher pass with an injected narrowphase; not original execution."""
changed = False
for xy, polygon in visits(grid, bounds):
candidate, hit = visitor(xy, polygon, candidate, nearest)
changed |= hit
if hit:
nearest.last_polygon = polygon[0]
return candidate, changed
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, bodies = [], []
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))
for name, begin, end, expected in BODIES:
digest = hashlib.sha256(read_va(pe, begin, end-begin)).hexdigest()
if digest != expected:
raise ValueError(f'{name} body changed: {digest}')
bodies.append(dict(name=name, va=hex(begin), file_offset=hex(pe.offset(begin-pe.base)), length=end-begin, sha256=digest))
return dict(sha256=EXE_SHA256, sites=sites, bodies=bodies,
constants={hex(a):struct.unpack('<f',read_va(pe,a,4))[0] for a in (0x4D5E40,0x4D6A04,0x49B1FC,0x4A3148)},
scope='Static source-byte checks and local algorithm projection; no original grid 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))