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
|
|
|
|
2013-12-29 22:28:31 +00:00
|
|
|
#include "i18n/i18n.h"
|
2016-09-24 17:37:30 +00:00
|
|
|
#include "thread/threadutil.h"
|
2018-06-15 00:52:44 +00:00
|
|
|
#include "util/text/parsers.h"
|
2013-12-29 22:28:31 +00:00
|
|
|
|
2013-05-22 16:00:06 +00:00
|
|
|
#include "Common/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"
|
2013-09-15 04:19:10 +00:00
|
|
|
#include "Core/Host.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-06-14 21:55:16 +00:00
|
|
|
#include "Core/HLE/sceDisplay.h"
|
2014-05-27 14:50:08 +00:00
|
|
|
#include "Core/HLE/ReplaceTables.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"
|
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
|
|
|
|
|
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.
|
|
|
|
Operation(OperationType t, const std::string &f, int slot_, Callback cb, void *cbUserData_)
|
|
|
|
: 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;
|
|
|
|
std::string filename;
|
|
|
|
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;
|
|
|
|
size_t sz = CChunkFileReader::MeasurePtr(state);
|
|
|
|
if (data.size() < sz)
|
|
|
|
data.resize(sz);
|
|
|
|
return CChunkFileReader::SavePtr(&data[0], state);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-09-30 02:57:51 +00:00
|
|
|
struct StateRingbuffer
|
|
|
|
{
|
2014-01-18 18:18:47 +00:00
|
|
|
StateRingbuffer(int size) : first_(0), next_(0), size_(size), base_(-1)
|
2013-09-30 02:57:51 +00:00
|
|
|
{
|
|
|
|
states_.resize(size);
|
2014-01-18 18:18:47 +00:00
|
|
|
baseMapping_.resize(size);
|
2013-09-30 02:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CChunkFileReader::Error Save()
|
|
|
|
{
|
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_;
|
|
|
|
|
2014-01-18 18:18:47 +00:00
|
|
|
static std::vector<u8> buffer;
|
|
|
|
std::vector<u8> *compressBuffer = &buffer;
|
|
|
|
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
|
|
|
|
err = SaveToRam(buffer);
|
|
|
|
|
|
|
|
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();
|
|
|
|
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]]);
|
2020-08-02 15:11:09 +00:00
|
|
|
return LoadFromRam(buffer, errorString);
|
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([=]{
|
2016-09-24 17:37:30 +00:00
|
|
|
setCurrentThreadName("SaveStateCompress");
|
|
|
|
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;
|
|
|
|
|
2014-01-18 18:18:47 +00:00
|
|
|
result.clear();
|
|
|
|
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);
|
|
|
|
result.insert(result.end(), state.begin() + i, state.begin() +i + blockSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result.push_back(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
basePos += blockSize;
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2014-12-03 20:16:11 +00:00
|
|
|
bool Empty() const
|
2013-11-03 01:32:34 +00:00
|
|
|
{
|
|
|
|
return next_ == first_;
|
|
|
|
}
|
|
|
|
|
2014-01-18 22:05:32 +00:00
|
|
|
static const int BLOCK_SIZE;
|
2014-01-18 18:18:47 +00:00
|
|
|
// TODO: Instead, based on size of compressed state?
|
2014-01-18 22:05:32 +00:00
|
|
|
static const int BASE_USAGE_INTERVAL;
|
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
|
|
|
|
2013-09-30 02:57:51 +00:00
|
|
|
int first_;
|
|
|
|
int next_;
|
|
|
|
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_;
|
2016-09-24 17:37:30 +00:00
|
|
|
|
2014-01-18 18:18:47 +00:00
|
|
|
int base_;
|
|
|
|
int baseUsage_;
|
2013-09-30 02:57:51 +00:00
|
|
|
};
|
|
|
|
|
2013-01-02 22:25:35 +00:00
|
|
|
static bool needsProcess = 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;
|
|
|
|
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?
|
2014-01-18 18:18:47 +00:00
|
|
|
static const int REWIND_NUM_STATES = 20;
|
2018-11-25 16:20:23 +00:00
|
|
|
static const int SCREENSHOT_FAILURE_RETRIES = 15;
|
2013-09-30 02:57:51 +00:00
|
|
|
static StateRingbuffer rewindStates(REWIND_NUM_STATES);
|
2013-09-30 03:04:23 +00:00
|
|
|
// TODO: Any reason for this to be configurable?
|
|
|
|
const static float rewindMaxWallFrequency = 1.0f;
|
2020-08-15 22:13:19 +00:00
|
|
|
static double rewindLastTime = 0.0f;
|
2014-01-18 22:05:32 +00:00
|
|
|
const int StateRingbuffer::BLOCK_SIZE = 8192;
|
|
|
|
const int StateRingbuffer::BASE_USAGE_INTERVAL = 15;
|
2013-09-30 02:57:51 +00:00
|
|
|
|
2012-12-27 19:28:12 +00:00
|
|
|
void SaveStart::DoState(PointerWrap &p)
|
|
|
|
{
|
2018-06-15 00:52:44 +00:00
|
|
|
auto s = p.Section("SaveStart", 1, 2);
|
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;
|
|
|
|
}
|
|
|
|
|
2012-12-27 19:58:15 +00:00
|
|
|
// Gotta do CoreTiming first since we'll restore into it.
|
2012-12-27 21:08:58 +00:00
|
|
|
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.
|
2014-05-31 06:29:03 +00:00
|
|
|
auto savedReplacements = SaveAndClearReplacements();
|
2013-09-29 20:51:09 +00:00
|
|
|
if (MIPSComp::jit && p.mode == p.MODE_WRITE)
|
|
|
|
{
|
2016-05-12 10:18:12 +00:00
|
|
|
std::vector<u32> savedBlocks;
|
2016-05-14 15:57:52 +00:00
|
|
|
savedBlocks = MIPSComp::jit->SaveAndClearEmuHackOps();
|
2013-09-29 20:51:09 +00:00
|
|
|
Memory::DoState(p);
|
2016-05-14 15:57:52 +00:00
|
|
|
MIPSComp::jit->RestoreSavedEmuHackOps(savedBlocks);
|
2013-09-29 20:51:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
Memory::DoState(p);
|
2014-05-31 06:29:03 +00:00
|
|
|
RestoreSavedReplacements(savedReplacements);
|
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);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void Enqueue(SaveState::Operation op)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2020-05-16 13:20:22 +00:00
|
|
|
void Load(const std::string &filename, int slot, Callback callback, void *cbUserData)
|
2012-12-27 19:28:12 +00:00
|
|
|
{
|
2020-05-16 13:20:22 +00:00
|
|
|
Enqueue(Operation(SAVESTATE_LOAD, filename, slot, callback, cbUserData));
|
2012-12-27 19:28:12 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 13:20:22 +00:00
|
|
|
void Save(const std::string &filename, int slot, Callback callback, void *cbUserData)
|
2012-12-27 19:28:12 +00:00
|
|
|
{
|
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)
|
|
|
|
{
|
2020-05-16 13:20:22 +00:00
|
|
|
Enqueue(Operation(SAVESTATE_VERIFY, std::string(""), -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)
|
|
|
|
{
|
2020-05-16 13:20:22 +00:00
|
|
|
Enqueue(Operation(SAVESTATE_REWIND, std::string(""), -1, callback, cbUserData));
|
2013-09-30 02:57:51 +00:00
|
|
|
}
|
|
|
|
|
2014-12-21 07:14:46 +00:00
|
|
|
void SaveScreenshot(const std::string &filename, Callback callback, void *cbUserData)
|
|
|
|
{
|
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)) {
|
2020-01-26 18:43:18 +00:00
|
|
|
auto sy = GetI18NCategory("System");
|
2018-03-17 21:24:23 +00:00
|
|
|
// Allow the number to be positioned where it makes sense.
|
|
|
|
std::string undo = sy->T("undo %c");
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetTitle(const std::string &filename) {
|
|
|
|
std::string title;
|
|
|
|
if (CChunkFileReader::GetFileTitle(filename, &title) == CChunkFileReader::ERROR_NONE) {
|
|
|
|
if (title.empty()) {
|
|
|
|
return File::GetFilename(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
return AppendSlotTitle(filename, title);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The file can't be loaded - let's note that.
|
2020-01-26 18:43:18 +00:00
|
|
|
auto sy = GetI18NCategory("System");
|
2016-01-23 21:06:30 +00:00
|
|
|
return File::GetFilename(filename) + " " + sy->T("(broken)");
|
|
|
|
}
|
|
|
|
|
2015-09-23 17:29:39 +00:00
|
|
|
std::string GenerateSaveSlotFilename(const std::string &gameFilename, int slot, const char *extension)
|
2013-01-02 20:00:10 +00:00
|
|
|
{
|
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");
|
2015-09-23 17:29:39 +00:00
|
|
|
std::string fullDiscId;
|
2017-05-29 01:42:45 +00:00
|
|
|
if (discId.empty()) {
|
|
|
|
discId = g_paramSFO.GenerateFakeID();
|
|
|
|
discVer = "1.00";
|
2015-09-23 17:29:39 +00:00
|
|
|
}
|
2017-05-29 01:42:45 +00:00
|
|
|
fullDiscId = StringFromFormat("%s_%s", discId.c_str(), discVer.c_str());
|
2015-09-23 17:29:39 +00:00
|
|
|
|
2018-09-02 17:27:11 +00:00
|
|
|
std::string filename = StringFromFormat("%s_%d.%s", fullDiscId.c_str(), slot, extension);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-09-23 17:29:39 +00:00
|
|
|
void LoadSlot(const std::string &gameFilename, int slot, Callback callback, void *cbUserData)
|
2013-01-02 20:00:10 +00:00
|
|
|
{
|
2015-09-23 17:29:39 +00:00
|
|
|
std::string fn = GenerateSaveSlotFilename(gameFilename, slot, STATE_EXTENSION);
|
2013-07-17 05:33:26 +00:00
|
|
|
if (!fn.empty()) {
|
2020-05-16 13:20:22 +00:00
|
|
|
Load(fn, slot, callback, cbUserData);
|
2013-07-17 05:33:26 +00:00
|
|
|
} else {
|
2020-01-26 18:43:18 +00:00
|
|
|
auto sy = GetI18NCategory("System");
|
2013-09-11 20:21:15 +00:00
|
|
|
if (callback)
|
2018-06-15 00:52:44 +00:00
|
|
|
callback(Status::FAILURE, sy->T("Failed to load state. Error in the file system."), cbUserData);
|
2013-07-17 05:33:26 +00:00
|
|
|
}
|
2013-01-02 20:00:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 03:51:29 +00:00
|
|
|
static void DeleteIfExists(const std::string &fn) {
|
|
|
|
// Just avoiding error messages.
|
|
|
|
if (File::Exists(fn)) {
|
|
|
|
File::Delete(fn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RenameIfExists(const std::string &from, const std::string &to) {
|
|
|
|
if (File::Exists(from)) {
|
|
|
|
File::Rename(from, to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SwapIfExists(const std::string &from, const std::string &to) {
|
|
|
|
std::string temp = from + ".tmp";
|
|
|
|
if (File::Exists(from)) {
|
|
|
|
File::Rename(from, temp);
|
|
|
|
File::Rename(to, from);
|
|
|
|
File::Rename(temp, to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-23 17:29:39 +00:00
|
|
|
void SaveSlot(const std::string &gameFilename, int slot, Callback callback, void *cbUserData)
|
2013-01-02 20:00:10 +00:00
|
|
|
{
|
2015-09-23 17:29:39 +00:00
|
|
|
std::string fn = GenerateSaveSlotFilename(gameFilename, slot, STATE_EXTENSION);
|
|
|
|
std::string shot = GenerateSaveSlotFilename(gameFilename, slot, SCREENSHOT_EXTENSION);
|
2018-03-17 21:00:06 +00:00
|
|
|
std::string fnUndo = GenerateSaveSlotFilename(gameFilename, slot, UNDO_STATE_EXTENSION);
|
|
|
|
std::string shotUndo = GenerateSaveSlotFilename(gameFilename, slot, UNDO_SCREENSHOT_EXTENSION);
|
2013-07-17 05:33:26 +00:00
|
|
|
if (!fn.empty()) {
|
2018-06-15 00:52:44 +00:00
|
|
|
auto renameCallback = [=](Status status, const std::string &message, void *data) {
|
|
|
|
if (status != Status::FAILURE) {
|
2018-05-21 03:51:29 +00:00
|
|
|
if (g_Config.bEnableStateUndo) {
|
|
|
|
DeleteIfExists(fnUndo);
|
|
|
|
RenameIfExists(fn, fnUndo);
|
|
|
|
} else {
|
|
|
|
DeleteIfExists(fn);
|
2014-12-21 06:24:02 +00:00
|
|
|
}
|
|
|
|
File::Rename(fn + ".tmp", fn);
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
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);
|
2020-05-16 13:20:22 +00:00
|
|
|
Save(fn + ".tmp", slot, renameCallback, cbUserData);
|
2013-07-17 05:33:26 +00:00
|
|
|
} else {
|
2020-01-26 18:43:18 +00:00
|
|
|
auto sy = GetI18NCategory("System");
|
2013-09-11 20:21:15 +00:00
|
|
|
if (callback)
|
2018-06-15 00:52:44 +00:00
|
|
|
callback(Status::FAILURE, sy->T("Failed to save state. Error in the file system."), cbUserData);
|
2013-07-17 05:33:26 +00:00
|
|
|
}
|
2013-01-02 20:00:10 +00:00
|
|
|
}
|
|
|
|
|
2018-03-17 21:00:06 +00:00
|
|
|
bool UndoSaveSlot(const std::string &gameFilename, int slot) {
|
|
|
|
std::string fn = GenerateSaveSlotFilename(gameFilename, slot, STATE_EXTENSION);
|
|
|
|
std::string shot = GenerateSaveSlotFilename(gameFilename, slot, SCREENSHOT_EXTENSION);
|
|
|
|
std::string fnUndo = GenerateSaveSlotFilename(gameFilename, slot, UNDO_STATE_EXTENSION);
|
|
|
|
std::string shotUndo = GenerateSaveSlotFilename(gameFilename, slot, UNDO_SCREENSHOT_EXTENSION);
|
|
|
|
|
|
|
|
// Do nothing if there's no undo.
|
|
|
|
if (File::Exists(fnUndo)) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2015-09-23 17:29:39 +00:00
|
|
|
bool HasSaveInSlot(const std::string &gameFilename, int slot)
|
2013-01-02 20:00:10 +00:00
|
|
|
{
|
2015-09-23 17:29:39 +00:00
|
|
|
std::string fn = GenerateSaveSlotFilename(gameFilename, slot, STATE_EXTENSION);
|
2013-12-02 16:24:20 +00:00
|
|
|
return File::Exists(fn);
|
|
|
|
}
|
|
|
|
|
2018-03-17 21:00:06 +00:00
|
|
|
bool HasUndoSaveInSlot(const std::string &gameFilename, int slot)
|
|
|
|
{
|
|
|
|
std::string fn = GenerateSaveSlotFilename(gameFilename, slot, STATE_EXTENSION);
|
|
|
|
return File::Exists(fn + ".undo");
|
|
|
|
}
|
|
|
|
|
2015-09-23 17:29:39 +00:00
|
|
|
bool HasScreenshotInSlot(const std::string &gameFilename, int slot)
|
2013-12-02 16:24:20 +00:00
|
|
|
{
|
2015-09-23 17:29:39 +00:00
|
|
|
std::string 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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-09-23 17:29:39 +00:00
|
|
|
int GetNewestSlot(const std::string &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++) {
|
2015-09-23 17:29:39 +00:00
|
|
|
std::string 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;
|
|
|
|
}
|
|
|
|
|
2018-06-21 08:00:57 +00:00
|
|
|
int GetOldestSlot(const std::string &gameFilename) {
|
|
|
|
int oldestSlot = -1;
|
|
|
|
tm oldestDate = {0};
|
|
|
|
for (int i = 0; i < NUM_SLOTS; i++) {
|
|
|
|
std::string fn = GenerateSaveSlotFilename(gameFilename, i, STATE_EXTENSION);
|
|
|
|
if (File::Exists(fn)) {
|
|
|
|
tm time;
|
|
|
|
bool success = File::GetModifTime(fn, time);
|
|
|
|
if (success && (!oldestDate || oldestDate > time)) {
|
|
|
|
oldestDate = time;
|
|
|
|
oldestSlot = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return oldestSlot;
|
|
|
|
}
|
|
|
|
|
2015-09-23 17:29:39 +00:00
|
|
|
std::string GetSlotDateAsString(const std::string &gameFilename, int slot) {
|
|
|
|
std::string 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
|
|
|
{
|
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.
|
2013-09-15 04:19:10 +00:00
|
|
|
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();
|
2013-09-30 03:20:20 +00:00
|
|
|
return false;
|
2013-09-15 04:19:10 +00:00
|
|
|
}
|
|
|
|
host->BootDone();
|
|
|
|
host->UpdateDisassembly();
|
2013-09-30 03:20:20 +00:00
|
|
|
return false;
|
2013-09-15 04:19:10 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 02:38:22 +00:00
|
|
|
#ifndef MOBILE_DEVICE
|
2013-09-30 03:04:23 +00:00
|
|
|
static inline void CheckRewindState()
|
|
|
|
{
|
2013-11-03 01:33:23 +00:00
|
|
|
if (gpuStats.numFlips % g_Config.iRewindFlipFrequency != 0)
|
2013-09-30 03:04:23 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// For fast-forwarding, otherwise they may be useless and too close.
|
2020-09-24 21:52:03 +00:00
|
|
|
double now = time_now_d();
|
|
|
|
float diff = now - rewindLastTime;
|
2013-09-30 03:04:23 +00:00
|
|
|
if (diff < rewindMaxWallFrequency)
|
|
|
|
return;
|
|
|
|
|
2020-09-24 21:52:03 +00:00
|
|
|
rewindLastTime = now;
|
2013-12-05 15:15:33 +00:00
|
|
|
DEBUG_LOG(BOOT, "saving rewind state");
|
2013-09-30 03:04:23 +00:00
|
|
|
rewindStates.Save();
|
|
|
|
}
|
2017-12-04 02:38:22 +00:00
|
|
|
#endif
|
2013-09-30 03:04:23 +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;
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:00:20 +00:00
|
|
|
void Process()
|
2012-12-27 19:28:12 +00:00
|
|
|
{
|
2014-02-08 18:29:22 +00:00
|
|
|
#ifndef MOBILE_DEVICE
|
2013-11-03 01:33:23 +00:00
|
|
|
if (g_Config.iRewindFlipFrequency != 0 && gpuStats.numFlips != 0)
|
2013-09-30 03:04:23 +00:00
|
|
|
CheckRewindState();
|
2013-12-05 15:15:33 +00:00
|
|
|
#endif
|
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
|
|
|
|
2020-01-26 18:43:18 +00:00
|
|
|
auto sc = GetI18NCategory("Screen");
|
2015-07-01 22:50:07 +00:00
|
|
|
const char *i18nLoadFailure = sc->T("Load savestate failed", "");
|
|
|
|
const char *i18nSaveFailure = sc->T("Save State Failed", "");
|
2013-09-30 03:24:23 +00:00
|
|
|
if (strlen(i18nLoadFailure) == 0)
|
2015-07-01 22:50:07 +00:00
|
|
|
i18nLoadFailure = sc->T("Failed to load state");
|
2013-09-30 03:24:23 +00:00
|
|
|
if (strlen(i18nSaveFailure) == 0)
|
2015-07-01 22:50:07 +00:00
|
|
|
i18nSaveFailure = sc->T("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) {
|
2020-05-16 13:20:22 +00:00
|
|
|
callbackMessage = slot_prefix + 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;
|
2018-06-15 00:52:44 +00:00
|
|
|
|
2018-06-15 00:54:13 +00:00
|
|
|
if (!g_Config.bHideStateWarnings && IsStale()) {
|
2018-06-15 00:52:44 +00:00
|
|
|
// 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.
|
2020-05-16 13:20:22 +00:00
|
|
|
callbackMessage = slot_prefix + sc->T("Loaded. Save in game, restart, and load for less bugs.");
|
2018-06-15 00:52:44 +00:00
|
|
|
callbackResult = Status::WARNING;
|
2018-06-15 00:54:13 +00:00
|
|
|
} else if (!g_Config.bHideStateWarnings && IsOldVersion()) {
|
2018-06-15 00:52:44 +00:00
|
|
|
// Save states also preserve bugs from old PPSSPP versions, so warn.
|
2020-05-16 13:20:22 +00:00
|
|
|
callbackMessage = slot_prefix + sc->T("Loaded. Save in game, restart, and load for less bugs.");
|
2018-06-15 00:52:44 +00:00
|
|
|
callbackResult = Status::WARNING;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
title = PSP_CoreParameter().fileToStart;
|
|
|
|
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) {
|
2020-05-16 13:20:22 +00:00
|
|
|
callbackMessage = slot_prefix + 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;
|
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;
|
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
|
|
|
{
|
|
|
|
int maxRes = g_Config.iInternalResolution > 2 ? 2 : -1;
|
|
|
|
tempResult = TakeGameScreenshot(op.filename.c_str(), ScreenshotFormat::JPG, SCREENSHOT_DISPLAY, nullptr, nullptr, maxRes);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
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
|
|
|
}
|