Files
mandy/tests/test_instance_lists.py
Shrey Ratna e9e02e7e23 vertex_types, instance_lists: --json can't overwrite an input archive
Both tools write their --json report after the whole scan, with no check
on the path. Given one of the input .rpf archives, the archive was
replaced by the report and the run exited 0; a directory, or a file in a
folder that doesn't exist, ended the scan in a traceback.

Now, before the scan, a --json that is a directory or the same file as an
input archive (for vertex_types also one found under an input folder) is
one error line with exit 2. The report's folder is created before
writing, and a write that still fails is one "cannot write" line (the
summary is already on stdout). Same checks as navmesh_decode's
same_report_path/_make_parent and bvh_index's same_output_path.

Synthetic archives only, no game files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 16:20:17 +05:30

239 lines
9.9 KiB
Python

"""Synthetic-data tests for tools/instance_lists.py.
python -I tests/test_instance_lists.py
Every byte here is built by the test: a minimal 0x7a resource with the four list structs, one prop list with
two instances and one grass list with three. No game file, value or name is used.
"""
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", "instance_lists.py")
sys.path.insert(0, os.path.join(ROOT, "tools"))
import instance_lists as il # noqa: E402
M = il.M
def members(spec):
"""Member table: 16 bytes each (name hash, offset, type byte, padding)."""
return b"".join(struct.pack("<IIBBHI", M[n], off, ty, 0, 0, 0) for n, off, ty in spec)
SCHEMA = {
il.PROP_LIST: (0x170, [("lodLevel", 0x7E, 0x60), ("lodDist", 0x60, 0x21), ("InstanceList", 0x90, 0x52),
("BatchAABB", 0xA0, 0x05), ("ScaleRange", 0x160, 0x30), ("InstanceLodDist", 0x168, 0x21)]),
il.PROP_INST: (0x40, [("InstMat", 0x0, 0x50), ("Tint", 0x34, 0x15)]),
il.GRASS_LIST: (0x1A0, [("lodLevel", 0x7E, 0x60), ("lodDist", 0x60, 0x21), ("BatchAABB", 0x80, 0x05),
("ScaleRange", 0xA0, 0x33), ("LodFadeStartDist", 0xB8, 0x21),
("InstanceLodDist", 0xBC, 0x21), ("OrientToTerrain", 0xC0, 0x21),
("IsSLODList", 0x150, 0x01), ("InstanceList", 0x178, 0x52)]),
il.GRASS_INST: (0x10, [("Position", 0x0, 0x50), ("NormalX", 0x6, 0x11), ("NormalY", 0x7, 0x11),
("Color", 0x8, 0x50), ("Scale", 0xB, 0x11), ("ScaleZ", 0xC, 0x11),
("RotationIdx", 0xD, 0x11)]),
}
def prop_list():
b = bytearray(0x170)
b[0x7E] = 1
struct.pack_into("<f", b, 0x60, 300.0)
struct.pack_into("<4f4f", b, 0xA0, 0, 0, 0, 0, 10, 10, 10, 0)
struct.pack_into("<2f", b, 0x160, 0.8, 1.2)
struct.pack_into("<f", b, 0x168, 150.0)
return b
def prop_instances():
# identity at (1,2,3) and a 90-degree yaw scaled by 2 at (4,5,6)
a = struct.pack("<12f", 1, 0, 0, 1, 0, 1, 0, 2, 0, 0, 1, 3) + b"\0" * 16
c = struct.pack("<12f", 0, -2, 0, 4, 2, 0, 0, 5, 0, 0, 2, 6) + b"\0" * 16
return a + c
def grass_list():
b = bytearray(0x1A0)
b[0x7E] = 5
struct.pack_into("<f", b, 0x60, 120.0)
struct.pack_into("<4f4f", b, 0x80, 0, 0, 0, 0, 5, 5, 1, 0)
struct.pack_into("<2f", b, 0xA0, 0.5, 1.0)
struct.pack_into("<3f", b, 0xB8, 80.0, 60.0, 1.0)
b[0x150] = 1
return b
def grass_instances():
out = b""
for i, (nx, ny, s, sz, r) in enumerate(((128, 128, 200, 200, 3), (255, 128, 100, 90, 71), (0, 0, 0, 0, 72))):
out += struct.pack("<3H", 1000 * i, 2000, 3000) + bytes((nx, ny, 10, 20, 30, s, sz, r)) + b"\0\0"
return out
def resource():
"""Virtual buffer (header, schema, lists) and physical buffer (instances) of one 0x7a resource."""
V = bytearray(0x1000)
V[0x10:0x15] = b"0DRPz"
names = list(SCHEMA)
struct.pack_into("<HHH", V, 0x48, len(names), 0, 4)
st, it, mt = 0x100, 0x200, 0x300
struct.pack_into("<Q", V, 0x28, 0x50000000 | st)
struct.pack_into("<Q", V, 0x38, 0x50000000 | it)
for i, n in enumerate(names):
size, spec = SCHEMA[n]
table = members(spec)
V[mt:mt + len(table)] = table
o = st + 32 * i
struct.pack_into("<I", V, o, n)
struct.pack_into("<Q", V, o + 0x10, 0x50000000 | mt)
struct.pack_into("<I", V, o + 0x18, size)
struct.pack_into("<H", V, o + 0x1E, len(spec))
mt += len(table)
pl, gl = prop_list(), grass_list()
pl_at, gl_at = 0x800, 0xA00
P = bytearray(prop_instances() + grass_instances())
blocks = [(il.PROP_LIST, len(pl), 0x50000000 | pl_at), (il.GRASS_LIST, len(gl), 0x50000000 | gl_at),
(il.PROP_INST, 0x80, 0x60000000), (il.GRASS_INST, 0x30, 0x60000000 | 0x80)]
for i, (n, size, p) in enumerate(blocks):
struct.pack_into("<IIQ", V, it + 16 * i, n, size, p)
# arrays: low 12 bits = 1-based block index, bits 20+ = offset in that block, then a u16 count
struct.pack_into("<QH", pl, 0x90, 3, 2)
struct.pack_into("<QH", gl, 0x178, 4, 3)
V[pl_at:pl_at + len(pl)] = pl
V[gl_at:gl_at + len(gl)] = gl
# block map at +0x08: one virtual and one physical stream
struct.pack_into("<Q", V, 8, 0x50000000 | 0xF00)
V[0xF00 + 8], V[0xF00 + 9] = 1, 1
return bytes(V), bytes(P)
class MeasureTests(unittest.TestCase):
def setUp(self):
self.V, self.P = resource()
self.T = {}
self.assertTrue(il.measure(self.V, self.P, self.T))
def test_schema_matches(self):
for name, ok in self.T["member_offset_matches"].items():
self.assertEqual(ok, 1, name)
self.assertEqual(set(self.T["struct_size_matches"].values()), {1})
def test_prop_matrices(self):
p = self.T["prop"]
self.assertEqual((p["lists"], p["instances"]), (1, 2))
self.assertEqual(p["matrix_orthonormal_axes"], 2)
self.assertEqual(p["matrix_right_handed"], 2)
self.assertEqual(p["matrix_uniform_scale"], 2)
self.assertEqual(p["matrix_unit_scale"], 1)
self.assertEqual(p["translation_in_aabb"], 2)
self.assertEqual(p["lodLevel"], {"1": 1})
def test_grass_fields(self):
g = self.T["grass"]
self.assertEqual((g["lists"], g["instances"]), (1, 3))
self.assertEqual(g["IsSLODList"], {"1": 1})
self.assertEqual(g["OrientToTerrain"], {"1.0": 1})
self.assertEqual(g["normal_xy_inside_unit_disc"], 2) # (0, 0) bytes is the corner (-1, -1)
self.assertEqual(g["scalez_equals_scale"], 2)
self.assertEqual(g["scale_zero"], 1)
self.assertEqual(g["rotation_idx_max"], 72)
self.assertEqual(g["instance_loddist_le_loddist"], 1)
def test_empty_list_is_counted(self):
V = bytearray(self.V)
struct.pack_into("<H", V, 0xA00 + 0x178 + 8, 0)
T = {}
il.measure(bytes(V), self.P, T)
self.assertEqual(T["grass"]["lists_empty"], 1)
def test_not_a_map_resource(self):
self.assertFalse(il.measure(b"\0" * 0x100, b"", {}))
class ArchiveTests(unittest.TestCase):
def test_end_to_end(self):
V, P = resource()
with tempfile.TemporaryDirectory() as tmp:
path = os.path.join(tmp, "fake.rpf")
first = zlib.compress(V)
first += b"\0" * (-len(first) % 16)
with open(path, "wb") as fh:
fh.write(b"RPF9" + struct.pack("<I", 16) + b"\0" * 8 + first + zlib.compress(P))
out = os.path.join(tmp, "out.json")
r = subprocess.run([sys.executable, "-I", TOOL, path, "--json", out], capture_output=True, text=True)
self.assertEqual(r.returncode, 0, r.stderr)
with open(out, encoding="utf-8") as fh:
T = json.load(fh)
self.assertEqual(T["resources_with_lists"], 1)
self.assertEqual(T["prop"]["instances"], 2)
self.assertEqual(T["grass"]["instances"], 3)
def test_bad_limit(self):
r = subprocess.run([sys.executable, "-I", TOOL, TOOL, "--limit", "0"], capture_output=True, text=True)
self.assertEqual(r.returncode, 2)
@staticmethod
def _archive(tmp):
V, P = resource()
path = os.path.join(tmp, "fake.rpf")
first = zlib.compress(V)
first += b"\0" * (-len(first) % 16)
data = b"RPF9" + struct.pack("<I", 16) + b"\0" * 8 + first + zlib.compress(P)
with open(path, "wb") as fh:
fh.write(data)
return path, data
def test_json_over_an_input_archive_is_refused(self):
with tempfile.TemporaryDirectory() as tmp:
path, data = self._archive(tmp)
other = os.path.join(tmp, ".", "fake.rpf")
r = subprocess.run([sys.executable, "-I", TOOL, path, "--json", other], capture_output=True, text=True)
self.assertEqual(r.returncode, 2, r.stderr)
self.assertIn("would overwrite the input", r.stderr)
self.assertNotIn("Traceback", r.stderr)
self.assertEqual(r.stdout, "")
with open(path, "rb") as fh:
self.assertEqual(fh.read(), data)
def test_json_that_is_a_directory_is_refused_before_the_scan(self):
with tempfile.TemporaryDirectory() as tmp:
path, _ = self._archive(tmp)
r = subprocess.run([sys.executable, "-I", TOOL, path, "--json", tmp], capture_output=True, text=True)
self.assertEqual(r.returncode, 2, r.stderr)
self.assertIn("is a directory", r.stderr)
self.assertNotIn("Traceback", r.stderr)
self.assertEqual(r.stdout, "")
def test_json_in_a_missing_folder_is_created(self):
with tempfile.TemporaryDirectory() as tmp:
path, _ = self._archive(tmp)
out = os.path.join(tmp, "reports", "lists", "out.json")
r = subprocess.run([sys.executable, "-I", TOOL, path, "--json", out], capture_output=True, text=True)
self.assertEqual(r.returncode, 0, r.stderr)
with open(out, encoding="utf-8") as fh:
self.assertEqual(json.load(fh)["prop"]["instances"], 2)
def test_json_that_cannot_be_written_is_one_error_line(self):
with tempfile.TemporaryDirectory() as tmp:
path, _ = self._archive(tmp)
blocker = os.path.join(tmp, "blocker")
with open(blocker, "w", encoding="utf-8") as fh:
fh.write("a file, not a folder")
out = os.path.join(blocker, "out.json")
r = subprocess.run([sys.executable, "-I", TOOL, path, "--json", out], capture_output=True, text=True)
self.assertEqual(r.returncode, 2, r.stderr)
self.assertIn("cannot write", r.stderr)
self.assertNotIn("Traceback", r.stderr)
self.assertIn('"resources_with_lists"', r.stdout)
if __name__ == "__main__":
unittest.main()