Files
firestorm/MW4COMPARE/tools/decompile/verify_subsystems.py
T
83478b7666 Add MW4COMPARE: .mw4 decompiler toolchain and V4H comparison harness
Tooling built to recover editable source for six 'Mech chassis that exist
in the parallel FS_Build_V4H build but not in this repo. Reverse-engineers
every compiled record type in the .mw4 package format back to the .data /
.instance / .subsystems / .damage / .contents / .torso / .engine /
.armature sources the content pipeline consumes.

Nothing here is wired into the game build. It is a standalone analysis
harness run from Linux.

Package format
--------------
"#VBD" container. Directory records are [len][name][FILETIME][origSize]
[storedSize][offset], payload base at dword 0x0C. A record is stored raw
when storedSize == origSize, otherwise LZW (9->12-bit LSB-first codes,
256=clear, 257=EOF, dict from 258), per Database.cpp:451.

GameModel records are flat /Zp4 structs following the C++ inheritance
chain Entity(0) -> Mover(28) -> MWObject(80) -> Vehicle(664) -> Mech(756),
1636 bytes total. CreateMessage records follow Replicator -> Entity ->
Mover -> MWMover -> MWObject -> Vehicle -> Mech from start=16 (the
undeclared Connection__Message header), ending at 341 and padded to 344.

tools/decompile/
----------------
  datamap.py        header-driven layout engine; CHAIN + ANCHORS
                    {Vehicle:664, Mech:756} assert the struct offsets
  mw4msg.py         CreateMessage reader/walker
  data.py           .data      constants.py  define/table symbol resolution
  damage.py         .damage    contents.py   .contents
  smallmodel.py     .torso + .engine         instance.py  .instance
  armature.py / armature_parts.py  .armature + armaturedata/armaturevideo
  assembly.py       joint hierarchy renderer
  make_generic_doll.py  builds generic MFD/Radar damage dolls
  verify_*.py       per-type round-trip verifiers

Verified round-trip across all 64 shared chassis:
  .armature      2938/2976 pages     .subsystems  7579/7585 keys
  .data map      6071/6071 values    .data trip   8291/8306 keys
  .damage        6605/6605 keys      .contents    7480/7480 keys
  .torso+.engine 1280/1280 keys      .instance     896/896 keys, 64/64 pages
  armature_parts 1202/1202 .data, 1149/1202 .video

Layout-discovery lessons (documented in DECOMPILING.md)
-------------------------------------------------------
- Never let a field map be discovered by the values that verify it. A
  value-matching pass reported 4288/4288 while mis-assigning 34 keys. The
  map was rebuilt from header declaration order, anchored on uniquely
  resolved fields.
- Read the factory, not the data. 12 .data fields and 5 Torso fields are
  declared plain Stuff::Scalar but multiplied by Radians_Per_Degree in
  Mech_Tool.cpp:889 / Torso_Tool.cpp.
- Strip typedefs before walking a header. A stray `typedef int AttributeID;`
  masked a missing ClassID - two 4-byte errors cancelling out, caught only
  by the ANCHORS assertion.
- A verifier that silently narrows its own input reports success. Braced
  blocks must be hidden before splitting pages, replacing both CR and LF,
  because a `Shadow={...}` block contains a line reading `[shadow]` and
  splitlines() also splits on bare CR.
- NSWIZZLE is undefined, so the #else branch is live and orders members
  differently. bool is 1 byte; char x[MaxStringLength] is 256.
- V4H carries stale Mech IDs (their Atlas is 5, ours 6), so 64 of 65 shared
  chassis are off by one; --retarget-ids emits $(M_<Chassis>)/$(IDS_<Chassis>).

reports/ holds generated diffs. The two ~5 MB manifest-*.tsv intermediates
are gitignored; regenerate everything with run-comparison.sh.

Co-authored-by: Claude Opus 5 (Anthropic) <noreply@anthropic.com>
Co-authored-by: GitHub Copilot <copilot@github.com>
2026-08-08 16:47:57 -05:00

128 lines
4.9 KiB
Python

#!/usr/bin/env python3
"""Verification harness for the .subsystems decompiler.
Rebuilds every chassis from our own packed records and compares the resulting
key/value pairs, page by page, against the known source .subsystems.
Page names are ignored - the packer does not store them (see subsystems.py), so
only order and content can be verified.
Chassis whose source has moved on since the package was built are reported
separately rather than counted as failures; core.mw4 has not been repacked since
the initial mirror, so battlemaster and battlemaster2c legitimately differ.
python3 verify_subsystems.py
"""
import sys, os, glob, re, collections
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import subsystems
def source_pages(path):
"""Parse a source .subsystems leniently.
The runtime NotationFile accepts a page header with no closing bracket -
hellspawn had '[HeatSink10' and sunder '[HeatSink16' - so match on a leading
'[' rather than a full bracketed pattern.
GroupIndex may appear several times in one page, so values are collected as
lists rather than overwritten.
"""
txt = open(path, "rb").read().decode("latin-1")
out = []
for m in re.finditer(r'^\[([^\r\n\]]*)\]?[ \t]*\r?\n(.*?)(?=^\[|\Z)', txt, re.M | re.S):
kv = collections.OrderedDict()
for k, v in re.findall(r'^([A-Za-z0-9_]+)=([^\r\n]*)', m.group(2), re.M):
if k in kv:
kv[k] = (kv[k] if isinstance(kv[k], list) else [kv[k]]) + [v]
else:
kv[k] = v
out.append((m.group(1), kv))
return out
def norm(key, value):
"""Compare as a sorted list so a single value and a one-element list match."""
if not isinstance(value, list):
value = [value]
return sorted(_norm1(key, v) for v in value)
def _norm1(key, value):
v = str(value).strip()
if key in ("Model", "Site", "EjectSite", "InternalLocation", "ExecutionState",
"ArmorType", "InternalType"):
return v.lower().replace("\\", "/")
try:
return f"{float(v):.4g}"
except ValueError:
return v.lower()
def main():
manifest = subsystems.load_manifest(subsystems.OUR_MANIFEST)
ppt = subsystems.armor_points_per_ton()
srcs = {os.path.basename(p)[:-len(".subsystems")].lower(): p
for p in glob.glob(subsystems.OUR_MECH_SOURCE + "/*/*.subsystems")}
t = collections.Counter()
mismatched_keys = collections.Counter()
stale, examples = [], []
for rec in sorted(glob.glob(subsystems.OUR_RECORDS + "/*/*.subsystems")):
chassis = os.path.basename(rec)[:-len(".subsystems")].lower()
if chassis not in srcs:
continue
src = source_pages(srcs[chassis])
got = subsystems.decompile(rec, manifest, ppt)
if len(src) != len(got):
stale.append((chassis, len(got), len(src)))
continue
t["chassis"] += 1
for (_sname, skv), (_gname, gkv) in zip(src, got):
t["pages"] += 1
page_ok = True
for key, sval in skv.items():
if key in ("SubsystemIndex",): # not emitted; defaults to 0
continue
# An explicit "=0" and an omitted key are identical to the packer.
if gkv.get(key) is None and str(sval).strip() in ("0", "0.0"):
continue
t["keys"] += 1
gval = gkv.get(key)
if gval is not None and norm(key, gval) == norm(key, sval):
t["keys_ok"] += 1
else:
page_ok = False
mismatched_keys[key] += 1
if len(examples) < 12:
examples.append((chassis, key, sval, gval))
extra = [k for k in gkv if k not in skv]
if extra:
page_ok = False
for k in extra:
mismatched_keys["EXTRA:" + k] += 1
t["pages_ok"] += page_ok
print(f"chassis verified : {t['chassis']}")
print(f"pages compared : {t['pages']} fully exact: {t['pages_ok']}")
print(f"keys compared : {t['keys']} exact: {t['keys_ok']}"
f" wrong: {t['keys'] - t['keys_ok']}")
if mismatched_keys:
print("\nmismatches by key:")
for k, v in mismatched_keys.most_common(12):
print(f" {k:22s} x{v}")
if examples:
print("\nexamples (chassis, key, source, decompiled):")
for e in examples:
print(f" {e[0]:14s} {e[1]:18s} src={e[2]!r:28s} got={e[3]!r}")
if stale:
print("\nskipped - source has moved on since the package was built:")
for ch, g, s in stale:
print(f" {ch:16s} messages={g} sourcePages={s}")
if __name__ == "__main__":
main()