Skip to content

Commit

Permalink
Remove static method for awarding achievements
Browse files Browse the repository at this point in the history
  • Loading branch information
NikosSiak committed Nov 5, 2021
1 parent f3832ae commit 874fc6e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 53 deletions.
19 changes: 6 additions & 13 deletions xbmc/addons/kodi-dev-kit/include/kodi/addon-instance/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

//----------------------------------------------------------------------------

///@}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -1328,13 +1328,6 @@ class ATTRIBUTE_HIDDEN CInstanceGame : public IAddonInstance
return static_cast<CInstanceGame*>(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<CInstanceGame*>(instance->toAddon->addonInstance)->GetCheevo_URL_ID(Callback);
}

inline static GAME_ERROR ADDON_RCResetRuntime(const AddonInstance_Game* instance)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/*!
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion xbmc/cores/RetroPlayer/RetroPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
29 changes: 13 additions & 16 deletions xbmc/cores/RetroPlayer/cheevos/Cheevos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ constexpr int RESPORNSE_SIZE = 64;
constexpr int URL_SIZE = 512;
constexpr int POST_DATA_SIZE = 1024;

std::unordered_map<unsigned, vector<std::string>> CCheevos::activated_cheevo_map;

CCheevos::CCheevos(GAME::CGameClient* gameClient,
CFileItem& fileItem,
std::string userName,
Expand All @@ -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();
Expand Down Expand Up @@ -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()};
}
Expand Down Expand Up @@ -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())
{
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
10 changes: 5 additions & 5 deletions xbmc/cores/RetroPlayer/cheevos/Cheevos.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned, std::vector<std::string> > activated_cheevo_map;
void ActivateAchievements();
void AwardAchievement(const char* achievementUrl, unsigned cheevoId);

private:
bool LoadData();
Expand All @@ -57,6 +55,8 @@ class CCheevos
std::string m_richPresenceScript;
unsigned m_gameID = 0;
bool m_richPresenceLoaded = false;

std::unordered_map<unsigned, std::vector<std::string>> m_activatedCheevoMap;


const std::map<std::string, int> m_extensionToConsole = {{".a26", RC_CONSOLE_ATARI_2600},
Expand Down
34 changes: 20 additions & 14 deletions xbmc/games/addons/GameClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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;
Expand All @@ -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: ------------------------------------");
Expand Down Expand Up @@ -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<CGameClient*>(kodiInstance);
if (!gameClient || !gameClient->m_cheevos)
{
return;
}

gameClient->m_cheevos->AwardAchievement(achievementUrl, cheevoId);
}
9 changes: 7 additions & 2 deletions xbmc/games/addons/GameClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace KODI
{
namespace RETRO
{
class CCheevos;
class IStreamManager;
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -235,6 +238,8 @@ class CGameClient : public ADDON::CAddonDll, private CGameClientStruct
std::unique_ptr<CGameClientInGameSaves> m_inGameSaves;

CCriticalSection m_critSection;

RETRO::CCheevos* m_cheevos;
};

} // namespace GAME
Expand Down

0 comments on commit 874fc6e

Please sign in to comment.