Files
Neutron/tests/test_rocketship_resources.py
T

102 lines
5.8 KiB
Python

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 rocketship_resources import CLIPS, SOURCES, TEXTURES, export_rocketship
from ase import expand_mesh, read_ase
from entities import resolve_asset
from omt import FormatError
@unittest.skipUnless(ORIGINAL.exists(), 'Original assets are local integration fixtures')
class RocketshipResourceTests(unittest.TestCase):
def test_exact_original_pixels_and_authored_sampling(self):
with tempfile.TemporaryDirectory() as directory:
output = Path(directory)/'rocketship'
manifest = export_rocketship(ORIGINAL, output)
self.assertEqual(manifest, json.loads((output/'rocketship.json').read_text()))
self.assertEqual((manifest['schema'], manifest['sources']), (1, SOURCES))
expected_textures = {name: (width, height, sha) for name, width, height, sha in TEXTURES}
for entry, (stem, _, texture, vertices, faces, times) in zip(manifest['animations'], CLIPS):
animation = json.loads((output/entry['path']).read_text())
self.assertEqual([frame['time'] for frame in animation['frames']], list(times))
self.assertEqual((entry['indexedVertices'], entry['polygons']), (vertices, faces))
rgba = (output/stem/animation['texture']['path']).read_bytes()
self.assertEqual((animation['texture']['width'], animation['texture']['height'],
hashlib.sha256(rgba).hexdigest()), expected_textures[texture])
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_morphs_use_exact_base_uv_topology_and_sample_positions(self):
with tempfile.TemporaryDirectory() as directory:
output = Path(directory)/'rocketship'
export_rocketship(ORIGINAL, output)
for stem, base in (('jimdrive', 'jimstop'), ('godrocket', 'godpoint')):
source = read_ase(resolve_asset(ORIGINAL, f'ASE/{stem}.ASE'))
topology = read_ase(resolve_asset(ORIGINAL, f'ASE/{base}.ASE'))['objects'][0]['samples'][0]
self.assertFalse(source['objects'][0]['samples'][0]['textureFaces'])
manifest = json.loads((output/stem/'animation.json').read_text())
self.assertEqual(manifest['morphBase']['sha256'], SOURCES[f'ASE/{base}.ASE'])
for sample, frame in zip(source['objects'][0]['samples'], manifest['frames']):
expected = expand_mesh({**sample, 'faces': topology['faces'],
'textureFaces': topology['textureFaces'], 'uvs': topology['uvs']})
actual = json.loads((output/stem/frame['path']).read_text())
self.assertEqual(actual['vertices'], [list(p) for p in expected['vertices']])
self.assertEqual(actual['uvs'], [list(p) for p in expected['uvs']])
def test_vr02_preserves_source_offsets_duplicate_tags_and_null_polygons(self):
with tempfile.TemporaryDirectory() as directory:
manifest = export_rocketship(ORIGINAL, Path(directory)/'rocketship')
entities = manifest['tutorial']['entities']
self.assertEqual(len(entities), 18)
rocket = next(e for e in entities if e['kind'] == '3ROC')
self.assertEqual((rocket['ordinal'], rocket['offset']), (1, 0x1cd))
values = {p['name']: p['value'] for p in rocket['properties']}
self.assertEqual((values['MaxHeight'], values['MaxSpeed'], values['AccelRate'],
values['DecelRate'], values['NewGravity']), (3900, 1400, 400, 1, 0))
points = [e for e in entities if e['kind'] == '3PAT']
tags = [next(p['value'] for p in e['properties'] if p['name'] == 'ObjectTag') for e in points]
self.assertEqual(tags, ['rs1', 'rs1', 'rs2', 'rs3'])
self.assertEqual(manifest['staticGeometry']['materialPolygons'],
{'0': 160, '2': 96, '3': 192, '5': 400, 'null': 32})
def test_original_ancestors_and_descendant_symlinks_rejected(self):
for output in (ORIGINAL, ORIGINAL/'generated', ORIGINAL.parent):
with self.subTest(output=output), self.assertRaises(ValueError):
export_rocketship(ORIGINAL, output)
with tempfile.TemporaryDirectory() as directory:
output = Path(directory)/'rocketship'
output.mkdir()
target = Path(directory)/'unchanged'
target.write_bytes(b'unchanged')
(output/'strato.rgba').symlink_to(target)
with self.assertRaises(ValueError):
export_rocketship(ORIGINAL, output)
self.assertEqual(target.read_bytes(), b'unchanged')
def test_changed_source_and_bad_pixels_rejected_before_output(self):
with tempfile.TemporaryDirectory() as directory:
output = Path(directory)/'rocketship'
with patch('rocketship_resources.digest', return_value='0'*64), \
patch('rocketship_resources.read_ase') as decoder, self.assertRaises(FormatError):
export_rocketship(ORIGINAL, output)
decoder.assert_not_called()
self.assertFalse(output.exists())
with patch('rocketship_resources.read_png', return_value=dict(width=1, height=1, rgba=b'\0'*4)), \
self.assertRaises(FormatError):
export_rocketship(ORIGINAL, output)
self.assertFalse(output.exists())
if __name__ == '__main__':
unittest.main()