#!/usr/bin/env python3 """Read-only resource/tutorial inventory for Bubble and Invisible follow-up work. Reports serialized data and pixel/topology observations, not activation or AI rules. Writes only a caller-selected generated JSON file outside the original tree. """ from __future__ import annotations import argparse from collections import Counter from dataclasses import asdict import hashlib import json from pathlib import Path import struct import sys from ase import read_ase from entities import resolve_asset from gam import parse from omt import Database, FormatError, read_canvas, read_shape, write_png from png import read_png sys.path.insert(0, str(Path(__file__).resolve().parents[1]/'binary')) from pe_inspect import PE from checkpoint_script_state import read_va METER_SPANS = ( ('Neutron.exe', 'meter_init', 0x40174D, 0x40189C, 'cfeb0003633ab684e0a0bc68faf39414ef946fea6a879e983b3277899286323f'), ('Neutron.exe', 'sprite_place', 0x402D70, 0x402E2D, '448b8a739fa730ae9d46f4f952b7086e8fd383d6d4ac8c01f56caf0f82bb2704'), ('Neutron.exe', 'meter_tint', 0x403730, 0x4037F0, 'cc3172e68a085d9fd2901e93c620002dfc2f9526d22999bbf05a25cc595057a6'), ('Neutron.exe', 'meter_percent', 0x4037F0, 0x40385B, '434216bb132d7e1a69fc74fcfd68c9fe5953d1894098dd65aa1c91424926cd2e'), ('OMT2.dll', 'canvas_render', 0x10029430, 0x1002959A, '041ce8342d11d4a3f553cdacc66b33381f1eff686b61311afb48bf3b1396c4bb'), ('OMT2.dll', 'canvas_update_align', 0x10029870, 0x1002992A, 'cb833afae3132776df9fd123ccad03f713d73d58058f06698b57d8ffb3df0387'), ('OMT2.dll', 'canvas_dimensions', 0x10029930, 0x1002998D, 'fb5be6cee77db2a3988a2b6e979dcc0083d48281686f7b855a2119f18783298a'), ('OMT2.dll', 'draw_surface', 0x10037AC0, 0x1003818C, 'c76738fe3dfb7556283654a233071be1d68b08aecff277a46535e3a0913d30e3'), ) def meter_code_contract(original: Path) -> dict: """Guard REA-backed original code; no target code is executed.""" expected = {'Neutron.exe': 'f5281076e1a2492f2d61f87aae3f4544b6d0f535ebe33f9fbb6f78710fe1aea8', 'OMT2.dll': '3ec41ead011adf56387a6dbd4580e5b5a9721b5f8020c8a56420f42824354943'} images = {name: PE(resolve_asset(original, name)) for name in expected} for name, image in images.items(): if digest(image.data) != expected[name]: raise FormatError(f'Unexpected meter source image: {name}') spans = [] for name, label, start, end, sha256 in METER_SPANS: image = images[name] if digest(read_va(image, start, end-start)) != sha256: raise FormatError(f'Unexpected source meter span: {label}') spans.append(dict(image=name, label=label, start=hex(start), end=hex(end), fileOffset=image.offset(start-image.base), bytes=end-start, sha256=sha256)) exe = images['Neutron.exe'] multiplier = struct.unpack(' str: return hashlib.sha256(data).hexdigest() def resource(database: Database, kind: str, identity: int) -> dict: matches = [e for e in database.entries if e.type == kind and e.id == identity] if len(matches) != 1: raise FormatError(f'Expected one {database.path.name}:{kind}:{identity}') entry = matches[0] payload = database.payload(entry) result = dict(archive=database.path.name, archiveSha256=digest(database.data), **asdict(entry), payloadSha256=digest(payload)) if kind == 'Canv': canvas = read_canvas(payload) rgba = canvas.pop('rgba') result.update(canvas=canvas, rgbaSha256=digest(rgba), alphaCounts=dict(sorted(Counter(rgba[3::4]).items()))) return result def tutorial(original: Path, name: str) -> dict: source = resolve_asset(original, f'gam/{name}.gam') data = source.read_bytes() level = parse(data) database = Database(resolve_asset(original, f'omt/{name}.omt')) meshes = [] for entry in database.entries: if entry.type != '3DSh': continue shape = read_shape(database.payload(entry)) vertices = shape['vertices'] meshes.append(dict(**asdict(entry), vertices=len(vertices), polygons=len(shape['polygons']), triangles=sum(max(0, len(p['corners'])-2) for p in shape['polygons']), bounds=dict(min=[min(v[i] for v in vertices) for i in range(3)], max=[max(v[i] for v in vertices) for i in range(3)]))) # Preserve duplicate tags and literal patrol links; do not silently repair # malformed/missing references or identify distinct actors by tag alone. records = [dict(ordinal=index, kind=e.kind, offset=e.offset, tag=e.values().get('ObjectTag', ''), properties=[asdict(p) for p in e.properties]) for index, e in enumerate(level.entities)] tags = Counter(str(r['tag']).casefold() for r in records) return dict(level=level.code, source=f'gam/{source.name}', bytes=len(data), sha256=digest(data), counts=dict(sorted(Counter(e.kind for e in level.entities).items())), duplicateTags={tag: count for tag, count in sorted(tags.items()) if count > 1}, archive=database.path.name, archiveSha256=digest(database.data), meshes=meshes, entities=records) def inspect(original: Path) -> dict: original = original.resolve() screens = Database(resolve_asset(original, 'omt/screens.omt')) retained = Database(resolve_asset(original, 'omt/RetainedSprites.omt')) voice = Database(resolve_asset(original, 'omt/voicepractice.omt')) sounds = Database(resolve_asset(original, 'omt/soundeffects.omt')) texture_source = resolve_asset(original, 'png/jimycarl.png') texture = read_png(texture_source) rgba = texture.pop('rgba') clips = [] for stem in ('jimstop', 'jimrun', 'jimbackpedal', 'jimjump', 'jimfall', 'jimfly', 'jimshoot'): source = resolve_asset(original, f'ASE/{stem}.ASE') clip = read_ase(source) clips.append({key: value for key, value in clip.items() if key != 'objects'} | dict( nodes=[dict(name=o['name'], samples=len(o['samples']), times=[s['time'] for s in o['samples']], vertices=len(o['samples'][0]['vertices']), faces=len(o['samples'][0]['faces']), uvs=len(o['samples'][0]['uvs']), textureFaces=len(o['samples'][0]['textureFaces'])) for o in clip['objects']])) return dict(schema=1, policy='Serialized source data and decoded pixels/topology only; no gameplay or AI inference.', resources=[resource(screens, 'Canv', i) for i in (11, 13, 21, 22, 47, 52, 88, 94, 109, 111)] + [resource(retained, 'Canv', 1)] + [resource(voice, 'Wave', i) for i in (20, 21, 22, 23, 24, 37, 38)] + [resource(sounds, 'Wave', i) for i in (2, 3, 106)], jimmyTexture=dict(source=f'png/{texture_source.name}', sha256=digest(texture_source.read_bytes()), **texture, rgbaSha256=digest(rgba), alphaCounts=dict(sorted(Counter(rgba[3::4]).items()))), jimmyClips=clips, meterCode=meter_code_contract(original), levels=[tutorial(original, name) for name in ('VR01', 'VR03')]) def write_report(original: Path, output: Path) -> dict: original, output = original.resolve(), output.resolve() if output.is_relative_to(original) or original.is_relative_to(output): raise ValueError('Output must be separate from the original installation') report = inspect(original) output.parent.mkdir(parents=True, exist_ok=True) output.write_text(json.dumps(report, indent=2, allow_nan=False)+'\n', encoding='utf-8') return report def export_meter(original: Path, output: Path) -> dict: """Export only the two source-bound recharge meter canvases, without tinting. Source 401430 binds 4F7CB0/4F8028 to screens.omt21/22. Gameplay controls visibility/percent/tint; output does not bake those changes into the pixels. """ original, output = original.resolve(), output.resolve() if output.is_relative_to(original) or original.is_relative_to(output): raise ValueError('Output must be separate from the original installation') database = Database(resolve_asset(original, 'omt/screens.omt')) recovered = [] for identity, expected in ((21, 'meter'), (22, 'metercolor')): record = resource(database, 'Canv', identity) if record['name'] != expected or (record['canvas']['width'], record['canvas']['height']) != (30, 110): raise FormatError(f'Unexpected source meter canvas {identity}') entry = next(e for e in database.entries if e.type == 'Canv' and e.id == identity) canvas = read_canvas(database.payload(entry)) recovered.append((record, canvas['rgba'])) # Validate both identities before producing any generated files. output.mkdir(parents=True, exist_ok=True) textures = [] for record, rgba in recovered: name = f"canvas_{record['id']}" (output/(name+'.rgba')).write_bytes(rgba) write_png(output/(name+'.png'), 30, 110, rgba) textures.append(dict(id=record['id'], name=record['name'], width=30, height=110, path=name+'.rgba', png=name+'.png', sourceOffset=record['offset'], sourceBytes=record['size'], sourceSha256=record['payloadSha256'], rgbaSha256=record['rgbaSha256'])) manifest = dict(schema=1, referenceWidth=640, referenceHeight=480, source=database.path.name, sourceSha256=digest(database.data), textures=textures) (output/'meter.json').write_text(json.dumps(manifest, indent=2)+'\n', encoding='utf-8') return manifest def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('original', type=Path) parser.add_argument('--output', type=Path, required=True) parser.add_argument('--meter-output', type=Path, help='Optional separate generated meter texture directory') args = parser.parse_args() report = write_report(args.original, args.output) if args.meter_output: export_meter(args.original, args.meter_output) print(f"{len(report['resources'])} resource identities, {len(report['jimmyClips'])} Jimmy clips, two tutorials: {args.output}") if __name__ == '__main__': main()