mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 20:38:37 +00:00
- int_hw_probe: VOP3 clamp on the carry-out add/sub ops (#1831) and v_cvt_rpi/flr's exact floor(x+0.5) and NaN sign saturation (#1856) now translate as gfx1035 measured, so KNOWN_DIVERGENCES is empty. The known-divergence test reports a synthetic entry. shader_validation.md says so, and design.md loses the stale duplicate v_cvt_flr/rpi row a "both" merge left that still called them open. - docs/README.md: one Other languages line (fr, pt-BR, zh-CN, pl-PL, tr, ru, ja); docs/i18n/README.md: one row each for pt-BR and zh-CN (the newer). - anim_rigs, d16_format_probe, pe_shader_containers: text writes name newline="\n" (#1615); pe_shader_containers guards stdout (#1428). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
173 lines
7.5 KiB
Python
173 lines
7.5 KiB
Python
"""Read the rig dictionaries in anim.rpf and join the RBF0 rig-set manager's entries to them (ASSET-20).
|
||
|
||
python -I tools/anim_rigs.py <package>\\anim.rpf <work>\\anim-rigs.json [--rigs <work>\\rigs.json]
|
||
|
||
A rig dictionary has the clip dictionary's root layout (docs/animation.md, "Clip dictionaries are RAGE
|
||
resources") with `RIG` + a version byte at +0x70 instead of the pack array. Its map is keyed by `joaat32` of a rig
|
||
path. Each value points at +0x20 to a `RIG` block, which holds tables of 8-byte `(hash, word)` items. `--rigs`
|
||
takes the JSON `anim_index.py --rigs` writes and resolves every rig-set entry (source = rig dictionary name,
|
||
rig = key). The output holds counts and one row per rig dictionary, no item values.
|
||
"""
|
||
import argparse
|
||
import collections
|
||
import json
|
||
import mmap
|
||
import os
|
||
import struct
|
||
import sys
|
||
import zlib
|
||
|
||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||
import anim_clips # noqa: E402
|
||
import anim_index # noqa: E402
|
||
|
||
SYSTEM = anim_clips.SYSTEM
|
||
MAGIC = b"RIG"
|
||
|
||
|
||
def is_rig_dictionary(data):
|
||
return len(data) >= 0x78 and data[0x70:0x73] == MAGIC
|
||
|
||
|
||
def block_tables(s, at):
|
||
"""Tables of a `RIG` block at page offset `at`: `RIG` + u8 version, u32 length of what follows, then tables
|
||
`{u32 kind hash; u32 size = 0xC + 8 * count; u32 count; u32 word}` + count × `{u32 hash; u32 word}`, with other
|
||
bytes between them. Returns (version, length, [(kind, count)])."""
|
||
if s.d[at:at + 3] != MAGIC:
|
||
raise anim_clips.DecodeError(f"no RIG block at 0x{at:x}")
|
||
version, length = s.d[at + 3], s.u32(at + 4)
|
||
end = min(len(s.d), at + 8 + length)
|
||
o, tables = at + 8, []
|
||
while o + 16 <= end:
|
||
kind, size, count, _word = struct.unpack_from("<4I", s.d, o)
|
||
if 0 < count < 4096 and size == 0xC + 8 * count and o + 16 + 8 * count <= end:
|
||
tables.append((kind, count))
|
||
o += 16 + 8 * count
|
||
else:
|
||
o += 4
|
||
return version, length, tables
|
||
|
||
|
||
def parse_rig_dictionary(s):
|
||
"""Root: +0x38 name, +0x48 map (key = joaat32(rig path), high half 0), +0x68 joaat32(name), +0x70 `RIG`."""
|
||
d = {"name": s.string(0x38), "hash": s.u32(0x68), "version": s.d[0x73], "entries": []}
|
||
for key, value in s.atmap(0x48):
|
||
if value is None:
|
||
continue
|
||
block = s.ptr(value + 0x20)
|
||
e = {"key": key & 0xFFFFFFFF, "key_high": key >> 32, "block": block}
|
||
if block is not None:
|
||
e["block_version"], e["block_length"], e["tables"] = block_tables(s, block)
|
||
d["entries"].append(e)
|
||
return d
|
||
|
||
|
||
def _read(path, segments):
|
||
with open(path, "rb") as fh:
|
||
data = b""
|
||
for off, stored, raw in segments:
|
||
fh.seek(off)
|
||
data += fh.read(stored) if raw else zlib.decompressobj().decompress(fh.read(stored))
|
||
return data
|
||
|
||
|
||
def scan(path, start=None, limit=None):
|
||
rows, walk = anim_index.scan_rpf(path, start, limit)
|
||
with open(path, "rb") as fh, mmap.mmap(fh.fileno(), 0, access=mmap.ACCESS_READ) as m:
|
||
groups, _unresolved, _gaps = anim_clips.group(m, rows)
|
||
stats, dicts = collections.Counter(), []
|
||
for _i, parts in groups:
|
||
segments = [anim_clips.segment(rows, k) for k in parts]
|
||
with open(path, "rb") as fh:
|
||
fh.seek(segments[0][0])
|
||
head = fh.read(segments[0][1])
|
||
first = head if segments[0][2] else zlib.decompressobj().decompress(head, 0x80)
|
||
if not is_rig_dictionary(first):
|
||
continue
|
||
s = anim_clips.System(_read(path, segments))
|
||
try:
|
||
d = parse_rig_dictionary(s)
|
||
except (anim_clips.DecodeError, struct.error) as e:
|
||
stats["rig_dictionaries_unparsed"] += 1
|
||
dicts.append({"offset": segments[0][0], "error": str(e)[:120]})
|
||
continue
|
||
d["offset"], d["streams"], d["system_bytes"] = segments[0][0], len(segments), len(s.d)
|
||
dicts.append(d)
|
||
stats["rig_dictionaries"] += 1
|
||
stats["rig_dictionary_hash_matches_name"] += d["name"] is not None and anim_index.joaat32(d["name"]) == d["hash"]
|
||
stats[f"rig_dictionary_version:{d['version']}"] += 1
|
||
for e in d["entries"]:
|
||
stats["entries"] += 1
|
||
stats["entries_key_high_zero"] += e["key_high"] == 0
|
||
if e["block"] is None:
|
||
stats["entries_without_block"] += 1
|
||
continue
|
||
stats["entries_with_block"] += 1
|
||
stats[f"block_version:{e['block_version']}"] += 1
|
||
stats["tables"] += len(e["tables"])
|
||
for kind, count in e["tables"]:
|
||
stats[f"table_kind:0x{kind:08x}"] += 1
|
||
stats[f"table_items:0x{kind:08x}"] += count
|
||
return dicts, {"streams": walk["files"], "dictionary_roots": len(groups), "counts": dict(sorted(stats.items()))}
|
||
|
||
|
||
def join_rbf0(dicts, rigs):
|
||
"""Resolve the rig-set manager's entries: source = rig dictionary name, joaat32(rig) = a key there."""
|
||
keys = {d["name"]: {e["key"] for e in d.get("entries", ())} for d in dicts if d.get("name")}
|
||
entries = [e for rs in rigs["rbf0"]["rigsets"] for e in rs["entries"]]
|
||
c = collections.Counter()
|
||
for e in entries:
|
||
h = anim_index.joaat32(e["rig"].lower())
|
||
if h in keys.get(e["source"], ()):
|
||
c["resolved"] += 1
|
||
elif any(h in k for k in keys.values()):
|
||
c["in_another_dictionary"] += 1
|
||
elif e["source"] not in keys:
|
||
c["no_dictionary_named_source"] += 1
|
||
else:
|
||
c["key_missing"] += 1
|
||
distinct = {(e["source"], e["rig"].lower()) for e in entries}
|
||
used = {(src, anim_index.joaat32(r)) for src, r in distinct}
|
||
c["distinct_rigs"] = len(distinct)
|
||
c["keys"] = sum(len(k) for k in keys.values())
|
||
c["keys_named_by_an_entry"] = sum(1 for name, k in keys.items() for h in k if (name, h) in used)
|
||
return dict(sorted(c.items()))
|
||
|
||
|
||
def main():
|
||
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||
ap.add_argument("archive", help="anim.rpf")
|
||
ap.add_argument("out", help="output .json (counts and one row per rig dictionary)")
|
||
ap.add_argument("--rigs", help="the JSON anim_index.py --rigs wrote, to resolve the RBF0 entries")
|
||
ap.add_argument("--start", type=anim_index.offset, help="first byte to scan (default: the RPF data start)")
|
||
ap.add_argument("--limit", type=anim_index.byte_count, help="number of bytes to scan")
|
||
a = ap.parse_args()
|
||
try:
|
||
dicts, summary = scan(a.archive, a.start, a.limit)
|
||
if a.rigs:
|
||
with open(a.rigs, encoding="utf-8") as f:
|
||
summary["rbf0_join"] = join_rbf0(dicts, json.load(f))
|
||
except ValueError as e:
|
||
sys.exit(f"{a.archive}: {e}")
|
||
except OSError as e:
|
||
sys.exit(f"{e.filename or a.archive}: {e.strerror or e}")
|
||
rows = [{k: v for k, v in d.items() if k != "entries"} | {"entries": len(d.get("entries", ()))} for d in dicts]
|
||
try:
|
||
with open(a.out, "w", encoding="utf-8", newline="\n") as f:
|
||
json.dump({"summary": summary, "rig_dictionaries": rows}, f, indent=1)
|
||
except OSError as e:
|
||
sys.exit(f"{a.out}: cannot write: {e.strerror or e}")
|
||
c = summary["counts"]
|
||
print(json.dumps({k: c.get(k, 0) for k in ("rig_dictionaries", "entries", "entries_with_block", "tables")}))
|
||
if "rbf0_join" in summary:
|
||
print(json.dumps(summary["rbf0_join"]))
|
||
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())
|