85 lines
4.9 KiB
Python
85 lines
4.9 KiB
Python
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
ORIGINAL = ROOT.parent / 'Original'
|
|
sys.path.insert(0, str(ROOT / 'tools' / 'extractors'))
|
|
from entities import prepare_entities, resolve_asset, resource_pairs
|
|
from omt import FormatError
|
|
|
|
|
|
class EntityPreparation(unittest.TestCase):
|
|
def test_explicit_resource_pairs_preserve_numbered_audio_and_ignore_defaults(self):
|
|
pairs = list(resource_pairs({'SpriteDatabase': 'sprites.omt', 'SpriteIndex': 135,
|
|
'SoundDatabase': 'voicepractice.omt', 'SoundIndex0': 24,
|
|
'SoundIndex1': -1, 'SoundIndex2': 22, 'MusicDatabase': 'none', 'MusicIndex': 0}))
|
|
self.assertEqual(pairs, [('SpriteIndex', 'sprites.omt', 'Canv', 135),
|
|
('SoundIndex0', 'voicepractice.omt', 'Wave', 24),
|
|
('SoundIndex2', 'voicepractice.omt', 'Wave', 22)])
|
|
self.assertEqual(list(resource_pairs({'SoundIndex': 3})), [])
|
|
|
|
def test_case_lookup_and_traversal_rejection(self):
|
|
self.assertEqual(resolve_asset(ORIGINAL, 'GaM/vr01.GAM').name, 'VR01.gam')
|
|
for name in ('../readme.txt', '/gam/VR01.gam', 'gam//VR01.gam', 'gam/./VR01.gam'):
|
|
with self.assertRaises(FormatError): resolve_asset(ORIGINAL, name)
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
root = Path(temporary)
|
|
(root / 'File').touch()
|
|
(root / 'FILE').touch()
|
|
with self.assertRaises(FormatError): resolve_asset(root, 'file')
|
|
|
|
def test_vr01_data_and_decoded_source_references(self):
|
|
source = ORIGINAL / 'gam/VR01.gam'
|
|
before = source.read_bytes()
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
output = Path(temporary) / 'prepared'
|
|
report = prepare_entities(ORIGINAL, 'vr01.GAM', output, decode=True)
|
|
self.assertEqual(report['level'], 'VR01')
|
|
self.assertEqual(len(report['entities']), 16)
|
|
self.assertEqual(report['counts']['3PIC'], 4)
|
|
self.assertEqual(report['sha256'], hashlib.sha256(before).hexdigest())
|
|
pickups = [e for e in report['entities'] if e['kind'] == '3PIC']
|
|
self.assertEqual([e['properties']['PickupIndex'] for e in pickups], [3001, 3002, 3003, 3004])
|
|
self.assertEqual([e['properties']['PointValue'] for e in pickups], [5] * 4)
|
|
self.assertEqual(pickups[0]['offset'], 0x1ba7)
|
|
self.assertIn('Radius', pickups[0]['propertyEvidence'])
|
|
self.assertEqual(report['duplicateTags']['c3dloadlevel'], [4, 9])
|
|
intro = next(e for e in report['entities'] if e['tag'] == 'bu')
|
|
target = next(r for r in intro['objectReferences'] if r['property'] == 'CameraTarget0')
|
|
self.assertEqual(target['candidateOrdinals'], [6])
|
|
star = next(r for r in report['resources'] if r['database'] == 'omt/sprites.omt' and r['id'] == 135)
|
|
self.assertEqual(star['name'], 'star')
|
|
self.assertEqual((output / star['path']).stat().st_size, star['width'] * star['height'] * 4)
|
|
self.assertTrue((output / star['png']).read_bytes().startswith(b'\x89PNG'))
|
|
voice = next(r for r in report['resources'] if r['database'] == 'omt/voicepractice.omt' and r['id'] == 24)
|
|
self.assertGreater(voice['audio']['seconds'], 0)
|
|
self.assertEqual((output / voice['path']).read_bytes()[:4], b'RIFF')
|
|
self.assertEqual(json.loads((output / 'entities.json').read_text()), report)
|
|
self.assertEqual(source.read_bytes(), before)
|
|
|
|
def test_lab_duplicates_authored_files_and_casefold_links(self):
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
report = prepare_entities(ORIGINAL, 'level1b.gam', Path(temporary) / 'prepared')
|
|
self.assertEqual(len(report['entities']), 129)
|
|
self.assertEqual(report['counts']['3OMT'], 4)
|
|
door = next(e for e in report['entities'] if e['tag'] == 'bars')
|
|
self.assertEqual({r['source'].lower() for r in door['fileReferences']}, {'ase/bars.ase', 'png/chain.png'})
|
|
self.assertTrue(report['duplicateTags']['c3dpickupitem'])
|
|
startup = next(e for e in report['entities'] if e['tag'] == 'STARTEXP')
|
|
link = next(r for r in startup['objectReferences'] if r['property'] == 'StartTrigger')
|
|
labexp = next(e for e in report['entities'] if e['tag'] == 'labexp')
|
|
self.assertEqual(link['candidateOrdinals'], [labexp['ordinal']])
|
|
self.assertTrue(all('unresolved' not in r for r in report['resources']))
|
|
|
|
def test_output_cannot_overlap_original_directory(self):
|
|
for path in (ORIGINAL, ORIGINAL / 'generated', ORIGINAL.parent):
|
|
with self.assertRaises(FormatError): prepare_entities(ORIGINAL, 'VR01.gam', path)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|