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

268 lines
11 KiB
Python

#!/usr/bin/env python3
"""
datamap.py - field map for the mech `<chassis>.data{GameModel}` record (1636 B).
Built from the engine headers, not from value matching.
Why: value matching alone cannot separate keys that hold the same value in every
mech. `dampenWorldJoint` and `fallAdjustmentSeconds` are both 0.5 everywhere, so
34 of the 80 numeric keys came out ambiguous, and ordering them by their
appearance in the source file assigned several of them wrongly - it put
`tiltSpeed` at 672, which is really `slopeDecel2`, and swapped
`percentageOfTurnToStartTilt` with `percentageOfSpeedToStartTilt`.
So the layout is taken from the declaration order in
Vehicle__GameModel mw4/Code/MW4/Vehicle.hpp
Mech__GameModel mw4/Code/MW4/Mech.hpp
and each block is anchored using the fields that value matching *did* resolve
unambiguously. Every anchor agrees:
Mech block base 756: footReturnSeconds 764, dampenTorsoJoint 800,
undampenRootJoint 816, undampenHipJoint 824,
scaleInternalTiltDegree 832
Vehicle block base 664: minSpeed 692, maxSpeed 696, acceleration 720,
decceleration 724, reverseAccelerationMultiplier 728
All fields in both blocks are 4-byte Scalar/Radian, so field i sits at
base + 4*i.
"""
import glob, math, os, re, struct, collections
REPO = "/home/rich/Repositories/firestorm/Gameleap"
SRC = REPO + "/mw4/Content/Mechs"
CODE = REPO + "/code"
REC = "/home/rich/Repositories/FS_Ours_extracted/core/mechs"
GAMEMODEL_SIZE = 1636
# The GameModel inheritance chain for a mech, base class first. Bases are NOT
# hardcoded: each block starts where the previous one ended. MWMover__GameModel
# is just `typedef Adept::Mover__GameModel` (MWMover.hpp:173), so it adds
# nothing. `Entity__GameModel` declares no base class and wraps its members in
# `#if NSWIZZLE`, which is never defined anywhere in the tree - the #else branch
# is live, and the two branches order their members DIFFERENTLY.
CHAIN = [
("Entity__GameModel", CODE + "/mw4/Libraries/Adept/Entity.hpp"),
("Mover__GameModel", CODE + "/mw4/Libraries/Adept/Mover.hpp"),
("MWObject__GameModel", CODE + "/mw4/Code/MW4/MWObject.hpp"),
("Vehicle__GameModel", CODE + "/mw4/Code/MW4/Vehicle.hpp"),
("Mech__GameModel", CODE + "/mw4/Code/MW4/Mech.hpp"),
]
# Independently measured block starts, used to check the computed chain.
ANCHORS = {"Vehicle__GameModel": 664, "Mech__GameModel": 756}
# The factories that convert authored degrees into stored radians.
FACTORIES = [CODE + "/mw4/Code/MW4/Mech_Tool.cpp", CODE + "/mw4/Code/MW4/Vehicle_Tool.cpp"]
SCALAR_TYPES = r'(?:Stuff::Scalar|Stuff::Radian|Stuff::Angle|float)'
DEG2RAD = math.pi / 180.0
MAX_STRING_LENGTH = 256 # Entity.hpp:199
# Member sizes. bool is ONE byte, not four -- getting this wrong shifts every
# field after the six m_canLoad* flags by exactly 16 bytes.
SIZES = {
"Scalar": 4, "Radian": 4, "Angle": 4, "float": 4, "int": 4, "unsigned": 4,
"DWORD": 4, "WORD": 2, "BYTE": 1, "bool": 1, "ResourceID": 4,
"Point3D": 12, "Vector3D": 12, "UnitQuaternion": 16, "RGBAColor": 16,
"Motion3D": 24, "LinearMatrix4D": 48, "char": 1,
"ClassID": 4, "ReplicatorID": 4, "FactoryRequest": 4, "ObjectID": 4,
}
MEMBER_RE = re.compile(
r'\b((?:Stuff::|Adept::)?(?:Scalar|Radian|Angle|Point3D|Vector3D|UnitQuaternion'
r'|RGBAColor|Motion3D|LinearMatrix4D|ResourceID)|(?:Stuff::)?(?:RegisteredClass::)?ClassID'
r'|ReplicatorID|ObjectID|(?:\w+::)?FactoryRequest|float|int|bool|BYTE|WORD|DWORD'
r'|unsigned|char)\s+([A-Za-z_][\w\s,\[\]]*?);', re.S)
def members(cls, path):
"""-> [(typeName, memberName, sizeInBytes)] in declaration order."""
txt = open(path, encoding="latin-1").read()
m = re.search(rf'class\s+{cls}\s*(?::[^{{;]*)?\{{', txt, re.S)
if not m:
return []
depth, i = 1, m.end() # brace-match; indentation is inconsistent between headers
while i < len(txt) and depth:
depth += (txt[i] == "{") - (txt[i] == "}")
i += 1
body = txt[m.end():i - 1]
ctor = body.find(f"{cls}(")
if ctor > 0:
body = body[:ctor]
body = re.sub(r'//[^\n]*', '', body)
# `typedef int AttributeID;` is not a member. This masked a missing ClassID
# for a while: two 4-byte errors that happened to cancel.
body = re.sub(r'\btypedef\b[^;]*;', '', body, flags=re.S)
body = re.sub(r'#\s*if\s+NSWIZZLE\b.*?#\s*else', '', body, flags=re.S)
body = re.sub(r'#\s*(endif|else|if\w*|ifdef|ifndef)[^\n]*', '', body)
out = []
for d in MEMBER_RE.finditer(body):
base = (d.group(1).replace("Stuff::", "").replace("Adept::", "")
.replace("RegisteredClass::", ""))
base = base.rsplit("::", 1)[-1]
for name in d.group(2).split(","):
name = name.strip()
arr = re.fullmatch(r'([A-Za-z_]\w*)\s*\[\s*([A-Za-z_]\w*|\d+)\s*\]', name)
if arr:
n = arr.group(2)
count = MAX_STRING_LENGTH if n == "MaxStringLength" else int(n)
out.append((base, arr.group(1), SIZES[base] * count))
elif re.fullmatch(r'[A-Za-z_]\w*', name):
out.append((base, name, SIZES[base]))
return out
def chain_layout(chain=None, anchors=None, start=0):
"""-> ({memberName: (offset, typeName, size)}, {className: baseOffset}).
Each block starts where the previous ended; /Zp4 means align = min(4, size).
Raises if a computed base contradicts an independently measured anchor.
Defaults to the mech chain; pass another for Torso/Engine and friends.
"""
chain = CHAIN if chain is None else chain
anchors = ANCHORS if anchors is None else anchors
fields, bases, off = {}, {}, start
for cls, path in chain:
off += (-off) % 4
bases[cls] = off
want = anchors.get(cls)
if want is not None and off != want:
raise AssertionError(f"{cls} computed at {off}, measured {want}")
for typ, name, size in members(cls, path):
off += (-off) % min(4, size)
fields.setdefault(name, (off, typ, size))
off += size
return fields, bases
def angle_fields():
"""Fields the tool factory scales by Radians_Per_Degree.
This cannot be inferred from the header: torsoHitSpringMotionLimit and its
siblings are declared plain Stuff::Scalar, yet Mech_Tool.cpp:889 stores
`model->torsoHitSpringMotionLimit * Radians_Per_Degree`. Only the writer
knows. Stuff::Radian fields (tiltSpeed, tiltDegree, topSpeedTurnRate,
fullStopTurnRate) are handled by their declared type as well.
"""
out = set()
for path in FACTORIES:
if not os.path.exists(path):
continue
txt = open(path, encoding="latin-1").read()
for m in re.finditer(r'model->(\w+)\s*=\s*model->\w+\s*\*\s*Radians_Per_Degree', txt):
out.add(norm(m.group(1)))
return out
def declared_fields(cls, path):
"""-> [(fieldName, isAngle)] in declaration order, scalars only.
isAngle marks Stuff::Radian / Stuff::Angle. Those are authored in DEGREES in
the .data but stored in RADIANS in the record, so they need a pi/180 factor
on the way in and 180/pi on the way back out.
"""
txt = open(path, encoding="latin-1").read()
m = re.search(rf'class\s+{cls}\s*:(.*?)^\t\t\}};', txt, re.S | re.M)
if not m:
return []
body = m.group(1)
ctor = body.find(f"{cls}(")
if ctor > 0:
body = body[:ctor]
body = re.sub(r'//[^\n]*', '', body)
out = []
for decl in re.finditer(rf'\b({SCALAR_TYPES})\s+([A-Za-z_][\w\s,]*?);', body, re.S):
angle = decl.group(1) in ("Stuff::Radian", "Stuff::Angle")
for name in decl.group(2).split(","):
n = name.strip()
if re.fullmatch(r'[A-Za-z_]\w*', n):
out.append((n, angle))
return out
def norm(name):
return re.sub(r'^m_', '', name).replace("_", "").lower()
def header_field_map():
"""normalised member name -> (offset, typeName, size, isAngle)."""
angles = angle_fields()
out = {}
for name, (off, typ, size) in chain_layout()[0].items():
key = norm(name)
out.setdefault(key, (off, typ, size, typ == "Radian" or key in angles))
return out
def gamedata(path):
txt = open(path, "rb").read().decode("latin-1")
# Shadow={...} contains a line reading "[shadow]"; without hiding braced
# blocks the page scan stops there and every later key is lost silently.
txt = re.sub(r'\{.*?\}',
lambda x: x.group(0).replace("\r", "").replace("\n", "\x01"),
txt, flags=re.S)
m = re.search(r'^\[GameData\]\r?\n(.*?)(?=^\[[A-Za-z]|\Z)', txt, re.M | re.S)
if not m:
return collections.OrderedDict()
kv = collections.OrderedDict()
for k, v in re.findall(r'^([A-Za-z0-9_]+)=([^\r\n]*)', m.group(1), re.M):
kv.setdefault(k, v.replace("\x01", "\n"))
return kv
def corpus():
"""-> [(chassis, {key: value}, gameModelBytes)] for our 64 mech chassis."""
out = []
for d in sorted(glob.glob(REC + "/*")):
ch = os.path.basename(d)
gm = [g for g in glob.glob(d + "/*.data{GameModel}")
if os.path.basename(g).lower().startswith(ch.lower() + ".data")]
if not gm:
continue
blob = open(gm[0], "rb").read()
if len(blob) != GAMEMODEL_SIZE:
continue
s = [p for p in glob.glob(SRC + "/*/*.data")
if os.path.basename(p).lower() == ch.lower() + ".data"]
if s:
out.append((ch, gamedata(s[0]), blob))
return out
def build():
"""-> (corpus, {sourceKey: (offset, typeName, size, isAngle)})"""
pairs = corpus()
hmap = header_field_map()
keys = collections.Counter()
for _c, kv, _b in pairs:
keys.update(kv.keys())
return pairs, {k: hmap[norm(k)] for k in keys if norm(k) in hmap}
FLOATS = {"Scalar", "Radian", "Angle", "float"}
VECTORS = {"Point3D": 3, "Vector3D": 3, "RGBAColor": 4, "UnitQuaternion": 4}
def read(blob, off, typ="Scalar", size=4, angle=False):
"""Decoded value in the units and form the .data source uses."""
if typ in FLOATS:
v = struct.unpack_from("<f", blob, off)[0]
return v / DEG2RAD if angle else v
if typ in VECTORS:
return list(struct.unpack_from(f"<{VECTORS[typ]}f", blob, off))
if typ == "char":
return blob[off:off + size].split(b"\0")[0].decode("latin-1")
if typ in ("bool", "BYTE"):
return blob[off]
if typ == "WORD":
return struct.unpack_from("<H", blob, off)[0]
if typ == "ResourceID":
return struct.unpack_from("<I", blob, off)[0]
return struct.unpack_from("<i", blob, off)[0]
if __name__ == "__main__":
pairs, field_map = build()
print(f"chassis: {len(pairs)} mapped keys: {len(field_map)}")
for k, (off, typ, size, angle) in sorted(field_map.items(), key=lambda kv: kv[1][0]):
note = " [degrees]" if angle else ""
print(f" {off:5d} {typ:14s} {k}{note}")