"""Index named RPF9 resources without the encrypted table of contents. The RPF9 data area is mostly zlib streams laid end to end. This walks it the same way ``rpf9_carve.py`` does, but inflates only a bounded prefix of each stream and records the measured resource-name spelling (``name.#dr``, ``name.#ft``, ``name.#dd``, ...). The result is a lightweight lookup index for targeted model/animation extraction; it does not decode a model and it never writes game data. Usage: python -I rpf9_names.py [--start N] [--limit BYTES] [--max-inflate BYTES] [--types dr,ft,dd] [--quiet] The output contains archive offsets, compressed sizes, inflated sizes and the names found in each stream. It is local scratch; do not commit the names or the index. """ import argparse import json import mmap import os import re import struct import sys import tempfile import time import zlib from contextlib import suppress sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from file_mode import published_mode # noqa: E402 from zero_span import gap_split # noqa: E402 # zlib stream start, or a Bink 2 video ('KB2' + revision letter, then u32 size-8). ZHDR = re.compile(rb"\x78[\x01\x5e\x9c\xda]|KB2[a-z]") # Cap on the transient output a single decompress() call may produce, and the largest # input feed read from the map. ``max_inflate`` remains the bound on # how much of the inflated stream is kept for name extraction. DECOMPRESS_MAX = 1 << 20 # Most short streams end in the first read. zlib copies any bytes beyond EOF # into unused_data, so reading a full MiB up front copies the next streams too. INITIAL_FEED = 4096 NAME = re.compile(rb"(? end: # header cut off by the end of the window: not a video pos = s + 1 continue blen = struct.unpack_from("= 0") if a.limit is not None and a.limit <= 0: raise ValueError("--limit must be > 0") if a.max_inflate <= 0: raise ValueError("--max-inflate must be > 0") if not os.path.isfile(a.archive): raise ValueError("archive does not exist") except ValueError as exc: print(f"ERROR: {exc}", file=sys.stderr) return 2 out_abs = os.path.abspath(a.out) try: if os.path.exists(a.out) and os.path.samefile(a.archive, a.out): raise ValueError(f"output {a.out} is the input archive") except (OSError, ValueError) as exc: print(f"ERROR: {exc}", file=sys.stderr) return 2 progress = None if not a.quiet: progress = lambda text: print(f" {text}", file=sys.stderr, flush=True) try: rows, summary = scan_archive(a.archive, a.start, a.limit, a.max_inflate, types, progress) except (OSError, ValueError) as exc: print(f"ERROR: {exc}", file=sys.stderr) return 2 try: publish_report(out_abs, rows, summary) except OSError as exc: print(f"ERROR: cannot write report {a.out}: {exc.strerror or exc}", file=sys.stderr) return 2 if not a.quiet: print(json.dumps(summary, indent=1)) return 0 if __name__ == "__main__": try: # a redirected stdout is strict ANSI code page under -I; write '?' for what it cannot encode sys.stdout.reconfigure(errors="replace") except (AttributeError, ValueError, OSError): pass sys.exit(main())