from pathlib import Path import hashlib import json import sys import tempfile import unittest from unittest.mock import patch ROOT = Path(__file__).resolve().parents[1] ORIGINAL = ROOT.parent/'Original' sys.path.insert(0, str(ROOT/'tools/extractors')) from grappler_resources import (CLIPS, CURSOR_RGBA_SHA256, JIMMY_RGBA_SHA256, SOURCES, export_grappler) from ase import expand_mesh, read_ase from entities import resolve_asset from omt import FormatError sys.path.insert(0,str(ROOT/"tools/binary")) from pe_inspect import PE from checkpoint_script_state import find_case,read_va @unittest.skipUnless(ORIGINAL.exists(), 'Original assets are local integration fixtures') class GrapplerResources(unittest.TestCase): def test_source_final_cursor_swing_binding_roll_and_shape_scale_consumer(self): spans=[('Neutron.exe',0x445580,96,'e2470f79238741d42a78acd678921e0810b93168fe9c53f9dc562d6596b4ae9e'), ('Neutron.exe',0x422bcc,19,'1c37f47d4c3f5235876a2330c28634645a704c28781fb7540e9f485a29d0df5f'), ('Neutron.exe',0x41f0fa,53,'0ec5e8e647578640c64290c69d6d193d2e14fbdbeb1eb424ee6b4d7305e8817b'), ('OMT2.dll',0x10005970,244,'5b3747756cf8a343b3b4007cbb9710591b9026db0661c4a60a127684eb37560a')] for name,va,size,digest in spans: pe=PE(find_case(ORIGINAL,name)) self.assertEqual(hashlib.sha256(read_va(pe,va,size)).hexdigest(),digest) def test_exact_resources_and_source_sampling(self): with tempfile.TemporaryDirectory() as directory: output = Path(directory)/'grappler' manifest = export_grappler(ORIGINAL, output) self.assertEqual(manifest, json.loads((output/'grappler.json').read_text())) self.assertEqual(manifest['sources'], SOURCES) self.assertEqual(hashlib.sha256((output/'jimycarl.rgba').read_bytes()).hexdigest(), JIMMY_RGBA_SHA256) self.assertEqual(hashlib.sha256((output/'canvas_8.rgba').read_bytes()).hexdigest(), CURSOR_RGBA_SHA256) self.assertEqual((manifest['cursor']['width'], manifest['cursor']['height']), (64, 64)) for stem, _, vertices, faces, times in CLIPS: animation = json.loads((output/stem/'animation.json').read_text()) self.assertEqual([f['time'] for f in animation['frames']], list(times)) self.assertEqual(animation['texture']['path'], '../jimycarl.rgba') for frame in animation['frames']: mesh = json.loads((output/stem/frame['path']).read_text()) self.assertEqual(len(mesh['vertices']), faces*3) self.assertEqual(mesh['indices'], list(range(faces*3))) def test_unscaled_rope_positions_and_swing_base_topology(self): with tempfile.TemporaryDirectory() as directory: output = Path(directory)/'grappler' export_grappler(ORIGINAL, output) base = read_ase(resolve_asset(ORIGINAL, 'ASE/jimstop.ASE'))['objects'][0]['samples'][0] for stem, base_stem, _, _, _ in CLIPS: source = read_ase(resolve_asset(ORIGINAL, f'ASE/{stem}.ASE')) animation = json.loads((output/stem/'animation.json').read_text()) for sample, frame in zip(source['objects'][0]['samples'], animation['frames'], strict=True): if base_stem: sample = {**sample, 'faces': base['faces'], 'textureFaces': base['textureFaces'], 'uvs': base['uvs']} expected = expand_mesh(sample) actual = json.loads((output/stem/frame['path']).read_text()) self.assertEqual(actual['vertices'], [list(v) for v in expected['vertices']]) self.assertEqual(actual['uvs'], [list(v) for v in expected['uvs']]) rope = json.loads((output/'rope01/frame_0000.json').read_text()) self.assertEqual(max(v[2] for v in rope['vertices']), 600.9758) self.assertEqual(min(v[2] for v in rope['vertices']), 0) def test_source_defaults_keep_final_cursor_size_distinct_from_sprite_size(self): with tempfile.TemporaryDirectory() as directory: m = export_grappler(ORIGINAL, Path(directory)/'grappler') self.assertEqual((m['bindings']['cursorInheritedSpriteSize'], m['bindings']['cursorWorldWidth'], m['bindings']['cursorWorldHeight']), (150, 110, 110)) self.assertEqual(m['bindings']['cursorBlend'], [6, 7]) self.assertEqual(m['bindings']['cursorAlign'], [3, 3]) self.assertEqual((m['bindings']['ropeInitialRoll'], m['bindings']['ropeUpdateRoll']), (180, 0)) self.assertEqual(len(m['tutorial']['entities']), 16) self.assertFalse(any(e['kind'] == '3GRA' for e in m['tutorial']['entities'])) def test_original_paths_ancestors_and_child_symlinks_rejected(self): for output in (ORIGINAL, ORIGINAL/'generated', ORIGINAL.parent): with self.subTest(output=output), self.assertRaises(ValueError):export_grappler(ORIGINAL, output) with tempfile.TemporaryDirectory() as directory: output = Path(directory)/'grappler';output.mkdir() target = Path(directory)/'unchanged';target.write_bytes(b'unchanged') (output/'jimycarl.rgba').symlink_to(target) with self.assertRaises(ValueError):export_grappler(ORIGINAL, output) self.assertEqual(target.read_bytes(), b'unchanged') def test_bad_source_or_decoded_pixels_rejected_before_output(self): with tempfile.TemporaryDirectory() as directory: output = Path(directory)/'grappler' with patch('grappler_resources.digest', return_value='0'*64), \ patch('grappler_resources.read_ase') as decoder, self.assertRaises(FormatError): export_grappler(ORIGINAL, output) decoder.assert_not_called();self.assertFalse(output.exists()) with patch('grappler_resources.read_png', return_value=dict(width=1,height=1,rgba=b'\0'*4)), self.assertRaises(FormatError): export_grappler(ORIGINAL, output) self.assertFalse(output.exists()) if __name__ == '__main__':unittest.main()