Record original inventory and recover all GAM level entities

This commit is contained in:
UnixNight committed 2026-10-23 17:08:18 -04:00
commit d62e2ab4de
16 files changed
+3954

No files matched your search

+67
View File
@@ -0,0 +1,67 @@
import json
from pathlib import Path
import struct
import sys
import unittest
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tools/levels"))
import gam
def fixture(properties=b"", count=0):
return b"TST1" + struct.pack(">I", 1) + b"3JIM" + struct.pack(">I", count) + properties + b"\x12\x34\x56\x78"
class GamTests(unittest.TestCase):
def test_real_levels_roundtrip_and_counts(self):
paths = sorted((ROOT.parent / "Original/gam").glob("*.gam"))
self.assertEqual(len(paths), 35, "original corpus is required for this integration test")
total = 0
for path in paths:
with self.subTest(path=path.name):
raw = path.read_bytes()
level = gam.parse(raw)
self.assertEqual(gam.encode(level), raw)
total += len(level.entities)
self.assertEqual(total, 3299)
def test_exact_original_start(self):
level = gam.parse((ROOT.parent / "Original/gam/level1b.gam").read_bytes())
self.assertEqual(level.code, "LV1B")
player = level.entities[0].values()
self.assertEqual(player["StartPoint"], "STARTEXP")
self.assertEqual(player["PositionX"], 5071.93310546875)
start = next(e.values() for e in level.entities
if e.kind == "STRT" and e.values()["ObjectTag"] == "STARTEXP")
self.assertEqual(start["StartTrigger"], "LABEXP")
self.assertEqual(start["MusicDatabase"], "musicsecretlab.omt")
def test_endianness_and_string_prefix(self):
props = b"\x01x" + struct.pack(">II f", 3, 4, -12.5)
props += b"\x01s" + struct.pack(">II", 1, 3) + b"\x03abc"
raw = fixture(props, 2)
level = gam.parse(raw)
self.assertEqual(level.entities[0].values(), {"x": -12.5, "s": "abc"})
self.assertEqual(gam.encode(level), raw)
def test_unknown_type_preserved(self):
raw = fixture(b"\x01x" + struct.pack(">II", 999, 3) + b"abc", 1)
self.assertEqual(gam.encode(gam.parse(raw)), raw)
def test_all_truncations_rejected(self):
raw = fixture(b"\x01x" + struct.pack(">II f", 3, 4, 12.5), 1)
for end in range(len(raw)):
with self.subTest(end=end), self.assertRaises(gam.FormatError):
gam.parse(raw[:end])
def test_overflows_trailing_data_and_bad_strings(self):
for data in [b"TST1\xff\xff\xff\xff", fixture() + b"x",
fixture(b"\x01x" + struct.pack(">II", 1, 2) + b"\x03abc", 1),
fixture(b"\x01x" + struct.pack(">II", 3, 3) + b"abc", 1)]:
with self.subTest(data=data), self.assertRaises(gam.FormatError):
gam.parse(data)
if __name__ == "__main__":
unittest.main()