Files
myrzx_bot/tools/compare_swf_v3.py

57 lines
2.2 KiB
Python

"""Verify that the surgical SWF build changes only the selected ABC tags."""
import hashlib
import struct
from pathlib import Path
def tags(path: Path):
data = path.read_bytes()
if data[:3] != b"FWS" or struct.unpack_from("<I", data, 4)[0] != len(data):
raise ValueError(f"Invalid FWS header: {path}")
rect_bits = data[8] >> 3
offset = 8 + (5 + rect_bits * 4 + 7) // 8 + 4
result = []
while offset < len(data):
start = offset
header, = struct.unpack_from("<H", data, offset)
offset += 2
kind, length = header >> 6, header & 63
if length == 63:
length, = struct.unpack_from("<I", data, offset)
offset += 4
payload = data[offset:offset + length]
if len(payload) != length:
raise ValueError("Truncated SWF tag")
name = payload[4:].split(b"\0", 1)[0].decode("utf-8") if kind == 82 else None
offset += length
result.append((kind, name, start, hashlib.sha256(data[start:offset]).hexdigest()))
if kind == 0:
break
if offset != len(data):
raise ValueError("Unexpected trailing SWF data")
return result
def compare(original: Path, patched: Path, expected_positions: set[int]):
before, after = tags(original), tags(patched)
if len(before) != len(after):
raise AssertionError("SWF tag count changed")
changed = set()
for old, new in zip(before, after):
if old[:2] != new[:2]:
raise AssertionError("SWF tag identity/order changed")
if old[3] != new[3]:
changed.add(old[2])
print(f"Changed {old[1]} at original {old[2]}")
if changed != expected_positions:
raise AssertionError(f"Unexpected modified tags: {changed}")
print(f"PASS {patched.name}: only {len(changed)} intended ABC tags changed; {len(before) - len(changed)} tags identical")
if __name__ == "__main__":
root = Path(__file__).resolve().parent.parent / "analysis"
compare(root / "MYRMain.app", root / "MYRMain_damage_v3.app", {1520, 579859, 609465})
compare(root / "MYRMain.app", root / "MYRMain_stats_v3.app", {1520, 579859})
compare(root / "MYRDyd.swf", root / "MYRDyd_v3.swf", {413})