Skip to content

Commit

Permalink
Savestates: Rename savestate when booting the game regularly (suspend…
Browse files Browse the repository at this point in the history
… mode)
  • Loading branch information
elad335 committed Oct 23, 2022
1 parent 2be476a commit 3c5b631
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
29 changes: 19 additions & 10 deletions rpcs3/Emu/System.cpp
Expand Up @@ -81,6 +81,7 @@ extern std::pair<std::shared_ptr<lv2_overlay>, CellError> ppu_load_overlay(const
extern bool ppu_load_rel_exec(const ppu_rel_object&);
extern bool is_savestate_version_compatible(const std::vector<std::pair<u16, u16>>& data, bool is_boot_check);
extern std::vector<std::pair<u16, u16>> 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<s64> g_tty_size{0};
Expand Down Expand Up @@ -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<std::string>{m_ar ? path : "", m_title_id.empty() ? "" : get_savestate_path(m_title_id, path)})
{
old_path.insert(insert_pos, prefix);
if (old_path.empty())
{
continue;
}

if (fs::rename(path, old_path, true))
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)
{
sys_log.notice("Savestate has been moved to path='%s'", old_path);
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);
}
}
}
}
Expand Down Expand Up @@ -2505,7 +2514,7 @@ std::shared_ptr<utils::serial> 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);

Expand Down
5 changes: 5 additions & 0 deletions rpcs3/Emu/savestate_utils.cpp
Expand Up @@ -146,6 +146,11 @@ bool is_savestate_version_compatible(const std::vector<std::pair<u16, u16>>& 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);
Expand Down

0 comments on commit 3c5b631

Please sign in to comment.