58 lines
3.4 KiB
Python
58 lines
3.4 KiB
Python
"""Exercise native CFM3 decoding, seeking, malformed input and fallback assets."""
|
|
from pathlib import Path
|
|
import hashlib
|
|
import ctypes as C
|
|
import struct
|
|
import subprocess
|
|
import numpy as np
|
|
from PIL import Image
|
|
from check_artifacts import toolchain
|
|
|
|
ROOT=Path(__file__).resolve().parents[2]
|
|
OUT=ROOT/'build/cinematic-v3'
|
|
|
|
|
|
def main():
|
|
fixtures=OUT/'fixtures';fixtures.mkdir(parents=True,exist_ok=True)
|
|
(fixtures/'short.cfm').write_bytes(b'CFM3')
|
|
header=struct.pack('<4s7I',b'CFM3',3,30,600,640,480,640*480*2,640*480*2+4096)
|
|
index=b''.join(struct.pack('<2I',32+600*8+i*2,2) for i in range(600))
|
|
(fixtures/'bad-chunk.cfm').write_bytes(header+index+b'\0\0'*600)
|
|
# A golden solid-color image encoded by Windows, then decoded by shipping ASM.
|
|
api=C.WinDLL('ntdll')
|
|
work_size=C.c_ulong();fragment_size=C.c_ulong();count=C.c_ulong()
|
|
assert api.RtlGetCompressionWorkSpaceSize(2,C.byref(work_size),C.byref(fragment_size))==0
|
|
work=C.create_string_buffer(work_size.value)
|
|
raw=struct.pack('<3H',0xF800,0x07E0,0x001F)*(640*480//3)
|
|
source=C.create_string_buffer(raw);compressed=C.create_string_buffer(len(raw)+4096)
|
|
assert api.RtlCompressBuffer(2,source,len(raw),compressed,len(compressed),4096,C.byref(count),work)==0
|
|
chunk=compressed.raw[:count.value]
|
|
valid_index=b''.join(struct.pack('<2I',32+600*8+i*len(chunk),len(chunk)) for i in range(600))
|
|
(fixtures/'valid.cfm').write_bytes(header+valid_index+chunk*600)
|
|
# Every actual AE frame is compared with the image reconstructed by MASM.
|
|
files=sorted((OUT/'render').glob('film_*.png'))
|
|
assert len(files)==600,len(files)
|
|
sums=[]
|
|
for file in files:
|
|
rgb=np.asarray(Image.open(file).convert('RGB'),dtype=np.uint32)
|
|
red=rgb[:,:,0]>>3;green=rgb[:,:,1]>>2;blue=rgb[:,:,2]>>3
|
|
pixels=(((red<<3)|(red>>2))<<16)|(((green<<2)|(green>>4))<<8)|((blue<<3)|(blue>>2))
|
|
sums.append(int(pixels.sum(dtype=np.uint64))&0xFFFFFFFF)
|
|
(fixtures/'expected-sums.bin').write_bytes(struct.pack('<600I',*sums))
|
|
before={p:hashlib.sha256(p.read_bytes()).digest() for p in (ROOT/'runtime').rglob('*') if p.is_file()}
|
|
source=(ROOT/'poker.asm').read_text(encoding='cp1251')
|
|
source=source.replace('main PROC','include tools\\tests\\cinematic_master_probe.inc\nmain PROC\n call RunCinematicMasterProbe\n invoke ExitProcess,eax',1)
|
|
(OUT/'master-probe.asm').write_bytes(source.encode('cp1251'))
|
|
ml,link=toolchain()
|
|
sdk=Path('C:/Program Files (x86)/Windows Kits/10/Lib')
|
|
lib=next(p/'um/x86' for p in sorted(sdk.iterdir(),reverse=True) if (p/'um/x86/kernel32.lib').exists())
|
|
for command in [[ml,'/c','/coff','/Cp','/nologo','/I.','/Fobuild/cinematic-v3/master-probe.obj','build/cinematic-v3/master-probe.asm'],
|
|
[link,'/nologo','/SUBSYSTEM:CONSOLE','/LIBPATH:'+str(lib),'/OUT:build/cinematic-v3/master-probe.exe','build/cinematic-v3/master-probe.obj','kernel32.lib','winmm.lib','user32.lib','ws2_32.lib','gdi32.lib'],
|
|
[OUT/'master-probe.exe']]:
|
|
subprocess.run([str(x) for x in command],cwd=ROOT,check=True,timeout=120)
|
|
assert before=={p:hashlib.sha256(p.read_bytes()).digest() for p in (ROOT/'runtime').rglob('*') if p.is_file()}
|
|
print('PASS: all 600 AE frames reconstructed by native MASM; seeking, cache, bounds, missing/truncated/corrupt files, fallback and cleanup; saves/bank/RNG unchanged.')
|
|
|
|
|
|
if __name__=='__main__':
|
|
main()
|