"""Synthetic-data tests for tools/meshlet_page.py. python -I tests/test_meshlet_page.py Every byte here is built by the test. No game file, value or name is used. """ import json import math import os import struct import subprocess import sys import tempfile import threading import unittest import zlib ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TOOL = os.path.join(ROOT, "tools", "meshlet_page.py") sys.path.insert(0, os.path.join(ROOT, "tools")) import meshlet_page as mp # noqa: E402 def pad16(buf): buf += b"\0" * (-len(buf) % 16) return buf def block(n, colour=True, uv1=True, normal=True, flags=0x200): """A vertex block with n vertices: slots 0 (pos), 1 (rgba), 2 (normal), 3 (uv0), 4 (uv1). The streams are stored in slot order 0, 2, 1, 3, 4, as measured. """ order = [(0, 0x63, 12), (2, 0x63, 12), (1, 0x26, 4), (3, 0x42, 8), (4, 0x42, 8)] if not normal: order = [o for o in order if o[0] != 2] if not colour: order = [o for o in order if o[0] != 1] if not uv1: order = [o for o in order if o[0] != 4] head = bytearray(0x60) off = 0x60 body = bytearray() for slot, tag, stride in order: struct.pack_into("> 8) << 32) | (c << 48) def group(sizes): """A meshlet group. sizes is a list of (vertices, triangles) per meshlet (each <= 128).""" recs = bytearray() tris = bytearray() refs = bytearray() vbase = tbase = 0 for m, (vc, tc) in enumerate(sizes): recs += struct.pack("<4I", (vc - 1) | ((tc - 1) << 7), vbase | (tbase << 16), 0x7FF7FF, 0x7FF) for t in range(tc): a, b, c = t % vc, (t + 1) % vc, (t + 2) % vc tris += struct.pack(" odd u16 reference count, so the next chunk needs the 16-byte realignment data = page(block(3, colour=False, uv1=False), group([(3, 1)]), block(5, colour=False, uv1=False)) chunks = mp.parse_page(data) self.assertEqual([c["kind"] for c in chunks], ["block", "group", "block"]) self.assertEqual([s[0] for s in chunks[0]["slots"]], [0, 2, 3]) def test_block_without_normals(self): # slots 0, 3, 4 only: the vertex count comes from slot 0's span up to the next stream (slot 3) data = page(block(7, colour=False, normal=False), group([(7, 5)])) chunks = mp.parse_page(data) self.assertEqual([c["kind"] for c in chunks], ["block", "group"]) self.assertEqual(chunks[0]["vertices"], 7) self.assertEqual([s[0] for s in chunks[0]["slots"]], [0, 3, 4]) self.assertTrue(chunks[0]["tags_expected"]) summary = mp.summarise_page(data, "x") self.assertNotIn("error", summary) c = summary["checks"] self.assertEqual((c["vertices"], c["positions_finite"], c["normals_unit"]), (7, 7, 0)) self.assertEqual((c["uv_vertices"], c["uv_finite"]), (14, 14)) def test_group_led_page(self): data = page(group([(10, 8)]), block(10)) self.assertTrue(mp.is_mesh_page(data)) summary = mp.summarise_page(data, "x") self.assertEqual(summary["starts_with"], "group") self.assertEqual(summary["sequence"], "GB") self.assertEqual(summary["groups_matching_block_here"], 1) def test_stop_at_other_block(self): other = struct.pack(" 0 else (down, up) rec = grp["offset"] + 12 w2, w3 = struct.unpack_from("<2I", data, rec + 8) # f = 31 is a zero half-angle: the 6-bit axis is not exactly the face normal, so only the 0.05 allowance holds for cone, want in ((facing | 31 << 26, (1, 1, 0, 1)), (away | 31 << 26, (1, 0, 0, 0)), (facing | 16 << 26, (1, 1, 1, 1)), (0, (0, 0, 0, 0))): struct.pack_into(" None: with tempfile.TemporaryDirectory() as tmp: block_path = os.path.join(tmp, "block.bin") bad_path = os.path.join(tmp, "malformed.bin") group_path = os.path.join(tmp, "group.bin") out = os.path.join(tmp, "report.json") for path, data in ((block_path, page(block(4))), (bad_path, b"bad"), (group_path, page(group([(4, 2)])))): with open(path, "wb") as fh: fh.write(data) for paths, exit_code, matches in ( ((block_path, group_path), 0, 1), ((group_path, block_path), 0, 1), ((block_path, bad_path, group_path), 1, 0), ): with self.subTest(paths=paths): result = self.run_tool(*paths, "--json", out, "--quiet") self.assertEqual(result.returncode, exit_code, result.stderr) with open(out, encoding="utf-8") as fh: totals = json.load(fh)["totals"] self.assertEqual(totals["groups_matching_block_near"], matches) def test_missing_input(self): r = self.run_tool(os.path.join(tempfile.gettempdir(), "no-such-meshlet-page.bin")) self.assertEqual(r.returncode, 2) def test_error_exit(self): with tempfile.TemporaryDirectory() as tmp: path = os.path.join(tmp, "bad.bin") with open(path, "wb") as fh: fh.write(page(block(4)) + struct.pack("/index.json is the carve manifest's name even before a carve has written it. with tempfile.TemporaryDirectory() as tmp: carve = os.path.join(tmp, "carve") os.mkdir(carve) with open(os.path.join(carve, "0000.bin"), "wb") as fh: fh.write(page(block(4))) manifest = os.path.join(carve, "index.json") r = self.run_tool(carve, "--json", manifest, "--quiet") self.assertEqual(r.returncode, 2, r.stderr) self.assertIn("would overwrite the input", r.stderr) self.assertNotIn("Traceback", r.stderr) self.assertFalse(os.path.exists(manifest)) def test_json_over_carve_dir_manifest_or_payload_is_refused(self): with tempfile.TemporaryDirectory() as tmp: carve = os.path.join(tmp, "carve") os.mkdir(carve) manifest = os.path.join(carve, "index.json") with open(manifest, "w", encoding="utf-8") as fh: fh.write('{"files": []}') bin_path = os.path.join(carve, "0000.bin") bin_data = page(block(4)) with open(bin_path, "wb") as fh: fh.write(bin_data) # Refuse overwrite of index.json r = self.run_tool(carve, "--json", manifest, "--quiet") self.assertEqual(r.returncode, 2, r.stderr) self.assertIn("would overwrite the input", r.stderr) self.assertNotIn("Traceback", r.stderr) with open(manifest, "r", encoding="utf-8") as fh: self.assertEqual(fh.read(), '{"files": []}') # Refuse overwrite of carved .bin payload r = self.run_tool(carve, "--json", bin_path, "--quiet") self.assertEqual(r.returncode, 2, r.stderr) self.assertIn("would overwrite the input", r.stderr) self.assertNotIn("Traceback", r.stderr) with open(bin_path, "rb") as fh: self.assertEqual(fh.read(), bin_data) def test_json_hard_link_to_a_page_in_a_scanned_dir_is_refused(self): with tempfile.TemporaryDirectory() as tmp: carve = os.path.join(tmp, "carve") os.mkdir(carve) source = os.path.join(carve, "page.bin") data = page(block(4)) with open(source, "wb") as fh: fh.write(data) report = os.path.join(tmp, "report.json") try: os.link(source, report) except (AttributeError, NotImplementedError, OSError) as exc: self.skipTest(f"hard links unavailable: {exc}") r = self.run_tool(carve, "--json", report, "--quiet") self.assertEqual(r.returncode, 2, r.stderr) self.assertIn("would overwrite the input", r.stderr) self.assertNotIn("Traceback", r.stderr) with open(source, "rb") as fh: self.assertEqual(fh.read(), data) def test_json_inside_carve_dir_can_be_written_and_replaced(self): with tempfile.TemporaryDirectory() as tmp: carve = os.path.join(tmp, "carve") os.mkdir(carve) bin_path = os.path.join(carve, "0000.bin") with open(bin_path, "wb") as fh: fh.write(page(block(4))) out = os.path.join(carve, "report.json") for run in ("new report", "existing report"): with self.subTest(run=run): r = self.run_tool(carve, "--json", out, "--quiet") self.assertEqual(r.returncode, 0, r.stderr) with open(out, "r", encoding="utf-8") as fh: self.assertEqual(json.load(fh)["totals"]["pages"], 1) def test_json_over_draw_system_inputs_is_refused(self): with tempfile.TemporaryDirectory() as tmp: sys_path = os.path.join(tmp, "system.bin") gfx_path = os.path.join(tmp, "graphics.bin") with open(sys_path, "wb") as fh: fh.write(b"\0" * 256) with open(gfx_path, "wb") as fh: fh.write(b"\0" * 256) # Refuse overwrite of graphics segment r = self.run_tool(gfx_path, "--draw-system", sys_path, "--geometry-offset", "0", "--json", gfx_path, "--quiet") self.assertEqual(r.returncode, 2, r.stderr) self.assertIn("would overwrite the input", r.stderr) self.assertNotIn("Traceback", r.stderr) # Refuse overwrite of system segment r = self.run_tool(gfx_path, "--draw-system", sys_path, "--geometry-offset", "0", "--json", sys_path, "--quiet") self.assertEqual(r.returncode, 2, r.stderr) self.assertIn("would overwrite the input", r.stderr) self.assertNotIn("Traceback", r.stderr) if __name__ == "__main__": unittest.main()