Files
mandy/tools/rpf_probe.py
AlxcBTW d083a2cc37 Make main's tests green: 12 test files that drifted from the code (#9896)
Twelve test files failed on main at 2d53a8055 (CI's check and semantic-tests steps, run locally): most are a test
and the code it checks changed by different pull requests in the same merge round. Each fix below is either taken
from a closed pull request that already had it, or restores what an earlier merged change intended.

From closed pull requests:
- test_make_game_resume: FakeProc gets poll(), which translate() now calls (#7018)
- test_make_game_empty_translation: the Exited stub gets poll() and the failed-shard path is checked (#7214)
- test_shader_checks: stub table_fetch_test in the Wave64Checks harness and count it (#7433)
- test_runtime_paths: runtime.yml's path filter lists tools/replace_retry.py, which rdna2spirv.py imports (#7029)
- test_psin_names: patch the same_report_path helpers that report_paths now calls (#8719)
- test_report_source_collision: the fixture is a real Bink 2 header; bink_index refuses other files (#7408)
- test_missing_input_exit_codes: tile_probe checks its inputs before it asks for numpy (#4771)

Restored:
- merge_check: a failed `git show` is an error again (5b88d58); 21016cf brought back the lenient read that
  treated it as a new file, while new files are already left out when the diff is read
- rpf_probe: a symlinked --out updates its target again (44d3595); 55d21cc's staged write replaced the link

Updated to the code:
- test_shader_stage_resume: TRANSLATION_SOURCES lists fssm_unpack.py and shader_stage_manifest.py, which the
  translator imports; existing work folders translate once more after this
- test_tool_stdout_encoding: the 15 programs merged without the stdout guard get the same block as the others
- test_rdna2spirv_decode: drop the test of decode_any_format, which SHADER-849 replaced with the table decode
  that tests/semantic/test_format_decode_table.py checks

Co-authored-by: jewmaster1337 <102986566+jewmaster1337@users.noreply.github.com>
Co-authored-by: TanmaySatyam <248538550+TanmaySatyam@users.noreply.github.com>
Co-authored-by: evilclanker9000 <45567075+evilclanker9000@users.noreply.github.com>
Co-authored-by: slint6614-dev <359954236+slint6614-dev@users.noreply.github.com>
Co-authored-by: BlackNife <108077267+Poussistar@users.noreply.github.com>
Co-authored-by: fiestorra <65175643+fiestorra@users.noreply.github.com>
Co-authored-by: fearkov <289226582+fearkov@users.noreply.github.com>
Co-authored-by: DoubleD17 <48109637+DoubleD17@users.noreply.github.com>
2026-11-24 00:21:26 +01:00

139 lines
5.0 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 file_mode import published_mode # noqa: E402
from replace_retry import replace as replace_retry # noqa: E402
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 _write_json_report(path: str, rows: list) -> None:
"""Publish complete JSON, or leave the previous report intact on replace failure."""
# Resolve a symlinked --out to its target: the staged file then replaces the target, not the link.
destination = os.path.realpath(path)
out_dir = os.path.dirname(destination) or "."
os.makedirs(out_dir, exist_ok=True)
try:
mode = os.stat(destination).st_mode
except FileNotFoundError:
mode = None
except OSError:
# Non-regular destinations (pipes/devices) keep a direct write.
with open(destination, "w", encoding="utf-8", newline="\n") as f:
json.dump(rows, f, indent=1)
return
if mode is not None:
# Retain the direct writer's permission check without truncating the report.
os.close(os.open(destination, os.O_WRONLY))
staged = tempfile.NamedTemporaryFile(
mode="w", encoding="utf-8", newline="\n", delete=False,
dir=out_dir, prefix=".rpf_probe-", suffix=".tmp",
)
try:
with staged:
json.dump(rows, staged, indent=1)
if os.name != "nt":
os.chmod(staged.name, published_mode(destination))
replace_retry(staged.name, destination)
finally:
try:
os.unlink(staged.name)
except FileNotFoundError:
pass
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()
if os.path.isdir(a.out):
print(f"ERROR: {a.out}: is a directory, not a file", file=sys.stderr)
return 2
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:
_write_json_report(a.out, rows)
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())