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/.
|
|
|
|
|
2012-12-27 19:28:12 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2013-12-29 22:28:31 +00:00
|
|
|
#include "base/timeutil.h"
|
|
|
|
#include "i18n/i18n.h"
|
|
|
|
|
2013-05-22 16:00:06 +00:00
|
|
|
#include "Common/StdMutex.h"
|
|
|
|
#include "Common/FileUtil.h"
|
2013-12-29 22:28:31 +00:00
|
|
|
#include "Common/ChunkFile.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"
|
|
|
|
#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"
|
|
|
|
#include "Core/HLE/sceKernel.h"
|
|
|
|
#include "Core/MemMap.h"
|
|
|
|
#include "Core/MIPS/MIPS.h"
|
|
|
|
#include "Core/MIPS/JitCommon/JitCommon.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"
|
2013-05-22 16:00:06 +00:00
|
|
|
#include "UI/OnScreenDisplay.h"
|
2012-12-27 18:34:22 +00:00
|
|
|
|
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,
|
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
|
|
|
|
{
|
2013-01-02 20:00:10 +00:00
|
|
|
Operation(OperationType t, const std::string &f, Callback cb, void *cbUserData_)
|
|
|
|
: type(t), filename(f), callback(cb), 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;
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
CChunkFileReader::Error LoadFromRam(std::vector<u8> &data) {
|
|
|
|
SaveStart state;
|
|
|
|
return CChunkFileReader::LoadPtr(&data[0], state);
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
Compress(states_[n], *compressBuffer, bases_[base_]);
|
|
|
|
else
|
|
|
|
states_[n].clear();
|
|
|
|
baseMapping_[n] = base_;
|
|
|
|
return err;
|
2013-09-30 02:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CChunkFileReader::Error Restore()
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
Decompress(buffer, states_[n], bases_[baseMapping_[n]]);
|
|
|
|
return LoadFromRam(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Compress(std::vector<u8> &result, const std::vector<u8> &state, const std::vector<u8> &base)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Decompress(std::vector<u8> &result, const std::vector<u8> &compressed, const std::vector<u8> &base)
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
first_ = 0;
|
|
|
|
next_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Empty()
|
|
|
|
{
|
|
|
|
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;
|
2013-09-30 02:57:51 +00:00
|
|
|
typedef std::vector<u8> StateBuffer;
|
|
|
|
int first_;
|
|
|
|
int next_;
|
|
|
|
int size_;
|
|
|
|
std::vector<StateBuffer> states_;
|
2014-01-18 18:18:47 +00:00
|
|
|
StateBuffer bases_[2];
|
|
|
|
std::vector<int> baseMapping_;
|
|
|
|
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;
|
|
|
|
static std::recursive_mutex mutex;
|
|
|
|
|
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;
|
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;
|
|
|
|
static float 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)
|
|
|
|
{
|
2013-09-15 03:23:03 +00:00
|
|
|
auto s = p.Section("SaveStart", 1);
|
|
|
|
if (!s)
|
|
|
|
return;
|
|
|
|
|
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.
|
|
|
|
if (MIPSComp::jit && p.mode == p.MODE_WRITE)
|
|
|
|
{
|
|
|
|
auto blocks = MIPSComp::jit->GetBlockCache();
|
|
|
|
auto saved = blocks->SaveAndClearEmuHackOps();
|
|
|
|
Memory::DoState(p);
|
|
|
|
blocks->RestoreSavedEmuHackOps(saved);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Memory::DoState(p);
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> guard(mutex);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-01-02 20:00:10 +00:00
|
|
|
void Load(const std::string &filename, Callback callback, void *cbUserData)
|
2012-12-27 19:28:12 +00:00
|
|
|
{
|
2013-01-02 20:00:10 +00:00
|
|
|
Enqueue(Operation(SAVESTATE_LOAD, filename, callback, cbUserData));
|
2012-12-27 19:28:12 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 20:00:10 +00:00
|
|
|
void Save(const std::string &filename, Callback callback, void *cbUserData)
|
2012-12-27 19:28:12 +00:00
|
|
|
{
|
2013-01-02 20:00:10 +00:00
|
|
|
Enqueue(Operation(SAVESTATE_SAVE, filename, callback, cbUserData));
|
2012-12-27 19:28:12 +00:00
|
|
|
}
|
|
|
|
|
2013-04-13 08:13:28 +00:00
|
|
|
void Verify(Callback callback, void *cbUserData)
|
|
|
|
{
|
|
|
|
Enqueue(Operation(SAVESTATE_VERIFY, std::string(""), callback, cbUserData));
|
|
|
|
}
|
|
|
|
|
2013-09-30 02:57:51 +00:00
|
|
|
void Rewind(Callback callback, void *cbUserData)
|
|
|
|
{
|
|
|
|
Enqueue(Operation(SAVESTATE_REWIND, std::string(""), callback, cbUserData));
|
|
|
|
}
|
|
|
|
|
2013-11-03 01:32:34 +00:00
|
|
|
bool CanRewind()
|
|
|
|
{
|
|
|
|
return !rewindStates.Empty();
|
|
|
|
}
|
|
|
|
|
2013-12-02 16:24:20 +00:00
|
|
|
static const char *STATE_EXTENSION = "ppst";
|
|
|
|
static const char *SCREENSHOT_EXTENSION = "jpg";
|
2013-01-02 20:00:10 +00:00
|
|
|
// Slot utilities
|
|
|
|
|
2013-12-02 16:24:20 +00:00
|
|
|
std::string GenerateSaveSlotFilename(int slot, const char *extension)
|
2013-01-02 20:00:10 +00:00
|
|
|
{
|
|
|
|
char discID[256];
|
|
|
|
char temp[256];
|
|
|
|
sprintf(discID, "%s_%s",
|
|
|
|
g_paramSFO.GetValueString("DISC_ID").c_str(),
|
|
|
|
g_paramSFO.GetValueString("DISC_VERSION").c_str());
|
2013-12-02 16:24:20 +00:00
|
|
|
sprintf(temp, "ms0:/PSP/PPSSPP_STATE/%s_%i.%s", discID, slot, extension);
|
2013-01-02 20:00:10 +00:00
|
|
|
std::string hostPath;
|
|
|
|
if (pspFileSystem.GetHostPath(std::string(temp), hostPath)) {
|
|
|
|
return hostPath;
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 14:56:04 +00:00
|
|
|
void NextSlot()
|
|
|
|
{
|
|
|
|
I18NCategory *sy = GetI18NCategory("System");
|
|
|
|
g_Config.iCurrentStateSlot = (g_Config.iCurrentStateSlot + 1) % SaveState::SAVESTATESLOTS;
|
|
|
|
char msg[30];
|
|
|
|
sprintf(msg, "%s: %d", sy->T("Savestate Slot"), g_Config.iCurrentStateSlot + 1);
|
|
|
|
osm.Show(msg);
|
|
|
|
}
|
|
|
|
|
2013-01-02 20:00:10 +00:00
|
|
|
void LoadSlot(int slot, Callback callback, void *cbUserData)
|
|
|
|
{
|
2013-12-02 16:24:20 +00:00
|
|
|
std::string fn = GenerateSaveSlotFilename(slot, STATE_EXTENSION);
|
2013-07-17 05:33:26 +00:00
|
|
|
if (!fn.empty()) {
|
2013-01-02 20:00:10 +00:00
|
|
|
Load(fn, callback, cbUserData);
|
2013-07-17 05:33:26 +00:00
|
|
|
} else {
|
|
|
|
I18NCategory *s = GetI18NCategory("Screen");
|
|
|
|
osm.Show("Failed to load state. Error in the file system.", 2.0);
|
2013-09-11 20:21:15 +00:00
|
|
|
if (callback)
|
|
|
|
(*callback)(false, cbUserData);
|
2013-07-17 05:33:26 +00:00
|
|
|
}
|
2013-01-02 20:00:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SaveSlot(int slot, Callback callback, void *cbUserData)
|
|
|
|
{
|
2013-12-02 16:24:20 +00:00
|
|
|
std::string fn = GenerateSaveSlotFilename(slot, STATE_EXTENSION);
|
2013-07-17 05:33:26 +00:00
|
|
|
if (!fn.empty()) {
|
2013-01-02 20:00:10 +00:00
|
|
|
Save(fn, callback, cbUserData);
|
2013-07-17 05:33:26 +00:00
|
|
|
} else {
|
|
|
|
I18NCategory *s = GetI18NCategory("Screen");
|
|
|
|
osm.Show("Failed to save state. Error in the file system.", 2.0);
|
2013-09-11 20:21:15 +00:00
|
|
|
if (callback)
|
|
|
|
(*callback)(false, cbUserData);
|
2013-07-17 05:33:26 +00:00
|
|
|
}
|
2013-01-02 20:00:10 +00:00
|
|
|
}
|
|
|
|
|
2013-04-13 08:13:28 +00:00
|
|
|
bool HasSaveInSlot(int slot)
|
2013-01-02 20:00:10 +00:00
|
|
|
{
|
2013-12-02 16:24:20 +00:00
|
|
|
std::string fn = GenerateSaveSlotFilename(slot, STATE_EXTENSION);
|
|
|
|
return File::Exists(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasScreenshotInSlot(int slot)
|
|
|
|
{
|
|
|
|
std::string fn = GenerateSaveSlotFilename(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;
|
|
|
|
}
|
|
|
|
|
2013-10-30 17:51:25 +00:00
|
|
|
int GetNewestSlot() {
|
2013-01-02 20:00:10 +00:00
|
|
|
int newestSlot = -1;
|
|
|
|
tm newestDate = {0};
|
|
|
|
for (int i = 0; i < SAVESTATESLOTS; i++) {
|
2013-12-02 16:24:20 +00:00
|
|
|
std::string fn = GenerateSaveSlotFilename(i, STATE_EXTENSION);
|
2013-01-02 20:00:10 +00:00
|
|
|
if (File::Exists(fn)) {
|
|
|
|
tm time = File::GetModifTime(fn);
|
|
|
|
if (newestDate < time) {
|
|
|
|
newestDate = time;
|
|
|
|
newestSlot = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newestSlot;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-27 19:28:12 +00:00
|
|
|
std::vector<Operation> Flush()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> guard(mutex);
|
|
|
|
std::vector<Operation> copy = pending;
|
|
|
|
pending.clear();
|
|
|
|
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
2013-09-30 03:20:20 +00:00
|
|
|
bool HandleFailure()
|
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;
|
|
|
|
do
|
|
|
|
result = rewindStates.Restore();
|
|
|
|
while (result == CChunkFileReader::ERROR_BROKEN_STATE);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
time_update();
|
|
|
|
float diff = time_now() - rewindLastTime;
|
|
|
|
if (diff < rewindMaxWallFrequency)
|
|
|
|
return;
|
|
|
|
|
|
|
|
rewindLastTime = time_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();
|
|
|
|
}
|
|
|
|
|
2013-08-07 06:00:20 +00:00
|
|
|
void Process()
|
2012-12-27 19:28:12 +00:00
|
|
|
{
|
2013-12-05 15:15:33 +00:00
|
|
|
#ifndef USING_GLES2
|
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())
|
|
|
|
{
|
|
|
|
ERROR_LOG(COMMON, "Savestate failure: Unable to load without kernel, this should never happen.");
|
|
|
|
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;
|
|
|
|
bool callbackResult;
|
2013-06-24 08:24:34 +00:00
|
|
|
std::string reason;
|
|
|
|
|
2013-09-30 03:24:23 +00:00
|
|
|
I18NCategory *s = GetI18NCategory("Screen");
|
|
|
|
// I couldn't stand the inconsistency. But trying not to break old lang files.
|
|
|
|
const char *i18nLoadFailure = s->T("Load savestate failed", "");
|
|
|
|
const char *i18nSaveFailure = s->T("Save State Failed", "");
|
|
|
|
if (strlen(i18nLoadFailure) == 0)
|
|
|
|
i18nLoadFailure = s->T("Failed to load state");
|
|
|
|
if (strlen(i18nSaveFailure) == 0)
|
|
|
|
i18nSaveFailure = s->T("Failed to save state");
|
2013-06-24 07:58:29 +00:00
|
|
|
|
2012-12-27 19:28:12 +00:00
|
|
|
switch (op.type)
|
|
|
|
{
|
|
|
|
case SAVESTATE_LOAD:
|
|
|
|
INFO_LOG(COMMON, "Loading state from %s", op.filename.c_str());
|
2013-09-15 03:28:41 +00:00
|
|
|
result = CChunkFileReader::Load(op.filename, REVISION, PPSSPP_GIT_VERSION, state, &reason);
|
2013-09-15 04:19:10 +00:00
|
|
|
if (result == CChunkFileReader::ERROR_NONE) {
|
2013-06-24 09:09:45 +00:00
|
|
|
osm.Show(s->T("Loaded State"), 2.0);
|
2013-09-15 04:19:10 +00:00
|
|
|
callbackResult = true;
|
|
|
|
} else if (result == CChunkFileReader::ERROR_BROKEN_STATE) {
|
|
|
|
HandleFailure();
|
2013-09-30 03:24:23 +00:00
|
|
|
osm.Show(i18nLoadFailure, 2.0);
|
2013-10-13 11:46:44 +00:00
|
|
|
ERROR_LOG(COMMON, "Load state failure: %s", reason.c_str());
|
2013-09-15 04:19:10 +00:00
|
|
|
callbackResult = false;
|
2013-09-11 20:21:15 +00:00
|
|
|
} else {
|
2013-09-30 03:24:23 +00:00
|
|
|
osm.Show(s->T(reason.c_str(), i18nLoadFailure), 2.0);
|
2013-09-15 04:19:10 +00:00
|
|
|
callbackResult = false;
|
2013-06-24 08:24:34 +00:00
|
|
|
}
|
2012-12-27 19:28:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SAVESTATE_SAVE:
|
|
|
|
INFO_LOG(COMMON, "Saving state to %s", op.filename.c_str());
|
2013-09-15 03:28:41 +00:00
|
|
|
result = CChunkFileReader::Save(op.filename, REVISION, PPSSPP_GIT_VERSION, state);
|
2013-09-15 04:19:10 +00:00
|
|
|
if (result == CChunkFileReader::ERROR_NONE) {
|
2013-06-24 09:09:45 +00:00
|
|
|
osm.Show(s->T("Saved State"), 2.0);
|
2013-09-15 04:19:10 +00:00
|
|
|
callbackResult = true;
|
|
|
|
} else if (result == CChunkFileReader::ERROR_BROKEN_STATE) {
|
|
|
|
HandleFailure();
|
2013-09-30 03:24:23 +00:00
|
|
|
osm.Show(i18nSaveFailure, 2.0);
|
2013-10-13 11:46:44 +00:00
|
|
|
ERROR_LOG(COMMON, "Save state failure: %s", reason.c_str());
|
2013-09-15 04:19:10 +00:00
|
|
|
callbackResult = false;
|
2013-09-11 20:21:15 +00:00
|
|
|
} else {
|
2013-09-30 03:24:23 +00:00
|
|
|
osm.Show(i18nSaveFailure, 2.0);
|
2013-09-15 04:19:10 +00:00
|
|
|
callbackResult = false;
|
2013-09-11 20:21:15 +00:00
|
|
|
}
|
2012-12-27 19:28:12 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SAVESTATE_VERIFY:
|
|
|
|
INFO_LOG(COMMON, "Verifying save state system");
|
2013-09-15 04:19:10 +00:00
|
|
|
callbackResult = CChunkFileReader::Verify(state) == CChunkFileReader::ERROR_NONE;
|
2012-12-27 19:28:12 +00:00
|
|
|
break;
|
|
|
|
|
2013-09-30 02:57:51 +00:00
|
|
|
case SAVESTATE_REWIND:
|
|
|
|
INFO_LOG(COMMON, "Rewinding to recent savestate snapshot");
|
|
|
|
result = rewindStates.Restore();
|
|
|
|
if (result == CChunkFileReader::ERROR_NONE) {
|
|
|
|
osm.Show(s->T("Loaded State"), 2.0);
|
|
|
|
callbackResult = true;
|
|
|
|
} else if (result == CChunkFileReader::ERROR_BROKEN_STATE) {
|
|
|
|
// Cripes. Good news is, we might have more. Let's try those too, better than a reset.
|
2013-09-30 03:20:20 +00:00
|
|
|
if (HandleFailure()) {
|
|
|
|
// Well, we did rewind, even if too much...
|
2013-09-30 02:57:51 +00:00
|
|
|
osm.Show(s->T("Loaded State"), 2.0);
|
|
|
|
callbackResult = true;
|
|
|
|
} else {
|
2013-09-30 03:24:23 +00:00
|
|
|
osm.Show(i18nLoadFailure, 2.0);
|
2013-09-30 02:57:51 +00:00
|
|
|
callbackResult = false;
|
|
|
|
}
|
|
|
|
} else {
|
2013-09-30 03:24:23 +00:00
|
|
|
osm.Show(i18nLoadFailure, 2.0);
|
2013-09-30 02:57:51 +00:00
|
|
|
callbackResult = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2012-12-27 19:28:12 +00:00
|
|
|
default:
|
|
|
|
ERROR_LOG(COMMON, "Savestate failure: unknown operation type %d", op.type);
|
2013-09-15 04:19:10 +00:00
|
|
|
callbackResult = false;
|
2012-12-27 19:28:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-09-11 20:21:15 +00:00
|
|
|
if (op.callback)
|
2013-09-15 04:19:10 +00:00
|
|
|
op.callback(callbackResult, op.cbUserData);
|
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
|
|
|
|
pspFileSystem.MkDir("ms0:/PSP/PPSSPP_STATE");
|
2013-01-02 22:25:35 +00:00
|
|
|
|
|
|
|
std::lock_guard<std::recursive_mutex> guard(mutex);
|
2013-11-03 01:32:34 +00:00
|
|
|
rewindStates.Clear();
|
2012-12-27 19:28:12 +00:00
|
|
|
}
|
|
|
|
}
|