#!/usr/bin/env python3 """Export exact Grappler rope, SWING morph and owned cursor artwork. Source geometry/pixels stay unchanged. Runtime owns first-length scaling, attachments, camera, sprite billboarding and helper lifetime. """ from __future__ import annotations import argparse from dataclasses import asdict import hashlib import json from pathlib import Path from ase import export_ase, read_ase from entities import resolve_asset from gam import parse from omt import Database, FormatError, read_canvas, write_png from png import read_png SOURCES = { 'ASE/rope01.ASE': '9929f5cba177b887a3b0c389e0783a3bc5dc2bc8c310d3394aa1156bd1f645fd', 'ASE/jimswing.ASE': '5e2f8ae134a7f35e440e1b8800bb354a6b1e77505df6515f04e34fdec330e768', 'ASE/jimstop.ASE': 'b294d090ac5c4ec449d845db6d837f2cbedf5f7b812f5b6ed2bb70f85246f1d0', 'png/jimycarl.png': '55ed165aaffe177cb62558a08d13fc3b2210da6e353065b108849d46df9dacb6', 'omt/RetainedSprites.omt': 'b584ffa4a38f4bc809b3ea8f4321e0c08f2719e22df41a3fb4cd143ed89b3bef', 'gam/VR08.gam': '8c52efbe95dc56cac136cb9137615261b2b3f84129aea25921e33a952c21738e', } CLIPS = (('rope01', None, 6, 6, (0,)), ('jimswing', 'jimstop', 426, 814, (0, 480))) JIMMY_RGBA_SHA256 = '5e54ccb99e9e060843cd7f80fd93ff8bf47f8779666e41d4240398354a278a3f' CURSOR_PAYLOAD_SHA256 = 'e0d92e1830031bd5ccdf5957779b874a4e7eae8821293ef3f082344da695712e' CURSOR_RGBA_SHA256 = '8d31014cb9b9661ebed902c864bf740f562460e74da3f6b86ed3d2be4d10f0ec' def digest(data: bytes) -> str: return hashlib.sha256(data).hexdigest() def _json(path: Path, value): path.write_text(json.dumps(value, separators=(',', ':'), allow_nan=False)+'\n', encoding='utf-8') def export_grappler(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') if output.exists() and any(p.is_symlink() for p in output.rglob('*')): raise ValueError('Generated Grappler paths must not contain symlinks') paths = {name: resolve_asset(original, name) for name in SOURCES} for name, path in paths.items(): if digest(path.read_bytes()) != SOURCES[name]: raise FormatError(f'Unexpected original Grappler source identity: {name}') animations = {stem: read_ase(paths[f'ASE/{stem}.ASE']) for stem in ('rope01', 'jimswing', 'jimstop')} for stem, base, vertices, faces, times in CLIPS: clip = animations[stem] if len(clip['objects']) != 1: raise FormatError('Unexpected Grappler animation object count') samples = clip['objects'][0]['samples'] if tuple(s['time'] for s in samples) != times or any( (len(s['vertices']), len(s['faces'])) != (vertices, faces) for s in samples): raise FormatError('Unexpected Grappler mesh or sample times') topology = animations[base]['objects'][0]['samples'][0] if base else samples[0] if len(topology['vertices']) != vertices or not topology['textureFaces']: raise FormatError('Grappler morph base lacks compatible textured topology') texture = read_png(paths['png/jimycarl.png']) rgba = texture['rgba'] if (texture['width'], texture['height'], len(rgba), digest(rgba)) != (256, 256, 256*256*4, JIMMY_RGBA_SHA256): raise FormatError('Unexpected Grappler atlas pixels') database = Database(paths['omt/RetainedSprites.omt']) entries = [e for e in database.entries if e.type == 'Canv' and e.id == 8] if len(entries) != 1: raise FormatError('Expected one owned Grappler cursor canvas') entry = entries[0] payload = database.payload(entry) if (entry.name, entry.offset, entry.size, digest(payload)) != ('target', 48796, 10034, CURSOR_PAYLOAD_SHA256): raise FormatError('Unexpected owned Grappler cursor metadata') canvas = read_canvas(payload) cursor_rgba = canvas['rgba'] if (canvas['width'], canvas['height'], len(cursor_rgba), digest(cursor_rgba)) != (64, 64, 64*64*4, CURSOR_RGBA_SHA256): raise FormatError('Unexpected Grappler cursor pixels') level = parse(paths['gam/VR08.gam'].read_bytes()) if len(level.entities) != 16 or level.entities[0].kind != '3JIM' or any(e.kind == '3GRA' for e in level.entities): raise FormatError('Unexpected VR08 owned-helper provenance') manifest = dict(schema=1, sources=SOURCES.copy(), animations=[], ropeAnimation='rope01/animation.json', swingAnimation='jimswing/animation.json', cursor=dict(id=8, name=entry.name, width=64, height=64, path='canvas_8.rgba', png='canvas_8.png', sourceOffset=entry.offset, sourceBytes=entry.size, sourceSha256=CURSOR_PAYLOAD_SHA256, rgbaSha256=CURSOR_RGBA_SHA256), bindings=dict(ropeAlias='HIROPE', swingAlias='HISWING', swingAction='SWING', ropeInitialRoll=180, ropeUpdateRoll=0, cursorInheritedSpriteSize=150, cursorWorldWidth=110, cursorWorldHeight=110, cursorFlags=0x248, cursorBlend=[6, 7], cursorAlign=[3, 3]), tutorial=dict(source=paths['gam/VR08.gam'].name, sha256=SOURCES['gam/VR08.gam'], entities=[dict(ordinal=i, **asdict(e)) for i, e in enumerate(level.entities)]), bindingPolicy='Exact resources/source defaults; runtime owns first-length shape scale, transforms, billboarding, timing and helper lifetime') output.mkdir(parents=True, exist_ok=True) (output/'jimycarl.rgba').write_bytes(rgba) (output/'canvas_8.rgba').write_bytes(cursor_rgba) write_png(output/'canvas_8.png', 64, 64, cursor_rgba) for stem, base, vertices, faces, times in CLIPS: animation = export_ase(paths[f'ASE/{stem}.ASE'], output/stem, paths[f'ASE/{base}.ASE'] if base else None) animation['texture'] = dict(path='../jimycarl.rgba', width=256, height=256, source=paths['png/jimycarl.png'].name, sourceSha256=SOURCES['png/jimycarl.png'], rgbaSha256=JIMMY_RGBA_SHA256) _json(output/stem/'animation.json', animation) manifest['animations'].append(dict(source=paths[f'ASE/{stem}.ASE'].name, path=f'{stem}/animation.json', indexedVertices=vertices, polygons=faces, sourceTimes=list(times))) _json(output/'grappler.json', manifest) return manifest if __name__ == '__main__': cli = argparse.ArgumentParser(description=__doc__) cli.add_argument('original', type=Path) cli.add_argument('--output', type=Path, required=True) args = cli.parse_args() export_grappler(args.original, args.output) print(f'Original Grappler resources exported: {args.output}/grappler.json')