| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- # Embeds LYNX's bundled JSFX pack in the executable (round 190pj, issue #241).
- # Usage: python3 tools/make_jsfx_pack.py (from the repository root)
- # Reads every file below resources/jsfx/ (the scripts and their licence files) and writes src/engine/JsfxPackData.h:
- # each file zlib-compressed, its path relative to resources/jsfx ('/' separators) and a hash of the whole pack. LYNX
- # writes the pack to its settings folder ("JSFX pack") when the hash changes (engine/JsfxFormat.cpp).
- # Only scripts with a clear permissive licence go in (owner decision 2: MIT; never REAPER's own effects).
- import hashlib, pathlib, zlib
- root = pathlib.Path(__file__).resolve().parent.parent
- pack = root / "resources/jsfx"
- files = sorted(p for p in pack.rglob("*") if p.is_file())
- digest = hashlib.sha256()
- out = ["#pragma once", "",
- "// Generated by tools/make_jsfx_pack.py from resources/jsfx/ - do not edit by hand.", "",
- "#include <cstddef>", "",
- "namespace lynx::jsfxpack", "{",
- "struct File",
- "{",
- " const char* path; // relative to the pack's folder, '/' separators",
- " const unsigned char* zlib; // the file, zlib-compressed",
- " std::size_t zlibSize, size; // compressed / original size in bytes",
- "};", ""]
- names = []
- for i, p in enumerate(files):
- rel = p.relative_to(pack).as_posix()
- data = p.read_bytes()
- digest.update(rel.encode("utf-8") + b"\0" + data)
- packed = zlib.compress(data, 9)
- name = f"f{i}"
- names.append((rel, name, len(packed), len(data)))
- out.append(f"// {rel}")
- out.append(f"inline constexpr unsigned char {name}[] = {{")
- for j in range(0, len(packed), 40):
- out.append(",".join(str(b) for b in packed[j:j + 40]) + ",")
- out.append("};")
- out.append("")
- out.append("inline constexpr File files[] = {")
- for rel, name, n, size in names:
- esc = rel.replace("\\", "\\\\").replace('"', '\\"')
- out.append(f' {{ "{esc}", {name}, {n}, {size} }},')
- out.append("};")
- out.append(f'inline constexpr const char* hash = "{digest.hexdigest()[:32]}";')
- out.append("} // namespace lynx::jsfxpack")
- out.append("")
- (root / "src/engine/JsfxPackData.h").write_text("\n".join(out))
- print(f"{len(names)} file(s) -> src/engine/JsfxPackData.h")
|