import json from pathlib import Path import struct import sys import tempfile import unittest import zlib ROOT = Path(__file__).resolve().parents[1] ORIGINAL = ROOT.parent / 'Original' sys.path.insert(0, str(ROOT / 'tools' / 'extractors')) from omt import Database, FormatError, Reader, export_scene, read_canvas, read_material, read_shape, unpack_row, validate_output, write_png from ase import read_ase, export_ase from png import read_png class BoundedReaders(unittest.TestCase): def test_read_bounds_and_negative_size(self): for size in (-1, 2): with self.assertRaises(FormatError): Reader(b'x').take(size) def test_serialized_bool_is_two_bytes(self): r = Reader(b'\x00\x01\x00\x00') self.assertTrue(r.boolean()) self.assertFalse(r.boolean()) r.end() def test_packbits_bytes_and_pixel_words(self): self.assertEqual(unpack_row(b'\x02abc\xfeZ', 6, 1), b'abcZZZ') self.assertEqual(unpack_row(b'\x01\x00\x7c\x00\x03\xe0\xfe\xff\x00\x1f', 10, 2), b'\x7c\x00\x03\xe0\x00\x1f\x00\x1f\x00\x1f') def test_packbits_rejects_overrun_truncation_and_leftovers(self): for encoded, expected, unit in ((b'\x7fX', 1, 1), (b'\xff', 2, 1), (b'\x00XY', 1, 1)): with self.assertRaises(FormatError): unpack_row(encoded, expected, unit) def test_canvas_rgb555_matches_original_shift_and_key(self): # Original pixel converter uses channel<<3, yielding 248 at full intensity. row = b'\x02\x00\x7c\x00\x03\xe0\x00\x1f' pixel_data = struct.pack('>H', len(row)) + row header = b'OmCv'+struct.pack('>I', 0)+b'OmGW'+struct.pack('>HHHBBH', 3, 1, 16, 1, 1, 6) header += struct.pack('>HIHI', 1, 0x03e0, 0, len(pixel_data)) canvas = read_canvas(header+pixel_data) self.assertEqual(canvas['rgba'], bytes((248,0,0,255,0,248,0,0,0,0,248,255))) def test_png_roundtrip_payload(self): rgba = bytes((4,5,6,255,10,20,30,0)) with tempfile.TemporaryDirectory() as t: path = Path(t)/'test.png' write_png(path, 2, 1, rgba) data = path.read_bytes() self.assertEqual(data[:8], b'\x89PNG\r\n\x1a\n') p, compressed = 8, bytearray() while p < len(data): size = struct.unpack_from('>I', data, p)[0] tag, payload = data[p+4:p+8], data[p+8:p+8+size] self.assertEqual(zlib.crc32(tag+payload), struct.unpack_from('>I',data,p+8+size)[0]) if tag == b'IDAT': compressed.extend(payload) p += size+12 self.assertEqual(zlib.decompress(compressed), b'\x00'+rgba) self.assertEqual(read_png(path)['rgba'],rgba) @unittest.skipUnless(ORIGINAL.exists(), 'Original game files are local integration fixtures') class OriginalResources(unittest.TestCase): def test_every_database_index_is_bounded(self): paths = list(ORIGINAL.rglob('*.omt')) self.assertEqual(len(paths), 100) self.assertEqual(sum(len(Database(p).entries) for p in paths), 7027) def test_first_scene_geometry_materials_and_textures(self): db = Database(ORIGINAL/'omt'/'level1b.omt') counts = {} for entry in db.entries: if entry.type == '3DSh': read_shape(db.payload(entry)) elif entry.type == '3DMa': read_material(db.payload(entry)) elif entry.type == 'Canv': canvas = read_canvas(db.payload(entry)) self.assertEqual(len(canvas['rgba']), canvas['width']*canvas['height']*4) counts[entry.type] = counts.get(entry.type, 0)+1 self.assertEqual(counts['Canv'], 33) def test_scene_export_contains_original_geometry_and_texture_links(self): db = Database(ORIGINAL/'omt'/'VR01.omt') with tempfile.TemporaryDirectory() as t: output = Path(t)/'scene' scene = export_scene(db, output) self.assertEqual(len(scene['indices']), 240) self.assertEqual(len(scene['vertices']), 240) self.assertEqual(len(scene['textures']), 7) self.assertTrue(all(0 <= i < len(scene['vertices']) for i in scene['indices'])) self.assertEqual([list(v) for v in scene['vertices']], json.loads((output/'scene.json').read_text())['vertices']) for texture in scene['textures']: self.assertEqual((output/texture['path']).stat().st_size, texture['width']*texture['height']*4) def test_extraction_cannot_target_original_directory(self): db = Database(ORIGINAL/'omt'/'VR01.omt') for target in (ORIGINAL, ORIGINAL/'omt', ORIGINAL/'omt'/'converted'): with self.assertRaises(ValueError): validate_output(db, target) def test_indexed_texture_recovery(self): db = Database(ORIGINAL/'omt'/'sprites.omt') entry = next(e for e in db.entries if e.type=='Canv' and e.name=='shadow2') canvas = read_canvas(db.payload(entry)) self.assertEqual((canvas['width'],canvas['height'],canvas['depth']), (64,32,8)) self.assertIn(0, canvas['rgba'][3::4]) def test_ase_coordinate_uv_and_winding_match_original_omt(self): db = Database(ORIGINAL/'omt'/'objects.omt') for name in ('gate1','yokianpart'): entry = next(e for e in db.entries if e.type=='3DSh' and e.name==name) omt = read_shape(db.payload(entry)) ase = read_ase(ORIGINAL/'ASE'/(name+'.ASE'))['objects'][0]['samples'][0] self.assertEqual(len(omt['vertices']),len(ase['vertices'])) self.assertEqual(len(omt['polygons']),len(ase['faces'])) for original,recovered in zip(omt['vertices'],ase['vertices']): for a,b in zip(original,recovered): self.assertAlmostEqual(a,b,places=3) for polygon,face,tf in zip(omt['polygons'],ase['faces'],ase['textureFaces']): self.assertEqual(tuple(c['vertex'] for c in polygon['corners']),face) for corner,uv_index in zip(polygon['corners'],tf): for a,b in zip(corner['uv'],ase['uvs'][uv_index]): self.assertAlmostEqual(a,b,places=6) def test_jimmy_morph_animation_samples_and_texture(self): for name,count in (('jimstop.ase',2),('jimrun.ASE',5),('jimjump.ASE',3)): animation=read_ase(ORIGINAL/'ASE'/name) self.assertEqual((animation['framesPerSecond'],animation['ticksPerFrame']),(10,480)) self.assertEqual(len(animation['objects'][0]['samples']),count) self.assertTrue(all(len(m['faces'])==814 for m in animation['objects'][0]['samples'])) texture=read_png(ORIGINAL/'png'/'jimycarl.png') self.assertEqual((texture['width'],texture['height'],len(texture['rgba'])),(256,256,256*256*4)) def test_morph_reuses_textured_base_topology(self): with tempfile.TemporaryDirectory() as t: output=Path(t) with self.assertRaises(FormatError): export_ase(ORIGINAL/'ASE'/'jimrun.ASE',output/'invalid') export_ase(ORIGINAL/'ASE'/'jimrun.ASE',output/'run',ORIGINAL/'ASE'/'jimstop.ase') export_ase(ORIGINAL/'ASE'/'jimstop.ase',output/'base') frame=json.loads((output/'run'/'frame_0000.json').read_text()) base=json.loads((output/'base'/'frame_0000.json').read_text()) self.assertEqual(frame['uvs'],base['uvs']) self.assertTrue(any(uv != [0,0] for uv in frame['uvs'])) self.assertNotEqual(frame['vertices'],base['vertices']) if __name__ == '__main__': unittest.main()