2012-12-27 18:34:22 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
2016-03-06 21:30:31 +00:00
|
|
|
#include <algorithm>
|
2012-12-27 19:28:12 +00:00
|
|
|
#include <vector>
|
2017-02-27 19:51:36 +00:00
|
|
|
#include <thread>
|
2017-02-27 20:57:46 +00:00
|
|
|
#include <mutex>
|
2012-12-27 19:28:12 +00:00
|
|
|
|
2020-10-01 11:05:04 +00:00
|
|
|
#include "Common/Data/Text/I18n.h"
|
2020-10-01 07:27:25 +00:00
|
|
|
#include "Common/Thread/ThreadUtil.h"
|
2020-10-01 11:05:04 +00:00
|
|
|
#include "Common/Data/Text/Parsers.h"
|
2023-03-21 10:10:09 +00:00
|
|
|
#include "Common/System/System.h"
|
2013-12-29 22:28:31 +00:00
|
|
|
|
2020-10-04 18:48:47 +00:00
|
|
|
#include "Common/File/FileUtil.h"
|
2020-08-10 07:12:51 +00:00
|
|
|
#include "Common/Serialize/Serializer.h"
|
|
|
|
#include "Common/Serialize/SerializeFuncs.h"
|
2020-09-29 10:19:22 +00:00
|
|
|
#include "Common/StringUtils.h"
|
2020-08-15 18:53:08 +00:00
|
|
|
#include "Common/TimeUtil.h"
|
2013-05-22 16:00:06 +00:00
|
|
|
|
|
|
|
#include "Core/SaveState.h"
|
2013-09-15 03:28:41 +00:00
|
|
|
#include "Core/Config.h"
|
2013-05-22 16:00:06 +00:00
|
|
|
#include "Core/Core.h"
|
|
|
|
#include "Core/CoreTiming.h"
|
2014-12-21 07:14:46 +00:00
|
|
|
#include "Core/Screenshot.h"
|
2013-09-15 04:19:10 +00:00
|
|
|
#include "Core/System.h"
|
2013-12-29 22:28:31 +00:00
|
|
|
#include "Core/FileSystems/MetaFileSystem.h"
|
2013-12-29 23:11:29 +00:00
|
|
|
#include "Core/ELF/ParamSFO.h"
|
2013-05-22 16:00:06 +00:00
|
|
|
#include "Core/HLE/HLE.h"
|
2014-05-27 14:50:08 +00:00
|
|
|
#include "Core/HLE/ReplaceTables.h"
|
2022-01-30 18:37:50 +00:00
|
|
|
#include "Core/HLE/sceDisplay.h"
|
2013-05-22 16:00:06 +00:00
|
|
|
#include "Core/HLE/sceKernel.h"
|
2020-09-17 05:52:09 +00:00
|
|
|
#include "Core/HLE/sceUtility.h"
|
2013-05-22 16:00:06 +00:00
|
|
|
#include "Core/MemMap.h"
|
|
|
|
#include "Core/MIPS/MIPS.h"
|
2016-04-30 23:20:21 +00:00
|
|
|
#include "Core/MIPS/JitCommon/JitBlockCache.h"
|
2023-07-01 10:31:46 +00:00
|
|
|
#include "Core/RetroAchievements.h"
|
2013-12-29 22:28:31 +00:00
|
|
|
#include "HW/MemoryStick.h"
|
2013-09-30 02:57:51 +00:00
|
|
|
#include "GPU/GPUState.h"
|
2012-12-27 18:34:22 +00:00
|
|
|
|
2017-11-14 04:12:27 +00:00
|
|
|
#ifndef MOBILE_DEVICE
|
|
|
|
#include "Core/AVIDump.h"
|
|
|
|
#include "Core/HLE/__sceAudio.h"
|
|
|
|
#endif
|
|
|
|
|
2021-08-05 07:16:10 +00:00
|
|
|
// Slot number is visual only, -2 will display special message
|
|
|
|
constexpr int LOAD_UNDO_SLOT = -2;
|
|
|
|
|
2012-12-27 19:28:12 +00:00
|
|
|
namespace SaveState
|
2012-12-27 18:34:22 +00:00
|
|
|
{
|
2012-12-27 19:28:12 +00:00
|
|
|
struct SaveStart
|
|
|
|
{
|
|
|
|
void DoState(PointerWrap &p);
|
|
|
|
};
|
2012-12-27 18:34:22 +00:00
|
|
|
|
2012-12-27 19:28:12 +00:00
|
|
|
enum OperationType
|
|
|
|
{
|
|
|
|
SAVESTATE_SAVE,
|
|
|
|
SAVESTATE_LOAD,
|
|
|
|
SAVESTATE_VERIFY,
|
2013-09-30 02:57:51 +00:00
|
|
|
SAVESTATE_REWIND,
|
2014-12-21 07:14:46 +00:00
|
|
|
SAVESTATE_SAVE_SCREENSHOT,
|
2012-12-27 19:28:12 +00:00
|
|
|
};
|
2012-12-27 18:34:22 +00:00
|
|
|
|
2012-12-27 19:28:12 +00:00
|
|
|
struct Operation
|
|
|
|
{
|
2020-05-16 13:20:22 +00:00
|
|
|
// The slot number is for visual purposes only. Set to -1 for operations where we don't display a message for example.
|
2021-05-05 23:31:38 +00:00
|
|
|
Operation(OperationType t, const Path &f, int slot_, Callback cb, void *cbUserData_)
|
2020-05-16 13:20:22 +00:00
|
|
|
: type(t), filename(f), callback(cb), slot(slot_), cbUserData(cbUserData_)
|
2012-12-27 19:28:12 +00:00
|
|
|
{
|
|
|
|
}
|
2012-12-27 18:34:22 +00:00
|
|
|
|
2012-12-27 19:28:12 +00:00
|
|
|
OperationType type;
|
2021-05-05 23:31:38 +00:00
|
|
|
Path filename;
|
2012-12-27 19:28:12 +00:00
|
|
|
Callback callback;
|
2020-05-16 13:20:22 +00:00
|
|
|
int slot;
|
2013-01-02 20:00:10 +00:00
|
|
|
void *cbUserData;
|
2012-12-27 19:28:12 +00:00
|
|
|
};
|
|
|
|
|
2013-11-15 12:11:44 +00:00
|
|
|
CChunkFileReader::Error SaveToRam(std::vector<u8> &data) {
|
|
|
|
SaveStart state;
|
2023-02-13 09:56:09 +00:00
|
|
|
return CChunkFileReader::MeasureAndSavePtr(state, &data);
|
2013-11-15 12:11:44 +00:00
|
|
|
}
|
|
|
|
|
2020-08-02 15:11:09 +00:00
|
|
|
CChunkFileReader::Error LoadFromRam(std::vector<u8> &data, std::string *errorString) {
|
2013-11-15 12:11:44 +00:00
|
|
|
SaveStart state;
|
2020-08-02 15:11:09 +00:00
|
|
|
return CChunkFileReader::LoadPtr(&data[0], state, errorString);
|
2013-11-15 12:11:44 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 09:56:09 +00:00
|
|
|
// This ring buffer of states is for rewind save states, which are kept in RAM.
|
|
|
|
// Save states are compressed against one of two reference saves (bases_), and the reference
|
|
|
|
// is switched to a fresh save every N saves, where N is BASE_USAGE_INTERVAL.
|
|
|
|
// The compression is a simple block based scheme where 0 means to copy a block from the base,
|
|
|
|
// and 1 means that the following bytes are the next block. See Compress/LockedDecompress.
|
|
|
|
class StateRingbuffer {
|
|
|
|
public:
|
2023-02-13 10:17:03 +00:00
|
|
|
StateRingbuffer() {
|
|
|
|
size_ = REWIND_NUM_STATES;
|
|
|
|
states_.resize(size_);
|
|
|
|
baseMapping_.resize(size_);
|
2013-09-30 02:57:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 09:56:09 +00:00
|
|
|
~StateRingbuffer() {
|
|
|
|
if (compressThread_.joinable()) {
|
|
|
|
compressThread_.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-30 02:57:51 +00:00
|
|
|
CChunkFileReader::Error Save()
|
|
|
|
{
|
2023-02-14 09:09:22 +00:00
|
|
|
rewindLastTime_ = time_now_d();
|
|
|
|
|
|
|
|
// Make sure we're not processing a previous save. That'll cause a hitch though, but at least won't
|
|
|
|
// crash due to contention over buffer_.
|
|
|
|
if (compressThread_.joinable())
|
|
|
|
compressThread_.join();
|
2023-02-13 10:17:03 +00:00
|
|
|
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::mutex> guard(lock_);
|
2016-09-24 17:37:30 +00:00
|
|
|
|
2013-09-30 02:57:51 +00:00
|
|
|
int n = next_++ % size_;
|
2013-11-03 01:32:34 +00:00
|
|
|
if ((next_ % size_) == first_)
|
2013-09-30 02:57:51 +00:00
|
|
|
++first_;
|
|
|
|
|
2023-02-13 09:56:09 +00:00
|
|
|
std::vector<u8> *compressBuffer = &buffer_;
|
2014-01-18 18:18:47 +00:00
|
|
|
CChunkFileReader::Error err;
|
|
|
|
|
|
|
|
if (base_ == -1 || ++baseUsage_ > BASE_USAGE_INTERVAL)
|
|
|
|
{
|
|
|
|
base_ = (base_ + 1) % ARRAY_SIZE(bases_);
|
|
|
|
baseUsage_ = 0;
|
|
|
|
err = SaveToRam(bases_[base_]);
|
|
|
|
// Let's not bother savestating twice.
|
|
|
|
compressBuffer = &bases_[base_];
|
|
|
|
}
|
|
|
|
else
|
2023-02-13 09:56:09 +00:00
|
|
|
err = SaveToRam(buffer_);
|
2014-01-18 18:18:47 +00:00
|
|
|
|
|
|
|
if (err == CChunkFileReader::ERROR_NONE)
|
2016-09-24 17:37:30 +00:00
|
|
|
ScheduleCompress(&states_[n], compressBuffer, &bases_[base_]);
|
2014-01-18 18:18:47 +00:00
|
|
|
else
|
|
|
|
states_[n].clear();
|
2023-02-13 09:56:09 +00:00
|
|
|
|
2014-01-18 18:18:47 +00:00
|
|
|
baseMapping_[n] = base_;
|
|
|
|
return err;
|
2013-09-30 02:57:51 +00:00
|
|
|
}
|
|
|
|
|
2020-08-02 15:11:09 +00:00
|
|
|
CChunkFileReader::Error Restore(std::string *errorString)
|
2013-09-30 02:57:51 +00:00
|
|
|
{
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::mutex> guard(lock_);
|
2016-09-24 17:37:30 +00:00
|
|
|
|
2013-09-30 02:57:51 +00:00
|
|
|
// No valid states left.
|
2013-11-03 01:32:34 +00:00
|
|
|
if (Empty())
|
2013-09-30 02:57:51 +00:00
|
|
|
return CChunkFileReader::ERROR_BAD_FILE;
|
|
|
|
|
2014-01-18 18:18:47 +00:00
|
|
|
int n = (--next_ + size_) % size_;
|
2013-09-30 02:57:51 +00:00
|
|
|
if (states_[n].empty())
|
|
|
|
return CChunkFileReader::ERROR_BAD_FILE;
|
2013-10-23 14:10:11 +00:00
|
|
|
|
2014-01-18 18:18:47 +00:00
|
|
|
static std::vector<u8> buffer;
|
2017-04-09 07:19:35 +00:00
|
|
|
LockedDecompress(buffer, states_[n], bases_[baseMapping_[n]]);
|
2023-02-13 10:17:03 +00:00
|
|
|
CChunkFileReader::Error error = LoadFromRam(buffer, errorString);
|
2023-02-14 09:09:22 +00:00
|
|
|
rewindLastTime_ = time_now_d();
|
2023-02-13 10:17:03 +00:00
|
|
|
return error;
|
2014-01-18 18:18:47 +00:00
|
|
|
}
|
|
|
|
|
2016-09-24 17:37:30 +00:00
|
|
|
void ScheduleCompress(std::vector<u8> *result, const std::vector<u8> *state, const std::vector<u8> *base)
|
|
|
|
{
|
2019-09-28 18:43:44 +00:00
|
|
|
if (compressThread_.joinable())
|
|
|
|
compressThread_.join();
|
|
|
|
compressThread_ = std::thread([=]{
|
2020-11-30 23:46:26 +00:00
|
|
|
SetCurrentThreadName("SaveStateCompress");
|
2023-01-03 22:29:22 +00:00
|
|
|
|
2023-01-04 12:48:40 +00:00
|
|
|
// Should do no I/O, so no JNI thread context needed.
|
2016-09-24 17:37:30 +00:00
|
|
|
Compress(*result, *state, *base);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-01-18 18:18:47 +00:00
|
|
|
void Compress(std::vector<u8> &result, const std::vector<u8> &state, const std::vector<u8> &base)
|
|
|
|
{
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::mutex> guard(lock_);
|
2016-09-24 17:37:30 +00:00
|
|
|
// Bail if we were cleared before locking.
|
|
|
|
if (first_ == 0 && next_ == 0)
|
|
|
|
return;
|
|
|
|
|
2023-02-13 09:56:09 +00:00
|
|
|
double start_time = time_now_d();
|
2014-01-18 18:18:47 +00:00
|
|
|
result.clear();
|
2023-02-13 09:56:09 +00:00
|
|
|
result.reserve(512 * 1024);
|
2014-01-18 18:18:47 +00:00
|
|
|
for (size_t i = 0; i < state.size(); i += BLOCK_SIZE)
|
|
|
|
{
|
|
|
|
int blockSize = std::min(BLOCK_SIZE, (int)(state.size() - i));
|
|
|
|
if (i + blockSize > base.size() || memcmp(&state[i], &base[i], blockSize) != 0)
|
|
|
|
{
|
|
|
|
result.push_back(1);
|
2023-02-13 09:56:09 +00:00
|
|
|
result.insert(result.end(), state.begin() + i, state.begin() + i + blockSize);
|
2014-01-18 18:18:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
result.push_back(0);
|
|
|
|
}
|
2023-02-13 09:56:09 +00:00
|
|
|
|
|
|
|
double taken_s = time_now_d() - start_time;
|
|
|
|
DEBUG_LOG(SAVESTATE, "Rewind: Compressed save from %d bytes to %d in %0.2f ms.", (int)state.size(), (int)result.size(), taken_s * 1000.0);
|
2014-01-18 18:18:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-09 07:19:35 +00:00
|
|
|
void LockedDecompress(std::vector<u8> &result, const std::vector<u8> &compressed, const std::vector<u8> &base)
|
2014-01-18 18:18:47 +00:00
|
|
|
{
|
|
|
|
result.clear();
|
|
|
|
result.reserve(base.size());
|
|
|
|
auto basePos = base.begin();
|
|
|
|
for (size_t i = 0; i < compressed.size(); )
|
|
|
|
{
|
|
|
|
if (compressed[i] == 0)
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
int blockSize = std::min(BLOCK_SIZE, (int)(base.size() - result.size()));
|
|
|
|
result.insert(result.end(), basePos, basePos + blockSize);
|
|
|
|
basePos += blockSize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
int blockSize = std::min(BLOCK_SIZE, (int)(compressed.size() - i));
|
|
|
|
result.insert(result.end(), compressed.begin() + i, compressed.begin() + i + blockSize);
|
|
|
|
i += blockSize;
|
2023-02-13 14:41:32 +00:00
|
|
|
// This check is to avoid advancing basePos out of range, which MSVC catches.
|
|
|
|
// When this happens, we're at the end of decoding anyway.
|
|
|
|
if (base.end() - basePos >= blockSize) {
|
|
|
|
basePos += blockSize;
|
|
|
|
}
|
2014-01-18 18:18:47 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-30 02:57:51 +00:00
|
|
|
}
|
|
|
|
|
2013-11-03 01:32:34 +00:00
|
|
|
void Clear()
|
|
|
|
{
|
2019-09-28 18:43:44 +00:00
|
|
|
if (compressThread_.joinable())
|
|
|
|
compressThread_.join();
|
|
|
|
|
2016-09-24 17:37:30 +00:00
|
|
|
// This lock is mainly for shutdown.
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::mutex> guard(lock_);
|
2013-11-03 01:32:34 +00:00
|
|
|
first_ = 0;
|
|
|
|
next_ = 0;
|
2023-02-13 14:41:32 +00:00
|
|
|
for (auto &b : bases_) {
|
|
|
|
b.clear();
|
|
|
|
}
|
|
|
|
baseMapping_.clear();
|
|
|
|
baseMapping_.resize(size_);
|
|
|
|
for (auto &s : states_) {
|
|
|
|
s.clear();
|
|
|
|
}
|
|
|
|
buffer_.clear();
|
|
|
|
base_ = -1;
|
|
|
|
baseUsage_ = 0;
|
2023-02-14 09:09:22 +00:00
|
|
|
rewindLastTime_ = time_now_d();
|
2013-11-03 01:32:34 +00:00
|
|
|
}
|
|
|
|
|
2014-12-03 20:16:11 +00:00
|
|
|
bool Empty() const
|
2013-11-03 01:32:34 +00:00
|
|
|
{
|
|
|
|
return next_ == first_;
|
|
|
|
}
|
|
|
|
|
2023-02-13 10:17:03 +00:00
|
|
|
void Process() {
|
|
|
|
if (g_Config.iRewindSnapshotInterval <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2023-12-17 23:21:53 +00:00
|
|
|
if (coreState != CORE_RUNNING) {
|
|
|
|
return;
|
|
|
|
}
|
2023-02-13 10:17:03 +00:00
|
|
|
|
|
|
|
// For fast-forwarding, otherwise they may be useless and too close.
|
|
|
|
double now = time_now_d();
|
2023-02-14 09:09:22 +00:00
|
|
|
double diff = now - rewindLastTime_;
|
2023-02-13 10:17:03 +00:00
|
|
|
if (diff < g_Config.iRewindSnapshotInterval)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DEBUG_LOG(SAVESTATE, "Saving rewind state");
|
|
|
|
Save();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NotifyState() {
|
|
|
|
// Prevent saving snapshots immediately after loading or saving a state.
|
2023-02-14 09:09:22 +00:00
|
|
|
rewindLastTime_ = time_now_d();
|
2023-02-13 10:17:03 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 09:56:09 +00:00
|
|
|
private:
|
2023-02-23 19:21:41 +00:00
|
|
|
const int BLOCK_SIZE = 8192;
|
|
|
|
const int REWIND_NUM_STATES = 20;
|
2014-01-18 18:18:47 +00:00
|
|
|
// TODO: Instead, based on size of compressed state?
|
2023-02-23 19:21:41 +00:00
|
|
|
const int BASE_USAGE_INTERVAL = 15;
|
2016-09-24 17:37:30 +00:00
|
|
|
|
2013-09-30 02:57:51 +00:00
|
|
|
typedef std::vector<u8> StateBuffer;
|
2016-09-24 17:37:30 +00:00
|
|
|
|
2023-02-13 09:56:09 +00:00
|
|
|
int first_ = 0;
|
|
|
|
int next_ = 0;
|
2013-09-30 02:57:51 +00:00
|
|
|
int size_;
|
2016-09-24 17:37:30 +00:00
|
|
|
|
2013-09-30 02:57:51 +00:00
|
|
|
std::vector<StateBuffer> states_;
|
2014-01-18 18:18:47 +00:00
|
|
|
StateBuffer bases_[2];
|
|
|
|
std::vector<int> baseMapping_;
|
2017-02-27 20:57:46 +00:00
|
|
|
std::mutex lock_;
|
2019-09-28 18:43:44 +00:00
|
|
|
std::thread compressThread_;
|
2023-02-13 09:56:09 +00:00
|
|
|
std::vector<u8> buffer_;
|
2016-09-24 17:37:30 +00:00
|
|
|
|
2023-02-13 09:56:09 +00:00
|
|
|
int base_ = -1;
|
|
|
|
int baseUsage_ = 0;
|
2023-02-13 10:17:03 +00:00
|
|
|
|
2023-02-14 09:09:22 +00:00
|
|
|
double rewindLastTime_ = 0.0f;
|
2013-09-30 02:57:51 +00:00
|
|
|
};
|
|
|
|
|
2013-01-02 22:25:35 +00:00
|
|
|
static bool needsProcess = false;
|
2021-02-21 16:18:13 +00:00
|
|
|
static bool needsRestart = false;
|
2012-12-27 19:28:12 +00:00
|
|
|
static std::vector<Operation> pending;
|
2017-02-27 20:57:46 +00:00
|
|
|
static std::mutex mutex;
|
2018-11-25 16:20:23 +00:00
|
|
|
static int screenshotFailures = 0;
|
2014-02-09 21:45:51 +00:00
|
|
|
static bool hasLoadedState = false;
|
2018-06-16 15:06:35 +00:00
|
|
|
static const int STALE_STATE_USES = 2;
|
|
|
|
// 4 hours of total gameplay since the virtual PSP started the game.
|
2018-07-05 01:00:06 +00:00
|
|
|
static const u64 STALE_STATE_TIME = 4 * 3600 * 1000000ULL;
|
2018-06-15 00:52:44 +00:00
|
|
|
static int saveStateGeneration = 0;
|
2021-05-08 16:42:52 +00:00
|
|
|
static int saveDataGeneration = 0;
|
|
|
|
static int lastSaveDataGeneration = 0;
|
2018-06-15 00:52:44 +00:00
|
|
|
static std::string saveStateInitialGitVersion = "";
|
2012-12-27 19:28:12 +00:00
|
|
|
|
2013-09-30 02:57:51 +00:00
|
|
|
// TODO: Should this be configurable?
|
2018-11-25 16:20:23 +00:00
|
|
|
static const int SCREENSHOT_FAILURE_RETRIES = 15;
|
2023-02-13 10:17:03 +00:00
|
|
|
static StateRingbuffer rewindStates;
|
2013-09-30 02:57:51 +00:00
|
|
|
|
2012-12-27 19:28:12 +00:00
|
|
|
void SaveStart::DoState(PointerWrap &p)
|
|
|
|
{
|
2023-02-14 15:43:22 +00:00
|
|
|
auto s = p.Section("SaveStart", 1, 3);
|
2013-09-15 03:23:03 +00:00
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
|
2018-06-15 00:52:44 +00:00
|
|
|
if (s >= 2) {
|
2018-06-16 15:06:35 +00:00
|
|
|
// This only increments on save, of course.
|
2018-06-15 00:52:44 +00:00
|
|
|
++saveStateGeneration;
|
2020-08-10 04:20:42 +00:00
|
|
|
Do(p, saveStateGeneration);
|
2018-06-16 15:06:35 +00:00
|
|
|
// This saves the first git version to create this save state (or generation of save states.)
|
2018-06-15 00:52:44 +00:00
|
|
|
if (saveStateInitialGitVersion.empty())
|
|
|
|
saveStateInitialGitVersion = PPSSPP_GIT_VERSION;
|
2020-08-10 04:20:42 +00:00
|
|
|
Do(p, saveStateInitialGitVersion);
|
2018-06-15 00:52:44 +00:00
|
|
|
} else {
|
|
|
|
saveStateGeneration = 1;
|
|
|
|
}
|
2021-05-08 16:42:52 +00:00
|
|
|
if (s >= 3) {
|
|
|
|
// Keep track of savedata (not save states) too.
|
|
|
|
Do(p, saveDataGeneration);
|
|
|
|
} else {
|
|
|
|
saveDataGeneration = 0;
|
|
|
|
}
|
2018-06-15 00:52:44 +00:00
|
|
|
|
2023-02-14 15:43:22 +00:00
|
|
|
// Gotta do CoreTiming before HLE, but from v3 we've moved it after the memory stuff.
|
|
|
|
if (s <= 2) {
|
|
|
|
CoreTiming::DoState(p);
|
|
|
|
}
|
2012-12-27 19:58:15 +00:00
|
|
|
|
2013-09-29 20:51:09 +00:00
|
|
|
// Memory is a bit tricky when jit is enabled, since there's emuhacks in it.
|
2023-12-29 05:14:23 +00:00
|
|
|
// These must be saved before copying out memory and restored after.
|
|
|
|
auto savedReplacements = SaveAndClearReplacements();
|
2021-11-28 00:11:51 +00:00
|
|
|
if (MIPSComp::jit && p.mode == p.MODE_WRITE) {
|
|
|
|
std::lock_guard<std::recursive_mutex> guard(MIPSComp::jitLock);
|
|
|
|
if (MIPSComp::jit) {
|
|
|
|
std::vector<u32> savedBlocks;
|
|
|
|
savedBlocks = MIPSComp::jit->SaveAndClearEmuHackOps();
|
|
|
|
Memory::DoState(p);
|
|
|
|
MIPSComp::jit->RestoreSavedEmuHackOps(savedBlocks);
|
|
|
|
} else {
|
|
|
|
Memory::DoState(p);
|
|
|
|
}
|
|
|
|
} else {
|
2013-09-29 20:51:09 +00:00
|
|
|
Memory::DoState(p);
|
|
|
|
}
|
2022-10-30 00:56:47 +00:00
|
|
|
|
2023-02-14 15:43:22 +00:00
|
|
|
if (s >= 3) {
|
|
|
|
CoreTiming::DoState(p);
|
|
|
|
}
|
|
|
|
|
2022-10-30 00:56:47 +00:00
|
|
|
// Don't bother restoring if reading, we'll deal with that in KernelModuleDoState.
|
|
|
|
// In theory, different functions might have been runtime loaded in the state.
|
2023-12-20 09:33:56 +00:00
|
|
|
if (p.mode != p.MODE_READ) {
|
2022-10-30 00:56:47 +00:00
|
|
|
RestoreSavedReplacements(savedReplacements);
|
2023-12-20 09:33:56 +00:00
|
|
|
}
|
2013-09-29 20:51:09 +00:00
|
|
|
|
2012-12-27 19:58:15 +00:00
|
|
|
MemoryStick_DoState(p);
|
2012-12-28 04:33:10 +00:00
|
|
|
currentMIPS->DoState(p);
|
2012-12-28 06:14:31 +00:00
|
|
|
HLEDoState(p);
|
2012-12-27 19:28:12 +00:00
|
|
|
__KernelDoState(p);
|
2023-07-01 10:31:46 +00:00
|
|
|
Achievements::DoState(p);
|
2013-02-08 16:10:20 +00:00
|
|
|
// Kernel object destructors might close open files, so do the filesystem last.
|
|
|
|
pspFileSystem.DoState(p);
|
2012-12-27 19:28:12 +00:00
|
|
|
}
|
|
|
|
|
2023-12-14 11:22:24 +00:00
|
|
|
void Enqueue(const SaveState::Operation &op)
|
2012-12-27 19:28:12 +00:00
|
|
|
{
|
2023-12-03 15:41:29 +00:00
|
|
|
if (Achievements::HardcoreModeActive()) {
|
2023-12-05 00:12:26 +00:00
|
|
|
if (g_Config.bAchievementsSaveStateInHardcoreMode && ((op.type == SaveState::SAVESTATE_SAVE) || (op.type == SAVESTATE_SAVE_SCREENSHOT))) {
|
2023-11-30 16:33:14 +00:00
|
|
|
// We allow saving in hardcore mode if this setting is on.
|
2023-11-14 21:54:39 +00:00
|
|
|
} else {
|
|
|
|
// Operation not allowed
|
|
|
|
return;
|
|
|
|
}
|
2023-07-02 22:47:54 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::mutex> guard(mutex);
|
2012-12-27 19:28:12 +00:00
|
|
|
pending.push_back(op);
|
|
|
|
|
2013-09-15 01:43:23 +00:00
|
|
|
// Don't actually run it until next frame.
|
2012-12-27 19:28:12 +00:00
|
|
|
// It's possible there might be a duplicate but it won't hurt us.
|
2013-09-15 01:43:23 +00:00
|
|
|
needsProcess = true;
|
|
|
|
Core_UpdateSingleStep();
|
2012-12-27 19:28:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
void Load(const Path &filename, int slot, Callback callback, void *cbUserData)
|
2012-12-27 19:28:12 +00:00
|
|
|
{
|
2023-02-13 10:17:03 +00:00
|
|
|
rewindStates.NotifyState();
|
2021-08-09 06:15:27 +00:00
|
|
|
if (coreState == CoreState::CORE_RUNTIME_ERROR)
|
2021-10-23 23:56:15 +00:00
|
|
|
Core_EnableStepping(true, "savestate.load", 0);
|
2020-05-16 13:20:22 +00:00
|
|
|
Enqueue(Operation(SAVESTATE_LOAD, filename, slot, callback, cbUserData));
|
2012-12-27 19:28:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
void Save(const Path &filename, int slot, Callback callback, void *cbUserData)
|
2012-12-27 19:28:12 +00:00
|
|
|
{
|
2023-02-13 10:17:03 +00:00
|
|
|
rewindStates.NotifyState();
|
2021-08-09 06:15:27 +00:00
|
|
|
if (coreState == CoreState::CORE_RUNTIME_ERROR)
|
2021-10-23 23:56:15 +00:00
|
|
|
Core_EnableStepping(true, "savestate.save", 0);
|
2020-05-16 13:20:22 +00:00
|
|
|
Enqueue(Operation(SAVESTATE_SAVE, filename, slot, callback, cbUserData));
|
2012-12-27 19:28:12 +00:00
|
|
|
}
|
|
|
|
|
2013-04-13 08:13:28 +00:00
|
|
|
void Verify(Callback callback, void *cbUserData)
|
|
|
|
{
|
2021-05-05 23:31:38 +00:00
|
|
|
Enqueue(Operation(SAVESTATE_VERIFY, Path(), -1, callback, cbUserData));
|
2013-04-13 08:13:28 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 02:57:51 +00:00
|
|
|
void Rewind(Callback callback, void *cbUserData)
|
|
|
|
{
|
2021-08-09 06:15:27 +00:00
|
|
|
if (coreState == CoreState::CORE_RUNTIME_ERROR)
|
2021-10-23 23:56:15 +00:00
|
|
|
Core_EnableStepping(true, "savestate.rewind", 0);
|
2021-05-05 23:31:38 +00:00
|
|
|
Enqueue(Operation(SAVESTATE_REWIND, Path(), -1, callback, cbUserData));
|
2013-09-30 02:57:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
void SaveScreenshot(const Path &filename, Callback callback, void *cbUserData)
|
2014-12-21 07:14:46 +00:00
|
|
|
{
|
2020-05-16 13:20:22 +00:00
|
|
|
Enqueue(Operation(SAVESTATE_SAVE_SCREENSHOT, filename, -1, callback, cbUserData));
|
2014-12-21 07:14:46 +00:00
|
|
|
}
|
|
|
|
|
2013-11-03 01:32:34 +00:00
|
|
|
bool CanRewind()
|
|
|
|
{
|
|
|
|
return !rewindStates.Empty();
|
|
|
|
}
|
|
|
|
|
2013-01-02 20:00:10 +00:00
|
|
|
// Slot utilities
|
|
|
|
|
2016-01-23 21:06:30 +00:00
|
|
|
std::string AppendSlotTitle(const std::string &filename, const std::string &title) {
|
2018-03-17 21:24:23 +00:00
|
|
|
char slotChar = 0;
|
|
|
|
auto detectSlot = [&](const std::string &ext) {
|
|
|
|
if (!endsWith(filename, std::string(".") + ext)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-23 21:06:30 +00:00
|
|
|
|
2018-03-17 21:24:23 +00:00
|
|
|
// Usually these are slots, let's check the slot # after the last '_'.
|
|
|
|
size_t slotNumPos = filename.find_last_of('_');
|
|
|
|
if (slotNumPos == filename.npos) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-23 21:06:30 +00:00
|
|
|
|
2018-03-17 21:24:23 +00:00
|
|
|
const size_t extLength = ext.length() + 1;
|
|
|
|
// If we take out the extension, '_', etc. we should be left with only a single digit.
|
|
|
|
if (slotNumPos + 1 + extLength != filename.length() - 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
slotChar = filename[slotNumPos + 1];
|
|
|
|
if (slotChar < '0' || slotChar > '8') {
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-23 21:06:30 +00:00
|
|
|
|
2018-03-17 21:24:23 +00:00
|
|
|
// Change from zero indexed to human friendly.
|
|
|
|
slotChar++;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (detectSlot(STATE_EXTENSION)) {
|
|
|
|
return StringFromFormat("%s (%c)", title.c_str(), slotChar);
|
|
|
|
}
|
|
|
|
if (detectSlot(UNDO_STATE_EXTENSION)) {
|
2023-04-05 22:34:50 +00:00
|
|
|
auto sy = GetI18NCategory(I18NCat::SYSTEM);
|
2018-03-17 21:24:23 +00:00
|
|
|
// Allow the number to be positioned where it makes sense.
|
2024-01-19 12:44:49 +00:00
|
|
|
std::string undo(sy->T("undo %c"));
|
2018-03-17 21:24:23 +00:00
|
|
|
return title + " (" + StringFromFormat(undo.c_str(), slotChar) + ")";
|
2016-01-23 21:06:30 +00:00
|
|
|
}
|
|
|
|
|
2018-03-17 21:24:23 +00:00
|
|
|
// Couldn't detect, use the filename.
|
|
|
|
return title + " (" + filename + ")";
|
2016-01-23 21:06:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 06:00:22 +00:00
|
|
|
std::string GetTitle(const Path &filename) {
|
2016-01-23 21:06:30 +00:00
|
|
|
std::string title;
|
2021-05-15 06:00:22 +00:00
|
|
|
if (CChunkFileReader::GetFileTitle(filename, &title) == CChunkFileReader::ERROR_NONE) {
|
2016-01-23 21:06:30 +00:00
|
|
|
if (title.empty()) {
|
2021-05-15 06:00:22 +00:00
|
|
|
return filename.GetFilename();
|
2016-01-23 21:06:30 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 06:00:22 +00:00
|
|
|
return AppendSlotTitle(filename.GetFilename(), title);
|
2016-01-23 21:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The file can't be loaded - let's note that.
|
2023-04-05 22:34:50 +00:00
|
|
|
auto sy = GetI18NCategory(I18NCat::SYSTEM);
|
2024-01-19 12:44:49 +00:00
|
|
|
return filename.GetFilename() + " " + std::string(sy->T("(broken)"));
|
2016-01-23 21:06:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 07:16:10 +00:00
|
|
|
std::string GenerateFullDiscId(const Path &gameFilename) {
|
2015-09-23 17:29:39 +00:00
|
|
|
std::string discId = g_paramSFO.GetValueString("DISC_ID");
|
2017-05-29 01:42:45 +00:00
|
|
|
std::string discVer = g_paramSFO.GetValueString("DISC_VERSION");
|
|
|
|
if (discId.empty()) {
|
2024-01-22 22:46:28 +00:00
|
|
|
discId = g_paramSFO.GenerateFakeID(Path());
|
2017-05-29 01:42:45 +00:00
|
|
|
discVer = "1.00";
|
2015-09-23 17:29:39 +00:00
|
|
|
}
|
2021-08-03 08:04:49 +00:00
|
|
|
return StringFromFormat("%s_%s", discId.c_str(), discVer.c_str());
|
|
|
|
}
|
2015-09-23 17:29:39 +00:00
|
|
|
|
2021-08-03 08:04:49 +00:00
|
|
|
Path GenerateSaveSlotFilename(const Path &gameFilename, int slot, const char *extension)
|
|
|
|
{
|
2021-08-05 07:16:10 +00:00
|
|
|
std::string filename = StringFromFormat("%s_%d.%s", GenerateFullDiscId(gameFilename).c_str(), slot, extension);
|
2021-05-05 23:31:38 +00:00
|
|
|
return GetSysDirectory(DIRECTORY_SAVESTATE) / filename;
|
2013-01-02 20:00:10 +00:00
|
|
|
}
|
|
|
|
|
2015-02-01 17:04:06 +00:00
|
|
|
int GetCurrentSlot()
|
|
|
|
{
|
|
|
|
return g_Config.iCurrentStateSlot;
|
|
|
|
}
|
|
|
|
|
2023-05-25 11:32:49 +00:00
|
|
|
void PrevSlot()
|
|
|
|
{
|
|
|
|
g_Config.iCurrentStateSlot = (g_Config.iCurrentStateSlot - 1 + NUM_SLOTS) % NUM_SLOTS;
|
|
|
|
}
|
|
|
|
|
2014-01-07 14:56:04 +00:00
|
|
|
void NextSlot()
|
|
|
|
{
|
2016-02-29 02:21:57 +00:00
|
|
|
g_Config.iCurrentStateSlot = (g_Config.iCurrentStateSlot + 1) % NUM_SLOTS;
|
2014-01-07 14:56:04 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
static void DeleteIfExists(const Path &fn) {
|
2018-05-21 03:51:29 +00:00
|
|
|
// Just avoiding error messages.
|
|
|
|
if (File::Exists(fn)) {
|
|
|
|
File::Delete(fn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
static void RenameIfExists(const Path &from, const Path &to) {
|
2018-05-21 03:51:29 +00:00
|
|
|
if (File::Exists(from)) {
|
2021-05-09 13:02:46 +00:00
|
|
|
File::Rename(from, to);
|
2018-05-21 03:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
static void SwapIfExists(const Path &from, const Path &to) {
|
2021-05-09 16:38:48 +00:00
|
|
|
Path temp = from.WithExtraExtension(".tmp");
|
2018-05-21 03:51:29 +00:00
|
|
|
if (File::Exists(from)) {
|
2021-05-09 13:02:46 +00:00
|
|
|
File::Rename(from, temp);
|
|
|
|
File::Rename(to, from);
|
|
|
|
File::Rename(temp, to);
|
2018-05-21 03:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 08:04:49 +00:00
|
|
|
void LoadSlot(const Path &gameFilename, int slot, Callback callback, void *cbUserData)
|
|
|
|
{
|
|
|
|
Path fn = GenerateSaveSlotFilename(gameFilename, slot, STATE_EXTENSION);
|
|
|
|
if (!fn.empty()) {
|
|
|
|
// This add only 1 extra state, should we just always enable it?
|
|
|
|
if (g_Config.bEnableStateUndo) {
|
|
|
|
Path backup = GetSysDirectory(DIRECTORY_SAVESTATE) / LOAD_UNDO_NAME;
|
|
|
|
|
2024-01-19 12:44:49 +00:00
|
|
|
auto saveCallback = [=](Status status, std::string_view message, void *data) {
|
2021-08-03 08:04:49 +00:00
|
|
|
if (status != Status::FAILURE) {
|
|
|
|
DeleteIfExists(backup);
|
|
|
|
File::Rename(backup.WithExtraExtension(".tmp"), backup);
|
2021-08-05 07:16:10 +00:00
|
|
|
g_Config.sStateLoadUndoGame = GenerateFullDiscId(gameFilename);
|
2021-08-11 06:35:41 +00:00
|
|
|
g_Config.Save("Saving config for savestate last load undo");
|
2021-08-05 07:16:10 +00:00
|
|
|
} else {
|
2024-01-19 12:44:49 +00:00
|
|
|
ERROR_LOG(SAVESTATE, "Saving load undo state failed: %.*s", (int)message.size(), message.data());
|
2021-08-03 08:04:49 +00:00
|
|
|
}
|
2021-08-05 07:16:10 +00:00
|
|
|
Load(fn, slot, callback, cbUserData);
|
2021-08-03 08:04:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!backup.empty()) {
|
2021-08-05 07:16:10 +00:00
|
|
|
Save(backup.WithExtraExtension(".tmp"), LOAD_UNDO_SLOT, saveCallback, cbUserData);
|
2021-08-03 08:04:49 +00:00
|
|
|
} else {
|
2021-08-05 07:16:10 +00:00
|
|
|
ERROR_LOG(SAVESTATE, "Saving load undo state failed. Error in the file system.");
|
|
|
|
Load(fn, slot, callback, cbUserData);
|
2021-08-03 08:04:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Load(fn, slot, callback, cbUserData);
|
|
|
|
}
|
|
|
|
} else {
|
2023-12-20 09:33:56 +00:00
|
|
|
if (callback) {
|
|
|
|
auto sy = GetI18NCategory(I18NCat::SYSTEM);
|
2021-08-03 08:04:49 +00:00
|
|
|
callback(Status::FAILURE, sy->T("Failed to load state. Error in the file system."), cbUserData);
|
2023-12-20 09:33:56 +00:00
|
|
|
}
|
2021-08-03 08:04:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UndoLoad(const Path &gameFilename, Callback callback, void *cbUserData)
|
|
|
|
{
|
2021-08-05 07:16:10 +00:00
|
|
|
if (g_Config.sStateLoadUndoGame != GenerateFullDiscId(gameFilename)) {
|
2023-12-20 09:33:56 +00:00
|
|
|
if (callback) {
|
|
|
|
auto sy = GetI18NCategory(I18NCat::SYSTEM);
|
2021-08-03 08:04:49 +00:00
|
|
|
callback(Status::FAILURE, sy->T("Error: load undo state is from a different game"), cbUserData);
|
2023-12-20 09:33:56 +00:00
|
|
|
}
|
2021-08-03 08:04:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Path fn = GetSysDirectory(DIRECTORY_SAVESTATE) / LOAD_UNDO_NAME;
|
|
|
|
if (!fn.empty()) {
|
2021-08-05 07:16:10 +00:00
|
|
|
Load(fn, LOAD_UNDO_SLOT, callback, cbUserData);
|
2021-08-03 08:04:49 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2023-12-20 09:33:56 +00:00
|
|
|
if (callback) {
|
|
|
|
auto sy = GetI18NCategory(I18NCat::SYSTEM);
|
2021-08-03 08:04:49 +00:00
|
|
|
callback(Status::FAILURE, sy->T("Failed to load state for load undo. Error in the file system."), cbUserData);
|
2023-12-20 09:33:56 +00:00
|
|
|
}
|
2021-08-03 08:04:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 06:00:22 +00:00
|
|
|
void SaveSlot(const Path &gameFilename, int slot, Callback callback, void *cbUserData)
|
2013-01-02 20:00:10 +00:00
|
|
|
{
|
2021-05-05 23:31:38 +00:00
|
|
|
Path fn = GenerateSaveSlotFilename(gameFilename, slot, STATE_EXTENSION);
|
|
|
|
Path fnUndo = GenerateSaveSlotFilename(gameFilename, slot, UNDO_STATE_EXTENSION);
|
2013-07-17 05:33:26 +00:00
|
|
|
if (!fn.empty()) {
|
2023-12-20 09:33:56 +00:00
|
|
|
Path shot = GenerateSaveSlotFilename(gameFilename, slot, SCREENSHOT_EXTENSION);
|
2024-01-19 12:44:49 +00:00
|
|
|
auto renameCallback = [=](Status status, std::string_view message, void *data) {
|
2018-06-15 00:52:44 +00:00
|
|
|
if (status != Status::FAILURE) {
|
2018-05-21 03:51:29 +00:00
|
|
|
if (g_Config.bEnableStateUndo) {
|
|
|
|
DeleteIfExists(fnUndo);
|
|
|
|
RenameIfExists(fn, fnUndo);
|
2021-08-07 11:28:43 +00:00
|
|
|
g_Config.sStateUndoLastSaveGame = GenerateFullDiscId(gameFilename);
|
|
|
|
g_Config.iStateUndoLastSaveSlot = slot;
|
2021-08-11 06:35:41 +00:00
|
|
|
g_Config.Save("Saving config for savestate last save undo");
|
2018-05-21 03:51:29 +00:00
|
|
|
} else {
|
|
|
|
DeleteIfExists(fn);
|
2014-12-21 06:24:02 +00:00
|
|
|
}
|
2021-05-09 16:38:48 +00:00
|
|
|
File::Rename(fn.WithExtraExtension(".tmp"), fn);
|
2014-12-21 06:24:02 +00:00
|
|
|
}
|
|
|
|
if (callback) {
|
2016-05-28 04:25:05 +00:00
|
|
|
callback(status, message, data);
|
2014-12-21 06:24:02 +00:00
|
|
|
}
|
|
|
|
};
|
2014-12-21 07:14:46 +00:00
|
|
|
// Let's also create a screenshot.
|
2018-05-21 03:51:29 +00:00
|
|
|
if (g_Config.bEnableStateUndo) {
|
2023-12-20 09:33:56 +00:00
|
|
|
Path shotUndo = GenerateSaveSlotFilename(gameFilename, slot, UNDO_SCREENSHOT_EXTENSION);
|
2018-05-21 03:51:29 +00:00
|
|
|
DeleteIfExists(shotUndo);
|
|
|
|
RenameIfExists(shot, shotUndo);
|
2018-03-17 21:00:06 +00:00
|
|
|
}
|
2014-12-21 07:34:11 +00:00
|
|
|
SaveScreenshot(shot, Callback(), 0);
|
2021-05-09 16:38:48 +00:00
|
|
|
Save(fn.WithExtraExtension(".tmp"), slot, renameCallback, cbUserData);
|
2013-07-17 05:33:26 +00:00
|
|
|
} else {
|
2023-12-20 09:33:56 +00:00
|
|
|
if (callback) {
|
|
|
|
auto sy = GetI18NCategory(I18NCat::SYSTEM);
|
2018-06-15 00:52:44 +00:00
|
|
|
callback(Status::FAILURE, sy->T("Failed to save state. Error in the file system."), cbUserData);
|
2023-12-20 09:33:56 +00:00
|
|
|
}
|
2013-07-17 05:33:26 +00:00
|
|
|
}
|
2013-01-02 20:00:10 +00:00
|
|
|
}
|
|
|
|
|
2023-12-20 09:33:56 +00:00
|
|
|
bool UndoSaveSlot(const Path &gameFilename, int slot) {
|
2021-05-05 23:31:38 +00:00
|
|
|
Path fnUndo = GenerateSaveSlotFilename(gameFilename, slot, UNDO_STATE_EXTENSION);
|
2018-03-17 21:00:06 +00:00
|
|
|
|
|
|
|
// Do nothing if there's no undo.
|
|
|
|
if (File::Exists(fnUndo)) {
|
2023-12-20 09:33:56 +00:00
|
|
|
Path fn = GenerateSaveSlotFilename(gameFilename, slot, STATE_EXTENSION);
|
|
|
|
Path shot = GenerateSaveSlotFilename(gameFilename, slot, SCREENSHOT_EXTENSION);
|
|
|
|
Path shotUndo = GenerateSaveSlotFilename(gameFilename, slot, UNDO_SCREENSHOT_EXTENSION);
|
2018-03-17 21:00:06 +00:00
|
|
|
// Swap them so they can undo again to redo. Mistakes happen.
|
2018-05-21 03:51:29 +00:00
|
|
|
SwapIfExists(shotUndo, shot);
|
|
|
|
SwapIfExists(fnUndo, fn);
|
2018-03-17 21:00:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-07 11:28:43 +00:00
|
|
|
|
|
|
|
bool UndoLastSave(const Path &gameFilename) {
|
|
|
|
if (g_Config.sStateUndoLastSaveGame != GenerateFullDiscId(gameFilename))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return UndoSaveSlot(gameFilename, g_Config.iStateUndoLastSaveSlot);
|
|
|
|
}
|
|
|
|
|
2021-05-15 06:00:22 +00:00
|
|
|
bool HasSaveInSlot(const Path &gameFilename, int slot)
|
2013-01-02 20:00:10 +00:00
|
|
|
{
|
2021-05-05 23:31:38 +00:00
|
|
|
Path fn = GenerateSaveSlotFilename(gameFilename, slot, STATE_EXTENSION);
|
2013-12-02 16:24:20 +00:00
|
|
|
return File::Exists(fn);
|
|
|
|
}
|
|
|
|
|
2021-05-15 06:00:22 +00:00
|
|
|
bool HasUndoSaveInSlot(const Path &gameFilename, int slot)
|
2018-03-17 21:00:06 +00:00
|
|
|
{
|
2021-08-02 13:40:59 +00:00
|
|
|
Path fn = GenerateSaveSlotFilename(gameFilename, slot, UNDO_STATE_EXTENSION);
|
|
|
|
return File::Exists(fn);
|
2018-03-17 21:00:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-07 11:28:43 +00:00
|
|
|
bool HasUndoLastSave(const Path &gameFilename)
|
|
|
|
{
|
|
|
|
if (g_Config.sStateUndoLastSaveGame != GenerateFullDiscId(gameFilename))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return HasUndoSaveInSlot(gameFilename, g_Config.iStateUndoLastSaveSlot);
|
|
|
|
}
|
|
|
|
|
2021-05-15 06:00:22 +00:00
|
|
|
bool HasScreenshotInSlot(const Path &gameFilename, int slot)
|
2013-12-02 16:24:20 +00:00
|
|
|
{
|
2021-05-05 23:31:38 +00:00
|
|
|
Path fn = GenerateSaveSlotFilename(gameFilename, slot, SCREENSHOT_EXTENSION);
|
2013-04-13 08:13:28 +00:00
|
|
|
return File::Exists(fn);
|
2013-01-02 20:00:10 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 08:04:49 +00:00
|
|
|
bool HasUndoLoad(const Path &gameFilename)
|
|
|
|
{
|
|
|
|
Path fn = GetSysDirectory(DIRECTORY_SAVESTATE) / LOAD_UNDO_NAME;
|
2021-08-05 07:16:10 +00:00
|
|
|
return File::Exists(fn) && g_Config.sStateLoadUndoGame == GenerateFullDiscId(gameFilename);
|
2021-08-03 08:04:49 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 20:00:10 +00:00
|
|
|
bool operator < (const tm &t1, const tm &t2) {
|
|
|
|
if (t1.tm_year < t2.tm_year) return true;
|
|
|
|
if (t1.tm_year > t2.tm_year) return false;
|
|
|
|
if (t1.tm_mon < t2.tm_mon) return true;
|
|
|
|
if (t1.tm_mon > t2.tm_mon) return false;
|
|
|
|
if (t1.tm_mday < t2.tm_mday) return true;
|
|
|
|
if (t1.tm_mday > t2.tm_mday) return false;
|
|
|
|
if (t1.tm_hour < t2.tm_hour) return true;
|
|
|
|
if (t1.tm_hour > t2.tm_hour) return false;
|
|
|
|
if (t1.tm_min < t2.tm_min) return true;
|
|
|
|
if (t1.tm_min > t2.tm_min) return false;
|
|
|
|
if (t1.tm_sec < t2.tm_sec) return true;
|
|
|
|
if (t1.tm_sec > t2.tm_sec) return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-21 08:00:57 +00:00
|
|
|
bool operator > (const tm &t1, const tm &t2) {
|
|
|
|
if (t1.tm_year > t2.tm_year) return true;
|
|
|
|
if (t1.tm_year < t2.tm_year) return false;
|
|
|
|
if (t1.tm_mon > t2.tm_mon) return true;
|
|
|
|
if (t1.tm_mon < t2.tm_mon) return false;
|
|
|
|
if (t1.tm_mday > t2.tm_mday) return true;
|
|
|
|
if (t1.tm_mday < t2.tm_mday) return false;
|
|
|
|
if (t1.tm_hour > t2.tm_hour) return true;
|
|
|
|
if (t1.tm_hour < t2.tm_hour) return false;
|
|
|
|
if (t1.tm_min > t2.tm_min) return true;
|
|
|
|
if (t1.tm_min < t2.tm_min) return false;
|
|
|
|
if (t1.tm_sec > t2.tm_sec) return true;
|
|
|
|
if (t1.tm_sec < t2.tm_sec) return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator ! (const tm &t1) {
|
|
|
|
if (t1.tm_year || t1.tm_mon || t1.tm_mday || t1.tm_hour || t1.tm_min || t1.tm_sec) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-15 06:00:22 +00:00
|
|
|
int GetNewestSlot(const Path &gameFilename) {
|
2013-01-02 20:00:10 +00:00
|
|
|
int newestSlot = -1;
|
|
|
|
tm newestDate = {0};
|
2016-02-29 02:21:57 +00:00
|
|
|
for (int i = 0; i < NUM_SLOTS; i++) {
|
2021-05-05 23:31:38 +00:00
|
|
|
Path fn = GenerateSaveSlotFilename(gameFilename, i, STATE_EXTENSION);
|
2013-01-02 20:00:10 +00:00
|
|
|
if (File::Exists(fn)) {
|
2015-02-01 18:54:07 +00:00
|
|
|
tm time;
|
|
|
|
bool success = File::GetModifTime(fn, time);
|
|
|
|
if (success && newestDate < time) {
|
2013-01-02 20:00:10 +00:00
|
|
|
newestDate = time;
|
|
|
|
newestSlot = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newestSlot;
|
|
|
|
}
|
|
|
|
|
2021-05-15 06:00:22 +00:00
|
|
|
int GetOldestSlot(const Path &gameFilename) {
|
2018-06-21 08:00:57 +00:00
|
|
|
int oldestSlot = -1;
|
|
|
|
tm oldestDate = {0};
|
|
|
|
for (int i = 0; i < NUM_SLOTS; i++) {
|
2021-05-05 23:31:38 +00:00
|
|
|
Path fn = GenerateSaveSlotFilename(gameFilename, i, STATE_EXTENSION);
|
2018-06-21 08:00:57 +00:00
|
|
|
if (File::Exists(fn)) {
|
|
|
|
tm time;
|
|
|
|
bool success = File::GetModifTime(fn, time);
|
|
|
|
if (success && (!oldestDate || oldestDate > time)) {
|
|
|
|
oldestDate = time;
|
|
|
|
oldestSlot = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return oldestSlot;
|
|
|
|
}
|
|
|
|
|
2021-05-15 06:00:22 +00:00
|
|
|
std::string GetSlotDateAsString(const Path &gameFilename, int slot) {
|
2021-05-05 23:31:38 +00:00
|
|
|
Path fn = GenerateSaveSlotFilename(gameFilename, slot, STATE_EXTENSION);
|
2014-12-31 19:42:28 +00:00
|
|
|
if (File::Exists(fn)) {
|
2015-02-01 18:54:07 +00:00
|
|
|
tm time;
|
|
|
|
if (File::GetModifTime(fn, time)) {
|
|
|
|
char buf[256];
|
|
|
|
// TODO: Use local time format? Americans and some others might not like ISO standard :)
|
2020-09-17 05:52:09 +00:00
|
|
|
switch (g_Config.iDateFormat) {
|
|
|
|
case PSP_SYSTEMPARAM_DATE_FORMAT_YYYYMMDD:
|
|
|
|
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &time);
|
|
|
|
break;
|
|
|
|
case PSP_SYSTEMPARAM_DATE_FORMAT_MMDDYYYY:
|
|
|
|
strftime(buf, sizeof(buf), "%m-%d-%Y %H:%M:%S", &time);
|
|
|
|
break;
|
|
|
|
case PSP_SYSTEMPARAM_DATE_FORMAT_DDMMYYYY:
|
|
|
|
strftime(buf, sizeof(buf), "%d-%m-%Y %H:%M:%S", &time);
|
|
|
|
break;
|
|
|
|
default: // Should never happen
|
|
|
|
return "";
|
|
|
|
}
|
2015-02-01 18:54:07 +00:00
|
|
|
return std::string(buf);
|
|
|
|
}
|
2014-12-31 19:42:28 +00:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2013-01-02 20:00:10 +00:00
|
|
|
|
2012-12-27 19:28:12 +00:00
|
|
|
std::vector<Operation> Flush()
|
|
|
|
{
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::mutex> guard(mutex);
|
2012-12-27 19:28:12 +00:00
|
|
|
std::vector<Operation> copy = pending;
|
|
|
|
pending.clear();
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2020-08-02 15:11:09 +00:00
|
|
|
bool HandleLoadFailure()
|
2013-09-15 04:19:10 +00:00
|
|
|
{
|
2023-02-14 21:46:32 +00:00
|
|
|
WARN_LOG(SAVESTATE, "HandleLoadFailure - trying a rewind state.");
|
|
|
|
|
2013-09-30 03:20:20 +00:00
|
|
|
// Okay, first, let's give the rewind state a shot - maybe we can at least not reset entirely.
|
|
|
|
// Even if this was a rewind, maybe we can still load a previous one.
|
|
|
|
CChunkFileReader::Error result;
|
2019-09-27 20:56:06 +00:00
|
|
|
do {
|
2020-08-02 15:11:09 +00:00
|
|
|
std::string errorString;
|
|
|
|
result = rewindStates.Restore(&errorString);
|
2019-09-27 20:56:06 +00:00
|
|
|
} while (result == CChunkFileReader::ERROR_BROKEN_STATE);
|
2013-09-30 03:20:20 +00:00
|
|
|
|
|
|
|
if (result == CChunkFileReader::ERROR_NONE) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We tried, our only remaining option is to reset the game.
|
2021-02-21 16:18:13 +00:00
|
|
|
needsRestart = true;
|
|
|
|
// Make sure we don't proceed to run anything yet.
|
|
|
|
coreState = CORE_NEXTFRAME;
|
2013-09-30 03:20:20 +00:00
|
|
|
return false;
|
2013-09-15 04:19:10 +00:00
|
|
|
}
|
|
|
|
|
2018-06-15 00:52:44 +00:00
|
|
|
bool HasLoadedState() {
|
2014-02-09 21:45:51 +00:00
|
|
|
return hasLoadedState;
|
|
|
|
}
|
|
|
|
|
2018-06-15 00:52:44 +00:00
|
|
|
bool IsStale() {
|
|
|
|
if (saveStateGeneration >= STALE_STATE_USES) {
|
2018-06-16 15:06:35 +00:00
|
|
|
return CoreTiming::GetGlobalTimeUs() > STALE_STATE_TIME;
|
2018-06-15 00:52:44 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsOldVersion() {
|
|
|
|
if (saveStateInitialGitVersion.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Version state(saveStateInitialGitVersion);
|
|
|
|
Version gitVer(PPSSPP_GIT_VERSION);
|
|
|
|
if (!state.IsValid() || !gitVer.IsValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return state < gitVer;
|
|
|
|
}
|
|
|
|
|
2021-05-08 16:42:52 +00:00
|
|
|
static Status TriggerLoadWarnings(std::string &callbackMessage) {
|
2023-04-05 22:34:50 +00:00
|
|
|
auto sc = GetI18NCategory(I18NCat::SCREEN);
|
2021-05-08 16:42:52 +00:00
|
|
|
|
|
|
|
if (g_Config.bHideStateWarnings)
|
|
|
|
return Status::SUCCESS;
|
|
|
|
|
|
|
|
if (IsStale()) {
|
|
|
|
// For anyone wondering why (too long to put on the screen in an osm):
|
|
|
|
// Using save states instead of saves simulates many hour play sessions.
|
|
|
|
// Sometimes this exposes game bugs that were rarely seen on real devices,
|
|
|
|
// because few people played on a real PSP for 10 hours straight.
|
|
|
|
callbackMessage = sc->T("Loaded. Save in game, restart, and load for less bugs.");
|
2023-07-07 08:33:27 +00:00
|
|
|
return Status::SUCCESS;
|
2021-05-08 16:42:52 +00:00
|
|
|
}
|
|
|
|
if (IsOldVersion()) {
|
|
|
|
// Save states also preserve bugs from old PPSSPP versions, so warn.
|
|
|
|
callbackMessage = sc->T("Loaded. Save in game, restart, and load for less bugs.");
|
2023-07-07 08:33:27 +00:00
|
|
|
return Status::SUCCESS;
|
2021-05-08 16:42:52 +00:00
|
|
|
}
|
|
|
|
// If the loaded state (saveDataGeneration) is older, the game may prevent saving again.
|
|
|
|
// This can happen with newer too, but ignore to/from 0 as a common likely safe case.
|
|
|
|
if (saveDataGeneration != lastSaveDataGeneration && saveDataGeneration != 0 && lastSaveDataGeneration != 0) {
|
|
|
|
if (saveDataGeneration < lastSaveDataGeneration)
|
|
|
|
callbackMessage = sc->T("Loaded. Game may refuse to save over newer savedata.");
|
|
|
|
else
|
|
|
|
callbackMessage = sc->T("Loaded. Game may refuse to save over different savedata.");
|
|
|
|
return Status::WARNING;
|
|
|
|
}
|
|
|
|
return Status::SUCCESS;
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:00:20 +00:00
|
|
|
void Process()
|
2012-12-27 19:28:12 +00:00
|
|
|
{
|
2023-02-13 10:17:03 +00:00
|
|
|
rewindStates.Process();
|
2013-09-30 02:57:51 +00:00
|
|
|
|
2013-08-07 06:00:20 +00:00
|
|
|
if (!needsProcess)
|
|
|
|
return;
|
|
|
|
needsProcess = false;
|
|
|
|
|
2012-12-29 01:23:05 +00:00
|
|
|
if (!__KernelIsRunning())
|
|
|
|
{
|
2017-03-06 12:10:23 +00:00
|
|
|
ERROR_LOG(SAVESTATE, "Savestate failure: Unable to load without kernel, this should never happen.");
|
2012-12-29 01:23:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-27 19:28:12 +00:00
|
|
|
std::vector<Operation> operations = Flush();
|
|
|
|
SaveStart state;
|
|
|
|
|
|
|
|
for (size_t i = 0, n = operations.size(); i < n; ++i)
|
|
|
|
{
|
|
|
|
Operation &op = operations[i];
|
2013-09-15 04:19:10 +00:00
|
|
|
CChunkFileReader::Error result;
|
2018-06-15 00:52:44 +00:00
|
|
|
Status callbackResult;
|
|
|
|
bool tempResult;
|
2016-05-28 04:25:05 +00:00
|
|
|
std::string callbackMessage;
|
2017-05-30 00:18:09 +00:00
|
|
|
std::string title;
|
2013-06-24 08:24:34 +00:00
|
|
|
|
2023-04-05 22:34:50 +00:00
|
|
|
auto sc = GetI18NCategory(I18NCat::SCREEN);
|
2024-01-19 12:44:49 +00:00
|
|
|
const char *i18nLoadFailure = sc->T_cstr("Failed to load state");
|
|
|
|
const char *i18nSaveFailure = sc->T_cstr("Failed to save state");
|
2013-06-24 07:58:29 +00:00
|
|
|
|
2020-05-16 13:20:22 +00:00
|
|
|
std::string slot_prefix = op.slot >= 0 ? StringFromFormat("(%d) ", op.slot + 1) : "";
|
2020-08-02 15:11:09 +00:00
|
|
|
std::string errorString;
|
2020-05-16 13:20:22 +00:00
|
|
|
|
2012-12-27 19:28:12 +00:00
|
|
|
switch (op.type)
|
|
|
|
{
|
|
|
|
case SAVESTATE_LOAD:
|
2020-08-02 13:41:00 +00:00
|
|
|
INFO_LOG(SAVESTATE, "Loading state from '%s'", op.filename.c_str());
|
2018-06-15 01:25:52 +00:00
|
|
|
// Use the state's latest version as a guess for saveStateInitialGitVersion.
|
2020-08-02 15:11:09 +00:00
|
|
|
result = CChunkFileReader::Load(op.filename, &saveStateInitialGitVersion, state, &errorString);
|
2013-09-15 04:19:10 +00:00
|
|
|
if (result == CChunkFileReader::ERROR_NONE) {
|
2021-05-08 16:42:52 +00:00
|
|
|
callbackMessage = op.slot != LOAD_UNDO_SLOT ? sc->T("Loaded State") : sc->T("State load undone");
|
|
|
|
callbackResult = TriggerLoadWarnings(callbackMessage);
|
2014-02-09 21:45:51 +00:00
|
|
|
hasLoadedState = true;
|
2021-08-09 06:15:27 +00:00
|
|
|
Core_ResetException();
|
2018-06-15 00:52:44 +00:00
|
|
|
|
2021-05-08 16:42:52 +00:00
|
|
|
if (!slot_prefix.empty())
|
|
|
|
callbackMessage = slot_prefix + callbackMessage;
|
2018-06-15 00:52:44 +00:00
|
|
|
|
2017-11-14 04:12:27 +00:00
|
|
|
#ifndef MOBILE_DEVICE
|
|
|
|
if (g_Config.bSaveLoadResetsAVdumping) {
|
|
|
|
if (g_Config.bDumpFrames) {
|
2017-11-14 05:33:49 +00:00
|
|
|
AVIDump::Stop();
|
|
|
|
AVIDump::Start(PSP_CoreParameter().renderWidth, PSP_CoreParameter().renderHeight);
|
2017-11-14 04:12:27 +00:00
|
|
|
}
|
|
|
|
if (g_Config.bDumpAudio) {
|
2017-11-14 05:33:49 +00:00
|
|
|
WAVDump::Reset();
|
2017-11-14 04:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2013-09-15 04:19:10 +00:00
|
|
|
} else if (result == CChunkFileReader::ERROR_BROKEN_STATE) {
|
2020-08-02 15:11:09 +00:00
|
|
|
HandleLoadFailure();
|
|
|
|
callbackMessage = std::string(i18nLoadFailure) + ": " + errorString;
|
|
|
|
ERROR_LOG(SAVESTATE, "Load state failure: %s", errorString.c_str());
|
2018-06-15 00:52:44 +00:00
|
|
|
callbackResult = Status::FAILURE;
|
2013-09-11 20:21:15 +00:00
|
|
|
} else {
|
2020-08-02 15:11:09 +00:00
|
|
|
callbackMessage = sc->T(errorString.c_str(), i18nLoadFailure);
|
2018-06-15 00:52:44 +00:00
|
|
|
callbackResult = Status::FAILURE;
|
2013-06-24 08:24:34 +00:00
|
|
|
}
|
2012-12-27 19:28:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SAVESTATE_SAVE:
|
2017-03-06 12:10:23 +00:00
|
|
|
INFO_LOG(SAVESTATE, "Saving state to %s", op.filename.c_str());
|
2017-05-30 00:18:09 +00:00
|
|
|
title = g_paramSFO.GetValueString("TITLE");
|
|
|
|
if (title.empty()) {
|
|
|
|
// Homebrew title
|
2021-05-05 23:31:38 +00:00
|
|
|
title = PSP_CoreParameter().fileToStart.ToVisualString();
|
2017-05-30 00:18:09 +00:00
|
|
|
std::size_t lslash = title.find_last_of("/");
|
|
|
|
title = title.substr(lslash + 1);
|
|
|
|
}
|
|
|
|
result = CChunkFileReader::Save(op.filename, title, PPSSPP_GIT_VERSION, state);
|
2013-09-15 04:19:10 +00:00
|
|
|
if (result == CChunkFileReader::ERROR_NONE) {
|
2024-01-19 12:44:49 +00:00
|
|
|
callbackMessage = slot_prefix + std::string(sc->T("Saved State"));
|
2018-06-15 00:52:44 +00:00
|
|
|
callbackResult = Status::SUCCESS;
|
2017-11-14 04:12:27 +00:00
|
|
|
#ifndef MOBILE_DEVICE
|
|
|
|
if (g_Config.bSaveLoadResetsAVdumping) {
|
|
|
|
if (g_Config.bDumpFrames) {
|
2017-11-14 05:33:49 +00:00
|
|
|
AVIDump::Stop();
|
|
|
|
AVIDump::Start(PSP_CoreParameter().renderWidth, PSP_CoreParameter().renderHeight);
|
2017-11-14 04:12:27 +00:00
|
|
|
}
|
|
|
|
if (g_Config.bDumpAudio) {
|
2017-11-14 05:33:49 +00:00
|
|
|
WAVDump::Reset();
|
2017-11-14 04:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2013-09-15 04:19:10 +00:00
|
|
|
} else if (result == CChunkFileReader::ERROR_BROKEN_STATE) {
|
2020-08-02 15:11:09 +00:00
|
|
|
// TODO: What else might we want to do here? This should be very unusual.
|
2016-05-28 04:25:05 +00:00
|
|
|
callbackMessage = i18nSaveFailure;
|
2020-08-02 15:11:09 +00:00
|
|
|
ERROR_LOG(SAVESTATE, "Save state failure");
|
2018-06-15 00:52:44 +00:00
|
|
|
callbackResult = Status::FAILURE;
|
2013-09-11 20:21:15 +00:00
|
|
|
} else {
|
2016-05-28 04:25:05 +00:00
|
|
|
callbackMessage = i18nSaveFailure;
|
2018-06-15 00:52:44 +00:00
|
|
|
callbackResult = Status::FAILURE;
|
2013-09-11 20:21:15 +00:00
|
|
|
}
|
2012-12-27 19:28:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SAVESTATE_VERIFY:
|
2018-06-15 00:52:44 +00:00
|
|
|
tempResult = CChunkFileReader::Verify(state) == CChunkFileReader::ERROR_NONE;
|
|
|
|
callbackResult = tempResult ? Status::SUCCESS : Status::FAILURE;
|
|
|
|
if (tempResult) {
|
2017-03-06 12:10:23 +00:00
|
|
|
INFO_LOG(SAVESTATE, "Verified save state system");
|
2016-05-28 04:25:05 +00:00
|
|
|
} else {
|
2017-03-06 12:10:23 +00:00
|
|
|
ERROR_LOG(SAVESTATE, "Save state system verification failed");
|
2016-05-28 04:25:05 +00:00
|
|
|
}
|
2012-12-27 19:28:12 +00:00
|
|
|
break;
|
|
|
|
|
2013-09-30 02:57:51 +00:00
|
|
|
case SAVESTATE_REWIND:
|
2017-03-06 12:10:23 +00:00
|
|
|
INFO_LOG(SAVESTATE, "Rewinding to recent savestate snapshot");
|
2020-08-02 15:11:09 +00:00
|
|
|
result = rewindStates.Restore(&errorString);
|
2013-09-30 02:57:51 +00:00
|
|
|
if (result == CChunkFileReader::ERROR_NONE) {
|
2016-05-28 04:25:05 +00:00
|
|
|
callbackMessage = sc->T("Loaded State");
|
2018-06-15 00:52:44 +00:00
|
|
|
callbackResult = Status::SUCCESS;
|
2014-02-09 21:45:51 +00:00
|
|
|
hasLoadedState = true;
|
2021-08-09 06:15:27 +00:00
|
|
|
Core_ResetException();
|
2013-09-30 02:57:51 +00:00
|
|
|
} else if (result == CChunkFileReader::ERROR_BROKEN_STATE) {
|
|
|
|
// Cripes. Good news is, we might have more. Let's try those too, better than a reset.
|
2020-08-02 15:11:09 +00:00
|
|
|
if (HandleLoadFailure()) {
|
2013-09-30 03:20:20 +00:00
|
|
|
// Well, we did rewind, even if too much...
|
2016-05-28 04:25:05 +00:00
|
|
|
callbackMessage = sc->T("Loaded State");
|
2018-06-15 00:52:44 +00:00
|
|
|
callbackResult = Status::SUCCESS;
|
2014-02-09 21:45:51 +00:00
|
|
|
hasLoadedState = true;
|
2021-08-09 06:15:27 +00:00
|
|
|
Core_ResetException();
|
2013-09-30 02:57:51 +00:00
|
|
|
} else {
|
2020-08-02 15:11:09 +00:00
|
|
|
callbackMessage = std::string(i18nLoadFailure) + ": " + errorString;
|
2018-06-15 00:52:44 +00:00
|
|
|
callbackResult = Status::FAILURE;
|
2013-09-30 02:57:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-08-02 15:11:09 +00:00
|
|
|
callbackMessage = std::string(i18nLoadFailure) + ": " + errorString;
|
2018-06-15 00:52:44 +00:00
|
|
|
callbackResult = Status::FAILURE;
|
2013-09-30 02:57:51 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-12-21 07:14:46 +00:00
|
|
|
case SAVESTATE_SAVE_SCREENSHOT:
|
2018-07-09 02:00:29 +00:00
|
|
|
{
|
2023-12-15 11:35:34 +00:00
|
|
|
int maxResMultiplier = 2;
|
|
|
|
tempResult = TakeGameScreenshot(op.filename, ScreenshotFormat::JPG, SCREENSHOT_DISPLAY, nullptr, nullptr, maxResMultiplier);
|
2018-06-15 00:52:44 +00:00
|
|
|
callbackResult = tempResult ? Status::SUCCESS : Status::FAILURE;
|
|
|
|
if (!tempResult) {
|
2017-03-06 12:10:23 +00:00
|
|
|
ERROR_LOG(SAVESTATE, "Failed to take a screenshot for the savestate! %s", op.filename.c_str());
|
2018-11-25 16:20:23 +00:00
|
|
|
if (screenshotFailures++ < SCREENSHOT_FAILURE_RETRIES) {
|
|
|
|
// Requeue for next frame.
|
|
|
|
SaveScreenshot(op.filename, op.callback, op.cbUserData);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
screenshotFailures = 0;
|
2014-12-31 19:42:28 +00:00
|
|
|
}
|
2014-12-21 07:14:46 +00:00
|
|
|
break;
|
2018-07-09 02:00:29 +00:00
|
|
|
}
|
2012-12-27 19:28:12 +00:00
|
|
|
default:
|
2017-03-06 12:10:23 +00:00
|
|
|
ERROR_LOG(SAVESTATE, "Savestate failure: unknown operation type %d", op.type);
|
2018-06-15 00:52:44 +00:00
|
|
|
callbackResult = Status::FAILURE;
|
2012-12-27 19:28:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-09-11 20:21:15 +00:00
|
|
|
if (op.callback)
|
2016-05-28 04:25:05 +00:00
|
|
|
op.callback(callbackResult, callbackMessage, op.cbUserData);
|
2012-12-27 19:28:12 +00:00
|
|
|
}
|
2014-06-14 21:55:16 +00:00
|
|
|
if (operations.size()) {
|
|
|
|
// Avoid triggering frame skipping due to slowdown
|
|
|
|
__DisplaySetWasPaused();
|
|
|
|
}
|
2012-12-27 19:28:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 16:42:52 +00:00
|
|
|
void NotifySaveData() {
|
|
|
|
saveDataGeneration++;
|
|
|
|
lastSaveDataGeneration = saveDataGeneration;
|
|
|
|
}
|
|
|
|
|
2021-02-21 16:18:13 +00:00
|
|
|
void Cleanup() {
|
|
|
|
if (needsRestart) {
|
|
|
|
PSP_Shutdown();
|
|
|
|
std::string resetError;
|
|
|
|
if (!PSP_Init(PSP_CoreParameter(), &resetError)) {
|
|
|
|
ERROR_LOG(BOOT, "Error resetting: %s", resetError.c_str());
|
|
|
|
// TODO: This probably doesn't clean up well enough.
|
|
|
|
Core_Stop();
|
|
|
|
return;
|
|
|
|
}
|
2023-03-21 10:10:09 +00:00
|
|
|
System_Notify(SystemNotification::BOOT_DONE);
|
2023-03-21 10:21:19 +00:00
|
|
|
System_Notify(SystemNotification::DISASSEMBLY);
|
2021-02-21 16:18:13 +00:00
|
|
|
needsRestart = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-27 19:28:12 +00:00
|
|
|
void Init()
|
|
|
|
{
|
2013-01-02 20:00:10 +00:00
|
|
|
// Make sure there's a directory for save slots
|
2018-09-02 17:27:11 +00:00
|
|
|
File::CreateFullPath(GetSysDirectory(DIRECTORY_SAVESTATE));
|
2013-01-02 22:25:35 +00:00
|
|
|
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::mutex> guard(mutex);
|
2013-11-03 01:32:34 +00:00
|
|
|
rewindStates.Clear();
|
2014-02-09 21:45:51 +00:00
|
|
|
|
|
|
|
hasLoadedState = false;
|
2018-06-15 00:52:44 +00:00
|
|
|
saveStateGeneration = 0;
|
2021-05-08 16:42:52 +00:00
|
|
|
saveDataGeneration = 0;
|
|
|
|
lastSaveDataGeneration = 0;
|
2018-06-15 00:52:44 +00:00
|
|
|
saveStateInitialGitVersion.clear();
|
2012-12-27 19:28:12 +00:00
|
|
|
}
|
2016-09-24 17:37:30 +00:00
|
|
|
|
|
|
|
void Shutdown()
|
|
|
|
{
|
2017-02-27 20:57:46 +00:00
|
|
|
std::lock_guard<std::mutex> guard(mutex);
|
2016-09-24 17:37:30 +00:00
|
|
|
rewindStates.Clear();
|
|
|
|
}
|
2012-12-27 19:28:12 +00:00
|
|
|
}
|