Files
mandy/tools/rpf_probe.py
tboris 9f2086e6de tools/report_paths.py: one same_report_path for seven tools instead of seven copies (BUILD-589)
same_report_path was copied into ten tools, and each new "refuse an output that overwrites an input" change added
another copy or a variant. Four copies (anim_index, gamedata_index, gamedata_xml, navmesh_decode) also folded case and
accents on a case-insensitive macOS volume (through _path_key), the other six did not, so the same report pair was
refused by one tool and written by another.

tools/report_paths.py holds one version (the stricter one: realpath, normcase, case-folded NFD on a case-insensitive
volume, then os.path.samefile) and awc_manifest, gamedata_index, gamedata_xml, navmesh_index, rpf_probe, tile_probe
and world_names import it. The tools keep exposing the name same_report_path. On Windows and Linux nothing changes;
on macOS the five tools that lacked the case/accent step now refuse a case-only alias of a report that doesn't exist
yet, as the other five already did.

anim_index, navmesh_decode and world_lights_paths keep their copy for now because open pull requests edit them
(#2051, #2050, #2052, #2107); tests/test_report_paths.py fails if any other tool adds a copy.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SAjLH2L6R72dBvVaRG8hka
2026-11-22 16:09:57 +00:00

104 lines
3.8 KiB
Python

"""Survey the .rpf archives in a game folder: version magic, header words, and whether the table looks encrypted.
Usage:
python -I rpf_probe.py <game_dir> [--out FILE]
Reads the first 64 KiB of each archive only. It doesn't decrypt or extract anything.
A directory scan or archive read error stops the survey before it writes the report.
The report destination must not name an input archive or an alias of one.
"""
import argparse
import json
import math
import os
import struct
import sys
import tempfile
from collections import Counter
# -I keeps the script directory off sys.path, so add it explicitly before importing the sibling module.
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from report_paths import same_report_path # noqa: E402
def entropy(b: bytes) -> float:
if not b:
return 0.0
n = len(b)
return -sum(c / n * math.log2(c / n) for c in Counter(b).values())
def probe(path: str) -> dict:
with open(path, "rb") as f:
head = f.read(0x10000)
magic = head[:4]
# Little-endian 'RPFn' is stored as bytes 'nFPR'.
version = magic[::-1].decode("latin-1") if magic[1:] == b"FPR" else None
words = list(struct.unpack_from("<4I", head, 4)) if len(head) >= 20 else []
body = head[16:]
return {
"file": path,
"size": os.path.getsize(path),
"version": version,
"header_words": [hex(w) for w in words],
"head_entropy": round(entropy(body), 3),
"looks_encrypted": entropy(body) > 7.9,
}
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("game_dir")
ap.add_argument("--out", default=os.path.join(os.path.dirname(__file__), "..", "reports", "rpf_probe.json"))
a = ap.parse_args()
rows = []
def scan_failed(error: OSError) -> None:
raise error
try:
archives = []
for root, _, files in os.walk(a.game_dir, onerror=scan_failed):
for name in files:
if name.lower().endswith(".rpf"):
archives.append(os.path.join(root, name))
for path in archives:
if same_report_path(a.out, path):
print(f"ERROR: --out would overwrite an input archive: {path}", file=sys.stderr)
return 2
for path in archives:
rows.append(probe(path))
except OSError as error:
path = error.filename or a.game_dir
reason = error.strerror or str(error)
print(f"ERROR: {path}: {reason}", file=sys.stderr)
return 2
rows.sort(key=lambda r: (-r["size"], r["file"]))
try:
out_dir = os.path.dirname(os.path.abspath(a.out))
os.makedirs(out_dir, exist_ok=True)
with tempfile.TemporaryDirectory(dir=out_dir) as staging:
staged = os.path.join(staging, "rpf_probe.json")
with open(staged, "w", encoding="utf-8", newline="\n") as f:
json.dump(rows, f, indent=1)
os.replace(staged, a.out)
except OSError as error:
reason = error.strerror or str(error)
print(f"ERROR: {a.out}: {reason}", file=sys.stderr)
return 2
versions = Counter(r["version"] for r in rows)
print(f"{len(rows)} archives, {sum(r['size'] for r in rows) / 2**30:.1f} GiB, versions {dict(versions)}")
for r in rows:
rel = os.path.relpath(r["file"], a.game_dir)
print(f"{rel:<52} {r['size'] / 2**30:7.2f} GiB {r['version']} {' '.join(r['header_words'])} "
f"H={r['head_entropy']}{' packed/encrypted' if r['looks_encrypted'] else ''}")
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())