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"
|
2015-02-01 17:04:06 +00:00
|
|
|
#include "base/NativeApp.h"
|
2013-12-29 22:28:31 +00:00
|
|
|
#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"
|
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"
|
|
|
|
#include "Core/MemMap.h"
|
|
|
|
#include "Core/MIPS/MIPS.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,
|
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
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
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;
|
2014-02-09 21:45:51 +00:00
|
|
|
static bool hasLoadedState = false;
|
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;
|
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.
|
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)
|
|
|
|
{
|
2014-05-27 14:50:08 +00:00
|
|
|
auto blockCache = MIPSComp::jit->GetBlockCache();
|
|
|
|
auto savedBlocks = blockCache->SaveAndClearEmuHackOps();
|
2013-09-29 20:51:09 +00:00
|
|
|
Memory::DoState(p);
|
2014-05-27 14:50:08 +00:00
|
|
|
blockCache->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)
|
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2014-12-21 07:14:46 +00:00
|
|
|
void SaveScreenshot(const std::string &filename, Callback callback, void *cbUserData)
|
|
|
|
{
|
|
|
|
Enqueue(Operation(SAVESTATE_SAVE_SCREENSHOT, filename, 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];
|
2014-09-13 22:14:11 +00:00
|
|
|
char temp[2048];
|
|
|
|
snprintf(discID, sizeof(discID), "%s_%s",
|
2013-01-02 20:00:10 +00:00
|
|
|
g_paramSFO.GetValueString("DISC_ID").c_str(),
|
|
|
|
g_paramSFO.GetValueString("DISC_VERSION").c_str());
|
2014-09-13 22:14:11 +00:00
|
|
|
snprintf(temp, sizeof(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 "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-01 17:04:06 +00:00
|
|
|
int GetCurrentSlot()
|
|
|
|
{
|
|
|
|
return g_Config.iCurrentStateSlot;
|
|
|
|
}
|
|
|
|
|
2014-01-07 14:56:04 +00:00
|
|
|
void NextSlot()
|
|
|
|
{
|
|
|
|
I18NCategory *sy = GetI18NCategory("System");
|
|
|
|
g_Config.iCurrentStateSlot = (g_Config.iCurrentStateSlot + 1) % SaveState::SAVESTATESLOTS;
|
2014-09-13 22:14:11 +00:00
|
|
|
char msg[128];
|
|
|
|
snprintf(msg, sizeof(msg), "%s: %d", sy->T("Savestate Slot"), g_Config.iCurrentStateSlot + 1);
|
2014-01-07 14:56:04 +00:00
|
|
|
osm.Show(msg);
|
2015-02-01 17:04:06 +00:00
|
|
|
NativeMessageReceived("slotchanged", "");
|
2014-01-07 14:56:04 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2015-01-29 15:32:13 +00:00
|
|
|
I18NCategory *sy = GetI18NCategory("System");
|
|
|
|
osm.Show(sy->T("Failed to load state. Error in the file system."), 2.0);
|
2013-09-11 20:21:15 +00:00
|
|
|
if (callback)
|
2014-12-21 06:23:26 +00:00
|
|
|
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);
|
2014-12-21 07:14:46 +00:00
|
|
|
std::string shot = GenerateSaveSlotFilename(slot, SCREENSHOT_EXTENSION);
|
2013-07-17 05:33:26 +00:00
|
|
|
if (!fn.empty()) {
|
2014-12-21 06:24:02 +00:00
|
|
|
auto renameCallback = [=](bool status, void *data) {
|
|
|
|
if (status) {
|
|
|
|
if (File::Exists(fn)) {
|
|
|
|
File::Delete(fn);
|
|
|
|
}
|
|
|
|
File::Rename(fn + ".tmp", fn);
|
|
|
|
}
|
|
|
|
if (callback) {
|
|
|
|
callback(status, data);
|
|
|
|
}
|
|
|
|
};
|
2014-12-21 07:14:46 +00:00
|
|
|
// Let's also create a screenshot.
|
2014-12-21 07:34:11 +00:00
|
|
|
SaveScreenshot(shot, Callback(), 0);
|
2014-12-21 06:24:02 +00:00
|
|
|
Save(fn + ".tmp", renameCallback, 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)
|
2014-12-21 06:23:26 +00:00
|
|
|
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)) {
|
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;
|
|
|
|
}
|
|
|
|
|
2014-12-31 19:42:28 +00:00
|
|
|
std::string GetSlotDateAsString(int slot) {
|
|
|
|
std::string fn = GenerateSaveSlotFilename(slot, STATE_EXTENSION);
|
|
|
|
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 :)
|
|
|
|
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &time);
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2014-02-09 21:45:51 +00:00
|
|
|
bool HasLoadedState()
|
|
|
|
{
|
|
|
|
return hasLoadedState;
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
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;
|
2014-02-09 21:45:51 +00:00
|
|
|
hasLoadedState = true;
|
2013-09-15 04:19:10 +00:00
|
|
|
} 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) {
|
2014-12-31 19:42:28 +00:00
|
|
|
|
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;
|
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.
|
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;
|
2014-02-09 21:45:51 +00:00
|
|
|
hasLoadedState = true;
|
2013-09-30 02:57:51 +00:00
|
|
|
} 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;
|
|
|
|
|
2014-12-21 07:14:46 +00:00
|
|
|
case SAVESTATE_SAVE_SCREENSHOT:
|
2014-12-31 19:42:28 +00:00
|
|
|
if (!TakeGameScreenshot(op.filename.c_str(), SCREENSHOT_JPG, SCREENSHOT_RENDER)) {
|
|
|
|
ERROR_LOG(COMMON, "Failed to take a screenshot for the savestate! %s", op.filename.c_str());
|
|
|
|
}
|
2014-12-21 07:14:46 +00:00
|
|
|
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
|
|
|
}
|
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
|
|
|
|
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();
|
2014-02-09 21:45:51 +00:00
|
|
|
|
|
|
|
hasLoadedState = false;
|
2012-12-27 19:28:12 +00:00
|
|
|
}
|
|
|
|
}
|