Files

113 lines
5.3 KiB
Python

#!/usr/bin/env python3
"""Export original gadget-screen canvases; resource identity is not gameplay policy."""
from __future__ import annotations
import argparse
from dataclasses import dataclass
import hashlib
import json
from pathlib import Path
from omt import Database, FormatError, read_canvas, validate_output, write_png
@dataclass(frozen=True)
class CanvasSpec:
id: int
name: str
width: int
height: int
role: str
TILE_LABELS = ('BUBBLE', 'ROCKETSHIP', 'INVISIBLE', 'SHRINK RAY',
'SCOOTER', 'REMOTE', 'BASEBALL', 'GRAPPLER')
TILE_IDS = tuple(range(11, 19))
TILE_B_IDS = (52, 46, 47, 48, 49, 50, 51, 45)
TILE_C_IDS = (178, 171, 172, 173, 174, 175, 176, 177)
def specifications() -> tuple[CanvasSpec, ...]:
specs = [CanvasSpec(20, 'menu', 640, 480, 'background')]
for index, (plain, alternate_b, alternate_c) in enumerate(zip(TILE_IDS, TILE_B_IDS, TILE_C_IDS), 1):
for identity, suffix in ((plain, ''), (alternate_b, 'b'), (alternate_c, 'c')):
specs.append(CanvasSpec(identity, f'item{index:02d}{suffix}', 141, 141, f'tile{suffix}'))
for identity, name in ((109, 'Bubble'), (110, 'Grappler'), (111, 'Invisible'),
(112, 'JetPack'), (113, 'RayGun'), (114, 'Remote'),
(115, 'Rocketship'), (116, 'Scooter'), (118, 'Baseball')):
specs.append(CanvasSpec(identity, 'BmenuL'+name, 393, 69, 'controls-left'))
for identity, name in ((108, 'BmenuR2'), (117, 'BmenuR1')):
specs.append(CanvasSpec(identity, name, 247, 69, 'controls-right'))
for identity, name in ((24, 'return'), (181, 'return2'), (182, 'return3'),
(170, 'practice'), (179, 'practice2'), (180, 'practice3')):
specs.append(CanvasSpec(identity, name, 141, 78, 'button'))
for identity, name in ((94, '1bubble'), (87, '2rocket'), (88, '3invisible'),
(89, '4raygun'), (90, '5scooter'), (91, '6remote'),
(92, '7baseball'), (93, '8grappler'), (119, '9jetpack')):
specs.append(CanvasSpec(identity, name, 80, 70, 'hud'))
for identity, name in ((84, 'backpack'), (85, 'backpack2')):
specs.append(CanvasSpec(identity, name, 141, 141, 'backpack'))
specs.append(CanvasSpec(253, 'CrossHairs', 15, 210, 'crosshairs-strip'))
return tuple(specs)
CANVASES = specifications()
def export_gadget_resources(database: Database, output: Path) -> dict:
"""Copy exact decoded pixels and metadata to a separate generated directory.
Tile roles preserve original resource suffixes. Which suffix means selected,
hovered, unavailable or enabled must come from the original dispatcher.
"""
validate_output(database, output)
# Supplied archives live beneath Original/omt. Protect that entire source
# tree, including sibling ASE/png/gam directories, rather than only omt/.
source_directory = database.path.parent.resolve()
source_root = source_directory.parent if source_directory.name.casefold() == 'omt' else source_directory
if output.resolve().is_relative_to(source_root):
raise ValueError('Gadget output must be separate from the source asset tree')
entries = {entry.id: entry for entry in database.entries if entry.type == 'Canv'}
recovered = []
for spec in CANVASES:
entry = entries.get(spec.id)
if entry is None or entry.name != spec.name:
raise FormatError(f'Missing or changed gadget canvas {spec.id}:{spec.name}')
canvas = read_canvas(database.payload(entry))
if (canvas['width'], canvas['height']) != (spec.width, spec.height):
raise FormatError(f'Unexpected gadget canvas dimensions {spec.id}')
recovered.append((spec, entry, canvas))
output.mkdir(parents=True, exist_ok=True)
textures = []
for spec, entry, canvas in recovered:
name = f'canvas_{spec.id}'
rgba = canvas['rgba']
(output/(name+'.rgba')).write_bytes(rgba)
write_png(output/(name+'.png'), spec.width, spec.height, rgba)
textures.append(dict(id=spec.id, name=entry.name, role=spec.role,
width=spec.width, height=spec.height, path=name+'.rgba', png=name+'.png',
sourceOffset=entry.offset, sourceBytes=entry.size,
sourceSha256=hashlib.sha256(database.payload(entry)).hexdigest(),
rgbaSha256=hashlib.sha256(rgba).hexdigest()))
manifest = dict(schema=1, source=database.path.name,
sourceSha256=hashlib.sha256(database.data).hexdigest(),
referenceWidth=640, referenceHeight=480, textures=textures,
tiles=[dict(artItem=index+1, label=TILE_LABELS[index], canvas=plain,
alternateB=alternate_b, alternateC=alternate_c)
for index, (plain, alternate_b, alternate_c) in enumerate(zip(TILE_IDS, TILE_B_IDS, TILE_C_IDS))])
(output/'gadgets.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)
args = parser.parse_args()
manifest = export_gadget_resources(Database(args.database), args.output)
print(f"{len(manifest['textures'])} original gadget canvases exported to {args.output}")
if __name__ == '__main__':
main()