"""Strict content boundary for the thin Android Arcade launcher.""" import hashlib import io import json import struct import subprocess from functools import lru_cache from pathlib import Path from node_config import node_source from hud_assets import HUD_PATHS, hud_asset_hashes ROOT = Path(__file__).resolve().parents[1] ESSENTIALS = {'arcade.zx', 'arcade.json', 'apps.zx', 'entity.zx', 'kryon-font.ttf'} @lru_cache(maxsize=1) def profile_asset_hashes(): flags = [] if (ROOT / 'ziran.local.toml').is_file() else ['--locked'] package = Path(subprocess.check_output( [str(ROOT / 'scripts/ziran.sh'), 'pkg', 'path', 'entity', *flags], cwd=ROOT, text=True).strip()) paths = json.loads((package / 'asset-groups.json').read_text())['profile'] result = {} for path in paths: with (package / path).open('rb') as source: result[path] = hashlib.file_digest(source, 'sha256').hexdigest() return result @lru_cache(maxsize=1) def hud_hashes(): flags = [] if (ROOT / 'ziran.local.toml').is_file() else ['--locked'] package = Path(subprocess.check_output( [str(ROOT / 'scripts/ziran.sh'), 'pkg', 'path', 'apps', *flags], cwd=ROOT, text=True).strip()) return hud_asset_hashes(package) def profile_assets(): return set(profile_asset_hashes()) def bundle_assets(payload, limit): # The catalog is tiny. Reject packed collections before allocating/reading # their contents, then inspect every actual asset rather than path strings # in the JSON (which intentionally names remote game and art resources). assert len(payload) <= limit, 'Bundle exceeds its asset policy budget' stream = io.BytesIO(payload) def take(size): assert 0 <= size <= len(payload), 'Invalid bundle field' data = stream.read(size) assert len(data) == size, 'Truncated bundle' return data def number(): return struct.unpack('