-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[games] Implement in-game saves #11380
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,162 @@ | ||
/* | ||
* 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 "filesystem/Directory.h" | ||
#include "profiles/ProfilesManager.h" | ||
#include "utils/URIUtils.h" | ||
#include "utils/log.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" | ||
This comment was marked as spam.
Sorry, something went wrong. |
||
|
||
CGameClientInGameSaves::CGameClientInGameSaves(CGameClient* addon, const KodiToAddonFuncTable_Game* 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. | ||
* | ||
* \details Some games do not implement state persistence 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 cartridges) 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 KodiToAddonFuncTable_Game* 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 KodiToAddonFuncTable_Game* 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.