From ee3e36672c5fc1db971ec20776234c82aeaa417a Mon Sep 17 00:00:00 2001 From: Eladash Date: Sun, 23 Oct 2022 09:40:31 +0300 Subject: [PATCH] Savestates: Rename savestate when booting the game regularly (suspend mode) --- rpcs3/Emu/System.cpp | 31 ++++++++++++++++++++----------- rpcs3/Emu/savestate_utils.cpp | 5 +++++ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/rpcs3/Emu/System.cpp b/rpcs3/Emu/System.cpp index 92e1ebb34a..434de37617 100644 --- a/rpcs3/Emu/System.cpp +++ b/rpcs3/Emu/System.cpp @@ -81,6 +81,7 @@ extern std::pair, CellError> ppu_load_overlay(const extern bool ppu_load_rel_exec(const ppu_rel_object&); extern bool is_savestate_version_compatible(const std::vector>& data, bool is_boot_check); extern std::vector> read_used_savestate_versions(); +std::string get_savestate_path(std::string_view title_id, std::string_view boot_path); fs::file g_tty; atomic_t g_tty_size{0}; @@ -676,19 +677,27 @@ game_boot_result Emulator::BootGame(const std::string& path, const std::string& auto error = Load(title_id, add_only); - if (g_cfg.savestate.suspend_emu && m_ar) + if (g_cfg.savestate.suspend_emu) { - std::string old_path = path.substr(0, path.find_last_not_of(fs::delim) + 1); - const usz insert_pos = old_path.find_last_of(fs::delim) + 1; - const auto prefix = "used_"sv; - - if (old_path.compare(insert_pos, prefix.size(), prefix) != 0) + for (std::string old_path : std::initializer_list{m_ar ? path : "", m_title_id.empty() ? "" : get_savestate_path(m_title_id, path)}) { - old_path.insert(insert_pos, prefix); - - if (fs::rename(path, old_path, true)) + if (old_path.empty()) { - sys_log.notice("Savestate has been moved to path='%s'", old_path); + continue; + } + + old_path = old_path.substr(0, old_path.find_last_not_of(fs::delim) + 1); + const usz insert_pos = old_path.find_last_of(fs::delim) + 1; + const auto prefix = "used_"sv; + + if (old_path.compare(insert_pos, prefix.size(), prefix) != 0) + { + old_path.insert(insert_pos, prefix); + + if (fs::rename(path, old_path, true)) + { + sys_log.notice("Savestate has been moved to path='%s'", old_path); + } } } } @@ -2505,7 +2514,7 @@ std::shared_ptr Emulator::Kill(bool allow_autoexit, bool savestat if (savestate) { - const std::string path = fs::get_cache_dir() + "/savestates/" + (m_title_id.empty() ? m_path.substr(m_path.find_last_of(fs::delim) + 1) : m_title_id) + ".SAVESTAT"; + const std::string path = get_savestate_path(m_title_id, m_path); fs::pending_file file(path); diff --git a/rpcs3/Emu/savestate_utils.cpp b/rpcs3/Emu/savestate_utils.cpp index 1c1d00d5bb..539a445d4d 100644 --- a/rpcs3/Emu/savestate_utils.cpp +++ b/rpcs3/Emu/savestate_utils.cpp @@ -146,6 +146,11 @@ bool is_savestate_version_compatible(const std::vector>& dat return ok; } +std::string get_savestate_path(std::string_view title_id, std::string_view boot_path) +{ + return fs::get_cache_dir() + "/savestates/" + std::string{title_id.empty() ? boot_path.substr(boot_path.find_last_of(fs::delim) + 1) : title_id} + ".SAVESTAT"; +} + bool is_savestate_compatible(const fs::file& file) { return is_savestate_version_compatible(get_savestate_versioning_data(file), false);