"""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 math import os import random import struct import subprocess import sys import tempfile import time 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(" tuple[bytes, bytes]: """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(" None: virtual, physical = resource(rotation_first=True) summary: dict = {} self.assertTrue(il.measure(virtual, physical, summary)) self.assertEqual(summary["grass"]["rotation_idx_max"], 72) self.assertEqual(summary["grass"], self.T["grass"]) def test_empty_list_is_counted(self): V = bytearray(self.V) struct.pack_into("= 48, but < 0x20 + 48 (80), so InstMat at +0x20 is truncated inst = b"\0" * 56 blocks = [(il.PROP_INST, len(inst), 0x60000000)] R = {} il.prop_list(bytes(pl), inst, blocks, structs, bytes(pl), 0, R) self.assertEqual(R.get("instances_truncated"), 1) 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("", r.stderr) self.assertNotIn("Traceback", r.stderr) self.assertEqual(r.stdout, "") def _oracle_grass(ib, io, n, isz, nx, ny, sc, sz, rot): """The per-instance reading of a grass list, one record at a time.""" need = max(isz, nx + 1, ny + 1, sc + 1, sz + 1, rot + 1) R = {"instances": n} rmax = 0 for i in range(n): o = io + i * isz if min(nx, ny, sc, sz, rot) < 0 or o + need > len(ib): R["instances_truncated"] = R.get("instances_truncated", 0) + 1 break x, y = ib[o + nx] / 127.5 - 1, ib[o + ny] / 127.5 - 1 for k, v in (("normal_xy_inside_unit_disc", x * x + y * y <= 1.0 + 2 / 127.5), ("scale_zero", ib[o + sc] == 0), ("scalez_equals_scale", ib[o + sz] == ib[o + sc])): R[k] = R.get(k, 0) + int(v) rmax = max(rmax, ib[o + rot]) R["rotation_idx_max"] = rmax return R def _oracle_prop(ib, io, n, isz, mat, box): R = {"instances": n} for i in range(n): o = io + i * isz if mat < 0 or o + max(48, mat + 48) > len(ib): R["instances_truncated"] = R.get("instances_truncated", 0) + 1 break m = struct.unpack_from("<12f", ib, o + mat) cols = [(m[0], m[4], m[8]), (m[1], m[5], m[9]), (m[2], m[6], m[10])] t = (m[3], m[7], m[11]) lens = [math.sqrt(sum(c * c for c in col)) for col in cols] if min(lens) <= 1e-6 or not all(math.isfinite(x) for x in m): R["matrix_degenerate"] = R.get("matrix_degenerate", 0) + 1 continue u = [[c / l for c in col] for col, l in zip(cols, lens)] ortho = max(abs(sum(u[a][k] * u[b][k] for k in range(3))) for a, b in ((0, 1), (0, 2), (1, 2))) det = (u[0][0] * (u[1][1] * u[2][2] - u[1][2] * u[2][1]) - u[0][1] * (u[1][0] * u[2][2] - u[1][2] * u[2][0]) + u[0][2] * (u[1][0] * u[2][1] - u[1][1] * u[2][0])) checks = [("matrix_orthonormal_axes", ortho < 0.01), ("matrix_right_handed", det > 0), ("matrix_uniform_scale", max(lens) - min(lens) < 0.01 * max(lens)), ("matrix_unit_scale", all(abs(x - 1) < 0.01 for x in lens))] if box: checks.append(("translation_in_aabb", all(box[0][k] - 0.01 <= t[k] <= box[1][k] + 0.01 for k in range(3)))) for k, v in checks: R[k] = R.get(k, 0) + int(v) return R def _random_matrices(rng, count, stride, at): out = bytearray(count * stride) for i in range(count): kind = rng.random() if kind < 0.05: m = [0.0] * 12 elif kind < 0.08: m = [float("nan")] + [1.0] * 11 elif kind < 0.5: m = [rng.uniform(-3, 3) for _ in range(12)] else: th, s = rng.uniform(0, 6.28), rng.choice((1.0, 1.0, 2.0, 0.5)) c, sn = math.cos(th) * s, math.sin(th) * s m = [c, -sn, 0, rng.uniform(-6, 16), sn, c, 0, rng.uniform(-6, 16), 0, 0, s * rng.choice((1, 1, -1)), rng.uniform(-6, 16)] struct.pack_into("<12f", out, i * stride + at, *m) return bytes(out) class ManyInstancesTests(unittest.TestCase): """Lists of tens of thousands of instances: same counts as the one-record-at-a-time reading, much faster.""" GRASS = {il.GRASS_LIST: (0x1A0, {M["InstanceList"]: (0x178, 0x52)}), il.GRASS_INST: (0x10, {M["NormalX"]: (6, 0x11), M["NormalY"]: (7, 0x11), M["Scale"]: (0xB, 0x11), M["ScaleZ"]: (0xC, 0x11), M["RotationIdx"]: (0xD, 0x11)})} def _grass(self, data, n, io=0): gl = bytearray(0x1A0) struct.pack_into("