#!/usr/bin/env python3 """ export.py - prepares the data the UI viewer draws from. Reads the compiled build/file_manager_plugin.rco (exactly what the console loads) and the firmware's common_resource.rco, and writes to tools/ui_viewer/cache/: app.json element tree of our RCO, ids resolved to names common.json styles and texture metadata of the system resource tex/*.png every texture of both, decoded (GIM, PNG) strings.json the string tables of our locales The firmware files are Sony's: cache/ is local only and must not be committed. python tools/ui_viewer/export.py [--firmware /vs0] """ import argparse import json import os import re import struct import sys import zlib HERE = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.normpath(os.path.join(HERE, "..", "..")) sys.path.insert(0, os.path.join(ROOT, "tools")) import rcoc # noqa: E402 DEFAULT_FW = os.path.join(os.environ.get("APPDATA", ""), "Vita3K", "Vita3K", "vs0") # ------------------------------------------------------------------ PNG / GIM def write_png(path, w, h, rgba): def chunk(tag, data): return struct.pack(">I", len(data)) + tag + data + struct.pack(">I", zlib.crc32(tag + data)) raw = bytearray() stride = w * 4 for y in range(h): raw.append(0) raw += rgba[y * stride:(y + 1) * stride] with open(path, "wb") as f: f.write(b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", w, h, 8, 6, 0, 0, 0)) + chunk(b"IDAT", zlib.compress(bytes(raw), 6)) + chunk(b"IEND", b"")) BPP = {0: 16, 1: 16, 2: 16, 3: 32, 4: 4, 5: 8, 6: 16, 7: 32} def px(data, fmt, i): if fmt == 3: return data[i * 4:i * 4 + 4] v = struct.unpack_from("> 4) & 0xF) * 17, ((v >> 8) & 0xF) * 17, ((v >> 12) & 0xF) * 17)) if fmt == 1: return bytes((round((v & 0x1F) * 255 / 31), round(((v >> 5) & 0x1F) * 255 / 31), round(((v >> 10) & 0x1F) * 255 / 31), 255 if v >> 15 else 0)) return bytes((round((v & 0x1F) * 255 / 31), round(((v >> 5) & 0x3F) * 255 / 63), round(((v >> 11) & 0x1F) * 255 / 31), 255)) def unswizzle(data, wb, h): out = bytearray(len(data)) src = 0 for by in range(h // 8): for bx in range(wb // 16): for y in range(8): d = (by * 8 + y) * wb + bx * 16 out[d:d + 16] = data[src:src + 16] src += 16 return bytes(out) def decode_gim(buf): image = palette = None def block(off, end): hs, _, fmt, sw, w, h = struct.unpack_from("> (4 * (x & 1))) & 0xF out += pal[i] if i < len(pal) else b"\0\0\0\0" else: bpp = BPP[fmt] // 8 rb = (w * bpp + 15) & ~15 for y in range(h): row = data[y * rb:y * rb + w * bpp] for x in range(w): out += px(row, fmt, x) return w, h, bytes(out) # ------------------------------------------------------------------ RCO to JSON def tree(r, names, texdir, prefix): hashes = r.s["hashes"] idh = r.s["idhash"] def nm(h): return names.get(h, "#%08x" % h) def value(el, ty, a, b, attrs): if ty == rcoc.T_INT: return struct.unpack("