PulseCanvas / app · pulsecanvas.ai / scripts / name-projectm-presets.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Assign stable two-word display names and shared icons; retain upstream credits.""" |
| 3 | import json |
| 4 | import sys |
| 5 | import re |
| 6 | from pathlib import Path |
| 7 | |
| 8 | root = Path(__file__).resolve().parents[1] / 'Sources/PulseCanvas/Resources/ProjectM' |
| 9 | vocabulary = json.loads((root / 'naming.json').read_text()) |
| 10 | catalog = json.loads((root / 'catalog.json').read_text()) |
| 11 | if '--icons-only' in sys.argv: |
| 12 | for preset in catalog: |
| 13 | value = 14695981039346656037 |
| 14 | for byte in preset['id'].encode(): |
| 15 | value = ((value ^ byte) * 1099511628211) & ((1 << 64) - 1) |
| 16 | preset['icon'] = vocabulary['icons'][value % len(vocabulary['icons'])] |
| 17 | (root / 'catalog.json').write_text(json.dumps(catalog, indent=2, ensure_ascii=False) + '\n') |
| 18 | print(f'Updated {len(catalog):,} icons from {len(vocabulary["icons"])} designs; names and identities preserved') |
| 19 | sys.exit(0) |
| 20 | |
| 21 | sources = root.parents[1] |
| 22 | native = (sources / 'VisualizerRenderer.swift').read_text().split('static let nativeSceneNames = ')[1].split('\n')[0] |
| 23 | used = set(re.findall(r'"([^"]+)"', native)) |
| 24 | used.update(re.findall(r'case \.\w+: return "([^"]+)"', (sources / 'OriginalGeometry.swift').read_text())) |
| 25 | reserved = len(used) |
| 26 | |
| 27 | def stable_hash(text): |
| 28 | value = 14695981039346656037 |
| 29 | for byte in text.encode(): |
| 30 | value = ((value ^ byte) * 1099511628211) & ((1 << 64) - 1) |
| 31 | return value |
| 32 | |
| 33 | for preset in catalog: |
| 34 | value = stable_hash(preset['id']) |
| 35 | slot = value % (len(vocabulary['adjectives']) * len(vocabulary['nouns'])) |
| 36 | while True: |
| 37 | name = vocabulary['adjectives'][slot // len(vocabulary['nouns'])] + ' ' + vocabulary['nouns'][slot % len(vocabulary['nouns'])] |
| 38 | if name not in used: |
| 39 | break |
| 40 | slot = (slot + 1) % (len(vocabulary['adjectives']) * len(vocabulary['nouns'])) |
| 41 | preset['originalTitle'] = preset.get('originalTitle', preset['title']) |
| 42 | preset['title'] = name |
| 43 | preset['icon'] = vocabulary['icons'][value % len(vocabulary['icons'])] |
| 44 | used.add(name) |
| 45 | |
| 46 | assert len(used) == len(catalog) + reserved |
| 47 | assert all(len(preset['title'].split()) == 2 for preset in catalog) |
| 48 | (root / 'catalog.json').write_text(json.dumps(catalog, indent=2, ensure_ascii=False) + '\n') |
| 49 | print(f'Named {len(catalog):,} presets with {len(vocabulary["icons"])} shared icons') |