From 874fc6e0741c17b3fd1d15170b828de1cc5e049d Mon Sep 17 00:00:00 2001 From: NickSiak Date: Tue, 2 Nov 2021 14:22:43 +0200 Subject: [PATCH] Remove static method for awarding achievements --- .../include/kodi/addon-instance/Game.h | 19 ++++------- .../include/kodi/c-api/addon-instance/game.h | 5 +-- xbmc/cores/RetroPlayer/RetroPlayer.cpp | 2 +- xbmc/cores/RetroPlayer/cheevos/Cheevos.cpp | 29 +++++++--------- xbmc/cores/RetroPlayer/cheevos/Cheevos.h | 10 +++--- xbmc/games/addons/GameClient.cpp | 34 +++++++++++-------- xbmc/games/addons/GameClient.h | 9 +++-- 7 files changed, 55 insertions(+), 53 deletions(-) diff --git a/xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/Game.h b/xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/Game.h index 137a5a6ba06af..074b78782a765 100644 --- a/xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/Game.h +++ b/xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/Game.h @@ -999,17 +999,18 @@ class ATTRIBUTE_HIDDEN CInstanceGame : public IAddonInstance { return GAME_ERROR_NOT_IMPLEMENTED; } - - virtual GAME_ERROR GetCheevo_URL_ID(void (*Callback)(const char* achievement_url, unsigned cheevo_id)) - { - return GAME_ERROR_NOT_IMPLEMENTED; - } virtual GAME_ERROR RCResetRuntime() { return GAME_ERROR_NOT_IMPLEMENTED; } + void AwardAchievement(const char* achievementUrl, unsigned cheevoId) + { + m_instanceData->toKodi->AwardAchievement(m_instanceData->toKodi->kodiInstance, achievementUrl, + cheevoId); + } + //---------------------------------------------------------------------------- ///@} @@ -1062,7 +1063,6 @@ class ATTRIBUTE_HIDDEN CInstanceGame : public IAddonInstance m_instanceData->toAddon->EnableRichPresence = ADDON_EnableRichPresence; m_instanceData->toAddon->GetRichPresenceEvaluation = ADDON_GetRichPresenceEvaluation; m_instanceData->toAddon->ActivateAchievement = ADDON_ActivateAchievement; - m_instanceData->toAddon->GetCheevo_URL_ID = ADDON_GetCheevo_URL_ID; m_instanceData->toAddon->RCResetRuntime = ADDON_RCResetRuntime; } @@ -1328,13 +1328,6 @@ class ATTRIBUTE_HIDDEN CInstanceGame : public IAddonInstance return static_cast(instance->toAddon->addonInstance) ->ActivateAchievement(cheevo_id, memaddr); } - - inline static GAME_ERROR ADDON_GetCheevo_URL_ID(const AddonInstance_Game* instance, - void (*Callback)(const char* achievement_url, - unsigned cheevo_id)) - { - return static_cast(instance->toAddon->addonInstance)->GetCheevo_URL_ID(Callback); - } inline static GAME_ERROR ADDON_RCResetRuntime(const AddonInstance_Game* instance) { diff --git a/xbmc/addons/kodi-dev-kit/include/kodi/c-api/addon-instance/game.h b/xbmc/addons/kodi-dev-kit/include/kodi/c-api/addon-instance/game.h index a4cb7287786a5..c1668d2220e38 100644 --- a/xbmc/addons/kodi-dev-kit/include/kodi/c-api/addon-instance/game.h +++ b/xbmc/addons/kodi-dev-kit/include/kodi/c-api/addon-instance/game.h @@ -1144,6 +1144,9 @@ extern "C" void (*CloseStream)(KODI_HANDLE, KODI_GAME_STREAM_HANDLE); game_proc_address_t (*HwGetProcAddress)(KODI_HANDLE kodiInstance, const char* symbol); bool (*InputEvent)(KODI_HANDLE kodiInstance, const struct game_input_event* event); + void (*AwardAchievement)(KODI_HANDLE kodiInstance, + const char* achievementUrl, + unsigned cheevoId); } AddonToKodiFuncTable_Game; /*! @@ -1209,8 +1212,6 @@ extern "C" GAME_ERROR(__cdecl* GetRichPresenceEvaluation)(const AddonInstance_Game*, char*, size_t); GAME_ERROR(__cdecl* ActivateAchievement)(const AddonInstance_Game*, unsigned, const char*); - GAME_ERROR(__cdecl* GetCheevo_URL_ID) - (const AddonInstance_Game*, void (*Callback)(const char* achievement_url, unsigned cheevo_id)); GAME_ERROR(__cdecl* RCResetRuntime)(const AddonInstance_Game*); } KodiToAddonFuncTable_Game; diff --git a/xbmc/cores/RetroPlayer/RetroPlayer.cpp b/xbmc/cores/RetroPlayer/RetroPlayer.cpp index 19c6e48dd8b84..7967aced37d09 100644 --- a/xbmc/cores/RetroPlayer/RetroPlayer.cpp +++ b/xbmc/cores/RetroPlayer/RetroPlayer.cpp @@ -194,7 +194,7 @@ bool CRetroPlayer::OpenFile(const CFileItem& file, const CPlayerOptions& options m_cheevos->EnableRichPresence(); - m_cheevos->ActivateAchievement(); + m_cheevos->ActivateAchievements(); // Calls to external code could mutate file item, so make a copy CFileItem fileCopy(*m_fileItem); diff --git a/xbmc/cores/RetroPlayer/cheevos/Cheevos.cpp b/xbmc/cores/RetroPlayer/cheevos/Cheevos.cpp index 3bb98ca1aefe4..83fd97846957e 100644 --- a/xbmc/cores/RetroPlayer/cheevos/Cheevos.cpp +++ b/xbmc/cores/RetroPlayer/cheevos/Cheevos.cpp @@ -52,8 +52,6 @@ constexpr int RESPORNSE_SIZE = 64; constexpr int URL_SIZE = 512; constexpr int POST_DATA_SIZE = 1024; -std::unordered_map> CCheevos::activated_cheevo_map; - CCheevos::CCheevos(GAME::CGameClient* gameClient, CFileItem& fileItem, std::string userName, @@ -66,6 +64,10 @@ CCheevos::CCheevos(GAME::CGameClient* gameClient, m_gameClient->SetRetroAchievementsCredentials(m_userName.c_str(), m_loginToken.c_str()); } +CCheevos::~CCheevos() { + m_gameClient->SetCheevos(nullptr); +} + void CCheevos::ResetRuntime() { m_gameClient->RCResetRuntime(); @@ -140,7 +142,7 @@ bool CCheevos::LoadData() const CVariant& achievement = *it; if (achievement[FLAGS].asUnsignedInteger() == 3) { - activated_cheevo_map[achievement[CHEEVO_ID].asUnsignedInteger()] = { + m_activatedCheevoMap[achievement[CHEEVO_ID].asUnsignedInteger()] = { achievement[MEM_ADDR].asString(), achievement[CHEEVO_TITLE].asString(), achievement[BADGE_NAME].asString()}; } @@ -175,9 +177,9 @@ void CCheevos::EnableRichPresence() m_richPresenceScript.clear(); } -void CCheevos::ActivateAchievement() +void CCheevos::ActivateAchievements() { - if (activated_cheevo_map.size() == 0) + if (m_activatedCheevoMap.size() == 0) { if (!LoadData()) { @@ -189,12 +191,12 @@ void CCheevos::ActivateAchievement() CLog::Log(LOGERROR, "No active core achievement for the game"); } } - for (auto &it:activated_cheevo_map) + for (auto &it:m_activatedCheevoMap) { m_gameClient->ActivateAchievement(it.first, it.second[0].c_str()); } - //call for checking triggered achievement - CheckTriggeredAchievement(); + + m_gameClient->SetCheevos(this); } bool CCheevos::GetRichPresenceEvaluation(char* evaluation, size_t size) @@ -229,19 +231,14 @@ bool CCheevos::GetRichPresenceEvaluation(char* evaluation, size_t size) return true; } -void CCheevos::Callback_URL_ID(const char* achievement_url, unsigned cheevo_id) +void CCheevos::AwardAchievement(const char* achievementUrl, unsigned cheevoId) { XFILE::CCurlFile curl; std::string res; - curl.Get(achievement_url, res); - std::string description = activated_cheevo_map[cheevo_id][1]; + curl.Get(achievementUrl, res); + std::string description = m_activatedCheevoMap[cheevoId][1]; std::string header = std::string("Congratulations, ") + std::string("Achievement Unlocked"); CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, header, description); } - -void CCheevos::CheckTriggeredAchievement() -{ - m_gameClient->GetAchievement_URL_ID(Callback_URL_ID); // Callback for triggered achievement URL and id -} \ No newline at end of file diff --git a/xbmc/cores/RetroPlayer/cheevos/Cheevos.h b/xbmc/cores/RetroPlayer/cheevos/Cheevos.h index f920b8a44ed0e..07081cb36c8c1 100644 --- a/xbmc/cores/RetroPlayer/cheevos/Cheevos.h +++ b/xbmc/cores/RetroPlayer/cheevos/Cheevos.h @@ -33,15 +33,13 @@ class CCheevos CFileItem& fileItem, std::string userName, std::string loginToken); + ~CCheevos(); void ResetRuntime(); void EnableRichPresence(); bool GetRichPresenceEvaluation(char* evaluation, size_t size); - void ActivateAchievement(); - static void Callback_URL_ID(const char* achievement_url, unsigned cheevo_id); - void CheckTriggeredAchievement(); - - static std::unordered_map > activated_cheevo_map; + void ActivateAchievements(); + void AwardAchievement(const char* achievementUrl, unsigned cheevoId); private: bool LoadData(); @@ -57,6 +55,8 @@ class CCheevos std::string m_richPresenceScript; unsigned m_gameID = 0; bool m_richPresenceLoaded = false; + + std::unordered_map> m_activatedCheevoMap; const std::map m_extensionToConsole = {{".a26", RC_CONSOLE_ATARI_2600}, diff --git a/xbmc/games/addons/GameClient.cpp b/xbmc/games/addons/GameClient.cpp index 37173c3de997c..cc5119d315dda 100644 --- a/xbmc/games/addons/GameClient.cpp +++ b/xbmc/games/addons/GameClient.cpp @@ -17,6 +17,7 @@ #include "URL.h" #include "addons/AddonManager.h" #include "addons/BinaryAddonCache.h" +#include "cores/RetroPlayer/cheevos/Cheevos.h" #include "filesystem/Directory.h" #include "filesystem/File.h" #include "filesystem/SpecialProtocol.h" @@ -169,6 +170,7 @@ bool CGameClient::Initialize(void) m_struct.toKodi->CloseStream = cb_close_stream; m_struct.toKodi->HwGetProcAddress = cb_hw_get_proc_address; m_struct.toKodi->InputEvent = cb_input_event; + m_struct.toKodi->AwardAchievement = cb_award_achievement; memset(m_struct.toAddon, 0, sizeof(KodiToAddonFuncTable_Game)); @@ -701,20 +703,6 @@ void CGameClient::ActivateAchievement(unsigned cheevo_id, const char* memaddr) } } -void CGameClient::GetAchievement_URL_ID(void (*Callback)(const char* achievement_url, unsigned cheevo_id)) -{ - GAME_ERROR error = GAME_ERROR_NO_ERROR; - - try - { - LogError(error = m_struct.toAddon->GetCheevo_URL_ID(&m_struct, Callback), "GetCheevo_ID_URL()"); - } - catch (...) - { - LogException("GetCheevoID()"); - } -} - void CGameClient::RCResetRuntime() { GAME_ERROR error = GAME_ERROR_NO_ERROR; @@ -729,6 +717,11 @@ void CGameClient::RCResetRuntime() } } +void CGameClient::SetCheevos(RETRO::CCheevos* cheevos) +{ + m_cheevos = cheevos; +} + void CGameClient::LogAddonProperties(void) const { CLog::Log(LOGINFO, "GAME: ------------------------------------"); @@ -860,3 +853,16 @@ bool CGameClient::cb_input_event(KODI_HANDLE kodiInstance, const game_input_even return gameClient->Input().ReceiveInputEvent(*event); } + +void CGameClient::cb_award_achievement(KODI_HANDLE kodiInstance, + const char* achievementUrl, + unsigned cheevoId) +{ + CGameClient* gameClient = static_cast(kodiInstance); + if (!gameClient || !gameClient->m_cheevos) + { + return; + } + + gameClient->m_cheevos->AwardAchievement(achievementUrl, cheevoId); +} diff --git a/xbmc/games/addons/GameClient.h b/xbmc/games/addons/GameClient.h index cb50a7fabe80d..ce6dcde7c1c1c 100644 --- a/xbmc/games/addons/GameClient.h +++ b/xbmc/games/addons/GameClient.h @@ -25,6 +25,7 @@ namespace KODI { namespace RETRO { +class CCheevos; class IStreamManager; } @@ -161,9 +162,8 @@ class CGameClient : public ADDON::CAddonDll, private CGameClientStruct // When the game is reset, the runtime should also be reset void ActivateAchievement(unsigned cheevo_id, const char* memaddr); - void GetAchievement_URL_ID(void (*Callback)(const char* achievement_url, unsigned cheevo_id)); - void RCResetRuntime(); + void SetCheevos(RETRO::CCheevos* cheevos); /*! * @brief To get the interface table used between addon and kodi @@ -209,6 +209,9 @@ class CGameClient : public ADDON::CAddonDll, private CGameClientStruct static void cb_close_stream(KODI_HANDLE kodiInstance, KODI_GAME_STREAM_HANDLE stream); static game_proc_address_t cb_hw_get_proc_address(KODI_HANDLE kodiInstance, const char* sym); static bool cb_input_event(KODI_HANDLE kodiInstance, const game_input_event* event); + static void cb_award_achievement(KODI_HANDLE kodiInstance, + const char* achievementUrl, + unsigned cheevoId); //@} // Game subsystems @@ -235,6 +238,8 @@ class CGameClient : public ADDON::CAddonDll, private CGameClientStruct std::unique_ptr m_inGameSaves; CCriticalSection m_critSection; + + RETRO::CCheevos* m_cheevos; }; } // namespace GAME