make_jsfx_pack.py 2.2 KB

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