Files
firestorm/MW4COMPARE/tools/extract-all.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

155 lines
6.2 KiB
Python

#!/usr/bin/env python3
"""
extract-all.py - unpack every *.mw4 under a resource root into a real directory tree.
python3 extract-all.py <resourceRoot> <outDir> [--no-merged]
Layout
------
<outDir>/<packagePathWithoutExtension>/<entryPath>
e.g. core.mw4 entry mechs\\atlas\\atlas.subsystems
-> <outDir>/core/mechs/atlas/atlas.subsystems
Missions/freezer.mw4 entry missions\\freezer\\freezer.contents
-> <outDir>/Missions/freezer/missions/freezer/freezer.contents
This is lossless: 842 entry paths are claimed by more than one package (281 of
them with different content - typically a mission packing its own copy of a
global props.mw4 asset), so a single flat tree cannot represent the data.
<outDir>/_merged/ is then built as a flattened view of the whole set using
HARDLINKS (no extra disk). It mirrors the layout of Gameleap/mw4/Content, which
makes it directly diffable against our source tree. Where two packages disagree,
precedence is props > core > textures > maps > missions and every conflict is
listed in _conflicts.tsv.
Entry-name qualifiers are preserved verbatim in the filename:
foo.data{gamemodel} foo.contents[joint_hip]{armature} bar.tga{hint}
All characters used by these packages are legal on Linux ('!' '#' '$' '%' '^'
appear in lobby skin names; no ':' or NUL).
"""
import sys, os, hashlib
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from mw4db import read_records, walk_packages
# Higher wins when the same entry path comes from several packages.
PRECEDENCE = {"props": 50, "core": 40, "textures": 30, "maps": 20, "missions": 10}
# Saved mechlab loadouts and pilot options name their records '{Mech}',
# '<chassis>{Subsystem}' etc. with no directory part, so they have no place in a
# Content-shaped tree - 797 variants would all collide on the same few names.
MERGE_EXCLUDE = ("variants/", "pilots/")
def in_merged(pkgrel):
return not pkgrel.lower().startswith(MERGE_EXCLUDE)
def precedence(pkgrel):
head = pkgrel.split("/")[0].lower()
if head.endswith(".mw4"):
head = head[:-4]
return PRECEDENCE.get(head, 0)
def entry_path(name):
"""Entry name -> relative filesystem path. Qualifiers kept as-is."""
p = name.replace("\\", "/").strip("/")
# Defensive: never let an entry escape the output directory.
parts = [seg for seg in p.split("/") if seg not in ("", ".", "..")]
return "/".join(parts) if parts else "_unnamed"
def main():
args = [a for a in sys.argv[1:] if not a.startswith("--")]
if len(args) != 2:
sys.exit(__doc__)
root, out = args
want_merged = "--no-merged" not in sys.argv
os.makedirs(out, exist_ok=True)
manifest = open(os.path.join(out, "_manifest.tsv"), "w", encoding="utf-8")
manifest.write("package\trecordId\tentryName\tdataLen\trecLen\tmd5\toutPath\n")
notes = open(os.path.join(out, "_notes.txt"), "w", encoding="utf-8")
# merged bookkeeping: mergedRelPath -> (precedence, pkgrel, md5, absSourceFile)
best = {}
seen_paths = {} # mergedRelPath -> {md5: [pkgrel, ...]}
total_files = total_bytes = 0
packages = 0
for path, pkgrel in walk_packages(root):
res = read_records(path)
if res is None:
notes.write(f"SKIP not-a-#VBD-package: {pkgrel}\n")
continue
_content, recs = res
packages += 1
pkgdir = os.path.join(out, pkgrel[:-4] if pkgrel.lower().endswith(".mw4") else pkgrel)
written = {} # relPath -> md5, for intra-package dupes
for rid, name, dlen, rlen, blob in recs:
rel = entry_path(name)
h = hashlib.md5(blob).hexdigest()
if rel in written: # duplicate entry name in one package
if written[rel] == h:
notes.write(f"DUP-IDENTICAL {pkgrel}\t{name}\trec{rid}\n")
continue
rel = f"{rel}#rec{rid}"
notes.write(f"DUP-DIFFERENT {pkgrel}\t{name}\trec{rid} -> {rel}\n")
written[rel] = h
dest = os.path.join(pkgdir, rel)
os.makedirs(os.path.dirname(dest), exist_ok=True)
with open(dest, "wb") as fh:
fh.write(blob)
total_files += 1
total_bytes += len(blob)
manifest.write(f"{pkgrel}\t{rid}\t{name}\t{dlen}\t{rlen}\t{h}\t"
f"{os.path.relpath(dest, out)}\n")
if want_merged and in_merged(pkgrel):
seen_paths.setdefault(rel, {}).setdefault(h, []).append(pkgrel)
pr = precedence(pkgrel)
cur = best.get(rel)
if cur is None or pr > cur[0]:
best[rel] = (pr, pkgrel, h, dest)
print(f" {pkgrel}: {len(recs)} records", flush=True)
manifest.close()
conflicts = 0
if want_merged:
mroot = os.path.join(out, "_merged")
with open(os.path.join(out, "_conflicts.tsv"), "w", encoding="utf-8") as cf:
cf.write("mergedPath\tchosenPackage\tallVersions\n")
for rel, (_pr, pkgrel, _h, src) in best.items():
dest = os.path.join(mroot, rel)
os.makedirs(os.path.dirname(dest), exist_ok=True)
if os.path.lexists(dest):
os.unlink(dest)
os.link(src, dest)
versions = seen_paths[rel]
if len(versions) > 1:
conflicts += 1
detail = "; ".join(f"{h[:8]}={','.join(ps)}" for h, ps in versions.items())
cf.write(f"{rel}\t{pkgrel}\t{detail}\n")
notes.write(f"\npackages={packages} files={total_files} bytes={total_bytes} "
f"mergedPaths={len(best)} conflicts={conflicts}\n")
notes.close()
print(f"\npackages : {packages}")
print(f"files : {total_files}")
print(f"bytes : {total_bytes/1e9:.2f} GB")
if want_merged:
print(f"merged : {len(best)} paths, {conflicts} with conflicting versions")
print(f"out : {out}")
if __name__ == "__main__":
main()