11#include "../AddonBase.h"
12#include "../c-api/addon-instance/game.h"
54 for (
unsigned int i = 0; i < layout.digital_button_count; ++i)
56 for (
unsigned int i = 0; i < layout.analog_button_count; ++i)
58 for (
unsigned int i = 0; i < layout.analog_stick_count; ++i)
60 for (
unsigned int i = 0; i < layout.accelerometer_count; ++i)
62 for (
unsigned int i = 0; i < layout.key_count; ++i)
63 keys.emplace_back(layout.keys[i]);
64 for (
unsigned int i = 0; i < layout.rel_pointer_count; ++i)
66 for (
unsigned int i = 0; i < layout.abs_pointer_count; ++i)
68 for (
unsigned int i = 0; i < layout.motor_count; ++i)
69 motors.emplace_back(layout.motors[i]);
94 std::vector<std::string>
keys;
162 if (CPrivateBase::m_interface->globalSingleInstance !=
nullptr)
164 throw std::logic_error(
165 "kodi::addon::CInstanceGame: Cannot create more than one game instance!");
167 SetAddonStruct(CPrivateBase::m_interface->firstKodiInstance);
168 CPrivateBase::m_interface->globalSingleInstance =
this;
201 for (
unsigned int i = 0; i < m_instanceData->props->proxy_dll_count; ++i)
203 if (m_instanceData->props->proxy_dll_paths[i] !=
nullptr)
204 paths.emplace_back(m_instanceData->props->proxy_dll_paths[i]);
206 return !paths.empty();
225 for (
unsigned int i = 0; i < m_instanceData->props->resource_directory_count; ++i)
227 if (m_instanceData->props->resource_directories[i] !=
nullptr)
228 dirs.emplace_back(m_instanceData->props->resource_directories[i]);
230 return !dirs.empty();
257 bool SupportsVFS()
const {
return m_instanceData->props->supports_vfs; }
272 for (
unsigned int i = 0; i < m_instanceData->props->extension_count; ++i)
274 if (m_instanceData->props->extensions[i] !=
nullptr)
275 extensions.emplace_back(m_instanceData->props->extensions[i]);
277 return !extensions.empty();
406 void CloseGame(
void) { m_instanceData->toKodi->CloseGame(m_instanceData->toKodi->kodiInstance); }
441 if (!CPrivateBase::m_interface->globalSingleInstance)
451 *
static_cast<CInstanceGame*
>(CPrivateBase::m_interface->globalSingleInstance)
452 ->m_instanceData->toKodi;
453 m_handle = cb.OpenStream(cb.kodiInstance, &properties);
454 return m_handle !=
nullptr;
466 if (!m_handle || !CPrivateBase::m_interface->globalSingleInstance)
470 *
static_cast<CInstanceGame*
>(CPrivateBase::m_interface->globalSingleInstance)
471 ->m_instanceData->toKodi;
472 cb.CloseStream(cb.kodiInstance, m_handle);
493 if (!m_handle || !CPrivateBase::m_interface->globalSingleInstance)
497 *
static_cast<CInstanceGame*
>(CPrivateBase::m_interface->globalSingleInstance)
498 ->m_instanceData->toKodi;
499 return cb.GetStreamBuffer(cb.kodiInstance, m_handle, width, height, &buffer);
513 if (!m_handle || !CPrivateBase::m_interface->globalSingleInstance)
517 *
static_cast<CInstanceGame*
>(CPrivateBase::m_interface->globalSingleInstance)
518 ->m_instanceData->toKodi;
519 cb.AddStreamData(cb.kodiInstance, m_handle, &packet);
533 if (!m_handle || !CPrivateBase::m_interface->globalSingleInstance)
537 *
static_cast<CInstanceGame*
>(CPrivateBase::m_interface->globalSingleInstance)
538 ->m_instanceData->toKodi;
539 cb.ReleaseStreamBuffer(cb.kodiInstance, m_handle, &buffer);
551 bool IsOpen()
const {
return m_handle !=
nullptr; }
555 KODI_GAME_STREAM_HANDLE m_handle =
nullptr;
590 return m_instanceData->toKodi->EnableHardwareRendering(m_instanceData->toKodi->kodiInstance,
625 return m_instanceData->toKodi->HwGetProcAddress(m_instanceData->toKodi->kodiInstance, sym);
661 virtual bool HasFeature(
const std::string& controller_id,
const std::string& feature_name)
698 const std::vector<kodi::addon::GameControllerLayout>& controllers)
711 virtual bool EnableKeyboard(
bool enable,
const std::string& controller_id) {
return false; }
722 virtual bool EnableMouse(
bool enable,
const std::string& controller_id) {
return false; }
770 const std::string& port_address,
771 const std::string& controller_id)
801 return m_instanceData->toKodi->InputEvent(m_instanceData->toKodi->kodiInstance, &event);
929 unsigned int consoleID,
930 const std::string& filePath)
961 const std::string& username,
962 const std::string& token,
985 std::string& postData,
986 const std::string& username,
987 const std::string& token,
989 const std::string& richPresence)
1040 instance->hdl =
this;
1042 instance->game->toAddon->LoadGame = ADDON_LoadGame;
1043 instance->game->toAddon->LoadGameSpecial = ADDON_LoadGameSpecial;
1044 instance->game->toAddon->LoadStandalone = ADDON_LoadStandalone;
1045 instance->game->toAddon->UnloadGame = ADDON_UnloadGame;
1046 instance->game->toAddon->GetGameTiming = ADDON_GetGameTiming;
1047 instance->game->toAddon->GetRegion = ADDON_GetRegion;
1048 instance->game->toAddon->RequiresGameLoop = ADDON_RequiresGameLoop;
1049 instance->game->toAddon->RunFrame = ADDON_RunFrame;
1050 instance->game->toAddon->Reset = ADDON_Reset;
1052 instance->game->toAddon->HwContextReset = ADDON_HwContextReset;
1053 instance->game->toAddon->HwContextDestroy = ADDON_HwContextDestroy;
1055 instance->game->toAddon->HasFeature = ADDON_HasFeature;
1056 instance->game->toAddon->GetTopology = ADDON_GetTopology;
1057 instance->game->toAddon->FreeTopology = ADDON_FreeTopology;
1058 instance->game->toAddon->SetControllerLayouts = ADDON_SetControllerLayouts;
1059 instance->game->toAddon->EnableKeyboard = ADDON_EnableKeyboard;
1060 instance->game->toAddon->EnableMouse = ADDON_EnableMouse;
1061 instance->game->toAddon->ConnectController = ADDON_ConnectController;
1062 instance->game->toAddon->InputEvent = ADDON_InputEvent;
1064 instance->game->toAddon->SerializeSize = ADDON_SerializeSize;
1065 instance->game->toAddon->Serialize = ADDON_Serialize;
1066 instance->game->toAddon->Deserialize = ADDON_Deserialize;
1068 instance->game->toAddon->CheatReset = ADDON_CheatReset;
1069 instance->game->toAddon->GetMemory = ADDON_GetMemory;
1070 instance->game->toAddon->SetCheat = ADDON_SetCheat;
1072 instance->game->toAddon->RCGenerateHashFromFile = ADDON_RCGenerateHashFromFile;
1073 instance->game->toAddon->RCGetGameIDUrl = ADDON_RCGetGameIDUrl;
1074 instance->game->toAddon->RCGetPatchFileUrl = ADDON_RCGetPatchFileUrl;
1075 instance->game->toAddon->RCPostRichPresenceUrl = ADDON_RCPostRichPresenceUrl;
1076 instance->game->toAddon->RCEnableRichPresence = ADDON_RCEnableRichPresence;
1077 instance->game->toAddon->RCGetRichPresenceEvaluation = ADDON_RCGetRichPresenceEvaluation;
1078 instance->game->toAddon->RCResetRuntime = ADDON_RCResetRuntime;
1080 instance->game->toAddon->FreeString = ADDON_FreeString;
1082 m_instanceData = instance->game;
1083 m_instanceData->toAddon->addonInstance =
this;
1090 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)->LoadGame(url);
1098 std::vector<std::string> urlList;
1099 for (
size_t i = 0; i < urlCount; ++i)
1101 if (urls[i] !=
nullptr)
1102 urlList.emplace_back(urls[i]);
1105 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)
1106 ->LoadGameSpecial(type, urlList);
1111 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)->LoadStandalone();
1116 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)->UnloadGame();
1122 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)
1123 ->GetGameTiming(*timing_info);
1128 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)->
GetRegion();
1133 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)->RequiresGameLoop();
1138 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)->RunFrame();
1143 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)->Reset();
1150 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)->HwContextReset();
1155 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)->HwContextDestroy();
1161 const char* controller_id,
1162 const char* feature_name)
1164 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)
1165 ->HasFeature(controller_id, feature_name);
1170 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)->GetTopology();
1176 static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)->FreeTopology(topology);
1181 unsigned int controller_count)
1183 if (controllers ==
nullptr)
1186 std::vector<GameControllerLayout> controllerList;
1187 controllerList.reserve(controller_count);
1188 for (
unsigned int i = 0; i < controller_count; ++i)
1189 controllerList.emplace_back(controllers[i]);
1191 static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)
1192 ->SetControllerLayouts(controllerList);
1197 const char* controller_id)
1199 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)
1200 ->EnableKeyboard(enable, controller_id);
1205 const char* controller_id)
1207 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)
1208 ->EnableMouse(enable, controller_id);
1213 const char* port_address,
1214 const char* controller_id)
1216 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)
1217 ->ConnectController(connect, port_address, controller_id);
1223 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)->InputEvent(*event);
1230 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)->SerializeSize();
1237 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)->Serialize(data, size);
1241 const uint8_t* data,
1244 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)->Deserialize(data, size);
1251 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)->CheatReset();
1259 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)
1260 ->GetMemory(type, *data, *size);
1268 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)
1269 ->SetCheat(index, enabled, code);
1274 unsigned int consoleID,
1275 const char* filePath)
1277 std::string cppHash;
1279 GAME_ERROR ret =
static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)
1280 ->RCGenerateHashFromFile(cppHash, consoleID, filePath);
1281 if (!cppHash.empty() && hash)
1283 *hash =
new char[cppHash.size() + 1];
1284 std::copy(cppHash.begin(), cppHash.end(), *hash);
1285 (*hash)[cppHash.size()] =
'\0';
1296 static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)->RCGetGameIDUrl(cppUrl, hash);
1297 if (!cppUrl.empty() && url)
1299 *url =
new char[cppUrl.size() + 1];
1300 std::copy(cppUrl.begin(), cppUrl.end(), *url);
1301 (*url)[cppUrl.size()] =
'\0';
1308 const char* username,
1310 unsigned int gameID)
1314 GAME_ERROR ret =
static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)
1315 ->RCGetPatchFileUrl(cppUrl, username, token, gameID);
1316 if (!cppUrl.empty() && url)
1318 *url =
new char[cppUrl.size() + 1];
1319 std::copy(cppUrl.begin(), cppUrl.end(), *url);
1320 (*url)[cppUrl.size()] =
'\0';
1328 const char* username,
1330 unsigned int gameID,
1331 const char* richPresence)
1334 std::string cppPostData;
1336 static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)
1337 ->RCPostRichPresenceUrl(cppUrl, cppPostData, username, token, gameID, richPresence);
1338 if (!cppUrl.empty())
1340 *url =
new char[cppUrl.size() + 1];
1341 std::copy(cppUrl.begin(), cppUrl.end(), *url);
1342 (*url)[cppUrl.size()] =
'\0';
1344 if (!cppPostData.empty())
1346 *postData =
new char[cppPostData.size() + 1];
1347 std::copy(cppPostData.begin(), cppPostData.end(), *postData);
1348 (*postData)[cppPostData.size()] =
'\0';
1357 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)
1358 ->RCEnableRichPresence(script);
1363 unsigned int consoleID)
1365 std::string cppEvaluation;
1366 GAME_ERROR ret =
static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)
1367 ->RCGetRichPresenceEvaluation(cppEvaluation, consoleID);
1368 if (!cppEvaluation.empty())
1370 *evaluation =
new char[cppEvaluation.size() + 1];
1371 std::copy(cppEvaluation.begin(), cppEvaluation.end(), *evaluation);
1372 (*evaluation)[cppEvaluation.size()] =
'\0';
1380 return static_cast<CInstanceGame*
>(instance->toAddon->addonInstance)->RCResetRuntime();
Definition AddonBase.h:566
Definition AddonBase.h:502
@ ADDON_LOG_INFO
1 : To include information messages in the log file.
Definition addon_base.h:187
CInstanceGame()
Game class constructor.
Definition Game.h:160
std::string GameClientDllPath() const
Callback to Kodi Function The path of the game client being loaded.
Definition Game.h:186
bool ProxyDllPaths(std::vector< std::string > &paths)
Callback to Kodi Function Paths to proxy DLLs used to load the game client.
Definition Game.h:199
bool ResourceDirectories(std::vector< std::string > &dirs)
Callback to Kodi Function The "system" directories of the frontend.
Definition Game.h:223
std::string ProfileDirectory() const
Callback to Kodi Function The writable directory of the frontend.
Definition Game.h:246
bool SupportsVFS() const
Callback to Kodi Function The value of the <supports_vfs> property from addon.xml.
Definition Game.h:257
~CInstanceGame() override=default
Destructor.
bool Extensions(std::vector< std::string > &extensions)
Callback to Kodi Function The extensions in the <extensions> property from addon.xml.
Definition Game.h:270
virtual GAME_ERROR RCGetRichPresenceEvaluation(std::string &evaluation, unsigned int consoleID)
Gets the rich presence evaluation for the current frame. Rich presence must be enabled first or this ...
Definition Game.h:1019
virtual GAME_ERROR RCGetPatchFileUrl(std::string &url, const std::string &username, const std::string &token, unsigned int gameID)
Gets a URL to the endpoint that returns the patch file.
Definition Game.h:960
virtual GAME_ERROR RCGenerateHashFromFile(std::string &hash, unsigned int consoleID, const std::string &filePath)
Generates a RetroAchievements hash for a given game that can be used to identify the game by RetroAch...
Definition Game.h:928
virtual GAME_ERROR RCPostRichPresenceUrl(std::string &url, std::string &postData, const std::string &username, const std::string &token, unsigned int gameID, const std::string &richPresence)
Gets a URL to the endpoint that updates the rich presence in the user's RetroAchievements profile.
Definition Game.h:984
virtual GAME_ERROR RCGetGameIDUrl(std::string &url, const std::string &hash)
Gets a URL to the endpoint that returns the game ID.
Definition Game.h:944
virtual GAME_ERROR RCEnableRichPresence(const std::string &script)
Enables rich presence.
Definition Game.h:1001
virtual GAME_ERROR RCResetRuntime()
Resets the runtime. Must be called each time a new rom is starting and when the savestate is changed.
Definition Game.h:1031
virtual GAME_ERROR CheatReset()
Reset the cheat system.
Definition Game.h:884
virtual GAME_ERROR GetMemory(GAME_MEMORY type, uint8_t *&data, size_t &size)
Get a region of memory.
Definition Game.h:896
virtual GAME_ERROR SetCheat(unsigned int index, bool enabled, const std::string &code)
Set a cheat code.
Definition Game.h:911
Game system timing.
Definition game.h:1134
GAME_REGION
Game region definition
Definition game.h:585
SPECIAL_GAME_TYPE
Special game types passed into game_load_game_special().
Definition game.h:603
GAME_MEMORY
Game Memory
Definition game.h:622
@ GAME_REGION_UNKNOWN
Game region unknown.
Definition game.h:587
void(* game_proc_address_t)(void)
Hardware framebuffer process function address
Definition game.h:425
Hardware rendering properties
Definition game.h:337
Stream buffers for hardware rendering and zero-copy support
Definition game.h:527
Stream packet and ephemeral metadata
Definition game.h:550
Immutable stream metadata
Definition game.h:502
GAME_ERROR
Game add-on error codes
Definition game.h:28
@ GAME_ERROR_NOT_IMPLEMENTED
The method called by the frontend is not implemented.
Definition game.h:36
virtual GAME_ERROR HwContextDestroy()
Called before the context is destroyed.
Definition Game.h:612
virtual GAME_ERROR HwContextReset()
Invalidates the current HW context and reinitializes GPU resources.
Definition Game.h:602
bool EnableHardwareRendering(const game_hw_rendering_properties &properties)
Callback to Kodi Function Enable hardware rendering functionality
Definition Game.h:588
game_proc_address_t HwGetProcAddress(const char *sym)
Callback to Kodi Function Get a symbol from the hardware context
Definition Game.h:623
bool IsOpen() const
Check if the stream opened correctly, e.g. after calling the constructor.
Definition Game.h:551
void ReleaseBuffer(game_stream_buffer &buffer)
Free an allocated buffer.
Definition Game.h:531
void Close()
Free the specified stream.
Definition Game.h:464
bool GetBuffer(unsigned int width, unsigned int height, game_stream_buffer &buffer)
Get a buffer for zero-copy stream data.
Definition Game.h:491
void AddData(const game_stream_packet &packet)
Add a data packet to a stream.
Definition Game.h:511
bool Open(const game_stream_properties &properties)
Create a stream for gameplay data.
Definition Game.h:439
virtual GAME_ERROR LoadGameSpecial(SPECIAL_GAME_TYPE type, const std::vector< std::string > &urls)
Load a game that requires multiple files.
Definition Game.h:323
virtual GAME_ERROR UnloadGame()
Unload the current game.
Definition Game.h:349
virtual GAME_ERROR GetGameTiming(game_system_timing &timing_info)
Get timing information about the loaded game.
Definition Game.h:359
virtual GAME_ERROR RunFrame()
Run a single frame for add-ons that use a game loop.
Definition Game.h:389
virtual bool RequiresGameLoop()
Return true if the client requires the frontend to provide a game loop.
Definition Game.h:381
virtual GAME_ERROR Reset()
Reset the current game.
Definition Game.h:397
virtual GAME_ERROR LoadStandalone()
Begin playing without a game file.
Definition Game.h:339
void CloseGame(void)
Callback to Kodi Function Requests the frontend to stop the current game
Definition Game.h:406
virtual GAME_ERROR LoadGame(const std::string &url)
Load a game.
Definition Game.h:312
virtual GAME_REGION GetRegion()
Get region of the loaded game.
Definition Game.h:370
virtual GAME_ERROR Deserialize(const uint8_t *data, size_t size)
Deserialize the game from the given state.
Definition Game.h:852
virtual GAME_ERROR Serialize(uint8_t *data, size_t size)
Serialize the state of the game.
Definition Game.h:841
virtual size_t SerializeSize()
Get the number of bytes required to serialize the game.
Definition Game.h:830
void ATTR_DLL_LOCAL Log(const ADDON_LOG loglevel, const char *format,...)
Add a message to Kodi's log.
Definition AddonBase.h:1939
std::string ATTR_DLL_LOCAL GetRegion(const std::string &id)
Returns your regions setting as a string for the specified id.
Definition General.h:331
Game instance.
Definition game.h:1306
Game callbacks.
Definition game.h:1219
Definition addon_base.h:268
Internal used structure to have stored C API data above and available for everything below.
Definition AddonBase.h:73