142 lines
7.5 KiB
Python
142 lines
7.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Export original Rocketship geometry and compatible morph resources, unchanged.
|
|
|
|
Resource compatibility does not establish runtime attachments, material flags,
|
|
animation timing or collision registration. Those belong to binary recovery.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
from collections import Counter
|
|
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_shape
|
|
from png import read_png
|
|
|
|
SOURCES = {
|
|
'ASE/strato.ASE': '80f7f25783b49243ff52048807b11949a1f8700da99ad1d6d434ab74277fc804',
|
|
'ASE/firestrato.ASE': '40a5e88dfb02bc230b3467c3a084eb7121c5262c9a9ec0d821543bdad2b607d3',
|
|
'ASE/jimdrive.ASE': '0b70a66fc8a75ce6ef96b9eb3ce46121ae98c5d8ca086556120979e4f811eff3',
|
|
'ASE/jimstop.ASE': 'b294d090ac5c4ec449d845db6d837f2cbedf5f7b812f5b6ed2bb70f85246f1d0',
|
|
'ASE/godrocket.ASE': 'd3787715d40fa03e2d746a7c3be04c4f4e053a8581495814e5017e4b0244cce0',
|
|
'ASE/godpoint.ASE': 'd8db5fc8929531fd0e60f4125f016bc75bbbd637c5bdc44b8d0d20a8729e653b',
|
|
'png/strato.png': '850756970c646431c1b3f0c2da1bb7eddd74c0673b51b0624c1822cee4a8e690',
|
|
'png/fire.png': 'ca98cb03feef6b6cf4266895ec26fbc8372f1cf60c342ee1316b17f668311f16',
|
|
'png/jimycarl.png': '55ed165aaffe177cb62558a08d13fc3b2210da6e353065b108849d46df9dacb6',
|
|
'png/goddard02.png': '4c89f34aa5dc2ae72b64c1efd76695fd56a7cef7ee850a5958f9b38b92ac9e9b',
|
|
'gam/VR02.gam': '3be92e270bd14e6fc41db702e68c894562128d23d46704b89f7abb1914bebc5c',
|
|
'omt/VR02.omt': 'ab041f663cd5e68d58e590feeb7661f308a70ccc682a32c49ccd82b74bec61c0',
|
|
}
|
|
|
|
# Stem, original textured base, explicit texture candidate, indexed count,
|
|
# polygon count and exact source samples. Caller owns gameplay/action binding.
|
|
CLIPS = (
|
|
('strato', None, 'strato', 159, 262, (0,)),
|
|
('firestrato', None, 'fire', 7, 6, (0, 480, 960)),
|
|
('jimdrive', 'jimstop', 'jimycarl', 426, 814, (0, 480)),
|
|
('godrocket', 'godpoint', 'goddard02', 222, 411, (0, 960, 1920)),
|
|
)
|
|
TEXTURES = (
|
|
('strato', 256, 256, 'b1b18454fe0413e3ce885a61e4407c808060da0fe29ef2aec26ccd4e39437f31'),
|
|
('fire', 100, 100, '07dbbc4c6b99b6fa5fe424f2f917e03352a9c635fdc1593fc12f354be83a9a7f'),
|
|
('jimycarl', 256, 256, '5e54ccb99e9e060843cd7f80fd93ff8bf47f8779666e41d4240398354a278a3f'),
|
|
('goddard02', 256, 256, '9eb24c8fa41ddf08d2f33dd7b7513b632d4262cfc26cab3383c54258b9647a62'),
|
|
)
|
|
|
|
|
|
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_rocketship(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 Rocketship 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 Rocketship source identity: {name}')
|
|
animations = {name: read_ase(path) for name, path in paths.items() if name.startswith('ASE/')}
|
|
for stem, base, _, vertices, faces, times in CLIPS:
|
|
animation = animations[f'ASE/{stem}.ASE']
|
|
if len(animation['objects']) != 1:
|
|
raise FormatError('Unexpected Rocketship animation object count')
|
|
samples = animation['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 Rocketship source mesh or sample times')
|
|
topology = animations[f'ASE/{base}.ASE']['objects'][0]['samples'][0] if base else samples[0]
|
|
if len(topology['vertices']) != vertices or not topology['textureFaces']:
|
|
raise FormatError('Rocketship morph base lacks compatible textured topology')
|
|
textures, rgba_pixels = {}, {}
|
|
for stem, width, height, rgba_sha in TEXTURES:
|
|
decoded = read_png(paths[f'png/{stem}.png'])
|
|
rgba = decoded.pop('rgba')
|
|
if (decoded['width'], decoded['height'], len(rgba), digest(rgba)) != (
|
|
width, height, width*height*4, rgba_sha):
|
|
raise FormatError('Unexpected Rocketship decoded pixels')
|
|
textures[stem] = dict(path=f'../{stem}.rgba', source=paths[f'png/{stem}.png'].name,
|
|
sourceSha256=SOURCES[f'png/{stem}.png'], rgbaSha256=rgba_sha, **decoded)
|
|
rgba_pixels[stem] = rgba
|
|
level = parse(paths['gam/VR02.gam'].read_bytes())
|
|
entities = [dict(ordinal=index, **asdict(entity)) for index, entity in enumerate(level.entities)]
|
|
rockets = [e for e in entities if e['kind'] == '3ROC']
|
|
if len(entities) != 18 or len(rockets) != 1 or rockets[0]['offset'] != 0x1cd:
|
|
raise FormatError('Unexpected VR02 authored Rocketship identity')
|
|
database = Database(paths['omt/VR02.omt'])
|
|
shape_entries = [entry for entry in database.entries if entry.type == '3DSh']
|
|
if len(shape_entries) != 1:
|
|
raise FormatError('Unexpected VR02 static shape set')
|
|
entry = shape_entries[0]
|
|
shape = read_shape(database.payload(entry))
|
|
if (entry.id, entry.name, entry.offset, entry.size, len(shape['vertices']), len(shape['polygons'])) != (
|
|
0, 'Box01', 74464, 93802, 424, 880):
|
|
raise FormatError('Unexpected VR02 static mesh metadata')
|
|
material_counts = Counter(str(p['material']['id']) if p['material'] else 'null' for p in shape['polygons'])
|
|
static = dict(source='VR02.omt', sha256=SOURCES['omt/VR02.omt'], shapeId=entry.id,
|
|
shapeName=entry.name, sourceOffset=entry.offset, sourceBytes=entry.size,
|
|
indexedVertices=424, polygons=880, materialPolygons=dict(sorted(material_counts.items())),
|
|
registrationPolicy='Asset inventory only; game-class filter and actor body admission require source proof')
|
|
manifest = dict(schema=1, sources=SOURCES.copy(), animations=[],
|
|
tutorial=dict(source='VR02.gam', sha256=SOURCES['gam/VR02.gam'], entities=entities),
|
|
staticGeometry=static,
|
|
bindingPolicy='Original resource data; caller owns verified action selection, attachment, transforms, material flags and timing')
|
|
output.mkdir(parents=True, exist_ok=True)
|
|
for stem, rgba in rgba_pixels.items():
|
|
(output/f'{stem}.rgba').write_bytes(rgba)
|
|
for stem, base, texture, 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'] = textures[texture]
|
|
_json(output/stem/'animation.json', animation)
|
|
manifest['animations'].append(dict(source=paths[f'ASE/{stem}.ASE'].name,
|
|
path=f'{stem}/animation.json', texture=texture,
|
|
indexedVertices=vertices, polygons=faces, sourceTimes=list(times)))
|
|
_json(output/'rocketship.json', manifest)
|
|
return manifest
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('original', type=Path)
|
|
parser.add_argument('--output', type=Path, required=True)
|
|
args = parser.parse_args()
|
|
export_rocketship(args.original, args.output)
|
|
print(f'Original Rocketship resources exported: {args.output}/rocketship.json')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|