WP-126: A player’s save rewritten while it loads
Status: ✅ Done (2026-11-30). Fork PR barefootbetters/scummvm#51 merged on 2026-11-30 (merge commit 5f76c9a955); checked on Windows and the TCL. Closed on Jeff’s word (2026-11-30) with criteria 2 (its second half) and 4 not run: a later room’s write, on the unchanged write path (§Result).
Phase: 5 (polish & distribution)
Depends on: none. Found during WP-118’s
3 m check, but independent of its code.
Companion EC: none (inline checklist under Exit criteria)
Pre-flight: pre-flight-WP-126-2026-11-30.
Required: the packet modifies engines/terraglyph/**.
Targets generation(s): Gen 1, both games (the save code is shared).
Estimated effort: Two hours, including the TV run.
Goal
Loading a player’s own save never crashes, whatever the bytes of the save.
Background
- Jeff, 2026-11-30, on the TCL, build 26041040: “the game crashed on the smart tv after the sign in screen”. He had turned on Showdown’s hotspots in Game Options just before.
- The crash (the TV’s crash log):
Assertion failure: 'newPos >= 0' in common/compression/zlib.cpp:278 (virtual bool Common::GZipReadStream::seek(int64, int)), on the engine thread. The backtrace stops at the assert. - The timing (the app’s own log, and
statof the save):- Showdown’s slot 3 (the player STEST) was written at 09:38:32.217.
- The assertion fired at 09:38:32.223, 6 ms later.
- The three saves are sound: each decompresses, and its trailer points
at the
SVMCextended header that follows the profile text.
What the code does
- Signing in as an existing player loads their save:
TerraglyphEngine::loadGameState()signs them in (_playerSlot), then callsEngine::loadGameState(). Engine::loadGameState()opens the slot for reading and callsloadGameStream(). That callsloadProfile(), which shows the profile’s start room (showRoom()), all inside the load.- The room’s entry writes the signed-in player’s profile when it isn’t a
menu room (
terraglyph.cpp, “The signed-in player’s profile is written as each game room starts”). That rewrites the very slot the load has open. - Back in
Engine::loadGameState(),MetaEngine::readSavegameHeader()seeks 4 bytes back from the end, using the size the old file’s gzip trailer gave. It reads the header offset from the rewritten bytes into anintand seeks there. A word with its top bit set is negative, passes the sanity check (headerOffset >= pos), and trips the assert.
Whether it crashes depends on the bytes, which is why it took until now:
- The TV: it crashed.
- Windows: the same three saves, the same settings and
--save-slot=3loaded and rewrote slot 3 without a crash.
Hotspots don’t take part: TerraGlyph supplies no hotspot positions. The same path runs from any load of a player’s own save: Sign In, the launcher’s Load, and the in-game menu’s Load.
Scope
In scope
- While a save loads, the room it starts in doesn’t write the player’s profile. The save already holds that state. The next room the player enters writes it as before.
- A debug line says when the write is skipped.
Out of scope
MetaEngine::readSavegameHeader()’s signedintand the gzip stream’s assert: upstream code, and correct for a file that doesn’t change under them.- When the original writes
<name>.sav(still untraced, as the comment says).
Exit criteria
- The change builds under GCC (Windows, MSYS2 UCRT64) and clang
(Android) with no warnings in
engines/terraglyph/. - On Windows, loading slot 3 (STEST) from the TV’s saves logs the
skipped write and leaves the file untouched. Partly: entering the
next game room wasn’t run. STEST’s start room, Town Center, keeps the
gang in until Scooby has a snack (
0x42D060), which a blind click script couldn’t reach. The write path itself is unchanged. - On the TCL, with hotspots on as Jeff had them, loading STEST no longer
crashes, and the load doesn’t rewrite slot 3. Loaded through the
launcher’s Restore and the in-game menu’s Load, the same
loadGameState()that Sign In calls. - A new player’s first game room still writes their save. Not run (the same unchanged write path as criterion 2’s second half).
Result
Fork branch wp-126-player-save-during-load, one commit on
scooby-engine @ d49e7e83d2, in fork PR
barefootbetters/scummvm#51
(merged on 2026-11-30, 5f76c9a955): TERRAGLYPH: Don't write a player's save while it loads
(terraglyph.{h,cpp}, profile.cpp).
_loadingSaveis set aroundEngine::loadGameState()inTerraglyphEngine::loadGameState().- The room-entry save skips while it’s set, with a debug line:
player <name> not saved in slot <n>: the slot is loading.
Windows (the TV’s three saves, its Showdown settings, --save-slot=3):
- Before the fix: the load rewrote slot 3 (09:45:10) and didn’t crash. That is the data-dependence described above.
- With the fix: the log reads
player STEST not saved in slot 3: the slot is loading, and slot 3 keeps its SHA-256 (c7854da897f64be2…) and time.
On the TCL (versionCode 26041041: the WP-118 test tree with WP-123, the
grid default and this fix as a patch; adb’s D-pad). Slot 3 stays at the
crash’s own write, 09:38:32.217, after both of these loads, and the crash log
has no new entry. Captures are in C:\pcloud\BB\DEV\wp-118-tv-captures\:
- The tray’s Saves: STEST (
201-w126-saves) loads into Town Center (204-w126-loaded). - The in-game menu’s Load: STEST again (
206-w126-gmm-load,207-w126-gmm-loaded). - Return to Launcher brings the app back to the grid
(
210-w126-launcher).
Open: criteria 2 (its second half) and 4, both on the unchanged write path. Jeff’s next play on the TV covers them: slot 3’s time changes when STEST enters a new game room.