"""IntoHole native playtest launcher. Use the project's uv-managed environment.""" from __future__ import annotations import argparse import json import os from pathlib import Path import shutil import subprocess import sys ROOT = Path(__file__).resolve().parents[2] GAME = ROOT / 'godot_engine' VALIDATION = ROOT / 'playtest/artifacts/baseline' def executable(name: str) -> Path: override = os.environ.get(f'INTOHOLE_{name.upper()}') candidates = [override] if override else [] if name == 'blender': candidates += ['/Applications/Blender.app/Contents/MacOS/Blender', shutil.which('blender')] else: candidates += [str(ROOT/'godot_engine/bin/godot'), str(ROOT/'godot_engine/bin/Godot.app/Contents/MacOS/Godot')] candidates += ['/Applications/Godot.app/Contents/MacOS/Godot', shutil.which('godot')] for base in [Path('/Applications'), Path.home() / 'Applications']: if base.exists(): for app in sorted(base.glob('*Godot*.app'), reverse=True): candidates.append(str(app/'Contents/MacOS/Godot')) for candidate in candidates: if candidate and Path(candidate).is_file(): # Blender needs its real bundle path to locate Python/resources. return Path(candidate).resolve() raise SystemExit(f'{name} executable not found. Set INTOHOLE_{name.upper()}.') def run(command: list[str | Path], name: str, live=False) -> None: VALIDATION.mkdir(parents=True, exist_ok=True) command = list(map(str, command)) print('Running:', ' '.join(command), flush=True) if live: subprocess.run(command, cwd=ROOT, check=True) else: log = VALIDATION / f'{name}.log' with log.open('w') as stream: result = subprocess.run(command, cwd=ROOT, stdout=stream, stderr=subprocess.STDOUT, timeout=180) text = log.read_text(errors='replace') if result.returncode or 'SCRIPT ERROR:' in text: print(text[-10000:]) raise SystemExit(result.returncode or 1) print(f'Log: {log}') def import_assets(godot: Path) -> None: run([godot, '--headless', '--path', GAME, '--log-file', VALIDATION/'import_engine.log', '--import'], 'import') def build() -> None: run([executable('blender'), '--background', '--factory-startup', '--python-exit-code', '1', '--python', ROOT/'assets/collections/baseline/source/build.py'], 'asset_build') import_assets(executable('godot')) report = json.loads((VALIDATION/'geometry_report.json').read_text()) for asset in report['assets']: flag = 'OK' if asset['declared_size_compliant'] else 'PROTOTYPE EXCEEDS DECLARED SIZE' print(f"{asset['id']}: {asset['size_m']} m — {flag}") def ensure_imported(godot: Path) -> None: if not (ROOT/'assets/collections/baseline/catalog.json').exists(): build() elif not (GAME/'.godot').exists(): import_assets(godot) def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('command', nargs='?', default='play', choices=['doctor','build','play','merge','capture']) args = parser.parse_args() godot = executable('godot') if args.command == 'doctor': print('Python:', sys.executable) for name in ['blender','godot']: path = executable(name) print(f'{name}: {path}') subprocess.run([str(path), '--version'], check=True) return if args.command == 'build': build() return ensure_imported(godot) common = [godot, '--path', GAME, '--log-file', VALIDATION/'playtest_engine.log'] if args.command == 'play': run(common+['--scene','res://playtest/baseline/scenes/main.tscn'],'playtest',live=True) elif args.command == 'merge': scenarios = json.loads((ROOT/'playtest/baseline/data/scenarios.json').read_text()) index = next(i for i, scenario in enumerate(scenarios) if scenario['id'] == 'dual_merge') run(common+['--scene','res://playtest/baseline/scenes/main.tscn','--',f'--scenario={index}'],'merge',live=True) else: run(common+['--scene','res://playtest/baseline/scenes/main.tscn','--',f'--capture={VALIDATION / "playtest_preview.png"}'],'capture') print('Preview:', VALIDATION/'playtest_preview.png') if __name__ == '__main__': main()