PulseCanvas / app · pulsecanvas.ai / scripts / native-visualizer-catalog.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Generate the native resource catalog from the ordered NativeEffects registry.""" |
| 3 | import argparse |
| 4 | import hashlib |
| 5 | import json |
| 6 | from pathlib import Path |
| 7 | import re |
| 8 | |
| 9 | root = Path(__file__).resolve().parents[1] |
| 10 | sources = root / 'Sources/PulseCanvas' |
| 11 | registry = (sources / 'NativeEffects.swift').read_text() |
| 12 | original = (sources / 'OriginalGeometry.swift').read_text().split('static var sceneNames:', 1)[0] |
| 13 | titles = dict(re.findall(r'case \.(\w+): return "([^"]+)"', original)) |
| 14 | body = registry.split('static let all: [NativeEffect] = [', 1)[1].split('\n ]\n', 1)[0] |
| 15 | effects = [] |
| 16 | for entry in re.findall(r'^\s*\.(original|metal|retired)\((.*)\),?\s*$', body, re.M): |
| 17 | kind, arguments = entry |
| 18 | literal = re.match(r'"([^"]+)"', arguments) |
| 19 | name = literal.group(1) if literal else titles[re.match(r'OriginalGeometry\.Scene\.(\w+)\.title', arguments).group(1)] |
| 20 | icon = re.search(r'icon: "([^"]+)"', arguments).group(1) |
| 21 | shader = re.search(r'icon: "[^"]+", "(\w+)", in: (\w+)', arguments) if kind == 'metal' else None |
| 22 | replacement = re.search(r'replacement: (\d+)', arguments) if kind == 'retired' else None |
| 23 | effects.append({'name': name, 'icon': icon, 'shader': shader.group(1) if shader else None, |
| 24 | 'replacement': int(replacement.group(1)) if replacement else None}) |
| 25 | # Retired designs keep their IDs for migration; only offered designs need unique names and icons. |
| 26 | active = [e for e in effects if e['replacement'] is None] |
| 27 | names = [e['name'] for e in active] |
| 28 | icons = [e['icon'] for e in active] |
| 29 | assert len(names) == len(set(names)), 'Native names must be unique' |
| 30 | assert len(effects) >= 31 |
| 31 | assert all(effects[e['replacement']]['replacement'] is None for e in effects if e['replacement'] is not None), 'Retired designs must open an offered design' |
| 32 | files = re.search(r'static let shaderFiles = \[(.*?)\]', registry, re.S).group(1) |
| 33 | shader_paths = ['Canvas.metal', 'SpatialVisuals.metal', 'Strobe.metal', 'NativeCommon.metal', 'NativeExpansionCommon.metal'] + re.findall(r'"([^"]+\.metal)"', files) |
| 34 | catalog = { |
| 35 | 'schema': 1, |
| 36 | 'nativeCount': len(effects), |
| 37 | 'activeCount': len(active), |
| 38 | 'effects': [{'id': f'native:{i}', 'scene': i, 'name': e['name'], 'icon': e['icon']} |
| 39 | | ({'retired': True, 'replacement': f"native:{e['replacement']}"} if e['replacement'] is not None else {}) |
| 40 | for i, e in enumerate(effects)], |
| 41 | 'shaders': {p: hashlib.sha256((sources / 'Resources' / p).read_bytes()).hexdigest() for p in shader_paths}, |
| 42 | } |
| 43 | parser = argparse.ArgumentParser() |
| 44 | parser.add_argument('--output', type=Path) |
| 45 | args = parser.parse_args() |
| 46 | encoded = json.dumps(catalog, indent=2) + '\n' |
| 47 | if args.output: |
| 48 | args.output.parent.mkdir(parents=True, exist_ok=True) |
| 49 | args.output.write_text(encoded) |
| 50 | else: |
| 51 | print(encoded, end='') |