-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GameClient] Implement in-game saves
- Loading branch information
Showing
7 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
* Copyright (C) 2016-2017 Team Kodi | ||
* http://kodi.tv | ||
* | ||
* 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; either version 2, or (at your option) | ||
* any later version. | ||
* | ||
* 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 for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this Program; see the file COPYING. If not, see | ||
* <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#include "GameClientInGameSaves.h" | ||
|
||
#include "GameClient.h" | ||
#include "GameClientTranslator.h" | ||
#include "filesystem/File.h" | ||
#include "profiles/ProfilesManager.h" | ||
#include "utils/URIUtils.h" | ||
|
||
#include <assert.h> | ||
|
||
using namespace GAME; | ||
|
||
#define INGAME_SAVES_DIRECTORY "InGameSaves" | ||
#define INGAME_SAVES_EXTENSION_SAVE_RAM ".sav" | ||
#define INGAME_SAVES_EXTENSION_RTC ".rtc" | ||
|
||
CGameClientInGameSaves::CGameClientInGameSaves(CGameClient* addon, const GameClient* dllStruct) : | ||
m_gameClient(addon), | ||
m_dllStruct(dllStruct) | ||
{ | ||
assert(m_gameClient != nullptr); | ||
assert(m_dllStruct != nullptr); | ||
} | ||
|
||
void CGameClientInGameSaves::Load() | ||
{ | ||
Load(GAME_MEMORY_SAVE_RAM); | ||
Load(GAME_MEMORY_RTC); | ||
} | ||
|
||
void CGameClientInGameSaves::Save() | ||
{ | ||
Save(GAME_MEMORY_SAVE_RAM); | ||
Save(GAME_MEMORY_RTC); | ||
} | ||
|
||
std::string CGameClientInGameSaves::GetPath(GAME_MEMORY memoryType) | ||
{ | ||
std::string path = URIUtils::AddFileToFolder(CProfilesManager::GetInstance().GetSavestatesFolder(), INGAME_SAVES_DIRECTORY); | ||
if (!XFILE::CDirectory::Exists(path)) | ||
XFILE::CDirectory::Create(path); | ||
|
||
// Append save game filename | ||
std::string gamePath = URIUtils::GetFileName(m_gameClient->GetGamePath()); | ||
path = URIUtils::AddFileToFolder(path, gamePath.empty() ? m_gameClient->ID() : gamePath); | ||
|
||
// Append file extension | ||
switch (memoryType) | ||
{ | ||
case GAME_MEMORY_SAVE_RAM: return path + INGAME_SAVES_EXTENSION_SAVE_RAM; | ||
case GAME_MEMORY_RTC: return path + INGAME_SAVES_EXTENSION_RTC; | ||
default: | ||
break; | ||
} | ||
return std::string(); | ||
} | ||
|
||
void CGameClientInGameSaves::Load(GAME_MEMORY memoryType) | ||
{ | ||
uint8_t *gameMemory = nullptr; | ||
size_t size = 0; | ||
|
||
try | ||
{ | ||
m_dllStruct->GetMemory(memoryType, &gameMemory, &size); | ||
} | ||
catch (...) | ||
{ | ||
CLog::Log(LOGERROR, "GAME: %s: Exception caught in GetMemory()", m_gameClient->ID().c_str()); | ||
} | ||
|
||
const std::string path = GetPath(memoryType); | ||
if (size > 0 && XFILE::CFile::Exists(path)) | ||
{ | ||
XFILE::CFile file; | ||
if (file.Open(path)) | ||
{ | ||
ssize_t read = file.Read(gameMemory, size); | ||
if (read == static_cast<ssize_t>(size)) | ||
{ | ||
CLog::Log(LOGINFO, "GAME: In-game saves (%s) loaded from %s", CGameClientTranslator::ToString(memoryType), path.c_str()); | ||
} | ||
else | ||
{ | ||
CLog::Log(LOGERROR, "GAME: Failed to read in-game saves (%s): %ld/%ld bytes read", CGameClientTranslator::ToString(memoryType), read, size); | ||
} | ||
} | ||
else | ||
{ | ||
CLog::Log(LOGERROR, "GAME: Unable to open in-game saves (%s) from file %s", CGameClientTranslator::ToString(memoryType), path.c_str()); | ||
} | ||
} | ||
else | ||
{ | ||
CLog::Log(LOGDEBUG, "GAME: No in-game saves (%s) to load", CGameClientTranslator::ToString(memoryType)); | ||
} | ||
} | ||
|
||
void CGameClientInGameSaves::Save(GAME_MEMORY memoryType) | ||
{ | ||
uint8_t *gameMemory = nullptr; | ||
size_t size = 0; | ||
|
||
try | ||
{ | ||
m_dllStruct->GetMemory(memoryType, &gameMemory, &size); | ||
} | ||
catch (...) | ||
{ | ||
CLog::Log(LOGERROR, "GAME: %s: Exception caught in GetMemory()", m_gameClient->ID().c_str()); | ||
} | ||
|
||
if (size > 0) | ||
{ | ||
const std::string path = GetPath(memoryType); | ||
|
||
|
||
|
||
XFILE::CFile file; | ||
if (file.OpenForWrite(path, true)) | ||
{ | ||
ssize_t written = 0; | ||
written = file.Write(gameMemory, size); | ||
file.Close(); | ||
if (written == static_cast<ssize_t>(size)) | ||
{ | ||
CLog::Log(LOGINFO, "GAME: In-game saves (%s) written to %s", CGameClientTranslator::ToString(memoryType), path.c_str()); | ||
} | ||
else | ||
{ | ||
CLog::Log(LOGERROR, "GAME: Failed to write in-game saves (%s): %ld/%ld bytes written", CGameClientTranslator::ToString(memoryType), written, size); | ||
} | ||
} | ||
else | ||
{ | ||
CLog::Log(LOGERROR, "GAME: Unable to open in-game saves (%s) from file %s", CGameClientTranslator::ToString(memoryType), path.c_str()); | ||
} | ||
} | ||
else | ||
{ | ||
CLog::Log(LOGDEBUG, "GAME: No in-game saves (%s) to save", CGameClientTranslator::ToString(memoryType)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright (C) 2016-2017 Team Kodi | ||
* http://kodi.tv | ||
* | ||
* 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; either version 2, or (at your option) | ||
* any later version. | ||
* | ||
* 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 for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this Program; see the file COPYING. If not, see | ||
* <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
#pragma once | ||
|
||
#include "addons/kodi-addon-dev-kit/include/kodi/kodi_game_types.h" | ||
|
||
#include <string> | ||
|
||
struct GameClient; | ||
|
||
namespace GAME | ||
{ | ||
class CGameClient; | ||
|
||
/*! | ||
* \brief This class implements in-game saves. | ||
* | ||
* Some games do not implement state persistance on their own, but rely on the frontend for saving their current | ||
* memory state to disk. This is mostly the case for emulators for SRAM (battery backed up ram on cadridges) or | ||
* memory cards. | ||
* | ||
* Differences to save states: | ||
* - Works only for supported games (e.g. emulated games with SRAM support) | ||
* - Often works emulator independent (and can be used to start a game with one emulator and continue with another) | ||
* - Visible in-game (e.g. in-game save game selection menus) | ||
*/ | ||
class CGameClientInGameSaves | ||
{ | ||
public: | ||
/*! | ||
* \brief Constructor. | ||
* \param addon The game client implementation. | ||
* \param dllStruct The emulator or game for which the in-game saves are processed. | ||
*/ | ||
CGameClientInGameSaves(CGameClient* addon, const GameClient* dllStruct); | ||
|
||
/*! | ||
* \brief Load in-game data. | ||
*/ | ||
void Load(); | ||
|
||
/*! | ||
* \brief Save in-game data. | ||
*/ | ||
void Save(); | ||
|
||
private: | ||
std::string GetPath(GAME_MEMORY memoryType); | ||
|
||
void Load(GAME_MEMORY memoryType); | ||
void Save(GAME_MEMORY memoryType); | ||
|
||
const CGameClient* const m_gameClient; | ||
const GameClient* const m_dllStruct; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters