Repair
scm-js/plugin-repair, a default that starts
on, is the worked example for the "document" event's payload, document.sections's
defaults / rebuild / trailing / required, and tx.rebuildIsom. When a map opens
it reads the file the way the game does and, when something is wrong, lists it in a
dialog with a tick per finding; Tools ▸ Repair Map… runs the same check by hand, and
Check maps when they open in the dialog's footer turns the automatic one off.
chk.ts there is the container reader (the editor's own rules: a chunk whose length runs
past the file keeps what is there and is marked truncated, a negative length stops the
read and leaves the rest as trailing bytes), analyze.ts turns a chunk list plus what the
editor knows (sections.known(), sections.required(), sections.defaults("VCOD"),
terrain.checkIsom()) into findings — each with a level, a title, a note saying what the
game does with the file as it is, a Repair and whether it is ticked by default — and
repair.ts applies the byte-level repairs to a chunk list, resolving indices to chunk
objects first so removals never shift a later one. All three are pure and tested. The
findings cover the container (negative and truncated lengths, junk names, trailing bytes
— which are recovered as sections when that is what they are — repeats, sizes off the
registry's, stray record bytes), the sections a file of the revision must carry (a missing
MTXM is restored from TILE and a missing TILE copied from MTXM, everything else on
defaults), the header values (DIM, ERA's high bits, VER, TYPE, OWNR and SIDE), the
string table's offsets, unit records the game cannot place, a VCOD that is not StarEdit's,
the order of the sections, a blank TILE, and the ISOM (missing, wrongly sized or stale —
rebuild-isom). plugin.ts gathers the inputs, shows the dialog, and applies a repair in
three steps: the byte-level ones as one replaceFile, then sections.rebuild for the
names that asked for it, then one document.edit with tx.rebuildIsom. The bytes as the
map came in are kept in memory until the next map opens, and Restore original puts them
back through replaceFile.