Files
Dreierpack-Manager/tools/modern_ui/prepare_stadium_assets.py
T

69 lines
3.4 KiB
Python

#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-only
"""Attach the approved calibrated Sprite Studio to a private Modern GUI package.
Use an independently extracted private reference tree, never an OBW directory.
The source configuration and PNG canvases are copied byte for byte.
"""
import argparse
import hashlib
import json
from pathlib import Path
import re
import shutil
import struct
CONFIG_HASH = '7e3e6a013703b392964a0151b17b00d40fb582ea6f51ce125345e080e49cbada'
SOURCE_REVISION = '82914ae809b12f69c6abfaa7addfcecc7378dd81'
def prepare(studio, output):
studio = studio.resolve(strict=True)
output = output.resolve(strict=True)
if studio == output or studio in output.parents or output in studio.parents:
raise ValueError('Source and GUI output must be separate directories')
manifest_path = output / 'gui-assets.json'
manifest = json.loads(manifest_path.read_text())
if manifest['schema'] != 'openbmh-gui-assets@1':
raise ValueError('Expected an existing private Modern GUI resource package')
raw = (studio / 'configurations/OpenBMH-configuration-Stadium.json').read_bytes()
if hashlib.sha256(raw).hexdigest() != CONFIG_HASH:
raise ValueError('Configuration differs from approved calibrated export')
config = json.loads(raw)
if len(config['assets']) != 34:
raise ValueError('Expected all 34 approved PNGs')
# Validate every input before copying anything or publishing the manifest.
sources = []
for asset_id, asset in config['assets'].items():
if not re.fullmatch(r'[a-z0-9-]+', asset_id) or asset['path'] != f'assets/{asset_id}.png':
raise ValueError('Unsafe stadium asset path')
source = (studio / asset['path']).resolve(strict=True)
if not source.is_relative_to(studio):
raise ValueError('Stadium PNG escapes source directory')
data = source.read_bytes()
if hashlib.sha256(data).hexdigest() != asset['sha256'] or data[:8] != b'\x89PNG\r\n\x1a\n' or struct.unpack('>II', data[16:24]) != (asset['width'], asset['height']):
raise ValueError(f'PNG hash/dimensions mismatch: {asset_id}')
sources.append((asset_id, source, asset))
destination = output / 'stadium-scene'
if not destination.resolve().is_relative_to(output):
raise ValueError('Stadium output escapes GUI package')
destination.mkdir(exist_ok=True)
for asset_id, source, asset in sources:
target = destination / f'{asset_id}.png'
if not target.resolve().is_relative_to(output):
raise ValueError('Stadium output escapes GUI package')
shutil.copyfile(source, target)
manifest['items'][f'stadium.scene.{asset_id}'] = {'file': f'stadium-scene/{asset_id}.png', 'sha256': asset['sha256']}
(destination / 'configuration.json').write_bytes(raw)
manifest['items']['stadium.scene'] = {'file': 'stadium-scene/configuration.json', 'sha256': CONFIG_HASH, 'source_revision': SOURCE_REVISION}
manifest_path.write_text(json.dumps(manifest, ensure_ascii=False, indent=2) + '\n')
print('Prepared calibrated stadium configuration and 34 unchanged GUI PNGs')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--studio', type=Path, required=True)
parser.add_argument('--output', type=Path, required=True)
args = parser.parse_args()
prepare(args.studio, args.output)