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

125 lines
3.9 KiB
Python

#!/usr/bin/env python3
"""
mw4msg.py - reader for the GameOS "CreateMessage" streams found inside .mw4
records (.subsystems, [joint_*]{armature}, .contents, .instance, ...).
Layout, derived from the engine source rather than guessed:
MWObject::CreateSubsystemStream / CreateArmatureStream (MWObject_Tool.cpp)
WORD span number of replicator IDs consumed
N x CreateMessage one per [Page] that was serialised
Each message is a plain C struct, /Zp4, built by a per-class factory in one of
the 51 mw4/Code/MW4/*_Tool.cpp files. The common prefix is:
off 0 u32 messageLength Connection__Message
off 4 u32 messageID
off 8 u32 priority
off 12 u32 messageFlags
off 16 u32 classID Replicator__CreateMessage
off 20 u32 replicatorFlags
off 24 u32 replicatorID
off 28 12f localToParent Entity__CreateMessage (LinearMatrix4D)
off 76 u32 executionState
off 80 f32 initialAge
off 84 u32 dataListID (ResourceID; record id is the HIGH word)
off 88 u32 alignment
off 92 u32 nameID
Mover__CreateMessage then adds two 24-byte Motion3D fields (96, 120), and
MWMover__CreateMessage adds:
off 144 u32 siteStreamResourceID
off 148 u32 armatureStreamResourceID
off 152 char jointName[128]
Verified: a joint message is exactly 280 bytes, which is 152 + 128.
localToParent is 3 rows of 4 floats; the rotation is columns 0..2 and the
translation is column 3 of each row. Checked against every joint of all 65
chassis: 1453/1453 translations matched the source .armature exactly.
"""
import struct
HDR_LEN = 0
HDR_CLASSID = 16
HDR_REPLICATORID = 24
ENT_MATRIX = 28
ENT_EXECSTATE = 76
ENT_INITIALAGE = 80
ENT_DATALISTID = 84
ENT_ALIGNMENT = 88
ENT_NAMEID = 92
MWMOVER_SITEID = 144
MWMOVER_ARMID = 148
MWMOVER_JOINTNAME = 152
def walk(data, start=2):
"""Yield (offset, messageBytes) for each message in a span-prefixed stream."""
off = start
while off + 4 <= len(data):
length, = struct.unpack_from("<I", data, off)
if length < 16 or off + length > len(data):
raise ValueError(f"bad messageLength {length} at offset {off}")
yield off, data[off:off + length]
off += length
if off != len(data):
raise ValueError(f"trailing {len(data) - off} bytes")
def span(data):
return struct.unpack_from("<H", data, 0)[0]
def u32(msg, off):
return struct.unpack_from("<I", msg, off)[0]
def class_id(msg):
return u32(msg, HDR_CLASSID)
def record_id(msg, off=ENT_DATALISTID):
"""ResourceID packs the package record id in its high word."""
return u32(msg, off) >> 16
def matrix(msg):
return struct.unpack_from("<12f", msg, ENT_MATRIX)
def translation(msg):
m = matrix(msg)
return (m[3], m[7], m[11])
def rotation3x3(msg):
m = matrix(msg)
return (m[0], m[1], m[2], m[4], m[5], m[6], m[8], m[9], m[10])
def joint_name(msg):
if len(msg) <= MWMOVER_JOINTNAME:
return None
return msg[MWMOVER_JOINTNAME:].split(b"\0")[0].decode("latin-1")
def read_sites(data):
"""{sites} record: repeated [YawPitchRoll 3f][Point3D 3f][u32 len][name][NUL].
Written by MWMover__CreateMessage::ConstructCreateMessage (MWMover_Tool.cpp
~145): `site_stream << rotation; << translation; << site_name;`
Angles are radians.
"""
out, off = [], 0
while off + 28 <= len(data):
rx, ry, rz, tx, ty, tz = struct.unpack_from("<6f", data, off)
off += 24
n, = struct.unpack_from("<I", data, off)
off += 4
name = data[off:off + n].decode("latin-1")
off += n + 1 # names are length-prefixed AND NUL-terminated
out.append((name, (rx, ry, rz), (tx, ty, tz)))
return out