diff --git a/src/cheevos/Cheevos.cpp b/src/cheevos/Cheevos.cpp index e5486e96..149ae6ba 100644 --- a/src/cheevos/Cheevos.cpp +++ b/src/cheevos/Cheevos.cpp @@ -17,9 +17,13 @@ using namespace LIBRETRO; +constexpr int URL_SIZE = 512; + CCheevos::CCheevos() { rc_runtime_init(&m_runtime); + m_cheevo_id = 0; + url = NULL; } void CCheevos::Initialize() @@ -51,6 +55,7 @@ bool CCheevos::GenerateHashFromFile(char* hash, int consoleID, const char* fileP bool CCheevos::GetGameIDUrl(char* url, size_t size, const char* hash) { + m_hash = hash; return rc_url_get_gameid(url, size, hash) == 0; } @@ -72,6 +77,8 @@ bool CCheevos::PostRichPresenceUrl(char* url, unsigned gameID, const char* richPresence) { + m_username = username; + m_token = token; return rc_url_ping(url, urlSize, postData, postSize, username, token, gameID, richPresence) >= 0; } @@ -91,6 +98,79 @@ void CCheevos::EvaluateRichPresence(char* evaluation, size_t size) rc_evaluate_richpresence(m_richPresence, evaluation, size, PeekInternal, this, NULL); } +void CCheevos::ActivateAchievement(unsigned cheevo_id, const char* memaddr) //1 +{ + int res = rc_runtime_activate_achievement(&m_runtime, cheevo_id, memaddr, NULL, 0); + if (res == 0) + { + kodi::Log(ADDON_LOG_ERROR, "AAAbAAAAA"); + } + + + // it will return integer value 0 in case achivement is activated successfully. +} + +bool CCheevos::AwardAchievement(char* url, + size_t size, + const char* username, + const char* token, + unsigned cheevo_id, + int hardcore, + const char* game_hash) +{ + + return rc_url_award_cheevo(url, size, username, token, cheevo_id, 0, game_hash) >= 0; +} + +bool LIBRETRO::CCheevos::GetCheevo_ID(void (*Callback)(char* achievement_url, unsigned cheevo_id)) +{ + kodi::Log(ADDON_LOG_ERROR, "Fine till nowwwww in Callback"); + Callback(url, m_cheevo_id); + return true; +} + +void CCheevos::DeactivateTriggeredAchievement(unsigned cheevo_id) +{ + kodi::Log(ADDON_LOG_ERROR, "Fine till nowwwww"); + kodi::Log(ADDON_LOG_ERROR, "Fine till nowwwww"); + kodi::Log(ADDON_LOG_ERROR, "Fine till nowwwww"); + if (CCheevos::Get().AwardAchievement(url, URL_SIZE, m_username, m_token, cheevo_id, 0, m_hash)) + { + m_cheevo_id = cheevo_id; + rc_runtime_deactivate_achievement(&m_runtime, cheevo_id); + kodi::Log(ADDON_LOG_ERROR, "urls now"); + kodi::Log(ADDON_LOG_ERROR, "urls now"); + kodi::Log(ADDON_LOG_ERROR, "urls now"); + kodi::Log(ADDON_LOG_ERROR, url); + CCheevos::Get().GetCheevo_ID(this->Callback); + url = NULL; + } +} + +void CCheevos::RuntimeEventHandler(const rc_runtime_event_t* runtime_event) +{ + if (runtime_event->type == RC_RUNTIME_EVENT_ACHIEVEMENT_TRIGGERED) + { + CCheevos::Get().DeactivateTriggeredAchievement(runtime_event->id); + } + else + { + kodi::Log(ADDON_LOG_ERROR, + "Means function is running fine though achievement is not triggered"); + printf("%u", runtime_event->id); + printf("%u", 1); + const char* b = &runtime_event->type; + kodi::Log(ADDON_LOG_ERROR, b); + + } + kodi::Log(ADDON_LOG_ERROR, "Runtime Event Handler function is called"); +} + +void CCheevos::TestAchievementPerFrame() +{ + rc_runtime_do_frame(&m_runtime, RuntimeEventHandler, PeekInternal, this, NULL); +} + unsigned int CCheevos::PeekInternal(unsigned address, unsigned num_bytes, void* ud) { CCheevos* cheevos = static_cast(ud); diff --git a/src/cheevos/Cheevos.h b/src/cheevos/Cheevos.h index f8e8167e..c2c573d3 100644 --- a/src/cheevos/Cheevos.h +++ b/src/cheevos/Cheevos.h @@ -42,7 +42,22 @@ class CCheevos const char* richPresence); void EnableRichPresence(const char* script); void EvaluateRichPresence(char* evaluation, size_t size); + void ActivateAchievement(unsigned cheevo_id, const char* memaddr); + bool AwardAchievement(char* url, + size_t size, + const char* username, + const char* token, + unsigned cheevo_id, + int hardcore, + const char* game_hash); + void DeactivateTriggeredAchievement(unsigned cheevo_id); + static void RuntimeEventHandler(const rc_runtime_event_t* runtime_event); + void TestAchievementPerFrame(); unsigned int Peek(unsigned int address, unsigned int numBytes); + bool GetCheevo_ID(void (*Callback)(char* achievement_url, unsigned cheevo_id)); + //static rc_runtime_event_handler_t RuntimeEventHandler; + void (*Callback)(char* achievement_url, unsigned cheevo_id); + private: const uint8_t* FixupFind(unsigned address, CMemoryMap& mmap, int consolecheevos); @@ -59,5 +74,12 @@ class CCheevos rc_richpresence_t* m_richPresence = nullptr; std::string m_richPresenceScript; std::vector m_richPresenceBuffer; + + const char* m_hash; + const char* m_username; + const char* m_token; + char* url; + char postData[1024]; + unsigned m_cheevo_id; }; } diff --git a/src/client.cpp b/src/client.cpp index 6ebcf693..c3cc5886 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -278,6 +278,7 @@ GAME_ERROR CGameLibRetro::RunFrame() m_clientBridge.FrameTime(delta); m_client.retro_run(); + TestAchievementPerFrame(); CLibretroEnvironment::Get().OnFrameEnd(); @@ -523,6 +524,63 @@ GAME_ERROR CGameLibRetro::GetRichPresenceEvaluation(char* evaluation, size_t siz return GAME_ERROR_NO_ERROR; } +GAME_ERROR CGameLibRetro::ActivateAchievement(unsigned cheevo_id, const char* memaddr) +{ + CCheevos::Get().ActivateAchievement(cheevo_id, memaddr); + return GAME_ERROR_NO_ERROR; +} + + +GAME_ERROR CGameLibRetro::GetCheevo_ID(void (*Callback)(char* achievement_url, unsigned cheevo_id)) +{ + if (!CCheevos::Get().GetCheevo_ID(Callback)) + { + return GAME_ERROR_FAILED; + } + + return GAME_ERROR_NO_ERROR(); +} + +GAME_ERROR CGameLibRetro::AwardAchievement(char* url, + size_t size, + const char* username, + const char* token, + unsigned cheevo_id, + int hardcore, + const char* game_hash) +{ + if (!CCheevos::Get().AwardAchievement(url, size, username, token, cheevo_id, hardcore, game_hash)) + { + return GAME_ERROR_FAILED; + } + return GAME_ERROR_NO_ERROR; +} + +/* GAME_ERROR CGameLibRetro::RuntimeEventHandler(char* url, + size_t size, + const char* username, + const char* token, + unsigned id, + unsigned cheevo_id, + int hardcore, + const char* game_hash) +{ + return GAME_ERROR_NO_ERROR; +} +*/ +GAME_ERROR CGameLibRetro::DeactivateTriggeredAchievement(unsigned cheevo_id) +{ + CCheevos::Get().DeactivateTriggeredAchievement(cheevo_id); + return GAME_ERROR_NO_ERROR; +} + + +GAME_ERROR CGameLibRetro::TestAchievementPerFrame() +{ + CCheevos::Get().TestAchievementPerFrame(); + return GAME_ERROR_NO_ERROR; +} + GAME_ERROR CGameLibRetro::RCResetRuntime() { CCheevos::Get().ResetRuntime(); diff --git a/src/client.h b/src/client.h index 7fca6d31..979efa73 100644 --- a/src/client.h +++ b/src/client.h @@ -88,7 +88,19 @@ class ATTRIBUTE_HIDDEN CGameLibRetro const char* richPresence) override; GAME_ERROR EnableRichPresence(const char* script) override; GAME_ERROR GetRichPresenceEvaluation(char* evaluation, size_t size) override; + GAME_ERROR ActivateAchievement(unsigned cheevo_id, const char* memaddr) override; + GAME_ERROR AwardAchievement(char* url, + size_t size, + const char* username, + const char* token, + unsigned cheevo_id, + int hardcore, + const char* game_hash) override; + GAME_ERROR DeactivateTriggeredAchievement(unsigned cheevo_id) override; + GAME_ERROR TestAchievementPerFrame() override; GAME_ERROR RCResetRuntime() override; + GAME_ERROR GetCheevo_ID(void (*Callback)(char* achievement_url, unsigned cheevo_id)) override; + //GAME_ERROR RCResetRuntime() override; private: GAME_ERROR AudioAvailable();