883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
143 lines
6.9 KiB
Python
143 lines
6.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Focused ModelBlob geometry, preservation, and conflicting-identity checks."""
|
|
import math
|
|
from pathlib import Path
|
|
import struct
|
|
import sys
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'ops'))
|
|
from model_blob import collision_vertex_aabb, extend_model_blob, normalize_reversed_collision_bounds
|
|
|
|
|
|
def chunk(tag, body):
|
|
return tag + struct.pack('<I', len(body)) + body
|
|
|
|
|
|
def blob(*rows):
|
|
return (chunk(b'RVBM', bytes(4)) + chunk(b'KEEP', b'opaque native prefix') +
|
|
chunk(b'BBBM', b''.join(struct.pack('<I6f', *row) for row in rows)) +
|
|
chunk(b'KEEP', b'opaque native suffix'))
|
|
|
|
|
|
def model(vertices=((-2., -1., 3.), (4., 5., -6.), (1., 2., 0.)), wrapped=True, collision=True):
|
|
body = bytearray(304)
|
|
body[:8] = b'MD20' + struct.pack('<I', 272)
|
|
# A deliberately reversed header cannot be used as the generated row.
|
|
struct.pack_into('<6f', body, 0xbc, 100., 100., 100., -100., -100., -100.)
|
|
if collision:
|
|
struct.pack_into('<II', body, 0xd8, 3, len(body))
|
|
body += struct.pack('<3H', 0, 1, 2)
|
|
struct.pack_into('<II', body, 0xe0, len(vertices), len(body))
|
|
body += b''.join(struct.pack('<3f', *vertex) for vertex in vertices)
|
|
struct.pack_into('<II', body, 0xe8, 1, len(body))
|
|
body += struct.pack('<3f', 0., 0., 1.)
|
|
return chunk(b'MD21', bytes(body)) + chunk(b'TXID', b'opaque') if wrapped else bytes(body)
|
|
|
|
|
|
class ModelBlobTest(unittest.TestCase):
|
|
def test_vertex_bounds_and_native_chunks_survive_sorted_addition(self):
|
|
row = (100, -1., -2., -3., 1., 2., 3.)
|
|
original = blob(row)
|
|
payload = model()
|
|
self.assertEqual(collision_vertex_aabb(payload), (-2., -1., -6., 4., 5., 3.))
|
|
out, audit = extend_model_blob(original, [(300, payload), (200, payload), (300, payload)])
|
|
expected = blob(row, (200, -2., -1., -6., 4., 5., 3.), (300, -2., -1., -6., 4., 5., 3.))
|
|
self.assertEqual(out, expected)
|
|
self.assertEqual(audit['added_ids'], [200, 300])
|
|
self.assertEqual(audit['identical_duplicate_inputs'], 1)
|
|
self.assertEqual(extend_model_blob(out, {200: payload, 300: payload})[0], out)
|
|
|
|
def test_no_collision_has_no_row_even_with_header_sentinel(self):
|
|
original = blob((100, 0., 0., 0., 1., 1., 1.))
|
|
empty = model(wrapped=False, collision=False)
|
|
self.assertIsNone(collision_vertex_aabb(empty))
|
|
out, audit = extend_model_blob(original, {200: empty})
|
|
self.assertEqual(out, original)
|
|
self.assertEqual(audit['no_collision_ids'], [200])
|
|
|
|
def test_conflicting_duplicate_payloads_are_not_hidden_by_equal_bounds(self):
|
|
first = model()
|
|
second = first + chunk(b'EXTR', b'different payload identity')
|
|
with self.assertRaisesRegex(ValueError, 'Conflicting payloads'):
|
|
extend_model_blob(blob(), [(200, first), (200, second)])
|
|
with self.assertRaisesRegex(ValueError, 'conflicts with model geometry'):
|
|
extend_model_blob(blob((200, 0., 0., 0., 1., 1., 1.)), {200: first})
|
|
with self.assertRaisesRegex(ValueError, 'no model collision indices'):
|
|
extend_model_blob(blob((200, 0., 0., 0., 1., 1., 1.)), {200: model(collision=False)})
|
|
|
|
def test_rejects_broken_collision_arrays_and_nonfinite_vertices(self):
|
|
original = model(wrapped=False)
|
|
bad = []
|
|
for offset, value in ((0xd8, 4), (0xdc, 999999), (0xe4, 4), (0xec, 999999)):
|
|
item = bytearray(original)
|
|
struct.pack_into('<I', item, offset, value)
|
|
bad.append(bytes(item))
|
|
item = bytearray(original)
|
|
struct.pack_into('<H', item, 304, 9)
|
|
bad.append(bytes(item))
|
|
bad.append(model(vertices=((math.nan, 0., 0.), (1., 0., 0.), (0., 1., 0.))))
|
|
bad.append(chunk(b'MD21', original)[:-1])
|
|
for payload in bad:
|
|
with self.subTest(payload=payload[:12]):
|
|
with self.assertRaises(ValueError):
|
|
collision_vertex_aabb(payload)
|
|
|
|
def test_rejects_invalid_modelblob_table_and_key_sentinels(self):
|
|
bounds = (0., 0., 0., 1., 1., 1.)
|
|
bad = [blob((2, *bounds), (1, *bounds)), blob((1, *bounds), (1, *bounds)),
|
|
blob((1, math.inf, 0., 0., 1., 1., 1.)),
|
|
blob((1, 2., 0., 0., 1., 1., 1.)), blob()[:-1],
|
|
chunk(b'RVBM', struct.pack('<I', 1)) + chunk(b'BBBM', b'')]
|
|
for raw in bad:
|
|
with self.subTest(raw=raw[:20]):
|
|
with self.assertRaises(ValueError):
|
|
extend_model_blob(raw, {})
|
|
for key in (0, 0xffffffff, -1, True):
|
|
with self.subTest(key=key):
|
|
with self.assertRaises(ValueError):
|
|
extend_model_blob(blob(), {key: model()})
|
|
|
|
def test_normalizes_only_exact_reversed_header_and_preserves_geometry(self):
|
|
raw = model()
|
|
expected = collision_vertex_aabb(raw)
|
|
reversed_raw = bytearray(raw)
|
|
struct.pack_into('<6f', reversed_raw, 8 + 0xbc, *(expected[3:] + expected[:3]))
|
|
reversed_raw = bytes(reversed_raw)
|
|
out, audit = normalize_reversed_collision_bounds(reversed_raw)
|
|
self.assertTrue(audit['changed'])
|
|
self.assertEqual(struct.unpack_from('<6f', out, 8 + 0xbc), expected)
|
|
self.assertEqual(out[:8 + 0xbc], reversed_raw[:8 + 0xbc])
|
|
self.assertEqual(out[8 + 0xd4:], reversed_raw[8 + 0xd4:])
|
|
self.assertEqual(collision_vertex_aabb(out), expected)
|
|
self.assertEqual(normalize_reversed_collision_bounds(out)[0], out)
|
|
# Preserve the existing converter API and its post-conversion hook.
|
|
from m2_equipment_convert import canonicalize_geometry_reversed_collision_bounds
|
|
self.assertEqual(canonicalize_geometry_reversed_collision_bounds(reversed_raw), (out, audit))
|
|
|
|
def test_ordered_native_exception_and_empty_sentinel_remain_exact(self):
|
|
raw = bytearray(model(wrapped=False))
|
|
struct.pack_into('<6f', raw, 0xbc, -.5, -.5, -.5, .5, .5, .5)
|
|
raw = bytes(raw)
|
|
# A stale or intentionally narrower native header is still authoritative.
|
|
self.assertNotEqual(struct.unpack_from('<6f', raw, 0xbc), collision_vertex_aabb(raw))
|
|
output, audit = normalize_reversed_collision_bounds(raw)
|
|
self.assertEqual(output, raw)
|
|
self.assertFalse(audit['changed'])
|
|
empty = model(collision=False)
|
|
self.assertEqual(normalize_reversed_collision_bounds(empty)[0], empty)
|
|
|
|
def test_unrecognized_reversal_and_broken_geometry_are_refused(self):
|
|
with self.assertRaisesRegex(ValueError, 'not exact vertex extrema'):
|
|
normalize_reversed_collision_bounds(model())
|
|
actual = collision_vertex_aabb(model())
|
|
raw = bytearray(model())
|
|
struct.pack_into('<6f', raw, 8 + 0xbc, *(actual[3:] + actual[:3]))
|
|
struct.pack_into('<H', raw, 8 + 304, 10)
|
|
with self.assertRaisesRegex(ValueError, 'outside vertex array'):
|
|
normalize_reversed_collision_bounds(bytes(raw))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|