| 1 |
/* |
| 2 |
* Copyright (C) 2012-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 |
#pragma once |
| 10 |
|
| 11 |
#include "GameClientSubsystem.h" |
| 12 |
#include "addons/binary-addons/AddonDll.h" |
| 13 |
#include "addons/kodi-dev-kit/include/kodi/addon-instance/Game.h" |
| 14 |
#include "threads/CriticalSection.h" |
| 15 |
|
| 16 |
#include <atomic> |
| 17 |
#include <memory> |
| 18 |
#include <set> |
| 19 |
#include <stdint.h> |
| 20 |
#include <string> |
| 21 |
#include <utility> |
| 22 |
|
| 23 |
class CFileItem; |
| 24 |
|
| 25 |
namespace KODI |
| 26 |
{ |
| 27 |
namespace RETRO |
| 28 |
{ |
| 29 |
class IStreamManager; |
| 30 |
} |
| 31 |
|
| 32 |
namespace GAME |
| 33 |
{ |
| 34 |
|
| 35 |
class CGameClientCheevos; |
| 36 |
class CGameClientInGameSaves; |
| 37 |
class CGameClientInput; |
| 38 |
class CGameClientProperties; |
| 39 |
class IGameInputCallback; |
| 40 |
|
| 41 |
/*! |
| 42 |
* \ingroup games |
| 43 |
* |
| 44 |
* \brief Helper class to have "C" struct created before other parts becomes his pointer. |
| 45 |
*/ |
| 46 |
class CGameClientStruct |
| 47 |
{ |
| 48 |
public: |
| 49 |
CGameClientStruct() |
| 50 |
{ |
| 51 |
// Create "C" interface structures, used as own parts to prevent API problems on update |
| 52 |
KODI_ADDON_INSTANCE_INFO* info = new KODI_ADDON_INSTANCE_INFO(); |
| 53 |
info->id = ""; |
| 54 |
info->version = kodi::addon::GetTypeVersion(ADDON_INSTANCE_GAME); |
| 55 |
info->type = ADDON_INSTANCE_GAME; |
| 56 |
info->kodi = this; |
| 57 |
info->parent = nullptr; |
| 58 |
info->first_instance = true; |
| 59 |
info->functions = new KODI_ADDON_INSTANCE_FUNC_CB(); |
| 60 |
m_ifc.info = info; |
| 61 |
m_ifc.functions = new KODI_ADDON_INSTANCE_FUNC(); |
| 62 |
|
| 63 |
m_ifc.game = new AddonInstance_Game; |
| 64 |
m_ifc.game->props = new AddonProps_Game(); |
| 65 |
m_ifc.game->toKodi = new AddonToKodiFuncTable_Game(); |
| 66 |
m_ifc.game->toAddon = new KodiToAddonFuncTable_Game(); |
| 67 |
} |
| 68 |
|
| 69 |
~CGameClientStruct() |
| 70 |
{ |
| 71 |
delete m_ifc.functions; |
| 72 |
if (m_ifc.info) |
| 73 |
delete m_ifc.info->functions; |
| 74 |
delete m_ifc.info; |
| 75 |
if (m_ifc.game) |
| 76 |
{ |
| 77 |
delete m_ifc.game->toAddon; |
| 78 |
delete m_ifc.game->toKodi; |
| 79 |
delete m_ifc.game->props; |
| 80 |
delete m_ifc.game; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
KODI_ADDON_INSTANCE_STRUCT m_ifc; |
| 85 |
}; |
| 86 |
|
| 87 |
/*! |
| 88 |
* \ingroup games |
| 89 |
* \brief Interface between Kodi and Game add-ons. |
| 90 |
* |
| 91 |
* The game add-on system is extremely large. To make the code more manageable, |
| 92 |
* a subsystem pattern has been put in place. This pattern takes functionality |
| 93 |
* that would normally be placed in this class, and puts it in another class |
| 94 |
* (a "subsystem"). |
| 95 |
* |
| 96 |
* The architecture is relatively simple. Subsystems are placed in a flat |
| 97 |
* struct and accessed by calling the subsystem name. For example, |
| 98 |
* historically, OpenJoystick() was a member of this class. Now, the function |
| 99 |
* is called like Input().OpenJoystick(). |
| 100 |
* |
| 101 |
* Although this pattern adds a layer of complexity, it enforces modularity and |
| 102 |
* separation of concerns by making it very clear when one subsystem becomes |
| 103 |
* dependent on another. Subsystems are all given access to each other by the |
| 104 |
* calling mechanism. However, calling a subsystem creates a dependency on it, |
| 105 |
* and an engineering decision must be made to justify the dependency. |
| 106 |
* |
| 107 |
* CONTRIBUTING |
| 108 |
* |
| 109 |
* If you wish to contribute, a beneficial task would be to refactor anything |
| 110 |
* in this class into a new subsystem: |
| 111 |
* |
| 112 |
* Using line count as a heuristic, the subsystem pattern has shrunk the .cpp |
| 113 |
* from 1,200 lines to just over 600. Reducing this further is the challenge. |
| 114 |
* You must now choose whether to accept. |
| 115 |
*/ |
| 116 |
class CGameClient : public ADDON::CAddonDll, private CGameClientStruct |
| 117 |
{ |
| 118 |
public: |
| 119 |
explicit CGameClient(const ADDON::AddonInfoPtr& addonInfo); |
| 120 |
|
| 121 |
~CGameClient() override; |
| 122 |
|
| 123 |
// Game subsystems (const) |
| 124 |
const CGameClientCheevos& Cheevos() const { return *m_subsystems.Cheevos; } |
| 125 |
const CGameClientInput& Input() const { return *m_subsystems.Input; } |
| 126 |
const CGameClientProperties& AddonProperties() const { return *m_subsystems.AddonProperties; } |
| 127 |
const CGameClientStreams& Streams() const { return *m_subsystems.Streams; } |
| 128 |
|
| 129 |
// Game subsystems (mutable) |
| 130 |
CGameClientCheevos& Cheevos() { return *m_subsystems.Cheevos; } |
| 131 |
CGameClientInput& Input() { return *m_subsystems.Input; } |
| 132 |
CGameClientProperties& AddonProperties() { return *m_subsystems.AddonProperties; } |
| 133 |
CGameClientStreams& Streams() { return *m_subsystems.Streams; } |
| 134 |
|
| 135 |
// Implementation of IAddon via CAddonDll |
| 136 |
std::string LibPath() const override; |
| 137 |
ADDON::AddonPtr GetRunningInstance() const override; |
| 138 |
|
| 139 |
// Query properties of the game client |
| 140 |
bool SupportsStandalone() const { return m_bSupportsStandalone; } |
| 141 |
bool SupportsPath() const; |
| 142 |
bool SupportsVFS() const { return m_bSupportsVFS; } |
| 143 |
const std::set<std::string>& GetExtensions() const { return m_extensions; } |
| 144 |
bool SupportsAllExtensions() const { return m_bSupportsAllExtensions; } |
| 145 |
bool IsExtensionValid(const std::string& strExtension) const; |
| 146 |
const std::string& GetEmulatorName() const { return m_emulatorName; } |
| 147 |
const std::string& GetPlatforms() const { return m_platforms; } |
| 148 |
|
| 149 |
// Start/stop gameplay |
| 150 |
bool Initialize(void); |
| 151 |
void Unload(); |
| 152 |
bool OpenFile(const CFileItem& file, |
| 153 |
RETRO::IStreamManager& streamManager, |
| 154 |
IGameInputCallback* input); |
| 155 |
bool OpenStandalone(RETRO::IStreamManager& streamManager, IGameInputCallback* input); |
| 156 |
void Reset(); |
| 157 |
void CloseFile(); |
| 158 |
const std::string& GetGamePath() const { return m_gamePath; } |
| 159 |
|
| 160 |
// Playback control |
| 161 |
bool RequiresGameLoop() const { return m_bRequiresGameLoop; } |
| 162 |
bool IsPlaying() const { return m_bIsPlaying; } |
| 163 |
size_t GetSerializeSize() const { return m_serializeSize; } |
| 164 |
double GetFrameRate() const { return m_framerate; } |
| 165 |
double GetSampleRate() const { return m_samplerate; } |
| 166 |
void RunFrame(); |
| 167 |
|
| 168 |
// Access memory |
| 169 |
size_t SerializeSize() const { return m_serializeSize; } |
| 170 |
bool Serialize(uint8_t* data, size_t size); |
| 171 |
bool Deserialize(const uint8_t* data, size_t size); |
| 172 |
|
| 173 |
/*! |
| 174 |
* @brief To get the interface table used between addon and kodi |
| 175 |
* @todo This function becomes removed after old callback library system |
| 176 |
* is removed. |
| 177 |
*/ |
| 178 |
AddonInstance_Game* GetInstanceInterface() { return m_ifc.game; } |
| 179 |
|
| 180 |
// Helper functions |
| 181 |
bool LogError(GAME_ERROR error, const char* strMethod) const; |
| 182 |
void LogException(const char* strFunctionName) const; |
| 183 |
|
| 184 |
private: |
| 185 |
// Private gameplay functions |
| 186 |
bool InitializeGameplay(const std::string& gamePath, |
| 187 |
RETRO::IStreamManager& streamManager, |
| 188 |
IGameInputCallback* input); |
| 189 |
bool LoadGameInfo(); |
| 190 |
void NotifyError(GAME_ERROR error); |
| 191 |
std::string GetMissingResource(); |
| 192 |
|
| 193 |
// Helper functions |
| 194 |
void LogAddonProperties(void) const; |
| 195 |
|
| 196 |
/*! |
| 197 |
* @brief Callback functions from addon to kodi |
| 198 |
*/ |
| 199 |
//@{ |
| 200 |
static void cb_close_game(KODI_HANDLE kodiInstance); |
| 201 |
static KODI_GAME_STREAM_HANDLE cb_open_stream(KODI_HANDLE kodiInstance, |
| 202 |
const game_stream_properties* properties); |
| 203 |
static bool cb_get_stream_buffer(KODI_HANDLE kodiInstance, |
| 204 |
KODI_GAME_STREAM_HANDLE stream, |
| 205 |
unsigned int width, |
| 206 |
unsigned int height, |
| 207 |
game_stream_buffer* buffer); |
| 208 |
static void cb_add_stream_data(KODI_HANDLE kodiInstance, |
| 209 |
KODI_GAME_STREAM_HANDLE stream, |
| 210 |
const game_stream_packet* packet); |
| 211 |
static void cb_release_stream_buffer(KODI_HANDLE kodiInstance, |
| 212 |
KODI_GAME_STREAM_HANDLE stream, |
| 213 |
game_stream_buffer* buffer); |
| 214 |
static void cb_close_stream(KODI_HANDLE kodiInstance, KODI_GAME_STREAM_HANDLE stream); |
| 215 |
static game_proc_address_t cb_hw_get_proc_address(KODI_HANDLE kodiInstance, const char* sym); |
| 216 |
static bool cb_input_event(KODI_HANDLE kodiInstance, const game_input_event* event); |
| 217 |
//@} |
| 218 |
|
| 219 |
/*! |
| 220 |
* \brief Parse the name of a libretro game add-on into its emulator name |
| 221 |
* and platforms |
| 222 |
* |
| 223 |
* \param addonName The name of the add-on, e.g. "Nintendo - SNES / SFC (Snes9x 2002)" |
| 224 |
* |
| 225 |
* \return A tuple of two strings: |
| 226 |
* - first: the emulator name, e.g. "Snes9x 2002" |
| 227 |
* - second: the platforms, e.g. "Nintendo - SNES / SFC" |
| 228 |
* |
| 229 |
* For cores that don't emulate a platform, such as 2048 with the add-on name |
| 230 |
* "2048", then the emulator name will be the add-on name and platforms |
| 231 |
* will be empty, e.g.: |
| 232 |
* - first: "2048" |
| 233 |
* - second: "" |
| 234 |
*/ |
| 235 |
static std::pair<std::string, std::string> ParseLibretroName(const std::string& addonName); |
| 236 |
|
| 237 |
// Game subsystems |
| 238 |
GameClientSubsystems m_subsystems; |
| 239 |
|
| 240 |
// Game API xml parameters |
| 241 |
bool m_bSupportsVFS; |
| 242 |
bool m_bSupportsStandalone; |
| 243 |
std::set<std::string> m_extensions; |
| 244 |
bool m_bSupportsAllExtensions = false; |
| 245 |
std::string m_emulatorName; |
| 246 |
std::string m_platforms; |
| 247 |
|
| 248 |
// Properties of the current playing file |
| 249 |
std::atomic_bool m_bIsPlaying; // True between OpenFile() and CloseFile() |
| 250 |
std::string m_gamePath; |
| 251 |
bool m_bRequiresGameLoop = false; |
| 252 |
size_t m_serializeSize = 0; |
| 253 |
IGameInputCallback* m_input = nullptr; // The input callback passed to OpenFile() |
| 254 |
double m_framerate = 0.0; // Video frame rate (fps) |
| 255 |
double m_samplerate = 0.0; // Audio sample rate (Hz) |
| 256 |
GAME_REGION m_region = GAME_REGION_UNKNOWN; // Region of the loaded game |
| 257 |
|
| 258 |
// In-game saves |
| 259 |
std::unique_ptr<CGameClientInGameSaves> m_inGameSaves; |
| 260 |
|
| 261 |
CCriticalSection m_critSection; |
| 262 |
}; |
| 263 |
|
| 264 |
} // namespace GAME |
| 265 |
} // namespace KODI |