#!/usr/bin/env python3 """Prepare only explicitly bound original static entity models and their materials.""" from __future__ import annotations import argparse import copy import hashlib import json from pathlib import Path from ase import expand_mesh, read_ase from entities import resolve_asset from gam import parse from omt import Database, FormatError, export_scene, read_material, read_shape from png import read_png def _digest(path: Path) -> str: return hashlib.sha256(path.read_bytes()).hexdigest() def _bounds(points: list) -> dict: if not points: raise FormatError('Original model has no source points for local bounds') return {'min': [min(p[i] for p in points) for i in range(3)], 'max': [max(p[i] for p in points) for i in range(3)]} def _one(database: Database, kind: str, identity: int): matches = [e for e in database.entries if e.type == kind and e.id == identity] if len(matches) != 1: raise FormatError(f'Missing or ambiguous {database.path.name}:{kind}:{identity}') return matches[0] def export_omt_model(database: Database, identity: int, output: Path) -> dict: """Index only the selected mesh and transitively referenced material/canvas.""" shape_entry = _one(database, '3DSh', identity) shape = read_shape(database.payload(shape_entry)) keys = {('3DSh', identity)} missing_materials = set() for polygon in shape['polygons']: link = polygon['material'] if link is None: continue # Null materials remain null, retaining invisible geometry. if link['type'] != '3DMa': raise FormatError(f'Unexpected shape material type {link["type"]}') matches = [e for e in database.entries if e.type == '3DMa' and e.id == link['id']] if not matches: # An unresolved original material stream link becomes a null pointer. # LV1B's invisible proxies name absent material18; preserve those # original polygons without inventing a visible replacement surface. missing_materials.add(link['id']) continue material_entry = _one(database, '3DMa', link['id']) keys.add(('3DMa', link['id'])) texture = read_material(database.payload(material_entry))['texture'] if texture is not None: if texture['type'] != 'Canv': raise FormatError(f'Unexpected material texture type {texture["type"]}') _one(database, 'Canv', texture['id']) keys.add(('Canv', texture['id'])) selected = copy.copy(database) selected.entries = [e for e in database.entries if (e.type, e.id) in keys] scene = export_scene(selected, output) return { 'kind': 'OMT', 'database': database.path.name, 'databaseSha256': _digest(database.path), 'shapeId': identity, 'shapeName': shape_entry.name, 'resources': [{'type': e.type, 'id': e.id, 'offset': e.offset, 'size': e.size, 'name': e.name} for e in selected.entries], 'vertices': len(scene['vertices']), 'triangles': len(scene['indices']) // 3, 'materials': len(scene['materials']), 'textures': len(scene['textures']), 'nullMaterialIds': sorted(missing_materials), 'localBounds': _bounds(shape['vertices']), } def export_ase_model(source: Path, texture_source: Path, output: Path) -> dict: """Keep first authored sample of each node, with the explicit game PNG override.""" animation = read_ase(source) scene = {'source': source.name, 'sha256': animation['sha256'], 'vertices': [], 'uvs': [], 'normals': [], 'indices': [], 'meshes': [], 'materials': [{'id': 0, 'texture': {'type': 'Canv', 'id': 0}, 'colours': {'diffuse': [1, 1, 1]}}], 'textures': []} for node in animation['objects']: sample = node['samples'][0] if not sample['textureFaces']: raise FormatError(f'Explicit static model lacks texture coordinates: {source.name}') mesh = expand_mesh(sample) first_vertex, first_index = len(scene['vertices']), len(scene['indices']) for field in ('vertices', 'uvs', 'normals'): scene[field].extend(mesh[field]) scene['indices'].extend(i + first_vertex for i in mesh['indices']) scene['meshes'].append({'name': node['name'], 'shapeId': len(scene['meshes']), 'firstIndex': first_index, 'indexCount': len(mesh['indices']), 'material': 0}) texture = read_png(texture_source) rgba = texture.pop('rgba') output.mkdir(parents=True, exist_ok=True) (output / 'texture.rgba').write_bytes(rgba) scene['textures'].append({'id': 0, 'path': 'texture.rgba', **texture}) (output / 'scene.json').write_text(json.dumps(scene, separators=(',', ':'), allow_nan=False) + '\n', encoding='utf8') return {'kind': 'ASE', 'ase': source.name, 'aseSha256': animation['sha256'], 'png': texture_source.name, 'pngSha256': _digest(texture_source), 'mode': 'First original authored sample; no morph playback implied', 'localBounds': _bounds([p for node in animation['objects'] for p in node['samples'][0]['vertices']]), 'vertices': len(scene['vertices']), 'triangles': len(scene['indices']) // 3, 'materials': 1, 'textures': 1} def prepare_models(original: Path, level_file: str, output: Path) -> dict: original, output = original.resolve(), output.resolve() if output.is_relative_to(original) or original.is_relative_to(output): raise FormatError('Output must be separate from the original installation') source = resolve_asset(original, 'gam/' + level_file) level = parse(source.read_bytes()) output.mkdir(parents=True, exist_ok=True) models, actors, unresolved, cache, databases = [], [], [], {}, {} def omt_model(name: str, identity: int) -> str: archive = resolve_asset(original, 'omt/' + name) key = ('OMT', str(archive), identity) if key not in cache: if archive not in databases: databases[archive] = Database(archive) database = databases[archive] model_key = f'model_{len(models):04d}' details = export_omt_model(database, identity, output / model_key) models.append({'key': model_key, 'path': model_key + '/scene.json', 'source': details}) cache[key] = model_key return cache[key] def ase_model(ase_name: str, png_name: str) -> str: ase = resolve_asset(original, 'ASE/' + ase_name) png = resolve_asset(original, 'png/' + png_name) key = ('ASE', str(ase), str(png)) if key not in cache: model_key = f'model_{len(models):04d}' details = export_ase_model(ase, png, output / model_key) models.append({'key': model_key, 'path': model_key + '/scene.json', 'source': details}) cache[key] = model_key return cache[key] for ordinal, entity in enumerate(level.entities): values, variants, evidence = entity.values(), {}, '' if entity.kind == '3OMT': database, identity = values.get('OmtDatabase'), values.get('OmtIndex') if not isinstance(database, str) or not isinstance(identity, int) or identity < 0: raise FormatError(f'3OMT has no explicit model at 0x{entity.offset:x}') variants['STATIC'] = omt_model(database, identity) evidence = 'Explicit GAM OmtDatabase/OmtIndex' elif entity.kind == '3PHO': # Verified original class binding, not the unrelated phone.ASE prop. variants['STATIC'] = omt_model('phone.omt', 0) evidence = 'EXE43579D opens phone.omt;4357C9 selects3DSh0' elif entity.kind == '3BUT': names = [values.get(key) for key in ('Up.ase', 'Down.ase', 'UpDown.Png')] if not all(isinstance(name, str) and name.casefold() not in ('none', '') for name in names): unresolved.append({'ordinal': ordinal, 'offset': entity.offset, 'kind': entity.kind, 'tag': values.get('ObjectTag'), 'reason': 'Button lacks a complete explicit ASE/PNG mapping; constructor fallback not inferred'}) continue variants['UP'] = ase_model(names[0], names[2]) variants['DOWN'] = ase_model(names[1], names[2]) evidence = 'Explicit GAM Up.ase/Down.ase/UpDown.Png; EXE411B93 initialUP' elif isinstance(values.get('ASEFile'), str) and isinstance(values.get('PNGFile'), str): if any(values[name].casefold() in ('none', '') for name in ('ASEFile', 'PNGFile')): unresolved.append({'ordinal': ordinal, 'offset': entity.offset, 'kind': entity.kind, 'tag': values.get('ObjectTag'), 'reason': 'Incomplete explicit ASEFile/PNGFile mapping; no default inferred'}) continue variants['STATIC'] = ase_model(values['ASEFile'], values['PNGFile']) evidence = 'Explicit GAM ASEFile/PNGFile (no class default inferred)' if variants: actors.append({'ordinal': ordinal, 'offset': entity.offset, 'tag': values.get('ObjectTag'), 'kind': entity.kind, 'defaultVariant': 'UP' if entity.kind == '3BUT' else 'STATIC', 'variants': variants, 'evidence': evidence}) report = {'schema': 1, 'source': source.relative_to(original).as_posix(), 'sha256': _digest(source), 'level': level.code, 'models': models, 'actors': actors, 'unresolved': unresolved, 'policy': 'Original local coordinates and source actor transforms; static samples only; no floor correction.'} (output / 'models.json').write_text(json.dumps(report, indent=2, allow_nan=False) + '\n', encoding='utf8') return report def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--original', type=Path, default=Path('../Original')) parser.add_argument('--level', default='level1b.gam') parser.add_argument('--output', type=Path, required=True) args = parser.parse_args() report = prepare_models(args.original, args.level, args.output) print(f"{report['level']}: {len(report['actors'])} authored model instances; {len(report['models'])} unique models") if __name__ == '__main__': main()