179 lines
7.4 KiB
Python
179 lines
7.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Read-only heading evidence and bounded projection of OMT2 vector::angles.
|
|
|
|
This module executes no original code. --emit-probe emits a separate component
|
|
probe for the existing isolated copied-DLL reference-capture harness.
|
|
"""
|
|
from functools import lru_cache
|
|
from pathlib import Path
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import math
|
|
import struct
|
|
|
|
from pe_inspect import PE
|
|
|
|
EXE_SHA256 = 'f5281076e1a2492f2d61f87aae3f4544b6d0f535ebe33f9fbb6f78710fe1aea8'
|
|
DLL_SHA256 = '3ec41ead011adf56387a6dbd4580e5b5a9721b5f8020c8a56420f42824354943'
|
|
ANGLE_EXPORT = '?angles@OMedia3DVector@@QAEXAAF0@Z'
|
|
UNITS_TO_DEGREES = 0.02197265625
|
|
|
|
|
|
def f32(value):
|
|
return struct.unpack('<f', struct.pack('<f', value))[0]
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def half_acos_table():
|
|
# OMT2 init_table100EDC8C..100EDCD0; the final endpoint is explicitly zero.
|
|
return tuple(int(math.acos(i * 0.0001220703125 - 1.0)
|
|
* 2607.5945876921437 * 0.5) for i in range(16384)) + (0,)
|
|
|
|
|
|
def heading_units(x, z):
|
|
"""Second output of original vector::angles, finite float32 horizontal input.
|
|
|
|
Products/division deliberately use Python double, approximating the original
|
|
x87 extended intermediates. Values at a ratio integer boundary can need a
|
|
copied-DLL check; do not claim arbitrary-input bit equivalence.
|
|
"""
|
|
x, z = f32(x), f32(z)
|
|
if not (math.isfinite(x) and math.isfinite(z)):
|
|
raise ValueError('finite float32 components required')
|
|
if x == 0 and z == 0:
|
|
return 0
|
|
index = int(16384.0 * z * z / (x * x + z * z))
|
|
angle = half_acos_table()[index]
|
|
if z < 0:
|
|
angle = 8192 + angle if x < 0 else 8192 - angle
|
|
elif x < 0:
|
|
angle = 16384 - angle
|
|
return (-angle) & 0x3fff
|
|
|
|
|
|
def within_heading_window(x, z, jimmy_yaw):
|
|
"""Source426E40's strict comparisons; deliberately no seam wrapping."""
|
|
heading = heading_units(x, z) * UNITS_TO_DEGREES
|
|
yaw = f32(jimmy_yaw)
|
|
return yaw - 5 < heading < yaw + 5
|
|
|
|
|
|
def _read(pe, va, length):
|
|
offset = pe.offset(va - pe.base)
|
|
if offset < 0 or offset + length > len(pe.data):
|
|
raise ValueError('source range outside file')
|
|
return pe.data[offset:offset + length]
|
|
|
|
|
|
def evidence(executable, dll):
|
|
pe, omt = PE(executable), PE(dll)
|
|
for binary, expected in [(pe, EXE_SHA256), (omt, DLL_SHA256)]:
|
|
if hashlib.sha256(binary.data).hexdigest() != expected:
|
|
raise ValueError('source hash mismatch')
|
|
imported = next(i for i in pe.imports() if int(i['iat'], 16) == 0x48d108)
|
|
exported = next(e for e in omt.exports() if e['name'] == ANGLE_EXPORT)
|
|
if imported['name'] != ANGLE_EXPORT or int(exported['va'], 16) != 0x1001fda0:
|
|
raise ValueError('heading import/export mismatch')
|
|
guards = [
|
|
(pe, 0x4270bf, 'ff1508d14800'),
|
|
(pe, 0x4270d7, '0fbf542416'),
|
|
(pe, 0x4270df, 'd82550d94800'),
|
|
(pe, 0x4270ed, 'd80db4e54800'),
|
|
(pe, 0x4270f7, 'f6c4410f8510020000'),
|
|
(pe, 0x42711a, 'd80550d94800'),
|
|
(pe, 0x427132, 'f6c4010f84d5010000'),
|
|
(omt, 0x1001fdf5, '668b0c4d301e1410'),
|
|
(omt, 0x1001fe32, '8b542414f7d825ff3f0000668902'),
|
|
(omt, 0x10104e13, '80cc0c'),
|
|
(omt, 0x10104e1d, 'df7df4'),
|
|
(omt, 0x100edcaf, 'e850710100'),
|
|
(omt, 0x100edd0d, '6689ae08800200'),
|
|
]
|
|
for binary, va, pattern in guards:
|
|
if _read(binary, va, len(pattern) // 2).hex() != pattern:
|
|
raise ValueError(f'instruction mismatch at {va:#x}')
|
|
for binary, va, fmt, expected in [
|
|
(pe, 0x48d950, '<f', 5.0),
|
|
(pe, 0x48e5b4, '<f', UNITS_TO_DEGREES),
|
|
(omt, 0x10114850, '<f', -16384.0),
|
|
(omt, 0x10114588, '<d', 0.0001220703125),
|
|
(omt, 0x101166b8, '<d', 2607.5945876921437),
|
|
(omt, 0x10115448, '<d', 0.5)]:
|
|
if struct.unpack(fmt, _read(binary, va, struct.calcsize(fmt)))[0] != expected:
|
|
raise ValueError(f'constant mismatch at {va:#x}')
|
|
table_bytes = b''.join(struct.pack('<H', n) for n in half_acos_table())
|
|
fnv = 2166136261
|
|
for byte in table_bytes[:-2]:
|
|
fnv = ((fnv ^ byte) * 16777619) & 0xffffffff
|
|
if fnv != 0x4f800433:
|
|
raise ValueError('regenerated half-acos table fingerprint mismatch')
|
|
bodies = [(omt, 0x1001fda0, 0x1001ff6e),
|
|
(omt, 0x100edc00, 0x100edd2a),
|
|
(omt, 0x10104e04, 0x10104e2b),
|
|
(pe, 0x4270b1, 0x42713b)]
|
|
return dict(imported=imported, exported=exported,
|
|
sources=[dict(sha256=EXE_SHA256), dict(sha256=DLL_SHA256)],
|
|
bodies=[dict(va=hex(start), end_exclusive=hex(end),
|
|
file_offset=hex(binary.offset(start - binary.base)),
|
|
sha256=hashlib.sha256(_read(binary, start, end - start)).hexdigest())
|
|
for binary, start, end in bodies],
|
|
instruction_guards=len(guards), table_fnv32=f'{fnv:08x}',
|
|
table_sha256=hashlib.sha256(table_bytes).hexdigest())
|
|
|
|
|
|
PROBE = r'''/* Generated by shrink_heading.py; copied DLL only, isolated harness. */
|
|
#include <windows.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <math.h>
|
|
typedef void (__attribute__((thiscall)) *Angles)(const float*,short*,short*);
|
|
int main(void) {
|
|
setbuf(stdout,NULL);
|
|
HMODULE h=LoadLibraryA("Z:\\work\\game\\OMT2.dll");
|
|
if(!h){fprintf(stderr,"LoadLibrary failed %lu\n",GetLastError());return 2;}
|
|
Angles angles=(Angles)GetProcAddress(h,"?angles@OMedia3DVector@@QAEXAAF0@Z");
|
|
if(!angles)return 3;
|
|
float vectors[][4]={{0,0,0,0},{0,0,1,0},{-1,0,0,0},{0,0,-1,0},{1,0,0,0},
|
|
{-1,0,1,0},{-1,0,-1,0},{1,0,-1,0},{1,0,1,0},
|
|
{1,100,1000,0},{-1,-100,1000,0},{1000,0,1,0},{-1000,0,1,0},
|
|
{.1234567f,88,.7654321f,0},{-99.1f,-20,15.7f,0}};
|
|
for(unsigned i=0;i<sizeof(vectors)/sizeof(vectors[0]);i++) {
|
|
short pitch=-123,yaw=-123;angles(vectors[i],&pitch,&yaw);
|
|
printf("ANGLE %.9g %.9g %.9g pitch=%d yaw=%d\n",vectors[i][0],vectors[i][1],vectors[i][2],pitch,yaw);
|
|
}
|
|
/* Cover all lookup-bin interiors/quadrants with synthetic finite vectors. */
|
|
unsigned cases=0;uint32_t hash=2166136261u;
|
|
for(unsigned i=0;i<=16384;i++)for(unsigned quadrant=0;quadrant<4;quadrant++) {
|
|
float a=(float)sqrt((i+.25)/16384.5),b=(float)sqrt(1.-(i+.25)/16384.5);
|
|
float v[4]={quadrant&1?-b:b,0,quadrant&2?-a:a,0};
|
|
short pitch,yaw;angles(v,&pitch,&yaw);
|
|
hash=(hash^(uint16_t)yaw)*16777619u;cases++;
|
|
}
|
|
printf("HEADING_SWEEP cases=%u fnv_words=%08lx\n",cases,(unsigned long)hash);
|
|
puts("SHRINK_HEADING_COMPLETE: isolated copied DLL component, no game entry point");
|
|
FreeLibrary(h);return 0;
|
|
}
|
|
'''
|
|
|
|
|
|
def main():
|
|
cli = argparse.ArgumentParser(description=__doc__)
|
|
cli.add_argument('--exe', type=Path, default=Path('artifacts/uncontrolled-phases/rea/Neutron.exe'))
|
|
cli.add_argument('--dll', type=Path, default=Path('artifacts/asset-probe/game/OMT2.dll'))
|
|
cli.add_argument('--emit-probe', type=Path)
|
|
args = cli.parse_args()
|
|
report = evidence(args.exe, args.dll)
|
|
if args.emit_probe:
|
|
originals = Path(__file__).resolve().parents[3] / 'Original'
|
|
if args.emit_probe.resolve().is_relative_to(originals.resolve()):
|
|
cli.error('probe output must be separate from originals')
|
|
args.emit_probe.parent.mkdir(parents=True, exist_ok=True)
|
|
args.emit_probe.write_text(PROBE)
|
|
print(json.dumps(report, indent=2))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|