380 lines
25 KiB
Python
380 lines
25 KiB
Python
#!/usr/bin/env python3
|
|
from pathlib import Path
|
|
import struct
|
|
import sys
|
|
import unittest
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
|
|
from m2_equipment_convert import convert_equipment,batch,canonicalize_equivalent_rigid_views,mark_generated_inline_sequences
|
|
from m2_format_compare import model,skin
|
|
from m2_format_compare_test import fixtures
|
|
|
|
|
|
def fixture():
|
|
raw=bytearray(fixtures()[0])
|
|
def array(header,count,data):
|
|
at=len(raw);raw.extend(data);struct.pack_into('<II',raw,header,count,at)
|
|
return at
|
|
seq=bytearray(68);struct.pack_into('<HHII',seq,0,0,0,3333,3366)
|
|
array(28,1,seq)
|
|
bone=bytearray(108);struct.pack_into('<iIhH',bone,0,-1,0,-1,0)
|
|
for at in(12,40,68):struct.pack_into('<Hh6I',bone,at,0,-1,0,0,0,0,0,0)
|
|
array(52,1,bone);array(140,1,struct.pack('<H',0))
|
|
array(92,1,struct.pack('<4I',2,0,0,0))
|
|
array(132,1,struct.pack('<HH',0,0))
|
|
array(148,1,bytes(2));array(156,1,bytes(2));array(164,1,bytes(2));array(172,1,b'\xff\xff')
|
|
array(100,1,struct.pack('<Hh6I',0,-1,0,0,0,0,0,0))
|
|
view=struct.unpack_from('<I',raw,80)[0]
|
|
array(view+16,3,bytes(12))
|
|
array(view+24,1,struct.pack('<10H3f',0,0,0,3,0,3,1,0,1,0,0,0,0))
|
|
array(view+32,1,struct.pack('<Bb11H',16,0,0,0,0,65535,0,0,1,0,0,0,0))
|
|
struct.pack_into('<I',raw,view+40,21)
|
|
return bytes(raw)
|
|
|
|
|
|
class EquipmentTest(unittest.TestCase):
|
|
def test_complete_private825_inline_storage_delta_and_consumer(self):
|
|
import json,hashlib
|
|
job=Path(__file__).resolve().parents[1]/'runtime/rea-system-jobs-20261022/custom-map-resource-closure-20261024'
|
|
olddir=job/'model825-private-existing-conversion';newdir=job/'model825-private-inline-sequences'
|
|
if not (newdir/'manifest.json').is_file():self.skipTest('Private new825-model batch not attached')
|
|
load=lambda p:json.loads(p.read_text());old=load(olddir/'manifest.json');new=load(newdir/'manifest.json')
|
|
before={r['source_sha256']:r for r in old['records']};after={r['source_sha256']:r for r in new['records']}
|
|
self.assertEqual(set(before),set(after));self.assertEqual(len(after),825)
|
|
primary=Path(__file__).resolve().parents[2]/'references/WoWee/src/pipeline/m2_loader.cpp'
|
|
code=primary.read_text()
|
|
self.assertIn('kM2SeqFlagEmbeddedData = 0x20',code)
|
|
self.assertIn('if (i < sequenceFlags.size() && !(sequenceFlags[i] & kM2SeqFlagEmbeddedData)) continue;',code)
|
|
# The primary reader's real skip condition is applied to actual stored
|
|
# track data, rather than just checking that a writer toggles its bit.
|
|
control=load(job/'native98-skin-draw-boundary/model55-animation-material-metadata-comparison.json')
|
|
for r in control['records']:
|
|
seq=bytes.fromhex(r['Native_metadata']['sequences'])
|
|
self.assertTrue(all(struct.unpack_from('<I',seq,i+12)[0]&0x20 for i in range(0,len(seq),64)))
|
|
models=0;sequences=0;payload_sequences=0;formerly_skipped=0;blocked=0
|
|
for sha,r in after.items():
|
|
oldr=before[sha]
|
|
if not r['structural_conversion_complete']:
|
|
self.assertEqual(oldr,r);blocked+=1;continue
|
|
a=next(x for x in oldr['outputs']if x['role']=='native-body-needs-registered-container-bindings')
|
|
b=next(x for x in r['outputs']if x['role']=='native-body-needs-registered-container-bindings')
|
|
source=(olddir/a['file']).read_bytes();body=(newdir/b['file']).read_bytes()
|
|
self.assertEqual(len(source),len(body));self.assertEqual(hashlib.sha256(body).hexdigest(),b['sha256'])
|
|
count,start=struct.unpack_from('<II',body,28);sequences+=count;models+=1
|
|
mask=bytearray(body)
|
|
for i in range(count):
|
|
at=start+i*64+12;oldflags=struct.unpack_from('<I',source,at)[0];flags=struct.unpack_from('<I',body,at)[0]
|
|
self.assertEqual(flags,oldflags|0x20);mask[at:at+4]=source[at:at+4]
|
|
self.assertEqual(bytes(mask),source,'Changed geometry, tracks, flags or opaque bytes outside sequence storage metadata')
|
|
self.assertEqual([x for x in oldr['outputs']if x['role']=='native-skin'],[x for x in r['outputs']if x['role']=='native-skin'])
|
|
def extent(at,n):
|
|
self.assertGreaterEqual(at,304);self.assertLessEqual(at+n,len(body));return body[at:at+n]
|
|
def track(at,key_stride):
|
|
nonlocal payload_sequences,formerly_skipped
|
|
interpolation,glob,nt,ot,nv,ov=struct.unpack_from('<Hh4I',body,at)
|
|
self.assertEqual(nt,nv)
|
|
if not nt:return
|
|
times=extent(ot,nt*8);values=extent(ov,nv*8)
|
|
for i in range(nt):
|
|
tc,to=struct.unpack_from('<II',times,i*8);vc,vo=struct.unpack_from('<II',values,i*8);self.assertEqual(tc,vc)
|
|
if not tc:continue
|
|
extent(to,tc*4);extent(vo,vc*key_stride)
|
|
# A global track uses one in-model array; ordinary tracks
|
|
# use the same per-sequence index as the actual reader.
|
|
seq=0 if glob>=0 else i;self.assertLess(seq,count)
|
|
now=struct.unpack_from('<I',body,start+seq*64+12)[0]
|
|
previous=struct.unpack_from('<I',source,start+seq*64+12)[0]
|
|
self.assertTrue(now&0x20);payload_sequences+=1
|
|
formerly_skipped+=not(previous&0x20)
|
|
for header,stride,tracks in((44,88,((16,12),(36,8),(56,12))),(72,40,((0,12),(20,2))),(88,20,((0,2),)),(96,60,((0,12),(20,8),(40,12))),(240,40,((20,1),))):
|
|
n,offset=struct.unpack_from('<II',body,header)
|
|
if n:extent(offset,n*stride)
|
|
for i in range(n):
|
|
for local,key_stride in tracks:track(offset+i*stride+local,key_stride)
|
|
self.assertEqual((models,blocked),(782,43));self.assertGreater(formerly_skipped,0)
|
|
# Typed helper refuses containers and broken sequence extents; no
|
|
# permissive native-model/AFID patch path is introduced.
|
|
with self.assertRaises(ValueError):mark_generated_inline_sequences(b'MD21'+bytes(400))
|
|
bad=bytearray(body);struct.pack_into('<II',bad,28,4097,304)
|
|
with self.assertRaises(ValueError):mark_generated_inline_sequences(bytes(bad))
|
|
report=dict(status='PASS_NEW825_PRIVATE_INLINE_STORAGE_BOUNDARY',models_changed=782,
|
|
original43strict_holds_retained=True,sequence_records=sequences,
|
|
in_body_payload_sequences_checked=payload_sequences,previously_skipped_by_primary_reader=formerly_skipped,
|
|
all_nonstorage_bytes_and_SKIN_metadata_exact=True,all_other_flags_preserved=True,
|
|
primary_consumer_sha256=hashlib.sha256(code.encode()).hexdigest(),
|
|
original55native_inline_controls_reused=True,source_or_native_exports=0,
|
|
original70334consumer_code_verified=False,native_acceptance=False,published_models_modified=False)
|
|
(job/'model782-inline-storage-focused-check.json').write_text(json.dumps(report,indent=2)+'\n')
|
|
|
|
def test_sequence_links_are_bounded_in_generated_table(self):
|
|
raw = bytearray(fixture())
|
|
_, old = struct.unpack_from('<II', raw, 28)
|
|
at = len(raw)
|
|
raw.extend(raw[old:old + 68] * 3)
|
|
struct.pack_into('<II', raw, 28, 3, at)
|
|
# Keep the valid alias/back-pointer pair and a variation chain; row 2
|
|
# has the same bad ordinary-sequence alias seen on the two helms.
|
|
for i, links in enumerate(((1, 1), (-1, 0), (-1, 24948))):
|
|
struct.pack_into('<hH', raw, at + i * 68 + 64, *links)
|
|
struct.pack_into('<I', raw, at + 68 + 16, 0x40)
|
|
body, _, audit = convert_equipment(bytes(raw))
|
|
count, records = struct.unpack_from('<II', body, 28)
|
|
links = [struct.unpack_from('<hH', body, records + 64 * i + 60) for i in range(count)]
|
|
self.assertEqual(links, [(1, 1), (-1, 0), (-1, 2)])
|
|
self.assertEqual(audit['repaired_alias_links'], [(2, 24948)])
|
|
|
|
for field, fmt, value, label in ((64, 'h', 3, 'variationNext'),
|
|
(64, 'h', -2, 'variationNext'),
|
|
(66, 'H', 3, 'aliasNext')):
|
|
with self.subTest(field=label, value=value):
|
|
bad = bytearray(raw)
|
|
struct.pack_into('<' + fmt, bad, at + 68 + field, value)
|
|
with self.assertRaisesRegex(ValueError, label):
|
|
convert_equipment(bytes(bad))
|
|
|
|
def test_shared_structure_preserves_geometry_topology_and_all_batches(self):
|
|
raw=fixture();body,skins,r=convert_equipment(raw)
|
|
self.assertEqual(model(body)['summary']['md20_version'],272)
|
|
self.assertEqual(model(raw)['vertices'],model(body)['vertices'])
|
|
self.assertEqual(skin(model(raw))['faces'],skin(model(body),skins[0])['faces'])
|
|
self.assertEqual(r['skins'][0]['source_batches'],r['skins'][0]['native_batches'])
|
|
self.assertEqual(r['skins'][0]['native_header_bytes'],64)
|
|
self.assertEqual(r['skins'][0]['legacy_bone_count_max'],21)
|
|
self.assertEqual(struct.unpack_from('<I',skins[0],44)[0],0)
|
|
self.assertEqual(struct.unpack_from('<II',skins[0],48),(0,0))
|
|
self.assertGreaterEqual(struct.unpack_from('<I',skins[0],8)[0],64)
|
|
self.assertTrue(r['structural_conversion_complete']);self.assertFalse(r['engine_loaded'])
|
|
self.assertFalse(r['registered_dependencies_complete']);self.assertFalse(r['material_engine_verified'])
|
|
self.assertEqual((body,skins,r),convert_equipment(raw))
|
|
n,at=struct.unpack_from('<II',body,28);self.assertEqual(n,1)
|
|
self.assertEqual(struct.unpack_from('<I',body,at+4)[0],33)
|
|
count,at=struct.unpack_from('<II',body,104);self.assertEqual(count,3)
|
|
self.assertEqual(struct.unpack_from('<3h',body,at),(-1,-1,0))
|
|
|
|
def test_duplicate_unused_replaceable_declaration_uses_active_index(self):
|
|
raw=bytearray(fixture());offset=len(raw);raw.extend(struct.pack('<8I',2,3,0,0,2,0,0,0));struct.pack_into('<II',raw,92,2,offset)
|
|
body,_,_=convert_equipment(bytes(raw));count,at=struct.unpack_from('<II',body,104)
|
|
self.assertEqual((count,struct.unpack_from('<3h',body,at)),(3,(-1,-1,0)))
|
|
offset=len(raw);raw.extend(struct.pack('<2H',0,1));struct.pack_into('<II',raw,148,2,offset)
|
|
with self.assertRaisesRegex(ValueError,'Multiple active'):convert_equipment(bytes(raw))
|
|
|
|
def test_unknown_texture_types_or_unconverted_uv_tracks_reject(self):
|
|
raw=bytearray(fixture());offset=struct.unpack_from('<I',raw,96)[0];struct.pack_into('<I',raw,offset,3)
|
|
with self.assertRaisesRegex(ValueError,'texture type'):convert_equipment(bytes(raw))
|
|
raw=bytearray(fixture());offset=len(raw);raw.extend(bytes(2));struct.pack_into('<II',raw,116,1,offset)
|
|
with self.assertRaisesRegex(ValueError,'UV animation'):convert_equipment(bytes(raw))
|
|
|
|
def test_source124_active_lookup_is_translated_unused_entries_retained_as_provenance(self):
|
|
# Actual source helmets carry1for unused types0/1 despite only one
|
|
# type2 texture. These values must not become native out-of-range IDs.
|
|
raw=bytearray(fixture());lookup=[1,1,0]+[-1]*12
|
|
at=len(raw);raw.extend(struct.pack('<15h',*lookup));struct.pack_into('<II',raw,124,15,at)
|
|
body,_,report=convert_equipment(bytes(raw))
|
|
n,offset=struct.unpack_from('<II',body,104)
|
|
self.assertEqual((n,list(struct.unpack_from('<3h',body,offset))),(3,[-1,-1,0]))
|
|
self.assertEqual(struct.unpack_from('<II',body,96),(0,0))
|
|
self.assertEqual(report['source_replaceable_lookup'],lookup)
|
|
bad=bytearray(raw);struct.pack_into('<h',bad,at+4,1)
|
|
with self.assertRaisesRegex(ValueError,'active replaceable'):convert_equipment(bytes(bad))
|
|
bad=bytearray(raw);struct.pack_into('<h',bad,at+4,-2)
|
|
with self.assertRaisesRegex(ValueError,'active replaceable'):convert_equipment(bytes(bad))
|
|
|
|
def test_submesh_sphere_encloses_every_vertex(self):
|
|
body,skins,_=convert_equipment(fixture());k=skins[0]
|
|
count,offset=struct.unpack_from('<II',k,28);self.assertEqual(count,1)
|
|
center=struct.unpack_from('<3f',k,offset+32);radius=struct.unpack_from('<f',k,offset+44)[0]
|
|
import math
|
|
for v in model(body)['vertices']:
|
|
self.assertLessEqual(math.dist(center,struct.unpack_from('<3f',v)),radius)
|
|
|
|
def test_unreviewed_features_and_nonzero_extra_lookup_reject(self):
|
|
for at in(116,284,292,300,308,316):
|
|
bad=bytearray(fixture());struct.pack_into('<I',bad,at,1)
|
|
with self.assertRaisesRegex(ValueError,'Unsupported'):convert_equipment(bytes(bad))
|
|
bad=bytearray(fixture());offset=len(bad);bad.extend(b'\x01\0');struct.pack_into('<II',bad,108,1,offset)
|
|
with self.assertRaisesRegex(ValueError,'extra lookup'):convert_equipment(bytes(bad))
|
|
|
|
def test_color_and_alpha_tracks_preserve_keys_and_material_reference(self):
|
|
raw=bytearray(fixture())
|
|
def add(data):
|
|
at=len(raw);raw.extend(data);return at
|
|
ranges=add(struct.pack('<II',0,1));times=add(struct.pack('<2I',3333,3366))
|
|
rgb=struct.pack('<6f',1,.25,0,.5,1,.75);alpha=struct.pack('<2h',32767,16384)
|
|
rgb_at=add(rgb);alpha_at=add(alpha)
|
|
colors=add(struct.pack('<Hh6I',1,-1,1,ranges,2,times,2,rgb_at)+
|
|
struct.pack('<Hh6I',1,-1,1,ranges,2,times,2,alpha_at))
|
|
struct.pack_into('<II',raw,84,1,colors)
|
|
view=struct.unpack_from('<I',raw,80)[0];batch_at=struct.unpack_from('<I',raw,view+36)[0]
|
|
struct.pack_into('<H',raw,batch_at+8,0) # Actual colorIndex in <Bb11H.
|
|
body,skins,report=convert_equipment(bytes(raw))
|
|
count,at=struct.unpack_from('<II',body,72);self.assertEqual(count,1)
|
|
for header,expected in ((at,rgb),(at+20,alpha)):
|
|
mode,glob,nt,to,nk,ko=struct.unpack_from('<Hh4I',body,header)
|
|
self.assertEqual((mode,glob,nt,nk),(1,-1,1,1))
|
|
n,t=struct.unpack_from('<II',body,to);self.assertEqual(n,2)
|
|
self.assertEqual(struct.unpack_from('<2I',body,t),(0,33))
|
|
n,k=struct.unpack_from('<II',body,ko);self.assertEqual(n,2)
|
|
self.assertEqual(body[k:k+len(expected)],expected)
|
|
self.assertEqual(skin(model(body),skins[0])['summary']['batch_rows'][0][5],0)
|
|
self.assertEqual([r['label']for r in report['tracks']if r['label'].startswith('color:')],
|
|
['color:0:rgb','color:0:alpha'])
|
|
self.assertFalse(report['material_engine_verified'])
|
|
bad=bytearray(raw);struct.pack_into('<H',bad,batch_at+8,1)
|
|
with self.assertRaisesRegex(ValueError,'material binding'):convert_equipment(bytes(bad))
|
|
bad=bytearray(raw);struct.pack_into('<I',bad,84,2)
|
|
with self.assertRaises(ValueError):convert_equipment(bytes(bad))
|
|
|
|
def test_length_bounded_source_texture_is_explicitly_terminated_in_target(self):
|
|
raw=bytearray(fixture());name=b'item/objectcomponents/shield/source.blp'
|
|
at=len(raw);raw.extend(name);texture=struct.unpack_from('<I',raw,96)[0]
|
|
struct.pack_into('<4I',raw,texture,2,0,len(name),at)
|
|
self.assertEqual(model(bytes(raw))['summary']['declared_textures'][0]['name'],name.decode())
|
|
body,_,report=convert_equipment(bytes(raw));slot=model(body)['summary']['declared_textures'][0]
|
|
self.assertEqual(slot['name'],name.decode());self.assertEqual(slot['declared_name_bytes'],len(name)+1)
|
|
self.assertEqual(report['normalized_length_bounded_texture_names'],[0])
|
|
tex=struct.unpack_from('<I',body,84)[0];length,offset=struct.unpack_from('<II',body,tex+8)
|
|
self.assertEqual(body[offset:offset+length],name+b'\0')
|
|
bad=bytearray(body);struct.pack_into('<I',bad,tex+8,len(name))
|
|
with self.assertRaisesRegex(ValueError,'terminator'):model(bytes(bad))
|
|
bad=bytearray(raw);bad[at:at+len(name)]=b'../'+b'x'*(len(name)-7)+b'.blp'
|
|
with self.assertRaisesRegex(ValueError,'Unsafe'):convert_equipment(bytes(bad))
|
|
|
|
def test_bad_batch_reference_and_unreviewed_shader_reject(self):
|
|
raw=fixture();view=struct.unpack_from('<I',raw,80)[0];batch=struct.unpack_from('<I',raw,view+36)[0]
|
|
for offset,value in((4,1),(10,5),(14,2),(16,1),(18,1)):
|
|
bad=bytearray(raw);struct.pack_into('<H',bad,batch+offset,value)
|
|
with self.assertRaises(ValueError):convert_equipment(bytes(bad))
|
|
|
|
def test_unknown_global_flags_and_missing_sequence_reject(self):
|
|
raw=fixture()
|
|
for at,value in((16,1),(28,0)):
|
|
bad=bytearray(raw);struct.pack_into('<I',bad,at,value)
|
|
with self.assertRaises(ValueError):convert_equipment(bytes(bad))
|
|
|
|
def test_creature_opt_in_allows_tilt_flag_and_monster_skin_types_only(self):
|
|
raw=bytearray(fixture());struct.pack_into('<I',raw,16,1)
|
|
with self.assertRaisesRegex(ValueError,'global flags'):convert_equipment(bytes(raw))
|
|
convert_equipment(bytes(raw),creature=True)
|
|
bad=bytearray(raw);struct.pack_into('<I',bad,16,3)
|
|
with self.assertRaisesRegex(ValueError,'global flags'):convert_equipment(bytes(bad),creature=True)
|
|
offset=struct.unpack_from('<I',raw,96)[0];struct.pack_into('<I',raw,offset,11)
|
|
plain=bytearray(raw);struct.pack_into('<I',plain,16,0)
|
|
with self.assertRaisesRegex(ValueError,'texture type'):convert_equipment(bytes(plain))
|
|
convert_equipment(bytes(raw),creature=True)
|
|
with self.assertRaisesRegex(ValueError,'creature opt-in'):convert_equipment(bytes(raw),creature=1)
|
|
|
|
def test_event_exclusive_range_preserves_relative_weapon_trail(self):
|
|
raw=bytearray(fixture());range_at=len(raw);raw.extend(struct.pack('<II',0,1))
|
|
time_at=len(raw);raw.extend(struct.pack('<I',33));event_at=len(raw)
|
|
raw.extend(b'$WTB'+bytes(20)+struct.pack('<Hh4I',0,-1,1,range_at,1,time_at))
|
|
struct.pack_into('<II',raw,276,1,event_at)
|
|
body,_,report=convert_equipment(bytes(raw))
|
|
self.assertEqual(report['events'][0]['timing'],['source-already-relative'])
|
|
count,at=struct.unpack_from('<II',body,256);self.assertEqual(count,1)
|
|
_,global_id,num,arrays=struct.unpack_from('<HhII',body,at+24)
|
|
n,times=struct.unpack_from('<II',body,arrays)
|
|
self.assertEqual((global_id,num,n),(-1,1,1));self.assertEqual(struct.unpack_from('<I',body,times)[0],33)
|
|
|
|
def test_event_unused_trailing_default_preserves_all_reachable_markers(self):
|
|
# Actual source40047 has one sequence and these two trail markers,
|
|
# each with ranges[(0,1),(0,0)]. The original reader indexes only
|
|
# the active source animation's range (7195dc..7195e9).
|
|
raw=bytearray(fixture());range_at=len(raw)
|
|
raw.extend(struct.pack('<4I',0,1,0,0))
|
|
time_at=len(raw);raw.extend(struct.pack('<I',3333));event_at=len(raw)
|
|
headers=[]
|
|
for number,tag in enumerate((b'$WTT',b'$WTB')):
|
|
header=tag+struct.pack('<II3f',17+number,0,1.0,2.0,3.0)
|
|
headers.append(header)
|
|
raw.extend(header+struct.pack('<Hh4I',0,-1,2,range_at,1,time_at))
|
|
struct.pack_into('<II',raw,276,2,event_at)
|
|
body,_,report=convert_equipment(bytes(raw))
|
|
count,at=struct.unpack_from('<II',body,256);self.assertEqual(count,2)
|
|
for index,header in enumerate(headers):
|
|
event=at+36*index
|
|
self.assertEqual(body[event:event+24],header)
|
|
self.assertEqual(struct.unpack_from('<HhI',body,event+24),(0,-1,1))
|
|
arrays=struct.unpack_from('<I',body,event+32)[0]
|
|
n,times=struct.unpack_from('<II',body,arrays)
|
|
self.assertEqual((n,struct.unpack_from('<I',body,times)[0]),(1,0))
|
|
self.assertEqual(report['events'][index]['unused_default_ranges'],[[0,0]])
|
|
# Keep the original default path and all unrelated timing guards.
|
|
for change in ('nonempty-extra','two-extra','global-sequence','interpolation'):
|
|
bad=bytearray(raw)
|
|
if change=='nonempty-extra':struct.pack_into('<II',bad,range_at+8,0,1)
|
|
elif change=='two-extra':struct.pack_into('<I',bad,event_at+28,3)
|
|
elif change=='global-sequence':struct.pack_into('<h',bad,event_at+26,0)
|
|
else:struct.pack_into('<H',bad,event_at+24,1)
|
|
with self.assertRaises(ValueError,msg=change):convert_equipment(bytes(bad))
|
|
|
|
def test_rigid_view_gate_preserves_draws_and_refuses_bone_weight_and_material_drift(self):
|
|
raw=bytearray(fixture())
|
|
def add(data):
|
|
at=len(raw);raw.extend(data);return at
|
|
original_view=struct.unpack_from('<I',raw,80)[0]
|
|
first=raw[original_view:original_view+44]
|
|
# Two authored hardware profiles share their draws; only the palette
|
|
# offset changes, and both entries resolve to the same global bone.
|
|
bones_at=struct.unpack_from('<I',raw,56)[0]
|
|
bones=add(raw[bones_at:bones_at+108]*2);struct.pack_into('<II',raw,52,2,bones)
|
|
lookup=add(struct.pack('<2H',0,0));struct.pack_into('<II',raw,140,2,lookup)
|
|
mesh_at=struct.unpack_from('<I',first,28)[0]
|
|
mesh=bytearray(raw[mesh_at:mesh_at+32]);struct.pack_into('<H',mesh,14,1)
|
|
second_mesh=add(mesh);second=bytearray(first)
|
|
struct.pack_into('<I',second,28,second_mesh);struct.pack_into('<I',second,40,21)
|
|
views=add(first+second);struct.pack_into('<II',raw,76,2,views)
|
|
def convert(value):
|
|
body,skins,_=convert_equipment(bytes(value))
|
|
return body,skins
|
|
body,skins=convert(raw)
|
|
self.assertEqual(model(body)['view_count'],2) # Default path unchanged.
|
|
one,selected,audit=canonicalize_equivalent_rigid_views(bytes(raw),body,skins)
|
|
self.assertEqual(model(one)['view_count'],1);self.assertEqual(selected,[skins[0]])
|
|
expected=bytearray(body);struct.pack_into('<I',expected,68,1)
|
|
self.assertEqual(one,bytes(expected));self.assertEqual(audit['authored_view_count'],2)
|
|
self.assertTrue(audit['all_vertices_weights_resolved_bones_materials_and_oriented_draws_equal'])
|
|
# Each refusal keeps the global triangle geometry while changing a
|
|
# binding the renderer consumes. Face counts cannot authorize collapse.
|
|
bad=bytearray(raw);struct.pack_into('<H',bad,lookup+2,1)
|
|
b,s=convert(bad)
|
|
with self.assertRaisesRegex(ValueError,'different rendered'):canonicalize_equivalent_rigid_views(bytes(bad),b,s)
|
|
bad=bytearray(raw);vertices=struct.unpack_from('<I',bad,72)[0]
|
|
bad[vertices+12:vertices+16]=bytes((128,127,0,0));b,s=convert(bad)
|
|
with self.assertRaisesRegex(ValueError,'Weighted'):canonicalize_equivalent_rigid_views(bytes(bad),b,s)
|
|
bad=bytearray(raw);batch_at=struct.unpack_from('<I',bad,views+44+36)[0]
|
|
batch=bytearray(bad[batch_at:batch_at+24]);batch[0]=0
|
|
new_batch=len(bad);bad.extend(batch);struct.pack_into('<I',bad,views+44+36,new_batch);b,s=convert(bad)
|
|
with self.assertRaisesRegex(ValueError,'different rendered'):canonicalize_equivalent_rigid_views(bytes(bad),b,s)
|
|
with self.assertRaisesRegex(ValueError,'complete native view'):canonicalize_equivalent_rigid_views(bytes(raw),body,skins[:1])
|
|
|
|
def test_batch_reuses_manifest_deduplicates_and_reproduces_payloads(self):
|
|
import hashlib,json,tempfile
|
|
raw=fixture();sha=hashlib.sha256(raw).hexdigest()
|
|
with tempfile.TemporaryDirectory()as directory:
|
|
root=Path(directory);source=root/(sha+'.m2');source.write_bytes(raw)
|
|
manifest=root/'manifest.json';row=dict(member='item/objectcomponents/weapon/test.m2',archive='patch-4.mpq',file=source.name,sha256=sha)
|
|
manifest.write_text(json.dumps(dict(matches=[row,dict(row,archive='patch.MPQ')])))
|
|
dry=batch(manifest);first=batch(manifest,root/'first');second=batch(manifest,root/'second')
|
|
self.assertEqual(dry,first);self.assertEqual(first,second)
|
|
self.assertEqual(first['converted_bodies'],1);self.assertEqual(len(first['records'][0]['source_candidates']),2)
|
|
for f in(root/'first').iterdir():self.assertEqual(f.read_bytes(),(root/'second'/f.name).read_bytes())
|
|
with self.assertRaises(FileExistsError):batch(manifest,root/'first')
|
|
source.write_bytes(raw+b'x')
|
|
with self.assertRaisesRegex(ValueError,'changed'):batch(manifest,root/'bad')
|
|
self.assertFalse((root/'bad').exists())
|
|
|
|
def test_rejected_model_is_recorded_without_hiding_other_candidates(self):
|
|
import hashlib,json,tempfile
|
|
good=fixture();bad=bytearray(good);struct.pack_into('<I',bad,316,1);bad=bytes(bad)
|
|
with tempfile.TemporaryDirectory()as directory:
|
|
root=Path(directory);rows=[]
|
|
for i,raw in enumerate((good,bad)):
|
|
sha=hashlib.sha256(raw).hexdigest();p=root/(sha+'.m2');p.write_bytes(raw)
|
|
rows.append(dict(member=f'item/objectcomponents/weapon/test{i}.m2',archive='patch-4.mpq',file=p.name,sha256=sha))
|
|
manifest=root/'manifest.json';manifest.write_text(json.dumps(dict(matches=rows)))
|
|
result=batch(manifest)
|
|
self.assertEqual((result['converted_bodies'],result['blocked_bodies']),(1,1))
|
|
self.assertTrue(any('Unsupported'in r.get('blocked','')for r in result['records']))
|
|
|
|
|
|
if __name__=='__main__':unittest.main()
|