mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 20:38:37 +00:00
78 lines
2.6 KiB
Python
78 lines
2.6 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.
|
|
"""
|
|
import argparse
|
|
import json
|
|
import math
|
|
import os
|
|
import struct
|
|
import sys
|
|
from collections import Counter
|
|
|
|
|
|
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:
|
|
for root, _, files in os.walk(a.game_dir, onerror=scan_failed):
|
|
for name in files:
|
|
if name.lower().endswith(".rpf"):
|
|
rows.append(probe(os.path.join(root, name)))
|
|
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"])
|
|
os.makedirs(os.path.dirname(os.path.abspath(a.out)), exist_ok=True)
|
|
with open(a.out, "w", encoding="utf-8") as f:
|
|
json.dump(rows, f, indent=1)
|
|
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__":
|
|
sys.exit(main())
|