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

What the code does

  1. Signing in as an existing player loads their save: TerraglyphEngine::loadGameState() signs them in (_playerSlot), then calls Engine::loadGameState().
  2. Engine::loadGameState() opens the slot for reading and calls loadGameStream(). That calls loadProfile(), which shows the profile’s start room (showRoom()), all inside the load.
  3. 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.
  4. 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 an int and 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:

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

  1. 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.
  2. A debug line says when the write is skipped.

Out of scope

Exit criteria

  1. The change builds under GCC (Windows, MSYS2 UCRT64) and clang (Android) with no warnings in engines/terraglyph/.
  2. 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.
  3. 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.
  4. 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).

Windows (the TV’s three saves, its Showdown settings, --save-slot=3):

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\:

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.