From bf2c81e37d6fd44538cbf1e4be7f75d990a713b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 1 Nov 2026 15:41:23 +0100 Subject: [PATCH] Headless: Don't save states while running from a state without its game The game runs without its disc there, so a state saved from it would keep that broken environment. Saves are refused, and so is --save-state together with a state to boot. Co-Authored-By: Claude Opus 5.5 --- Core/FileSystems/MetaFileSystem.h | 2 +- Core/SaveState.cpp | 9 +++++++++ docs/debugging.md | 3 ++- headless/Headless.cpp | 4 ++-- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Core/FileSystems/MetaFileSystem.h b/Core/FileSystems/MetaFileSystem.h index 37a7864ddc..be79268f0b 100644 --- a/Core/FileSystems/MetaFileSystem.h +++ b/Core/FileSystems/MetaFileSystem.h @@ -67,7 +67,7 @@ public: } // Opens of files that aren't on the disc, and reads and seeks on handles whose file was dropped - // when a savestate loaded. On a real disc these don't happen, so they tell when a game booted + // when a savestate loaded. With the real disc these are rare, so they tell when a game booted // from a state alone has missed data it needed. int FailedDiscReads() const { return failedDiscReads_; } void ResetFailedDiscReads() { failedDiscReads_ = 0; } diff --git a/Core/SaveState.cpp b/Core/SaveState.cpp index 3cc486e86d..836cbacba7 100644 --- a/Core/SaveState.cpp +++ b/Core/SaveState.cpp @@ -498,6 +498,15 @@ int g_screenshotFailures; CallbackRefused(op.callback); return false; } + // A game booted from a state alone (headless) runs without its disc. A state saved from that + // would hold the broken environment, and be no use with the game or without it. + if (op.type == OperationType::Save && PSP_CoreParameter().fileType == IdentifiedFileType::PPSSPP_SAVESTATE) { + ERROR_LOG(Log::SaveState, "Can't save a state while running from a state without its game"); + if (op.callback) { + op.callback(Status::FAILURE, "Can't save a state while running from a state without its game", ""); + } + return false; + } // Verify saves and loads back in place, with the mounts as they are. if (op.type != OperationType::Verify && WarnUserIfVSHSession()) { CallbackRefused(op.callback); diff --git a/docs/debugging.md b/docs/debugging.md index 43f2441e66..359a3c8a1c 100644 --- a/docs/debugging.md +++ b/docs/debugging.md @@ -231,7 +231,8 @@ but a state taken while the game streams music or video, or just before it loads so save perf states in the middle of gameplay. Headless prints `Failed disc reads: N` at the end, counted from the load: opens of files on the disc and reads and seeks on dropped handles. A state that keeps it at 0, or close, measures the game as it really runs. Only states saved since the disc ID was added to them (`SaveStart` version 6) -boot this way. The app doesn't: it refuses a `.ppst` as before. +boot this way. The app doesn't: it refuses a `.ppst` as before. States can't be saved while running from one this +way (`--save-state` is refused): without the disc the game isn't in a state worth keeping. `Tools/headless_bench.py` takes such states as games (`--game some.ppst`, or `--state-dir DIR` for every `.ppst` in a directory), and reports the failed disc reads with the timings. diff --git a/headless/Headless.cpp b/headless/Headless.cpp index 60b7576a1d..e4a2fba86d 100644 --- a/headless/Headless.cpp +++ b/headless/Headless.cpp @@ -1051,8 +1051,8 @@ int main(int argc, const char* argv[]) { std::string stateBootPath; if (testFilenames.size() == 1 && endsWithNoCase(testFilenames[0], ".ppst")) { stateBootPath = testFilenames[0]; - if (stateToLoad) { - fprintf(stderr, "--state can't be used when booting a state\n"); + if (stateToLoad || cmdLineOptions.stateToSave.has_value()) { + fprintf(stderr, "--state and --save-state can't be used when booting a state\n"); return 1; } stateToLoad = stateBootPath.c_str();