| 1 |
/* |
| 2 |
* Copyright (C) 2016-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "GameClientInGameSaves.h" |
| 10 |
|
| 11 |
#include "GameClient.h" |
| 12 |
#include "GameClientTranslator.h" |
| 13 |
#include "ServiceBroker.h" |
| 14 |
#include "filesystem/Directory.h" |
| 15 |
#include "filesystem/File.h" |
| 16 |
#include "games/GameServices.h" |
| 17 |
#include "utils/URIUtils.h" |
| 18 |
#include "utils/log.h" |
| 19 |
|
| 20 |
#include <assert.h> |
| 21 |
|
| 22 |
using namespace KODI; |
| 23 |
using namespace GAME; |
| 24 |
|
| 25 |
#define INGAME_SAVES_DIRECTORY "InGameSaves" |
| 26 |
#define INGAME_SAVES_EXTENSION_SAVE_RAM ".sav" |
| 27 |
#define INGAME_SAVES_EXTENSION_RTC ".rtc" |
| 28 |
|
| 29 |
CGameClientInGameSaves::CGameClientInGameSaves(CGameClient* addon, |
| 30 |
const AddonInstance_Game* dllStruct) |
| 31 |
: m_gameClient(addon), m_dllStruct(dllStruct) |
| 32 |
{ |
| 33 |
assert(m_gameClient != nullptr); |
| 34 |
assert(m_dllStruct != nullptr); |
| 35 |
} |
| 36 |
|
| 37 |
void CGameClientInGameSaves::Load() |
| 38 |
{ |
| 39 |
Load(GAME_MEMORY_SAVE_RAM); |
| 40 |
Load(GAME_MEMORY_RTC); |
| 41 |
} |
| 42 |
|
| 43 |
void CGameClientInGameSaves::Save() |
| 44 |
{ |
| 45 |
Save(GAME_MEMORY_SAVE_RAM); |
| 46 |
Save(GAME_MEMORY_RTC); |
| 47 |
} |
| 48 |
|
| 49 |
std::string CGameClientInGameSaves::GetPath(GAME_MEMORY memoryType) |
| 50 |
{ |
| 51 |
const CGameServices& gameServices = CServiceBroker::GetGameServices(); |
| 52 |
std::string path = |
| 53 |
URIUtils::AddFileToFolder(gameServices.GetSavestatesFolder(), INGAME_SAVES_DIRECTORY); |
| 54 |
if (!XFILE::CDirectory::Exists(path)) |
| 55 |
XFILE::CDirectory::Create(path); |
| 56 |
|
| 57 |
// Append save game filename |
| 58 |
std::string gamePath = URIUtils::GetFileName(m_gameClient->GetGamePath()); |
| 59 |
path = URIUtils::AddFileToFolder(path, gamePath.empty() ? m_gameClient->ID() : gamePath); |
| 60 |
|
| 61 |
// Append file extension |
| 62 |
switch (memoryType) |
| 63 |
{ |
| 64 |
case GAME_MEMORY_SAVE_RAM: |
| 65 |
return path + INGAME_SAVES_EXTENSION_SAVE_RAM; |
| 66 |
case GAME_MEMORY_RTC: |
| 67 |
return path + INGAME_SAVES_EXTENSION_RTC; |
| 68 |
default: |
| 69 |
break; |
| 70 |
} |
| 71 |
return std::string(); |
| 72 |
} |
| 73 |
|
| 74 |
void CGameClientInGameSaves::Load(GAME_MEMORY memoryType) |
| 75 |
{ |
| 76 |
uint8_t* gameMemory = nullptr; |
| 77 |
size_t size = 0; |
| 78 |
|
| 79 |
try |
| 80 |
{ |
| 81 |
m_dllStruct->toAddon->GetMemory(m_dllStruct, memoryType, &gameMemory, &size); |
| 82 |
} |
| 83 |
catch (...) |
| 84 |
{ |
| 85 |
CLog::Log(LOGERROR, "GAME: {}: Exception caught in GetMemory()", m_gameClient->ID()); |
| 86 |
} |
| 87 |
|
| 88 |
const std::string path = GetPath(memoryType); |
| 89 |
if (size > 0 && XFILE::CFile::Exists(path)) |
| 90 |
{ |
| 91 |
XFILE::CFile file; |
| 92 |
if (file.Open(path)) |
| 93 |
{ |
| 94 |
ssize_t read = file.Read(gameMemory, size); |
| 95 |
if (read == static_cast<ssize_t>(size)) |
| 96 |
{ |
| 97 |
CLog::Log(LOGINFO, "GAME: In-game saves ({}) loaded from {}", |
| 98 |
CGameClientTranslator::ToString(memoryType), path); |
| 99 |
} |
| 100 |
else |
| 101 |
{ |
| 102 |
CLog::Log(LOGERROR, "GAME: Failed to read in-game saves ({}): {}/{} bytes read", |
| 103 |
CGameClientTranslator::ToString(memoryType), read, size); |
| 104 |
} |
| 105 |
} |
| 106 |
else |
| 107 |
{ |
| 108 |
CLog::Log(LOGERROR, "GAME: Unable to open in-game saves ({}) from file {}", |
| 109 |
CGameClientTranslator::ToString(memoryType), path); |
| 110 |
} |
| 111 |
} |
| 112 |
else |
| 113 |
{ |
| 114 |
CLog::Log(LOGDEBUG, "GAME: No in-game saves ({}) to load", |
| 115 |
CGameClientTranslator::ToString(memoryType)); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
void CGameClientInGameSaves::Save(GAME_MEMORY memoryType) |
| 120 |
{ |
| 121 |
uint8_t* gameMemory = nullptr; |
| 122 |
size_t size = 0; |
| 123 |
|
| 124 |
try |
| 125 |
{ |
| 126 |
m_dllStruct->toAddon->GetMemory(m_dllStruct, memoryType, &gameMemory, &size); |
| 127 |
} |
| 128 |
catch (...) |
| 129 |
{ |
| 130 |
CLog::Log(LOGERROR, "GAME: {}: Exception caught in GetMemory()", m_gameClient->ID()); |
| 131 |
} |
| 132 |
|
| 133 |
if (size > 0) |
| 134 |
{ |
| 135 |
const std::string path = GetPath(memoryType); |
| 136 |
XFILE::CFile file; |
| 137 |
if (file.OpenForWrite(path, true)) |
| 138 |
{ |
| 139 |
ssize_t written = 0; |
| 140 |
written = file.Write(gameMemory, size); |
| 141 |
file.Close(); |
| 142 |
if (written == static_cast<ssize_t>(size)) |
| 143 |
{ |
| 144 |
CLog::Log(LOGINFO, "GAME: In-game saves ({}) written to {}", |
| 145 |
CGameClientTranslator::ToString(memoryType), path); |
| 146 |
} |
| 147 |
else |
| 148 |
{ |
| 149 |
CLog::Log(LOGERROR, "GAME: Failed to write in-game saves ({}): {}/{} bytes written", |
| 150 |
CGameClientTranslator::ToString(memoryType), written, size); |
| 151 |
} |
| 152 |
} |
| 153 |
else |
| 154 |
{ |
| 155 |
CLog::Log(LOGERROR, "GAME: Unable to open in-game saves ({}) from file {}", |
| 156 |
CGameClientTranslator::ToString(memoryType), path); |
| 157 |
} |
| 158 |
} |
| 159 |
else |
| 160 |
{ |
| 161 |
CLog::Log(LOGDEBUG, "GAME: No in-game saves ({}) to save", |
| 162 |
CGameClientTranslator::ToString(memoryType)); |
| 163 |
} |
| 164 |
} |