Files
firestorm/MW4COMPARE/tools/decompile/armature_parts.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

226 lines
6.8 KiB
Python

#!/usr/bin/env python3
"""
armature_parts.py - generate a mech's `armaturedata/` and `armaturevideo/` files.
Every joint in a mech's `.contents` points at `armaturedata\\<part>.data`, which
in turn points at `armaturevideo\\<part>.video`, which names the geometry. Both
are packed as compiled records -- the `.data` becomes a 12-byte stub and the
`.video` becomes a binary renderer graph -- so neither survives extraction as
text and both have to be regenerated.
They are formulaic. Measured over the 89 shipped mechs:
* `armaturedata/<part>.data` -- **one single shape** across all 1336 files,
parameterised only by the part name.
* `armaturevideo/<part>.video` -- three shapes cover 91% of 1354 files:
with a damaged LOD, without one, and with a running-lights block.
Rules, and their measured accuracy:
* A `DAMLOD` block iff `<part>_dam.erf` exists next to the mech. This holds for
1327 of 1354 shipped files. **All 27 exceptions run the same way**: the
`_dam.erf` exists but the author's `.video` ignores it. No shipped `.video`
ever references a `_dam.erf` that is absent, so generating by this rule is
safe by construction -- it can never produce a dangling reference, only
occasionally more damage-state switching than the original author chose.
The exceptions are mostly `_gun` and `_toe` parts and follow no clean rule.
* A running-lights block iff the part is the torso and a running-lights `.erf`
is present. 68 of the 71 torsos carry it; almost nothing else does.
python3 armature_parts.py <mech-source-dir>
python3 armature_parts.py --verify
"""
import argparse, os, re, sys, collections
DATA_TEMPLATE = """[gamedata]
Class=MechWarrior4::MWMover
[renderers]
VideoRenderer=armaturevideo\\{part}.video
"""
LIGHT_BLOCK = """[lightlod]
Type=ShapeComponent
Geometry={lights}
Unique=true
[lightwatcher]
Type=AttributeWatcherOfInt
Attribute=IsDark
SimulationShouldExecute=1
[lightswitch]
Type=SwitchComponent
Input=LightWatcher
Child=LightLOD
"""
LOD_BLOCK = """[lod]
Type=ShapeComponent
Geometry={part}.erf
Unique=true
"""
DAM_BLOCK = """[damlod]
Type=ShapeComponent
Geometry={part}_DAM.erf
Unique=true
"""
# joint_cage has its own shape: two empty group components, then the cage
# geometry twice (intact and destroyed), shadow-disabled.
CAGE_TEMPLATE = """[fake1]
Type=GroupComponent
[fake2]
Type=GroupComponent
[cage]
Type=ShapeComponent
Geometry={cage}
Unique=true
DisableShadow=true
[destroyedcage]
Type=ShapeComponent
Geometry={cage}
Unique=true
DisableShadow=true
[watcher]
Type=AttributeWatcherOfInt
Attribute=VisualRepresentation
SimulationShouldExecute=1
[damageappearance]
Type=SwitchComponent
Input=Watcher
Child=Fake1
Child=Fake2
Child=Cage
Child=DestroyedCage
[locator]
Child=DamageAppearance
"""
def parts_of(mech_dir):
"""Part names referenced as armaturedata\\<part>.data by the .contents."""
out = []
for fn in os.listdir(mech_dir):
if not fn.lower().endswith(".contents"):
continue
t = open(os.path.join(mech_dir, fn), "rb").read().decode("latin-1")
for m in re.finditer(r'armaturedata[\\/]([A-Za-z0-9_\-]+)\.data', t, re.I):
if m.group(1) not in out:
out.append(m.group(1))
return out
def video_text(mech_dir, part):
files = {f.lower(): f for f in os.listdir(mech_dir)}
cage = next((files[f] for f in files if f.endswith("_cage.erf")), None)
if part.lower() == "joint_cage" and cage:
return CAGE_TEMPLATE.format(cage=cage)
has_dam = f"{part.lower()}_dam.erf" in files
lights = next((files[f] for f in files
if f.startswith("runninglights") and f.endswith(".erf")), None)
is_torso = part.lower().endswith("torso")
text = ""
if lights and is_torso:
text += LIGHT_BLOCK.format(lights=lights)
text += LOD_BLOCK.format(part=part)
if has_dam:
text += DAM_BLOCK.format(part=part)
text += """[watcher]
Type=AttributeWatcherOfInt
Attribute=VisualRepresentation
SimulationShouldExecute=1
[damageappearance]
Type=SwitchComponent
Input=Watcher
Child=LOD
"""
if has_dam:
text += "Child=DAMLOD\n"
text += "\n[locator]\n"
if lights and is_torso:
text += "Child=LightSwitch\n"
text += "Child=DamageAppearance\n"
return text
def generate(mech_dir, write=True):
"""-> {relativePath: text} for every part."""
out = {}
for part in parts_of(mech_dir):
out[f"armaturedata/{part}.data"] = DATA_TEMPLATE.format(part=part)
out[f"armaturevideo/{part}.video"] = video_text(mech_dir, part)
if write:
for rel, text in out.items():
p = os.path.join(mech_dir, rel)
os.makedirs(os.path.dirname(p), exist_ok=True)
with open(p, "wb") as fh:
fh.write(text.replace("\n", "\r\n").encode("latin-1"))
return out
def norm(text, part):
t = re.sub(r'//[^\n]*', '', text).replace("\r", "")
t = re.sub(re.escape(part), "PART", t, flags=re.I)
return re.sub(r'\n+', "\n", t).strip().lower()
def verify():
import glob
MECHS = "/home/rich/Repositories/firestorm/Gameleap/mw4/Content/Mechs"
t = collections.Counter()
diffs = collections.Counter()
for d in sorted(glob.glob(MECHS + "/*")):
if not os.path.isdir(d) or not glob.glob(d + "/armaturedata/*.data"):
continue
gen = generate(d, write=False)
for rel, text in gen.items():
p = os.path.join(d, rel)
part = os.path.basename(rel).rsplit(".", 1)[0]
kind = rel.split("/")[0]
if not os.path.exists(p):
t[kind + " absent"] += 1
continue
t[kind] += 1
want = open(p, "rb").read().decode("latin-1")
if norm(want, part) == norm(text, part):
t[kind + " ok"] += 1
else:
diffs[kind] += 1
print(f"armaturedata : {t['armaturedata ok']}/{t['armaturedata']} reproduced")
print(f"armaturevideo: {t['armaturevideo ok']}/{t['armaturevideo']} reproduced")
if t["armaturedata absent"] or t["armaturevideo absent"]:
print(f" referenced but absent on disk: "
f"{t['armaturedata absent']} data, {t['armaturevideo absent']} video")
def main():
ap = argparse.ArgumentParser()
ap.add_argument("mech_dir", nargs="?")
ap.add_argument("--verify", action="store_true")
args = ap.parse_args()
if args.verify:
verify()
return
if not args.mech_dir:
ap.error("mech_dir required")
out = generate(args.mech_dir)
print(f"wrote {len(out)} files into {args.mech_dir}")
if __name__ == "__main__":
main()