47 lines
3.2 KiB
Python
47 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
import hashlib,io,json,os,struct,sys,tempfile
|
|
from pathlib import Path
|
|
import numpy as np
|
|
from PIL import Image
|
|
ROOT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(ROOT/'ops'))
|
|
import booty_bay_minimap as m
|
|
from content_candidate_rehearsal import rehearse
|
|
|
|
def synthetic(size):
|
|
header=bytearray(148);header[:12]=b'BLP2\x01\0\0\0\x02\0\0\0'
|
|
struct.pack_into('<II',header,12,size,size);struct.pack_into('<I',header,20,148);struct.pack_into('<I',header,84,size*size//2)
|
|
blocks=b''.join(struct.pack('<HHI',0xf800,0x001f,(n*0x1020305)&0xffffffff)for n in range((size//4)**2))
|
|
return bytes(header)+blocks
|
|
|
|
def main():
|
|
source,native=synthetic(256),synthetic(512);mask=np.zeros((128,128),dtype=bool)
|
|
out=m.replace_cells(source,native,mask)
|
|
expected=Image.open(io.BytesIO(source)).convert('RGB').resize((512,512),Image.Resampling.NEAREST)
|
|
assert Image.open(io.BytesIO(out)).convert('RGB').tobytes()==expected.tobytes()
|
|
mask[:]=True;assert m.replace_cells(source,native,mask)[148:]==native[148:]
|
|
for blob in [source[:-1],source[:8]+b'\x03'+source[9:]]:
|
|
try:m.replace_cells(blob,native,mask)
|
|
except ValueError:pass
|
|
else:raise AssertionError('malformed source accepted')
|
|
try:m.mask_from_cells([[0,0,0,0],[0,0,0,0]])
|
|
except ValueError:pass
|
|
else:raise AssertionError('duplicate cell accepted')
|
|
artifact=os.environ.get('GW_BOOTY_BAY_Q102_ROOT')
|
|
if not artifact:print('Lossless BC1 unit checks PASS (frozen candidate gate requires GW_BOOTY_BAY_Q102_ROOT)');return
|
|
r=Path(artifact);load=lambda p:json.loads(p.read_text());digest=lambda p:hashlib.sha256(p.read_bytes()).hexdigest()
|
|
for path,sha in load(r/'input-pins.json').items():assert digest(Path(path))==sha
|
|
mask=m.qualified_mask(r);src=r/'current/_unresolved/FILE0084D1C5.dat';native=r/'current/world/minimaps/azeroth/map30_58.blp';neighbor=r/'current/world/minimaps/azeroth/map31_58.blp';candidate=r/'candidate/map30_58.blp'
|
|
assert m.replace_cells(src.read_bytes(),native.read_bytes(),mask)==candidate.read_bytes()
|
|
measured=m.verify(src,native,neighbor,candidate,mask);report=load(r/'report.json')
|
|
assert all(report[k]==v for k,v in measured.items())
|
|
rows=load(r/'candidate-assets.json');assert len(rows)==1 and rows[0]['file_data_id']==8704453
|
|
assert digest(candidate)==rows[0]['after_sha256'] and digest(src)==rows[0]['before_sha256']
|
|
core=load(r/'content-step/candidate/core.json');assert core['complete_root_inverse'] and {v['file_data_id']for v in core['assets']}=={8704453}
|
|
assert len(core['components'])==1 and core['components'][0]['type']=='root_selector'
|
|
parent=Path(load(r/'current-content-parent-path.json')[0]);before=(parent/'decoded/root.bin').read_bytes();after=(r/'content-step/candidate/decoded/root.bin').read_bytes()
|
|
component=load(r/'content-step/8704453.json');old=bytes.fromhex(component['original_decoded']['md5']);new=bytes.fromhex(component['after_decoded']['md5'])
|
|
assert len(before)==len(after) and before.count(old)==1 and before.replace(old,new)==after
|
|
rehearse(r)
|
|
print('Q102 frozen candidate: exact mask, exact donor/source pixels, native-native water edge and forward/inverse PASS')
|
|
if __name__=='__main__':main()
|