Files
breeze/scripts/embed-app-assets.py
T
waozi 74c4a6d935 Build and run apps in the ZX format
Ziran 0.3.1 writes portable bundles as .zx with a ZX header and rejects
.zib. Pin Ziran 620c407, Kryon 935d5fc1, Apps 12e52c0 and Entity 86e72720,
and build, embed, download, verify and store every bundle as .zx: the root
and cell bundles, node apps (the node is asked for format=zx releases),
practice packs and the shared Apps controller, now the only trusted one.

Practice packs stored by older versions count as installed and update
themselves to ZX; a node app's unusable .zib payload is removed so its ZX
release installs. Bundle staging starts empty, so no stale bundle is
embedded, and Android reads asset names by their length.
The generated app codec matches the pinned generator again.
2026-10-23 22:05:05 -03:00

96 lines
3.4 KiB
Python

#!/usr/bin/env python3
"""Embed the app's files in a deterministic, borrowed asset table."""
import argparse
import os
from pathlib import Path
import json
MIME_TYPES = {
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".ogg": "audio/ogg",
".wav": "audio/wav",
".mp3": "audio/mpeg",
".ttf": "font/ttf",
".otf": "font/otf",
".kss": "text/plain",
".txt": "text/plain",
".dat": "application/octet-stream",
}
def emit(output: Path, files: list[Path], root: Path) -> None:
entries = {}
for file in files:
resolved = file.resolve(strict=True)
# Keep build/packages/<name> links unresolved so package assets get
# stable embedded names regardless of the package cache location.
path = Path(os.path.abspath(file)).relative_to(root).as_posix()
if path.startswith("build/cells/"):
path = path.removeprefix("build/")
if path == "build/apps-ui/apps.zx":
path = "apps.zx"
if path == "build/inbe.zx":
path = "inbe.zx"
if path.startswith("build/packages/kss/styles/"):
path = path.removeprefix("build/packages/kss/")
if path.startswith("build/packages/kryon/icons/"):
path = path.removeprefix("build/packages/kryon/")
if path in entries:
raise ValueError(f"duplicate asset path: {path}")
mime = MIME_TYPES.get(resolved.suffix.lower(), "application/octet-stream")
entries[path] = (resolved.read_bytes(), mime)
lines = [
"/* Generated by scripts/embed-app-assets.py. */",
'#include "assets_host.h"',
"",
]
for index, (_, (data, _)) in enumerate(sorted(entries.items())):
# String literals preserve the bytes while avoiding millions of
# initializer-expression nodes in the native compiler's memory.
lines.append(f"static const unsigned char asset_{index}[] =")
for offset in range(0, len(data), 2048):
literal = "".join(f"\\x{byte:02x}" for byte in data[offset:offset + 2048])
lines.append(' "' + literal + '"')
if not data:
lines.append(' ""')
lines.append(";")
lines.extend(["", "const AssetEntry asset_entries[] = {"])
for index, (path, (data, mime)) in enumerate(sorted(entries.items())):
lines.append(
f" {{(uint8_t *){json.dumps(path)}, (uint8_t *){json.dumps(mime)}, "
f"(uint8_t *)asset_{index}, {len(data)}u}},"
)
if not entries:
lines.append(' {(uint8_t *)"", (uint8_t *)"", NULL, 0u},')
lines.extend([
"};",
f"const size_t asset_entry_count = {len(entries)}u;",
"const AssetEntry *asset_entry_at(size_t index) {",
" return index < asset_entry_count ? &asset_entries[index] : NULL;",
"}",
"size_t asset_entry_total(void) { return asset_entry_count; }",
"",
])
output.parent.mkdir(parents=True, exist_ok=True)
new_content = "\n".join(lines)
if not output.exists() or output.read_text() != new_content:
output.write_text(new_content)
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("output", type=Path)
parser.add_argument("files", nargs="*", type=Path)
args = parser.parse_args()
root = Path(__file__).resolve().parent.parent
emit(args.output, args.files, root)
if __name__ == "__main__":
main()