"""Repair RGBA4444 cache sets without re-encoding already valid BC3 files. This is intentionally narrow: it only recreates missing R444 payloads, refreshes headers whose data.win offsets changed, removes stale extra R444 files and then reopens the complete R444/BC3 set with DeltaCache's validator. """ from __future__ import annotations import importlib.util import json import shutil import struct import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] TOOL_PATH = ( ROOT / "data/deltarune_builder/prepare_texture_cache_v0_3_6_bundle/source" / "prepare_texture_cache_v0_3_6.py" ) WORK_ROOT = ROOT / ".tmp/v071-r444-repair" def load_tool(): spec = importlib.util.spec_from_file_location("deltacache_prepare", TOOL_PATH) if spec is None or spec.loader is None: raise RuntimeError(f"Nao foi possivel carregar {TOOL_PATH}") module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module TARGETS = ( ("Original", ROOT / "data/prepared/deltarune/deltarunevita/chapter0"), ("Original", ROOT / "data/prepared/deltarune/deltarunevita/chapter1"), ("Original", ROOT / "data/prepared/deltarune/deltarunevita/chapter2"), ("Original", ROOT / "data/prepared/deltarune/deltarunevita/chapter3"), ("Original", ROOT / "data/prepared/deltarune/deltarunevita/chapter4"), ("Portuguese-BR", ROOT / "mods/Lang/Portuguese-BR/chapter0"), ("Portuguese-BR", ROOT / "mods/Lang/Portuguese-BR/chapter2"), ) def chapter_number(path: Path) -> int: return int(path.name.removeprefix("chapter")) def repair_target(tool, language: str, chapter_dir: Path) -> None: chapter = chapter_number(chapter_dir) data_win = chapter_dir / "data.win" cache_dir = chapter_dir / "texture-cache" pvr_dir = chapter_dir / "pvr" tx = tool.parse_txtr(data_win) required = [page for page in tx["pages"] if page["blobOffset"] != 0] required_names = {f"page_{page['page']:03d}" for page in required} actual = {path.stem for path in cache_dir.glob("page_*.r444")} missing = sorted(required_names - actual) extras = sorted(actual - required_names) invalid = [] for page in required: path = cache_dir / f"page_{page['page']:03d}.r444" if not path.is_file(): continue try: tool.validate_r444(chapter, page, path) except RuntimeError: invalid.append(page) for name in extras: (cache_dir / f"{name}.r444").unlink() # Header-only mismatches are safe to repair without recompressing pixels. payload_refresh = [] for page in invalid: path = cache_dir / f"page_{page['page']:03d}.r444" blob = path.read_bytes() expected_size = page["width"] * page["height"] * 2 if len(blob) == 20 + expected_size: header = struct.pack( " int: tool = load_tool() WORK_ROOT.mkdir(parents=True, exist_ok=True) for language, chapter_dir in TARGETS: repair_target(tool, language, chapter_dir) print("SUMMARY repaired=7 failed=0") return 0 if __name__ == "__main__": sys.exit(main())