Branchwell

PulseCanvas / app · pulsecanvas.ai / scripts / name-projectm-presets.py

Raw 2.2 KB on 87dd19288bbd69b69b6ac6e250fb5d18bfebb877
1#!/usr/bin/env python3
2"""Assign stable two-word display names and shared icons; retain upstream credits."""
3import json
4import sys
5import re
6from pathlib import Path
7
8root = Path(__file__).resolve().parents[1] / 'Sources/PulseCanvas/Resources/ProjectM'
9vocabulary = json.loads((root / 'naming.json').read_text())
10catalog = json.loads((root / 'catalog.json').read_text())
11if '--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
21sources = root.parents[1]
22native = (sources / 'VisualizerRenderer.swift').read_text().split('static let nativeSceneNames = ')[1].split('\n')[0]
23used = set(re.findall(r'"([^"]+)"', native))
24used.update(re.findall(r'case \.\w+: return "([^"]+)"', (sources / 'OriginalGeometry.swift').read_text()))
25reserved = len(used)
26
27def stable_hash(text):
28 value = 14695981039346656037
29 for byte in text.encode():
30 value = ((value ^ byte) * 1099511628211) & ((1 << 64) - 1)
31 return value
32
33for 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
46assert len(used) == len(catalog) + reserved
47assert 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')
49print(f'Named {len(catalog):,} presets with {len(vocabulary["icons"])} shared icons')