2019-03-12 09:15:57 -04:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "../Utilities/FolderUtilities.h"
|
|
|
|
#include "../Utilities/ZipWriter.h"
|
|
|
|
#include "../Utilities/ZipReader.h"
|
|
|
|
#include "SaveStateManager.h"
|
|
|
|
#include "MessageManager.h"
|
|
|
|
#include "Console.h"
|
|
|
|
#include "EmuSettings.h"
|
|
|
|
#include "VideoDecoder.h"
|
|
|
|
#include "BaseCartridge.h"
|
2019-08-09 16:25:59 -04:00
|
|
|
#include "MovieManager.h"
|
2019-10-06 19:06:18 -04:00
|
|
|
#include "EventType.h"
|
|
|
|
#include "Debugger.h"
|
2019-03-12 09:15:57 -04:00
|
|
|
|
|
|
|
SaveStateManager::SaveStateManager(shared_ptr<Console> console)
|
|
|
|
{
|
|
|
|
_console = console;
|
|
|
|
_lastIndex = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
string SaveStateManager::GetStateFilepath(int stateIndex)
|
|
|
|
{
|
2019-03-12 12:28:41 -04:00
|
|
|
string romFile = _console->GetRomInfo().RomFile.GetFileName();
|
2019-03-12 09:15:57 -04:00
|
|
|
string folder = FolderUtilities::GetSaveStateFolder();
|
2019-03-31 15:58:59 -04:00
|
|
|
string filename = FolderUtilities::GetFilename(romFile, false) + "_" + std::to_string(stateIndex) + ".mss";
|
2019-03-12 09:15:57 -04:00
|
|
|
return FolderUtilities::CombinePath(folder, filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t SaveStateManager::GetStateInfo(int stateIndex)
|
|
|
|
{
|
|
|
|
string filepath = SaveStateManager::GetStateFilepath(stateIndex);
|
|
|
|
ifstream file(filepath, ios::in | ios::binary);
|
|
|
|
|
|
|
|
if(file) {
|
|
|
|
file.close();
|
|
|
|
return FolderUtilities::GetFileModificationTime(filepath);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SaveStateManager::MoveToNextSlot()
|
|
|
|
{
|
|
|
|
_lastIndex = (_lastIndex % MaxIndex) + 1;
|
|
|
|
MessageManager::DisplayMessage("SaveStates", "SaveStateSlotSelected", std::to_string(_lastIndex));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SaveStateManager::MoveToPreviousSlot()
|
|
|
|
{
|
|
|
|
_lastIndex = (_lastIndex == 1 ? SaveStateManager::MaxIndex : (_lastIndex - 1));
|
|
|
|
MessageManager::DisplayMessage("SaveStates", "SaveStateSlotSelected", std::to_string(_lastIndex));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SaveStateManager::SaveState()
|
|
|
|
{
|
|
|
|
SaveState(_lastIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SaveStateManager::LoadState()
|
|
|
|
{
|
|
|
|
return LoadState(_lastIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SaveStateManager::GetSaveStateHeader(ostream &stream)
|
|
|
|
{
|
|
|
|
uint32_t emuVersion = _console->GetSettings()->GetVersion();
|
|
|
|
uint32_t formatVersion = SaveStateManager::FileFormatVersion;
|
2019-03-31 15:58:59 -04:00
|
|
|
stream.write("MSS", 3);
|
2019-03-12 09:15:57 -04:00
|
|
|
stream.write((char*)&emuVersion, sizeof(emuVersion));
|
|
|
|
stream.write((char*)&formatVersion, sizeof(uint32_t));
|
|
|
|
|
|
|
|
string sha1Hash = _console->GetCartridge()->GetSha1Hash();
|
|
|
|
stream.write(sha1Hash.c_str(), sha1Hash.size());
|
|
|
|
|
|
|
|
RomInfo romInfo = _console->GetCartridge()->GetRomInfo();
|
2019-03-12 12:28:41 -04:00
|
|
|
string romName = FolderUtilities::GetFilename(romInfo.RomFile.GetFileName(), true);
|
2019-03-12 09:15:57 -04:00
|
|
|
uint32_t nameLength = (uint32_t)romName.size();
|
|
|
|
stream.write((char*)&nameLength, sizeof(uint32_t));
|
|
|
|
stream.write(romName.c_str(), romName.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SaveStateManager::SaveState(ostream &stream)
|
|
|
|
{
|
|
|
|
GetSaveStateHeader(stream);
|
|
|
|
_console->Serialize(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SaveStateManager::SaveState(string filepath)
|
|
|
|
{
|
|
|
|
ofstream file(filepath, ios::out | ios::binary);
|
|
|
|
|
|
|
|
if(file) {
|
2019-10-06 19:06:18 -04:00
|
|
|
_console->Lock();
|
2019-03-12 09:15:57 -04:00
|
|
|
SaveState(file);
|
2019-10-06 19:06:18 -04:00
|
|
|
_console->Unlock();
|
2019-03-12 09:15:57 -04:00
|
|
|
file.close();
|
|
|
|
|
2019-10-06 19:06:18 -04:00
|
|
|
shared_ptr<Debugger> debugger = _console->GetDebugger(false);
|
2019-03-12 09:15:57 -04:00
|
|
|
if(debugger) {
|
|
|
|
debugger->ProcessEvent(EventType::StateSaved);
|
2019-10-06 19:06:18 -04:00
|
|
|
}
|
2019-03-12 09:15:57 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SaveStateManager::SaveState(int stateIndex, bool displayMessage)
|
|
|
|
{
|
|
|
|
string filepath = SaveStateManager::GetStateFilepath(stateIndex);
|
|
|
|
if(SaveState(filepath)) {
|
|
|
|
if(displayMessage) {
|
|
|
|
MessageManager::DisplayMessage("SaveStates", "SaveStateSaved", std::to_string(stateIndex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SaveStateManager::LoadState(istream &stream, bool hashCheckRequired)
|
|
|
|
{
|
|
|
|
char header[3];
|
|
|
|
stream.read(header, 3);
|
2019-03-31 15:58:59 -04:00
|
|
|
if(memcmp(header, "MSS", 3) == 0) {
|
2019-03-12 09:15:57 -04:00
|
|
|
uint32_t emuVersion, fileFormatVersion;
|
|
|
|
|
|
|
|
stream.read((char*)&emuVersion, sizeof(emuVersion));
|
|
|
|
if(emuVersion > _console->GetSettings()->GetVersion()) {
|
|
|
|
MessageManager::DisplayMessage("SaveStates", "SaveStateNewerVersion");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.read((char*)&fileFormatVersion, sizeof(fileFormatVersion));
|
2019-07-06 18:07:09 -04:00
|
|
|
if(fileFormatVersion <= 4) {
|
2019-03-12 09:15:57 -04:00
|
|
|
MessageManager::DisplayMessage("SaveStates", "SaveStateIncompatibleVersion");
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
char hash[41] = {};
|
|
|
|
stream.read(hash, 40);
|
|
|
|
|
|
|
|
uint32_t nameLength = 0;
|
|
|
|
stream.read((char*)&nameLength, sizeof(uint32_t));
|
|
|
|
|
|
|
|
vector<char> nameBuffer(nameLength);
|
|
|
|
stream.read(nameBuffer.data(), nameBuffer.size());
|
|
|
|
string romName(nameBuffer.data(), nameLength);
|
|
|
|
|
2019-03-31 15:58:59 -04:00
|
|
|
shared_ptr<BaseCartridge> cartridge = _console->GetCartridge();
|
2019-05-10 21:14:47 -04:00
|
|
|
if(!cartridge /*|| cartridge->GetSha1Hash() != string(hash)*/) {
|
2019-03-31 15:58:59 -04:00
|
|
|
//Game isn't loaded, or CRC doesn't match
|
|
|
|
//TODO: Try to find and load the game
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-12 09:15:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//Stop any movie that might have been playing/recording if a state is loaded
|
|
|
|
//(Note: Loading a state is disabled in the UI while a movie is playing/recording)
|
2019-08-09 16:25:59 -04:00
|
|
|
MovieManager::Stop();
|
2019-03-12 09:15:57 -04:00
|
|
|
|
|
|
|
_console->Deserialize(stream, fileFormatVersion);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
MessageManager::DisplayMessage("SaveStates", "SaveStateInvalidFile");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SaveStateManager::LoadState(string filepath, bool hashCheckRequired)
|
|
|
|
{
|
|
|
|
ifstream file(filepath, ios::in | ios::binary);
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
if(file.good()) {
|
2019-10-06 19:06:18 -04:00
|
|
|
_console->Lock();
|
|
|
|
result = LoadState(file, hashCheckRequired);
|
|
|
|
_console->Unlock();
|
2019-03-12 09:15:57 -04:00
|
|
|
file.close();
|
|
|
|
|
2019-10-06 19:06:18 -04:00
|
|
|
if(result) {
|
|
|
|
shared_ptr<Debugger> debugger = _console->GetDebugger(false);
|
|
|
|
if(debugger) {
|
|
|
|
debugger->ProcessEvent(EventType::StateLoaded);
|
|
|
|
}
|
|
|
|
}
|
2019-03-12 09:15:57 -04:00
|
|
|
} else {
|
|
|
|
MessageManager::DisplayMessage("SaveStates", "SaveStateEmpty");
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SaveStateManager::LoadState(int stateIndex)
|
|
|
|
{
|
|
|
|
string filepath = SaveStateManager::GetStateFilepath(stateIndex);
|
|
|
|
if(LoadState(filepath, false)) {
|
|
|
|
MessageManager::DisplayMessage("SaveStates", "SaveStateLoaded", std::to_string(stateIndex));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SaveStateManager::SaveRecentGame(string romName, string romPath, string patchPath)
|
|
|
|
{
|
2019-07-03 18:57:30 -04:00
|
|
|
#ifndef LIBRETRO
|
|
|
|
//Don't do this for libretro core
|
2019-03-14 18:07:25 -04:00
|
|
|
string filename = FolderUtilities::GetFilename(_console->GetRomInfo().RomFile.GetFileName(), false) + ".rgd";
|
|
|
|
ZipWriter writer;
|
|
|
|
writer.Initialize(FolderUtilities::CombinePath(FolderUtilities::GetRecentGamesFolder(), filename));
|
|
|
|
|
|
|
|
std::stringstream pngStream;
|
|
|
|
_console->GetVideoDecoder()->TakeScreenshot(pngStream);
|
|
|
|
writer.AddFile(pngStream, "Screenshot.png");
|
|
|
|
|
|
|
|
std::stringstream stateStream;
|
|
|
|
SaveStateManager::SaveState(stateStream);
|
2019-03-31 15:58:59 -04:00
|
|
|
writer.AddFile(stateStream, "Savestate.mss");
|
2019-03-14 18:07:25 -04:00
|
|
|
|
|
|
|
std::stringstream romInfoStream;
|
|
|
|
romInfoStream << romName << std::endl;
|
|
|
|
romInfoStream << romPath << std::endl;
|
|
|
|
romInfoStream << patchPath << std::endl;
|
|
|
|
writer.AddFile(romInfoStream, "RomInfo.txt");
|
|
|
|
writer.Save();
|
2019-07-03 18:57:30 -04:00
|
|
|
#endif
|
2019-03-12 09:15:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SaveStateManager::LoadRecentGame(string filename, bool resetGame)
|
|
|
|
{
|
|
|
|
ZipReader reader;
|
|
|
|
reader.LoadArchive(filename);
|
|
|
|
|
|
|
|
stringstream romInfoStream, stateStream;
|
|
|
|
reader.GetStream("RomInfo.txt", romInfoStream);
|
2019-03-31 15:58:59 -04:00
|
|
|
reader.GetStream("Savestate.mss", stateStream);
|
2019-03-12 09:15:57 -04:00
|
|
|
|
|
|
|
string romName, romPath, patchPath;
|
|
|
|
std::getline(romInfoStream, romName);
|
|
|
|
std::getline(romInfoStream, romPath);
|
|
|
|
std::getline(romInfoStream, patchPath);
|
|
|
|
|
2019-03-16 12:20:18 -04:00
|
|
|
_console->Lock();
|
2019-03-12 09:15:57 -04:00
|
|
|
try {
|
2019-03-14 18:07:25 -04:00
|
|
|
if(_console->LoadRom(romPath, patchPath)) {
|
2019-03-12 09:15:57 -04:00
|
|
|
if(!resetGame) {
|
|
|
|
SaveStateManager::LoadState(stateStream, false);
|
|
|
|
}
|
|
|
|
}
|
2019-07-03 19:12:01 -04:00
|
|
|
} catch(std::exception&) {
|
2019-03-14 18:07:25 -04:00
|
|
|
_console->Stop(true);
|
2019-03-12 09:15:57 -04:00
|
|
|
}
|
2019-03-16 12:20:18 -04:00
|
|
|
_console->Unlock();
|
2019-03-12 09:15:57 -04:00
|
|
|
}
|