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

176 lines
7.2 KiB
Python

#!/usr/bin/env python3
"""
armature.py - rebuild a mech's .armature source from its packed records.
python3 armature.py <mechRecordDir> [-o out.armature]
python3 armature.py --verify # check against all 65 known chassis
The packer merges <mech>.armature into <mech>.contents (via `!include=`) and
then, for every contents page that has Child= entries, emits two records
(MWMover_Tool.cpp ~78):
<mech>.contents[<joint>]{sites} children whose name starts 'site_'
but is not 'site_eye*' - stored as
YawPitchRoll + Point3D + name
<mech>.contents[<joint>]{armature} every other child, as a CreateMessage
carrying jointName + localToParent
Between them those two records hold every page of the original .armature.
Verified on Atlas: 61 of 61 page names recovered, values exact.
Rotation notes
--------------
* {sites} stores YawPitchRoll directly, so those angles are exact.
* Joint messages only carry a matrix. Across all 65 chassis, 1348 of 1453 joint
matrices are the identity, i.e. `Rotation=0 0 0`.
* site_lfoot / site_rfoot appear in BOTH streams - a deliberate hack in
MWMover_Tool.cpp ("Jerry this will get deleted when you fix your foot
problem") - and their matrix is the identity while the real angle is in the
{sites} record. The {sites} value always wins.
* site_eye* goes only to the armature stream, so its angle comes from the
matrix.
"""
import sys, os, glob, math, struct, re, argparse, collections
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import mw4msg
OUR_MECH_SOURCE = "/home/rich/Repositories/firestorm/Gameleap/mw4/Content/Mechs"
OUR_RECORDS = "/home/rich/Repositories/FS_Ours_extracted/core/mechs"
REC_RE = re.compile(r'^(?P<chassis>.+?)\.contents\[(?P<parent>[^\]]+)\]\{(?P<kind>armature|sites)\}$',
re.I)
def ypr_from_matrix(r):
"""3x3 -> the (a, b, c) triple as written in Rotation=, in degrees.
Stuff's YawPitchRoll writes the X-axis angle in the middle slot; confirmed
against kodiak/site_eyepoint (matrix gives 0.0688 deg, source says
0.069104).
"""
m00, m01, m02, m10, m11, m12, m20, m21, m22 = r
pitch = math.asin(max(-1.0, min(1.0, m21)))
if abs(m21) < 0.999999:
yaw = math.atan2(-m20, m22)
roll = math.atan2(-m01, m11)
else:
yaw = math.atan2(m02, m00)
roll = 0.0
return (math.degrees(yaw), math.degrees(pitch), math.degrees(roll))
def is_identity(r, eps=1e-5):
ident = (1, 0, 0, 0, 1, 0, 0, 0, 1)
return all(abs(a - b) < eps for a, b in zip(r, ident))
def rebuild(mech_dir):
"""-> (chassisName, entries, children)
entries list of {'name','parent','rot','trans'} - one per armature page.
Keyed on (parent, name), not name alone: Victor legitimately has
two different [site_lshellport] pages hanging off different joints.
children {parentName: [childName, ...]} preserving order and multiplicity.
"""
entries = [] # one dict per armature page occurrence
children = collections.defaultdict(list)
site_children = collections.defaultdict(set)
chassis = None
# {sites} first: site_lfoot/site_rfoot are written to BOTH streams and only
# the {sites} copy carries their real angle.
records = (sorted(glob.glob(os.path.join(mech_dir, "*{sites}"))) +
sorted(glob.glob(os.path.join(mech_dir, "*{armature}"))))
for path in records:
m = REC_RE.match(os.path.basename(path))
if not m:
continue
chassis = chassis or m.group("chassis")
parent, kind = m.group("parent"), m.group("kind").lower()
with open(path, "rb") as fh:
data = fh.read()
children[parent] # ensure the parent exists
if kind == "sites":
for name, rot, trans in mw4msg.read_sites(data):
entries.append({
"name": name, "parent": parent,
"rot": tuple(math.degrees(a) for a in rot), "trans": trans})
children[parent].append(name)
site_children[parent].add(name)
else:
for _off, msg in mw4msg.walk(data):
name = mw4msg.joint_name(msg)
if not name:
continue
# site_lfoot/site_rfoot are deliberately written to both streams;
# counting the armature copy too would double them.
if name in site_children[parent]:
continue
children[parent].append(name)
r = mw4msg.rotation3x3(msg)
entries.append({
"name": name, "parent": parent,
"rot": (0.0, 0.0, 0.0) if is_identity(r) else ypr_from_matrix(r),
"trans": mw4msg.translation(msg)})
return chassis, entries, children
def emit(entries, children):
"""Render as .armature text, children before the joint that owns them."""
by_name = {}
for e in entries:
by_name.setdefault(e["name"], []).append(e)
out, done = [], set()
def visit(name):
if name in done:
return
done.add(name)
for c in children.get(name, ()):
visit(c)
for e in by_name.get(name, [{"rot": (0.0, 0.0, 0.0), "trans": (0.0, 0.0, 0.0)}]):
out.append(f"[{name}]")
out.append("Rotation=%.6f %.6f %.6f" % (e["rot"] or (0.0, 0.0, 0.0)))
out.append("Translation=%.6f %.6f %.6f" % (e["trans"] or (0.0, 0.0, 0.0)))
for c in children.get(name, ()):
out.append(f"Child={c}")
out.append("")
all_children = {c for lst in children.values() for c in lst}
for name in list(children) + list(by_name):
if name not in all_children:
visit(name)
for name in list(children) + list(by_name):
visit(name)
return "\r\n".join(out) + "\r\n"
def main():
ap = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("mechdir", nargs="?", help="directory of packed records for one mech")
ap.add_argument("-o", "--out", help="write .armature here instead of stdout")
ap.add_argument("--verify", action="store_true",
help="rebuild all 65 known chassis and diff against their source")
args = ap.parse_args()
if args.verify:
here = os.path.dirname(os.path.abspath(__file__))
os.execvp("python3", ["python3", os.path.join(here, "verify_armature.py")])
if not args.mechdir:
ap.error("give a mech record directory, or --verify")
chassis, entries, children = rebuild(args.mechdir)
text = emit(entries, children)
if args.out:
os.makedirs(os.path.dirname(args.out), exist_ok=True)
open(args.out, "wb").write(text.encode("latin-1"))
print(f"{chassis}: {len(entries)} pages -> {args.out}")
else:
sys.stdout.write(text)
if __name__ == "__main__":
main()