Files
breeze/scripts/embed-app-assets.py
T
waoziandClaude Opus 5.5 94b3bd5f9d Fetch dependencies as Ziran packages instead of submodules
ziran.toml and ziran.lock pin the Ziran toolchain, Kryon, Game2D, the Daochi
client, and the C libraries (as source packages) at the commits the vendor
submodules used. scripts/packages.sh bootstraps the locked toolchain, runs
ziran fetch, and links each checkout under build/packages/; make, Gradle, and
CI run it before building. make package-check replaces no-vendor-edits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-09-27 16:44:19 -03:00

87 lines
3.0 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/packages/kryon/styles/"):
path = path.removeprefix("build/packages/kryon/")
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())):
lines.append(f"static const unsigned char asset_{index}[] = {{")
for offset in range(0, len(data), 16):
lines.append(" " + ", ".join(f"0x{byte:02x}" for byte in data[offset:offset + 16]) + ",")
if not data:
lines.append(" 0x00,")
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()