#!/usr/bin/env python3 """Guard original Carl talk admission and bootstrap boundaries, read-only.""" import argparse import hashlib import json from pathlib import Path import re import struct import sys from checkpoint_script_state import EXE_SHA256, find_case, read_va, rel_call_target from pe_inspect import PE sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'levels')) import gam REA_ID = 'ev_0d64cc58fa8f5573fc8e61e0f2c20e36288099212bd2257d3a3121361fd6c904' CALLS = ( ('AI contact base', 0x40a3cd, 0x470a90), ('Jimmy contact base', 0x422d50, 0x470a90), ('friend update runs AI first', 0x41b95d, 0x408000), ('Carl progress base', 0x413e50, 0x41b810), ('bootstrap constructs app', 0x46a641, 0x46b160), ('bootstrap selects MAIN', 0x46a660, 0x46a310), ('bootstrap MAIN factory', 0x46a33d, 0x44d0e0), ('MAIN constructor', 0x44d122, 0x459110), ('MAIN base constructor', 0x459168, 0x44d2b0), ('Jimmy game generic base constructor', 0x44d306, 0x4745a0), ('MAIN initialize', 0x4591c3, 0x4749a0), ('menu New Game task route', 0x404b54, 0x460e70), ('script RESTARTGAME task route', 0x40d08e, 0x460e70), ('Jimmy sole direct constructor caller', 0x45b1e2, 0x422160), ) SITES = ( ('contact target pointer', 0x40a44c, '8b860006000085'), ('contact exact target identity', 0x40a45a, '3bf80f8531020000'), ('contact state6 admission', 0x40a676, '83be08060000067514'), ('contact state setter', 0x40a68d, 'ff9224010000'), ('AI state0 target invisible flag', 0x40847c, '8a889d04000084'), ('friend resulting AI state', 0x41b972, '8b860806000085'), ('friend state0 talk', 0x41b988, 'ff9240010000'), ('talk progress before trigger', 0x41b7cd, 'ff9280010000'), ('talk synchronous trigger', 0x41b7d8, 'ff505c'), ('AI state owner store', 0x4089fe, '8986c8060000'), ('lab contact game callback is no-op', 0x475d40, '83c8ffc20800'), ('pair permissions are unconditional', 0x472f80, 'b001c20400'), ('pair enabled reads own collision byte', 0x471a30, '8a8140010000c3'), ('pair geometry reads own collision kind', 0x471720, '8b8124010000c3'), ('MAIN enum', 0x46a65b, '684e49414d'), ('menu NewGame task string', 0x404b4f, '681cc74e00'), ('RESTARTGAME NewGame task string', 0x40d089, '681cc74e00'), ) SLOTS = ( ('Carl contact game+C0', 0x495364, 0x40, 0x40a3c0), ('Jimmy counterpart contact', 0x4a373c, 0x40, 0x422d30), ('Jimmy pair permission', 0x4a373c, 0x3a0, 0x472f80), ('Carl pair permission', 0x495364, 0x3a0, 0x472f80), ('Jimmy collision enabled', 0x4a373c, 0x220, 0x471a30), ('Carl collision enabled', 0x495364, 0x220, 0x471a30), ('Jimmy collision kind', 0x4a373c, 0x178, 0x471720), ('Carl collision kind', 0x495364, 0x178, 0x471720), ('Carl duplicate contact membership', 0x495364, 0x3c, 0x470a60), ('Carl update game+C0', 0x495364, 0x3c4, 0x41b950), ('Carl prepare is NPC velocity helper', 0x495364, 0x410, 0x40a6b0), ('Carl AI state complete', 0x495804, 0x124, 0x4089b0), ('Carl talk complete', 0x495804, 0x140, 0x41b7b0), ('Carl trigger resolver complete', 0x495804, 0x17c, 0x41bb20), ('Carl progress complete', 0x495804, 0x180, 0x413e50), ('Lab contact game callback complete', 0x4c3cd0, 0x144, 0x475d40), ('MAIN InitGame complete', 0x4d1070, 0xb4, 0x44d3d0), ('app bootstrap database', 0x4d5ea0, 0x118, 0x46f760), ('app bootstrap image', 0x4d5ea0, 0x12c, 0x46f830), ) RANGES = ( ('contact', 0x40a3c0, 0x40a69a), ('duplicate contact', 0x470a60, 0x470a88), ('contact base', 0x470a90, 0x470b19), ('AI state', 0x4089b0, 0x408bc0), ('physical pair notifications', 0x47a430, 0x47a5a4), ('Jimmy counterpart contact', 0x422d30, 0x423575), ('AI update', 0x408000, 0x408980), ('friend update', 0x41b950, 0x41bb20), ('talk', 0x41b7b0, 0x41b831), ('talk resolver', 0x41bb20, 0x41bc20), ('Carl progress', 0x413e50, 0x413ea6), ('NPC velocity', 0x40a6b0, 0x40a6ca), ('bootstrap MAIN route', 0x46a310, 0x46a355), ('bootstrap selection', 0x46a641, 0x46a6b0), ('MAIN factory', 0x44d0e0, 0x44d161), ('MAIN constructor', 0x459110, 0x4591f7), ('MAIN base constructor', 0x44d2b0, 0x44d353), ('MAIN InitGame', 0x44d3d0, 0x44d491), ('bootstrap database', 0x46f760, 0x46f830), ('bootstrap image', 0x46f830, 0x46fa84), ) def collect(original, rea=None): original = Path(original) pe = PE(find_case(original, 'Neutron.exe')) if hashlib.sha256(pe.data).hexdigest() != EXE_SHA256: raise ValueError('Unexpected original executable') calls=[] for name, va, target in CALLS: if rel_call_target(pe, va) != target: raise ValueError(name) calls.append(dict(name=name, va=hex(va), file_offset=hex(pe.offset(va-pe.base)), target=hex(target))) sites=[] for name, va, expected in SITES: if read_va(pe,va,len(bytes.fromhex(expected))).hex()!=expected: raise ValueError(name) sites.append(dict(name=name, va=hex(va), file_offset=hex(pe.offset(va-pe.base)), bytes=expected)) slots=[] for name, table, slot, target in SLOTS: if struct.unpack('