mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 20:58:36 +00:00
main() caught only ValueError from the scan, so a missing source (os.path.getsize), a folder that can't be listed or a stream that can't be read ended in an OSError traceback, as did an output path in a folder that doesn't exist. Both now exit through sys.exit() with one line naming the path, like the ValueError case already does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
353 lines
15 KiB
Python
353 lines
15 KiB
Python
"""Synthetic-header tests for tools/anim_index.py.
|
|
|
|
python -I tests/test_anim_index.py
|
|
|
|
Every byte here is built by the test. No game file or game asset name is used.
|
|
"""
|
|
import csv
|
|
import json
|
|
import os
|
|
import struct
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
import zlib
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
TOOL = os.path.join(ROOT, "tools", "anim_index.py")
|
|
sys.path.insert(0, os.path.join(ROOT, "tools"))
|
|
|
|
import anim_index # noqa: E402
|
|
|
|
|
|
def move():
|
|
data = bytearray(72)
|
|
data[:4] = b"MoVE"
|
|
struct.pack_into("<I", data, 4, 19)
|
|
struct.pack_into("<I", data, 0x14, 852)
|
|
return bytes(data)
|
|
|
|
|
|
def page():
|
|
data = bytearray(0x60)
|
|
struct.pack_into("<Q", data, 8, 0x50100420)
|
|
data[16:20] = b"0DRP"
|
|
struct.pack_into("<I", data, 0x14, 122)
|
|
struct.pack_into("<Q", data, 0x20, 3)
|
|
return bytes(data)
|
|
|
|
|
|
def names():
|
|
data = b"pack:/alpha.clip\x00pack:/bravo.anim\x00"
|
|
return data + bytes(64 - len(data))
|
|
|
|
|
|
def skeleton():
|
|
data = b"SKEL_Spine\x00SKEL_Neck\x00"
|
|
return data + bytes(64 - len(data))
|
|
|
|
|
|
def chunks():
|
|
return struct.pack("<III", 0x88560988, 8, 7) + bytes(52)
|
|
|
|
|
|
def clip():
|
|
return struct.pack("<I", 3) + b"\x01\x01\x00\x00" + bytes(56)
|
|
|
|
|
|
def rbf0():
|
|
data = b"RBF0" + struct.pack("<H", 0xFFFB) + struct.pack("<I", 1) + struct.pack("<H", 21)
|
|
data += b"rage__crRigSetManager\x00"
|
|
return data + bytes(64 - len(data))
|
|
|
|
|
|
def raw():
|
|
return b"\x11\x22\x33\x44" + bytes(60)
|
|
|
|
|
|
def text():
|
|
data = b"<AnimData></AnimData>" * 4
|
|
return data + bytes(max(0, 64 - len(data)))
|
|
|
|
|
|
def bink():
|
|
data = bytearray(0x40)
|
|
data[:4] = b"KB2n"
|
|
struct.pack_into("<I", data, 4, len(data) - 8)
|
|
struct.pack_into("<I", data, 8, 1)
|
|
struct.pack_into("<I", data, 0x14, 64)
|
|
struct.pack_into("<I", data, 0x18, 64)
|
|
return bytes(data)
|
|
|
|
|
|
def _u16s(text):
|
|
return struct.pack("<H", len(text)) + text
|
|
|
|
|
|
def _fd(text):
|
|
return b"\xfd\xff" + struct.pack("<I", len(text) + 1) + text + b"\x00"
|
|
|
|
|
|
def _entry(source, rig, group):
|
|
return _fd(source) + b"\xff\xff\x06\x00" + bytes(6) + _fd(rig) + b"\xff\xff\x07\x00" + bytes(6) + _fd(group)
|
|
|
|
|
|
def rbf0_blob():
|
|
"""Synthetic RBF0 with three rig sets (one empty), three rig entries and three source kinds."""
|
|
b = bytearray(b"RBF0" + struct.pack("<H", 0xFFFB) + struct.pack("<I", 1) + _u16s(b"rage__crRigSetManager") + b"\x00")
|
|
b += b"\x03\x60\x03\x00key" + _u16s(b"anim/rigset/synthetic/one")
|
|
b += b"\x04\x00" + bytes(6) + b"\x02\x00" + bytes(6) + b"\x05\x00" + bytes(6)
|
|
b += _entry(b"canonical@source", b"anim/rig/canonical/source/synthetic/one", b"pose")
|
|
b += _entry(b"generated@source", b"anim/rig/generated/source/synthetic/two", b"deformation")
|
|
b += b"\xff" * 6
|
|
b += b"\x02\x00\x00\x00\x00\x00\x01\x00\x03\x60" + _u16s(b"anim/rigset/synthetic/empty")
|
|
b += b"\x04\x00" + bytes(6) + b"\xff" * 4
|
|
b += b"\x02\x00\x00\x00\x00\x00\x01\x00\x03\x60" + _u16s(b"anim/rigset/synthetic/three")
|
|
b += b"\x04\x00" + bytes(6) + b"\x02\x00" + bytes(6) + b"\x05\x00" + bytes(6)
|
|
b += _entry(b"pipelined@source", b"anim/rig/pipelined/source/synthetic/three", b"facial")
|
|
b += b"\xff" * 6
|
|
return bytes(b)
|
|
|
|
|
|
PAYLOADS = [move(), page(), names(), skeleton(), chunks(), clip(), rbf0(), raw(), text(), bink()]
|
|
EXPECTED = ["move", "page-directory", "name-table", "skeleton-names", "typehash", "clip-stream",
|
|
"rbf0", "raw:11223344", "text", "bink2"]
|
|
|
|
|
|
class ClassifyTest(unittest.TestCase):
|
|
def test_signatures(self):
|
|
got = [anim_index.classify(p, 0, len(p))["signature"] for p in PAYLOADS]
|
|
self.assertEqual(got, EXPECTED)
|
|
|
|
def test_move_header(self):
|
|
r = anim_index.classify(move(), 0x100, 90)
|
|
self.assertEqual((r["version"], r["detail"]), (19, "size_hint=852"))
|
|
|
|
def test_page_header(self):
|
|
r = anim_index.classify(page(), 0x200, 40)
|
|
self.assertEqual((r["version"], r["count"], r["detail"]), (122, 3, "0DRP at +16"))
|
|
|
|
def test_names(self):
|
|
r = anim_index.classify(names(), 0x300, 30)
|
|
self.assertEqual((r["name"], r["count"]), ("pack:/alpha.clip", 2))
|
|
|
|
def test_skeleton(self):
|
|
r = anim_index.classify(skeleton(), 0x400, 20)
|
|
self.assertEqual((r["name"], r["count"]), ("SKEL_Spine", 2))
|
|
|
|
def test_typehash(self):
|
|
r = anim_index.classify(chunks(), 0x500, 70)
|
|
self.assertEqual((r["version"], r["count"], r["detail"]), (8, 7, "type=0x88560988"))
|
|
|
|
def test_clip_header(self):
|
|
r = anim_index.classify(clip(), 0x600, 70)
|
|
self.assertEqual((r["version"], r["detail"]), (3, "24-byte header"))
|
|
|
|
def test_rbf0(self):
|
|
r = anim_index.classify(rbf0(), 0x700, 30)
|
|
self.assertEqual((r["name"], r["detail"]), ("rage__crRigSetManager", "rig metadata"))
|
|
|
|
|
|
class RigParseTest(unittest.TestCase):
|
|
def test_joaat_known_value(self):
|
|
self.assertEqual(anim_index.joaat32("adder"), 0xB779A091)
|
|
|
|
def test_rbf0_rigsets_and_entries(self):
|
|
r = anim_index.parse_rbf0(rbf0_blob())
|
|
self.assertEqual(r["header"]["schema"], "rage__crRigSetManager")
|
|
self.assertEqual((r["summary"]["rigsets"], r["summary"]["entries"], r["summary"]["distinct_rigs"]),
|
|
(3, 3, 3))
|
|
self.assertEqual(r["summary"]["empty_rigsets"], 1)
|
|
self.assertEqual(r["summary"]["sources"]["canonical@source"]["entries"], 1)
|
|
self.assertEqual(r["summary"]["sources"]["generated@source"]["distinct_rigs"], 1)
|
|
self.assertEqual(r["summary"]["sources"]["pipelined@source"]["entries"], 1)
|
|
self.assertEqual(r["summary"]["groups"], {"pose": 1, "deformation": 1, "facial": 1})
|
|
self.assertEqual(r["rigsets"][0]["entries"][0]["source_offset"],
|
|
rbf0_blob().find(b"canonical@source") - 6)
|
|
self.assertEqual(r["rigsets"][0]["distinct_rigs"], 2)
|
|
|
|
def test_rbf0_rejects_non_rbf0(self):
|
|
with self.assertRaises(ValueError):
|
|
anim_index.parse_rbf0(b"not an RBF0 blob")
|
|
|
|
def test_skel_names_and_hashes(self):
|
|
name = b"SKEL_Synthetic"
|
|
data = name + b"\x00" + struct.pack("<I", anim_index.joaat32(name.decode())) + b"\x00SKEL_Second\x00"
|
|
r = anim_index.parse_skel_names(data, 0x123)
|
|
self.assertEqual((r["offset"], r["occurrences"], r["distinct"], r["hashes_present"]),
|
|
(0x123, 2, 2, 1))
|
|
|
|
|
|
class ArchiveTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.root = self.tmp.name
|
|
self.rpf = os.path.join(self.root, "synthetic.rpf")
|
|
header = bytearray(0x100)
|
|
header[:4] = b"9FPR"
|
|
struct.pack_into("<I", header, 4, len(header))
|
|
body = bytearray()
|
|
self.offsets = []
|
|
for payload in PAYLOADS:
|
|
self.offsets.append(len(header) + len(body))
|
|
body += payload if payload[:3] == b"KB2" else zlib.compress(payload)
|
|
body += bytes((-len(body)) % 16)
|
|
body += bytes(64)
|
|
with open(self.rpf, "wb") as f:
|
|
f.write(header + body)
|
|
self.span = len(body)
|
|
|
|
def tearDown(self):
|
|
self.tmp.cleanup()
|
|
|
|
def test_scan_offsets_and_summary(self):
|
|
rows, summary = anim_index.scan_rpf(self.rpf)
|
|
self.assertEqual([r["offset"] for r in rows], self.offsets)
|
|
self.assertEqual([r["signature"] for r in rows], EXPECTED)
|
|
self.assertEqual(summary["files"], len(PAYLOADS))
|
|
self.assertEqual(summary["bink2_files"], 1)
|
|
self.assertEqual(summary["unread_bytes"], 0)
|
|
self.assertEqual(summary["span"], self.span)
|
|
self.assertEqual(summary["inflated_bytes"], sum(len(p) for p in PAYLOADS))
|
|
# A stored length must stop at the zlib end, not the end of the read block.
|
|
zlibs = [r for r in rows if r["signature"] != "bink2"]
|
|
self.assertEqual(sum(r["stored"] for r in zlibs), sum(len(zlib.compress(p)) for p in PAYLOADS[:-1]))
|
|
self.assertEqual(rows[-1]["stored"], 0x40)
|
|
self.assertGreater(summary["readable_fraction"], 0.5)
|
|
|
|
def test_scan_limit(self):
|
|
limit = self.offsets[1] - 0x100 - 1
|
|
rows, summary = anim_index.scan_rpf(self.rpf, limit=limit)
|
|
self.assertEqual(len(rows), 1)
|
|
self.assertEqual(rows[0]["signature"], "move")
|
|
self.assertEqual(summary["span"], limit)
|
|
|
|
def test_csv_and_json(self):
|
|
rows, summary = anim_index.scan_rpf(self.rpf)
|
|
csv_path = os.path.join(self.root, "out.csv")
|
|
json_path = os.path.join(self.root, "out.json")
|
|
anim_index.write_output(csv_path, rows, summary)
|
|
anim_index.write_output(json_path, rows, summary)
|
|
with open(csv_path, newline="", encoding="utf-8") as f:
|
|
table = list(csv.reader(f))
|
|
self.assertEqual(table[0], anim_index.COLUMNS)
|
|
self.assertEqual(len(table), len(PAYLOADS) + 1)
|
|
self.assertEqual(table[1][3], "move")
|
|
with open(json_path, encoding="utf-8") as f:
|
|
doc = json.load(f)
|
|
self.assertEqual(doc["summary"]["files"], len(PAYLOADS))
|
|
self.assertEqual(len(doc["files"]), len(PAYLOADS))
|
|
|
|
def test_bad_scan_arguments_fail_before_an_index_is_written(self):
|
|
for flag, value, message in (("--start", "-1", "must be 0 or more"),
|
|
("--limit", "0", "must be at least 1"),
|
|
("--limit", "-100", "must be at least 1")):
|
|
out = os.path.join(self.root, "bad.json")
|
|
p = subprocess.run([sys.executable, "-I", TOOL, self.rpf, out, "--quiet", flag, value],
|
|
capture_output=True, text=True, encoding="utf-8", errors="replace")
|
|
self.assertEqual(p.returncode, 2, p.stderr)
|
|
self.assertIn(message, p.stderr)
|
|
self.assertNotIn("Traceback", p.stderr)
|
|
self.assertFalse(os.path.exists(out))
|
|
|
|
def test_missing_source_or_out_folder_is_one_line(self):
|
|
missing = os.path.join(self.root, "missing.rpf")
|
|
out_in_missing = os.path.join(self.root, "no_dir", "out.json")
|
|
for source, out, path in ((missing, os.path.join(self.root, "m.json"), missing),
|
|
(self.rpf, out_in_missing, out_in_missing)):
|
|
with self.subTest(path=os.path.relpath(path, self.root)):
|
|
p = subprocess.run([sys.executable, "-I", TOOL, source, out, "--quiet"],
|
|
capture_output=True, text=True, encoding="utf-8", errors="replace")
|
|
self.assertEqual(p.returncode, 1, p.stderr)
|
|
self.assertTrue(p.stderr.startswith(path + ": "), p.stderr)
|
|
self.assertNotIn("Traceback", p.stderr)
|
|
self.assertEqual(p.stdout, "")
|
|
self.assertFalse(os.path.exists(os.path.join(self.root, "m.json")))
|
|
|
|
def test_cli(self):
|
|
out = os.path.join(self.root, "cli.json")
|
|
p = subprocess.run([sys.executable, "-I", TOOL, self.rpf, out, "--quiet"],
|
|
capture_output=True, text=True, encoding="utf-8", errors="replace")
|
|
self.assertEqual(p.returncode, 0, p.stdout + p.stderr)
|
|
with open(out, encoding="utf-8") as f:
|
|
doc = json.load(f)
|
|
self.assertEqual(doc["summary"]["signatures"]["move"]["files"], 1)
|
|
self.assertEqual(doc["summary"]["signatures"]["page-directory"]["files"], 1)
|
|
|
|
def test_cli_rigs_without_rigset_keys_keeps_existing_file(self):
|
|
# PAYLOADS' RBF0 stream has a schema name but no anim/rigset/ keys, so parse_rbf0 rejects it.
|
|
out = os.path.join(self.root, "index.json")
|
|
rigs = os.path.join(self.root, "rigs.json")
|
|
with open(rigs, "w", encoding="utf-8") as f:
|
|
f.write('{"previous": true}')
|
|
p = subprocess.run([sys.executable, "-I", TOOL, self.rpf, out, "--rigs", rigs, "--quiet"],
|
|
capture_output=True, text=True, encoding="utf-8", errors="replace")
|
|
self.assertEqual(p.returncode, 2, p.stdout + p.stderr)
|
|
self.assertIn("no anim/rigset/ keys found", p.stderr)
|
|
self.assertNotIn("Traceback", p.stderr)
|
|
with open(rigs, encoding="utf-8") as f:
|
|
self.assertEqual(json.load(f), {"previous": True})
|
|
|
|
def test_directory(self):
|
|
folder = os.path.join(self.root, "carved")
|
|
os.makedirs(folder)
|
|
for i, payload in enumerate((move(), page(), names())):
|
|
with open(os.path.join(folder, f"{0x1000 + i * 0x100:012x}.bin"), "wb") as f:
|
|
f.write(payload)
|
|
rows, summary = anim_index.scan_dir(folder)
|
|
self.assertEqual([r["offset"] for r in rows], [0x1000, 0x1100, 0x1200])
|
|
self.assertEqual([r["signature"] for r in rows], ["move", "page-directory", "name-table"])
|
|
self.assertEqual(summary["mode"], "directory")
|
|
|
|
|
|
class RigArchiveTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.root = self.tmp.name
|
|
self.rpf = os.path.join(self.root, "rigs.rpf")
|
|
blob = rbf0_blob()
|
|
cut = blob.find(b"\xff" * 6) + 6
|
|
self.parts = [blob[:cut], blob[cut:]]
|
|
self.skel = b"SKEL_Synthetic\x00SKEL_Second\x00"
|
|
self.skel += bytes(64 - len(self.skel))
|
|
self.front = b"\x0f\x00\xe0\x00\x0c\x00\x16\x00SKEL_Front\x00"
|
|
self.front += struct.pack("<I", anim_index.joaat32("SKEL_Front")) + bytes(64)
|
|
header = bytearray(0x100)
|
|
header[:4] = b"9FPR"
|
|
struct.pack_into("<I", header, 4, len(header))
|
|
body = bytearray()
|
|
for payload in self.parts + [self.skel, self.front]:
|
|
body += zlib.compress(payload)
|
|
body += bytes((-len(body)) % 16)
|
|
with open(self.rpf, "wb") as f:
|
|
f.write(header + body)
|
|
|
|
def tearDown(self):
|
|
self.tmp.cleanup()
|
|
|
|
def test_collect_and_parse_continuation(self):
|
|
rows, _ = anim_index.scan_rpf(self.rpf)
|
|
blob = anim_index.collect_rbf0(self.rpf, rows)
|
|
self.assertIsNotNone(blob)
|
|
r = anim_index.parse_rbf0(blob)
|
|
self.assertEqual(r["summary"]["rigsets"], 3)
|
|
self.assertEqual(r["summary"]["entries"], 3)
|
|
|
|
def test_cli_rigs(self):
|
|
out = os.path.join(self.root, "index.json")
|
|
rigs = os.path.join(self.root, "rigs.json")
|
|
p = subprocess.run([sys.executable, "-I", TOOL, self.rpf, out, "--rigs", rigs, "--quiet"],
|
|
capture_output=True, text=True, encoding="utf-8", errors="replace")
|
|
self.assertEqual(p.returncode, 0, p.stdout + p.stderr)
|
|
with open(rigs, encoding="utf-8") as f:
|
|
doc = json.load(f)
|
|
self.assertEqual(doc["rbf0"]["summary"]["rigsets"], 3)
|
|
self.assertEqual(doc["skeletons"][0]["distinct"], 2)
|
|
self.assertEqual(doc["skeletons"][0]["kind"], "plain-bone-table")
|
|
self.assertEqual(doc["skeletons"][1]["hashes_present"], 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|