Reconstruct lab story, interactions, patrol and jet pack practice flow
This commit is contained in:
1 parent
58424120bb
commit
c929f942e1
84 files changed
+7957
-305
No files matched your search
@@ -0,0 +1,196 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Prepare source-backed GAM entity references without assigning gameplay rules.
|
||||
|
||||
Keeps source order/offsets, duplicate tags and exact typed property values. Optional
|
||||
resource decoding exports only resources named by explicit database/index pairs.
|
||||
It does not turn editor icons into rendered actors or infer class asset defaults.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from collections import Counter, defaultdict
|
||||
from dataclasses import asdict
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import struct
|
||||
import sys
|
||||
import wave
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'levels'))
|
||||
from gam import parse
|
||||
from omt import Database, FormatError, read_canvas, read_shape, safe_name, write_png
|
||||
|
||||
|
||||
SENTINELS = {'none', 'null', 'default', 'end', ''}
|
||||
OBJECT_LINKS = {
|
||||
'StartTrigger', 'NextTrigger', 'ToggleObject', 'ActivateObject', 'CameraTarget',
|
||||
'PlayerControlled', 'FaceObject', 'AITarget', 'AIPatrol', 'AINewPos', 'PatrolPoint',
|
||||
'NextPatrolPoint', 'CallObjectTag', 'ActivateBy', 'PickupLink', 'ActivateButton',
|
||||
}
|
||||
|
||||
|
||||
def resolve_asset(root: Path, relative: str) -> Path:
|
||||
"""Portable case-insensitive lookup, rejecting traversal and ambiguities."""
|
||||
root = root.resolve()
|
||||
current = root
|
||||
parts = relative.replace('\\', '/').split('/')
|
||||
if not relative or any(p in ('', '.', '..') for p in parts):
|
||||
raise FormatError(f'Invalid asset path: {relative!r}')
|
||||
for part in parts:
|
||||
matches = [p for p in current.iterdir() if p.name.casefold() == part.casefold()]
|
||||
if len(matches) != 1:
|
||||
raise FormatError(f'Missing or ambiguous asset: {relative}')
|
||||
current = matches[0]
|
||||
if not current.resolve().is_relative_to(root):
|
||||
raise FormatError(f'Asset escapes original root: {relative}')
|
||||
return current
|
||||
|
||||
|
||||
def _object_link(name: str) -> bool:
|
||||
return name in OBJECT_LINKS or bool(re.fullmatch(r'(CameraTarget|ActivateObject)\d+', name))
|
||||
|
||||
|
||||
def resource_pairs(values: dict):
|
||||
"""Explicit serialized pairings only; no implicit global sound bank assumption."""
|
||||
for prefix, resource_type in (('Sprite', 'Canv'), ('Music', 'Wave'), ('Omt', '3DSh')):
|
||||
database, index = values.get(prefix + 'Database'), values.get(prefix + 'Index')
|
||||
if isinstance(database, str) and isinstance(index, int) and index >= 0 and database.casefold() not in SENTINELS:
|
||||
yield prefix + 'Index', database, resource_type, index
|
||||
database = values.get('SpriteDatabase')
|
||||
if isinstance(database, str) and database.casefold() not in SENTINELS:
|
||||
for name, index in values.items():
|
||||
if re.fullmatch(r'Sprite[1-9]', name) and isinstance(index, int) and index >= 0:
|
||||
yield name, database, 'Canv', index
|
||||
database = values.get('SoundDatabase')
|
||||
if isinstance(database, str) and database.casefold() not in SENTINELS:
|
||||
for name, index in values.items():
|
||||
if re.fullmatch(r'SoundIndex\d*', name) and isinstance(index, int) and index >= 0:
|
||||
yield name, database, 'Wave', index
|
||||
|
||||
|
||||
def prepare_entities(original: Path, level_file: str, output: Path, decode: bool = False) -> dict:
|
||||
original = original.resolve()
|
||||
output = output.resolve()
|
||||
if output.is_relative_to(original) or original.is_relative_to(output):
|
||||
raise FormatError('Output must be separate from the original directory')
|
||||
source = resolve_asset(original, 'gam/' + level_file)
|
||||
data = source.read_bytes()
|
||||
level = parse(data)
|
||||
output.mkdir(parents=True, exist_ok=True)
|
||||
tags = defaultdict(list)
|
||||
for ordinal, entity in enumerate(level.entities):
|
||||
tag = entity.values().get('ObjectTag', '')
|
||||
tags[str(tag).casefold()].append(ordinal)
|
||||
databases, resources = {}, {}
|
||||
entities = []
|
||||
for ordinal, entity in enumerate(level.entities):
|
||||
values = entity.values()
|
||||
record = {
|
||||
'ordinal': ordinal, 'kind': entity.kind, 'offset': entity.offset,
|
||||
'tag': values.get('ObjectTag'),
|
||||
'position': [values.get('Position' + axis, 0) for axis in 'XYZ'],
|
||||
'rotation': [values.get('Rotation' + axis, 0) for axis in 'XYZ'],
|
||||
'properties': values,
|
||||
'propertyEvidence': {p.name: {'offset': p.offset, 'type': p.type, 'length': p.length, 'raw': p.raw}
|
||||
for p in entity.properties},
|
||||
'objectReferences': [], 'resourceReferences': [], 'fileReferences': [],
|
||||
}
|
||||
for name, value in values.items():
|
||||
if _object_link(name) and isinstance(value, str) and value.casefold() not in SENTINELS:
|
||||
candidates = tags.get(value.casefold(), [])
|
||||
record['objectReferences'].append({'property': name, 'tag': value,
|
||||
'candidateOrdinals': candidates})
|
||||
if isinstance(value, str) and value.lower().endswith(('.ase', '.png')):
|
||||
folder = 'ASE' if value.lower().endswith('.ase') else 'png'
|
||||
try:
|
||||
path = resolve_asset(original, folder + '/' + value)
|
||||
record['fileReferences'].append({'property': name, 'source': path.relative_to(original).as_posix(),
|
||||
'sha256': hashlib.sha256(path.read_bytes()).hexdigest()})
|
||||
except FormatError as error:
|
||||
record['fileReferences'].append({'property': name, 'source': value, 'unresolved': str(error)})
|
||||
for property_name, database_name, resource_type, identity in resource_pairs(values):
|
||||
database_path = resolve_asset(original, 'omt/' + database_name)
|
||||
canonical = database_path.relative_to(original).as_posix()
|
||||
if canonical not in databases:
|
||||
databases[canonical] = Database(database_path)
|
||||
database = databases[canonical]
|
||||
key = (canonical, resource_type, identity)
|
||||
if key not in resources:
|
||||
matches = [e for e in database.entries if e.type == resource_type and e.id == identity]
|
||||
resource = {'database': canonical, 'type': resource_type, 'id': identity,
|
||||
'databaseSha256': hashlib.sha256(database.data).hexdigest()}
|
||||
if len(matches) != 1:
|
||||
resource['unresolved'] = 'No matching resource in explicitly named database'
|
||||
else:
|
||||
entry = matches[0]
|
||||
resource.update(name=entry.name, offset=entry.offset, size=entry.size)
|
||||
if decode:
|
||||
_decode(database, entry, resource, output)
|
||||
resources[key] = resource
|
||||
record['resourceReferences'].append({'property': property_name, 'database': canonical,
|
||||
'type': resource_type, 'id': identity})
|
||||
entities.append(record)
|
||||
report = {
|
||||
'schema': 1, 'source': source.relative_to(original).as_posix(),
|
||||
'sha256': hashlib.sha256(data).hexdigest(), 'level': level.code,
|
||||
'policy': 'Exact source entity data and explicit references only; gameplay/visibility rules are not inferred.',
|
||||
'counts': dict(sorted(Counter(e.kind for e in level.entities).items())),
|
||||
'duplicateTags': {tag: ids for tag, ids in sorted(tags.items()) if len(ids) > 1},
|
||||
'entities': entities, 'resources': [resources[key] for key in sorted(resources)],
|
||||
}
|
||||
(output / 'entities.json').write_text(json.dumps(report, indent=2, allow_nan=False) + '\n', encoding='utf8')
|
||||
return report
|
||||
|
||||
|
||||
def _decode(database, entry, resource: dict, output: Path):
|
||||
folder = output / 'resources' / safe_name(database.path.stem)
|
||||
folder.mkdir(parents=True, exist_ok=True)
|
||||
stem = f'{entry.type}_{entry.id:08x}_{safe_name(entry.name)}'
|
||||
payload = database.payload(entry)
|
||||
if entry.type == 'Canv':
|
||||
image = read_canvas(payload)
|
||||
rgba = image.pop('rgba')
|
||||
raw, png = folder / (stem + '.rgba'), folder / (stem + '.png')
|
||||
raw.write_bytes(rgba)
|
||||
write_png(png, image['width'], image['height'], rgba)
|
||||
resource.update(image, path=raw.relative_to(output).as_posix(), png=png.relative_to(output).as_posix())
|
||||
elif entry.type == '3DSh':
|
||||
path = folder / (stem + '.json')
|
||||
path.write_text(json.dumps(read_shape(payload), allow_nan=False) + '\n', encoding='utf8')
|
||||
resource['path'] = path.relative_to(output).as_posix()
|
||||
elif entry.type == 'Wave':
|
||||
start = payload.find(b'RIFF')
|
||||
if start < 0 or payload[start + 8:start + 12] != b'WAVE' or start + 12 > len(payload):
|
||||
raise FormatError(f'Wave resource is missing bounded RIFF: {entry.id}')
|
||||
size = struct.unpack_from('<I', payload, start + 4)[0] + 8
|
||||
if size < 12 or start + size > len(payload):
|
||||
raise FormatError(f'RIFF outside resource: {entry.id}')
|
||||
audio = payload[start:start + size]
|
||||
path = folder / (stem + '.wav')
|
||||
path.write_bytes(audio)
|
||||
resource['path'] = path.relative_to(output).as_posix()
|
||||
try:
|
||||
with wave.open(io.BytesIO(audio)) as wav:
|
||||
resource['audio'] = {'rate': wav.getframerate(), 'channels': wav.getnchannels(),
|
||||
'sampleWidth': wav.getsampwidth(), 'frames': wav.getnframes(),
|
||||
'seconds': wav.getnframes() / wav.getframerate()}
|
||||
except wave.Error:
|
||||
resource['audio'] = {'encoding': 'non-PCM RIFF retained unchanged'}
|
||||
|
||||
|
||||
def main():
|
||||
cli = argparse.ArgumentParser(description=__doc__)
|
||||
cli.add_argument('--original', type=Path, default=Path('../Original'))
|
||||
cli.add_argument('--level', default='level1b.gam')
|
||||
cli.add_argument('--output', type=Path, required=True)
|
||||
cli.add_argument('--decode', action='store_true')
|
||||
args = cli.parse_args()
|
||||
report = prepare_entities(args.original, args.level, args.output, args.decode)
|
||||
print(f"{report['level']}: {len(report['entities'])} source entities, {len(report['resources'])} explicit resource references")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,194 @@
|
||||
#!/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()
|
||||
@@ -265,12 +265,13 @@ def export_scene(database: Database, output: Path):
|
||||
scene = {'source': database.path.name, 'sha256': hashlib.sha256(database.data).hexdigest(),
|
||||
'coordinates': 'Original OMT world coordinates, unchanged',
|
||||
'vertices': [], 'uvs': [], 'normals': [], 'indices': [], 'meshes': [],
|
||||
'materials': [], 'textures': [], 'warnings': []}
|
||||
'materials': [], 'textures': [], 'warnings': [], 'collisionPolygons': []}
|
||||
material_names = {entry.id: entry.name for entry in database.entries if entry.type == '3DMa'}
|
||||
for entry in database.entries:
|
||||
payload = database.payload(entry)
|
||||
if entry.type == '3DSh':
|
||||
shape = read_shape(payload)
|
||||
for poly in shape['polygons']:
|
||||
for polygon_index, poly in enumerate(shape['polygons']):
|
||||
first_index = len(scene['indices'])
|
||||
first_vertex = len(scene['vertices'])
|
||||
for corner in poly['corners']:
|
||||
@@ -280,6 +281,8 @@ def export_scene(database: Database, output: Path):
|
||||
scene['normals'].append(shape['normals'][ni] if ni < len(shape['normals']) else poly['normal'])
|
||||
for i in range(1, len(poly['corners'])-1):
|
||||
scene['indices'].extend((first_vertex, first_vertex+i, first_vertex+i+1))
|
||||
scene['collisionPolygons'].append({'shapeId': entry.id, 'polygonIndex': polygon_index,
|
||||
'normal': poly['normal'], 'materialName': material_names.get(poly['material']['id']) if poly['material'] else None})
|
||||
if len(poly['corners']) > 3:
|
||||
scene['warnings'].append(f'{entry.id}: polygon with {len(poly["corners"])} corners fan triangulated; concavity not validated')
|
||||
material = poly['material']['id'] if poly['material'] else -1
|
||||
|
||||
@@ -7,6 +7,10 @@ from pathlib import Path
|
||||
from omt import Database, export_scene, extract
|
||||
from ase import export_ase
|
||||
from png import read_png
|
||||
from entities import prepare_entities
|
||||
from models import prepare_models
|
||||
from ui import export_ui
|
||||
from sound_bank import export_sound_bank
|
||||
|
||||
|
||||
def find_archive(original: Path, name: str) -> Path:
|
||||
@@ -17,20 +21,65 @@ def find_archive(original: Path, name: str) -> Path:
|
||||
return matches[0]
|
||||
|
||||
|
||||
def prepare_actor(original: Path, output: Path, name: str, texture_name: str, actions: dict, base_name: str | None = None,
|
||||
ignored_animations: tuple[str, ...] = ()):
|
||||
"""Explicit EXE registration mappings; no texture guessed from ASE adjacency."""
|
||||
target = output / 'actors' / name
|
||||
target.mkdir(parents=True, exist_ok=True)
|
||||
texture_source = next(p for p in (original/'png').iterdir() if p.name.casefold() == texture_name.casefold())
|
||||
texture = read_png(texture_source)
|
||||
(target/'texture.rgba').write_bytes(texture.pop('rgba'))
|
||||
base_name = base_name or next(iter(actions.values()))
|
||||
base = next(p for p in (original/'ASE').iterdir() if p.stem.casefold() == base_name.casefold())
|
||||
for stem in dict.fromkeys(actions.values()):
|
||||
source = next(p for p in (original/'ASE').iterdir() if p.stem.casefold() == stem.casefold())
|
||||
manifest = export_ase(source, target/stem, base)
|
||||
manifest['texture'] = {'path': '../texture.rgba', **texture}
|
||||
(target/stem/'animation.json').write_text(json.dumps(manifest, indent=2)+'\n')
|
||||
(target/'family.json').write_text(json.dumps({'default': next(iter(actions)),
|
||||
'actions': {key: value+'/animation.json' for key, value in actions.items()},
|
||||
'ignoredAnimations': list(ignored_animations)}, indent=2)+'\n')
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--original', type=Path, default=Path('../Original'))
|
||||
parser.add_argument('--output', type=Path, default=Path('artifacts/extracted'))
|
||||
parser.add_argument('--scenes', nargs='+', default=['level1b', 'VR01'])
|
||||
parser.add_argument('--all-scenes', action='store_true', help='Prepare every supplied GAM/OMT scene pair')
|
||||
parser.add_argument('--audio', nargs='*', default=['sounds', 'musicjimmyshouse', 'musicneighborhood'])
|
||||
parser.add_argument('--animations', nargs='*', default=['jimstop', 'jimrun', 'jimjump', 'jimscratch', 'jimbackpedal', 'jimfall'])
|
||||
parser.add_argument('--animations', nargs='*', default=['jimstop', 'jimrun', 'jimjump', 'jimscratch', 'jimbackpedal', 'jimfall', 'jimtalk', 'jimfly'])
|
||||
args = parser.parse_args()
|
||||
if args.output.resolve().is_relative_to(args.original.resolve()):
|
||||
parser.error('Output must be outside the original installation')
|
||||
if args.all_scenes:
|
||||
args.scenes = sorted((p.stem for p in (args.original/'gam').iterdir() if p.suffix.casefold()=='.gam'), key=str.casefold)
|
||||
catalog = {'prepared': [], 'unavailable': []}
|
||||
for name in args.scenes:
|
||||
source = find_archive(args.original, name)
|
||||
try:
|
||||
source = find_archive(args.original, name)
|
||||
except ValueError:
|
||||
if not args.all_scenes:
|
||||
raise
|
||||
# GAM entities and OMT geometry are distinct. The executable registers
|
||||
# level3b/3e, but their use in normal progression is not established.
|
||||
# Report the absent geometry without calling it required game content.
|
||||
catalog['unavailable'].append({'level': name+'.gam', 'archive': name+'.omt',
|
||||
'reason': 'GAM supplied; matching geometry archive absent; gameplay use unverified'})
|
||||
print(f'{name}.gam is supplied; no matching {name}.omt geometry archive '
|
||||
'(gameplay use unverified; no substitution)')
|
||||
continue
|
||||
scene = export_scene(Database(source), args.output / source.stem)
|
||||
print(f"{source.name}: {len(scene['indices'])//3} triangles, {len(scene['textures'])} textures")
|
||||
report = prepare_entities(args.original, source.stem + '.gam', args.output / source.stem / 'entities', True)
|
||||
print(f"{report['level']}: {len(report['resources'])} explicit entity resources")
|
||||
models = prepare_models(args.original, source.stem + '.gam', args.output / source.stem / 'models')
|
||||
print(f"{models['level']}: {len(models['actors'])} model actors; {len(models['unresolved'])} unresolved bindings")
|
||||
catalog['prepared'].append({'level': name+'.gam', 'archive': source.name, 'scene': source.stem+'/scene.json'})
|
||||
(args.output/'scene-catalog.json').write_text(json.dumps(catalog, indent=2)+'\n')
|
||||
export_ui(Database(find_archive(args.original, 'screens')), args.output / 'ui', Database(args.original / 'alpha.omt'))
|
||||
effects = export_sound_bank(args.original, args.output / 'effects')
|
||||
print(f"SoundEffects.omt: {len(effects['resources'])} original sound identities")
|
||||
for name in args.audio:
|
||||
source = find_archive(args.original, name)
|
||||
report = extract(Database(source), args.output / source.stem)
|
||||
@@ -50,6 +99,16 @@ def main():
|
||||
manifest['texture'] = {'path':'../jimycarl.rgba', **texture}
|
||||
(target/name/'animation.json').write_text(json.dumps(manifest,indent=2)+'\n',encoding='utf-8')
|
||||
print(f"{matches[0].name}: {len(manifest['frames'])} animation samples")
|
||||
# EXE413D8C..413DF9 (Carl);41CDA0..41CE5F (Goddard high-detail clips).
|
||||
prepare_actor(args.original, args.output, 'carl', 'carl.png', {
|
||||
'STOP': 'carlstop', 'WALK': 'carlwalk', 'TALK': 'carltalk', 'INHALE': 'carlinhale',
|
||||
'CHEER': 'carlcheer', 'TELE': 'carlteleport'})
|
||||
prepare_actor(args.original, args.output, 'goddard', 'goddard02.png', {
|
||||
'SIT': 'godsit', 'RUN': 'godrun', 'GROWL': 'godgrowl', 'WAG': 'godwag',
|
||||
'EAT': 'godeat', 'FLY': 'godfly', 'POINT': 'godpoint'}, 'godpoint', ('STOP', 'WALK', 'TALK'))
|
||||
prepare_actor(args.original, args.output, 'trophy', 'trophy.png', {'STOP': 'trophy'})
|
||||
# C3DJetpackFire421F30..96: DEFAULT fire.ase, HINONE fire2.ase, fire.png.
|
||||
prepare_actor(args.original, args.output, 'jetpack', 'fire.png', {'DEFAULT': 'fire', 'NONE': 'fire2'}, 'fire')
|
||||
|
||||
|
||||
if __name__ == '__main__': main()
|
||||
@@ -0,0 +1,58 @@
|
||||
"""Prepare the original default SoundEffects.omt bank (EXE423642..656).
|
||||
|
||||
Numeric effect IDs are archive identities, not offsets into the four-entry
|
||||
sounds.omt archive. Preserve the original PCM/RIFF bytes and explicit metadata.
|
||||
"""
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import struct
|
||||
|
||||
from entities import _decode, resolve_asset
|
||||
from omt import Database, FormatError
|
||||
|
||||
|
||||
def export_sound_bank(original: Path, output: Path) -> dict:
|
||||
original, output = original.resolve(), output.resolve()
|
||||
if output.is_relative_to(original) or original.is_relative_to(output):
|
||||
raise FormatError('Sound output must be separate from original files')
|
||||
source = resolve_asset(original, 'omt/SoundEffects.omt')
|
||||
database = Database(source)
|
||||
source_hash = hashlib.sha256(database.data).hexdigest()
|
||||
resources = []
|
||||
for entry in database.entries:
|
||||
if entry.type != 'Wave':
|
||||
continue
|
||||
resource = {'database': source.relative_to(original).as_posix(), 'type': 'Wave',
|
||||
'id': entry.id, 'name': entry.name, 'offset': entry.offset,
|
||||
'size': entry.size, 'databaseSha256': source_hash}
|
||||
_decode(database, entry, resource, output)
|
||||
if 'seconds' not in resource['audio']:
|
||||
# Some original resources contain MPEG Layer3 in RIFF. Preserve the
|
||||
# encoded bytes; duration comes from the explicit fact sample count.
|
||||
raw = (output/resource['path']).read_bytes()
|
||||
position, rate, frames, encoding = 12, None, None, None
|
||||
while position + 8 <= len(raw):
|
||||
tag = raw[position:position+4]
|
||||
size = struct.unpack_from('<I', raw, position+4)[0]
|
||||
begin, end = position+8, position+8+size
|
||||
if end > len(raw):
|
||||
raise FormatError('Sound RIFF chunk escapes resource')
|
||||
if tag == b'fmt ' and size >= 16:
|
||||
encoding, channels, rate = struct.unpack_from('<HHI', raw, begin)
|
||||
elif tag == b'fact' and size >= 4:
|
||||
frames = struct.unpack_from('<I', raw, begin)[0]
|
||||
position = end+(size & 1)
|
||||
if not rate or frames is None:
|
||||
raise FormatError(f'No explicit sound duration for encoding: {entry.id}')
|
||||
resource['audio'] = {'formatTag': encoding, 'channels': channels, 'rate': rate,
|
||||
'frames': frames, 'seconds': frames/rate,
|
||||
'encoding': 'encoded RIFF preserved; decoder support is separate'}
|
||||
resource['sha256'] = hashlib.sha256((output/resource['path']).read_bytes()).hexdigest()
|
||||
resources.append(resource)
|
||||
if not resources or len({r['id'] for r in resources}) != len(resources):
|
||||
raise FormatError('Missing or duplicate original sound identities')
|
||||
report = {'schema': 1, 'evidence': 'Neutron.exe423642..423656; default current sound database',
|
||||
'resources': resources}
|
||||
(output/'effects.json').write_text(json.dumps(report, indent=2)+'\n', encoding='utf8')
|
||||
return report
|
||||
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Export proven laboratory HUD/PracticeYesNo Canv resources, without changing originals."""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from omt import Database, FormatError, read_canvas, validate_output, write_png
|
||||
|
||||
|
||||
# Neutron.exe 402687..4026E8 registers pad and laboratory objective IDs;
|
||||
# 40224F registers PracticeYesNo. These are original identities, not guessed art.
|
||||
CANVASES = {120: (256, 29), 121: (256, 29), 122: (256, 29),
|
||||
156: (320, 173), 169: (207, 100)}
|
||||
ALPHA_CANVASES = {83: (8, 8), 84: (8, 8), 85: (8, 8)}
|
||||
|
||||
|
||||
def export_ui(database: Database, output: Path, alpha: Database | None = None) -> dict:
|
||||
validate_output(database, output)
|
||||
if alpha is None:
|
||||
# The original font database is beside omt/, not within screens.omt.
|
||||
alpha = Database(database.path.parent.parent / 'alpha.omt')
|
||||
validate_output(alpha, output)
|
||||
recovered = []
|
||||
for source, identities in ((database, CANVASES), (alpha, ALPHA_CANVASES)):
|
||||
for identity, expected in identities.items():
|
||||
entry = next((e for e in source.entries if e.type == 'Canv' and e.id == identity), None)
|
||||
if entry is None:
|
||||
raise FormatError(f'Missing original UI canvas {source.path.name}:{identity}')
|
||||
canvas = read_canvas(source.payload(entry))
|
||||
if (canvas['width'], canvas['height']) != expected:
|
||||
raise FormatError(f'Unexpected original UI canvas dimensions {identity}')
|
||||
recovered.append((source, entry, canvas))
|
||||
output.mkdir(parents=True, exist_ok=True)
|
||||
textures = []
|
||||
for source, entry, canvas in recovered:
|
||||
name = f'canvas_{entry.id}'
|
||||
rgba = canvas['rgba']
|
||||
(output / (name + '.rgba')).write_bytes(rgba)
|
||||
write_png(output / (name + '.png'), canvas['width'], canvas['height'], rgba)
|
||||
textures.append({'id': entry.id, 'name': entry.name, 'width': canvas['width'],
|
||||
'height': canvas['height'], 'path': name + '.rgba',
|
||||
'source': source.path.name, 'sourceOffset': entry.offset, 'sourceBytes': entry.size,
|
||||
'rgbaSha256': hashlib.sha256(rgba).hexdigest()})
|
||||
manifest = {'version': 1, 'source': database.path.name,
|
||||
'sourceSha256': hashlib.sha256(database.data).hexdigest(),
|
||||
'alphaSha256': hashlib.sha256(alpha.data).hexdigest(),
|
||||
'referenceWidth': 640, 'referenceHeight': 480, 'textures': textures}
|
||||
(output / 'ui.json').write_text(json.dumps(manifest, indent=2) + '\n', encoding='utf-8')
|
||||
return manifest
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('database', type=Path)
|
||||
parser.add_argument('--output', type=Path, required=True)
|
||||
parser.add_argument('--alpha', type=Path, help='Original alpha.omt (default: beside the omt directory)')
|
||||
args = parser.parse_args()
|
||||
result = export_ui(Database(args.database), args.output, Database(args.alpha) if args.alpha else None)
|
||||
print(f"{len(result['textures'])} original UI canvases exported to {args.output}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in new issue
Block a user