"""Carve shader containers out of an RPF9 archive without reading its (encrypted) table of contents. Each shader in shaders.rpf is stored as: 00 and inflates to a DXBC container (`size` bytes) holding DXIL plus an Xbox PRIV part. Usage: python -I shader_carve.py Writes /.dxbc and /index.json (name, offset, stage, shader model, parts). """ import argparse import json import mmap import os import re import struct import sys import tempfile import zlib # `python -I` leaves the script's directory off sys.path. sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from replace_retry import replace as replace_retry # noqa: E402 from shader_carve_accel import entry_search # noqa: E402 ENTRY = re.compile(rb"([\x21-\x7e]{3,200})\x00(.)(....)(....)\3(....)", re.DOTALL) STAGES = {0: "ps", 1: "vs", 2: "gs", 3: "hs", 4: "ds", 5: "cs", 6: "lib", 13: "ms", 14: "as"} RESERVED = {"CON", "PRN", "AUX", "NUL", *(f"COM{i}" for i in range(1, 10)), *(f"LPT{i}" for i in range(1, 10))} def parts(dxbc: bytes) -> dict: if len(dxbc) < 32: raise ValueError("truncated DXBC header") count = struct.unpack_from(" len(dxbc): raise ValueError("truncated DXBC part table") out = {} for entry in range(32, table_end, 4): off = struct.unpack_from(" len(dxbc): raise ValueError("truncated DXBC part payload") out[fourcc.decode("latin-1")] = (off + 8, size) return out def shader_model(dxbc: bytes, p: dict): if "DXIL" not in p: return None, None off, size = p["DXIL"] if size < 4: raise ValueError("truncated DXIL version and stage") ver, kind = struct.unpack_from("> 4}.{ver & 0xF}" def file_name(name: str, seen: dict, used: set) -> str: """A file name for `name` that no earlier shader has, as Windows compares them: without case, and with the device names (CON, NUL, COM1...) that open a device instead of a file. Same name twice means distinct variants; the second gets ~1, the third ~2.""" base = re.sub(r'[<>:"/\\|?*]', "_", name) if base.split(".")[0].upper() in RESERVED: base = "_" + base key = base.lower() n = seen.get(key, 0) while True: fname = base + (f"~{n}" if n else "") + ".dxbc" if fname.lower() not in used: break n += 1 seen[key] = n + 1 used.add(fname.lower()) return fname def main(argv=None) -> int: ap = argparse.ArgumentParser(description="Carve DXBC shader containers out of an RPF9 archive.") ap.add_argument("src", help="the shaders.rpf archive") ap.add_argument("out", help="folder for the .dxbc files and index.json") a = ap.parse_args(argv) src, out = a.src, a.out if not os.path.isfile(src): ap.error(f"{src} is not a file") if os.path.getsize(src) == 0: ap.error(f"{src} is empty") if os.path.isfile(out): ap.error(f"{out} is a file, not a folder") try: os.makedirs(out, exist_ok=True) except OSError as e: # a regular file where a parent folder is expected ap.error(f"cannot create {out}: {e.strerror or e}") index, seen, used, bad = [], {}, set(), 0 with (open(src, "rb") as fh, mmap.mmap(fh.fileno(), 0, access=mmap.ACCESS_READ) as m, entry_search(m, ENTRY) as search): pos = 0 while (mt := search(pos)) is not None: # A rejected header is rescanned past its name (a real entry can start inside it); a carved # shader resumes after its stored bytes, so deflate output never becomes a name or a match. pos = mt.end(1) + 1 name = mt.group(1).decode("latin-1") stored = struct.unpack("