Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Game API v4.0.0: Achievement support for RetroPlayer #104

Open
wants to merge 1 commit into
base: Omega
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion game.libretro/addon.xml.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="game.libretro"
name="Libretro Compatibility"
version="20.2.2"
version="20.3.0"
provider-name="Team Kodi">
<backwards-compatibility abi="1.0.0"/>
<requires>@ADDON_DEPENDS@</requires>
Expand Down
48 changes: 48 additions & 0 deletions src/cheevos/Cheevos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ bool CCheevos::GetPatchFileUrl(std::string& url,
return res == 0;
}

void CCheevos::SetRetroAchievementsCredentials(const std::string& username, const std::string& token)
{
m_username = username;
m_token = token;
}

bool CCheevos::PostRichPresenceUrl(std::string& url,
std::string& postData,
const std::string& username,
Expand Down Expand Up @@ -129,6 +135,48 @@ void CCheevos::EvaluateRichPresence(std::string& evaluation, unsigned int consol
evaluation = _evaluation;
}

void CCheevos::ActivateAchievement(unsigned cheevo_id, const char* memaddr)
{
rc_runtime_activate_achievement(&m_runtime, cheevo_id, memaddr, NULL, 0);
// it will return integer value 0 in case achivement is activated successfully.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should add a check here instead of a comment like we do in the other methods

}

bool CCheevos::AwardAchievement(
char* url, size_t size, unsigned cheevo_id, int hardcore, const std::string& game_hash)
{
return rc_url_award_cheevo(url, size, m_username.c_str(), m_token.c_str(), cheevo_id, 0,
game_hash.c_str()) >= 0;
}

void CCheevos::GetCheevo_URL_ID(void (*callback)(const char* achievement_url, unsigned cheevo_id))
{
m_callback = callback;
}

void CCheevos::DeactivateTriggeredAchievement(unsigned cheevo_id)
{
rc_runtime_deactivate_achievement(&m_runtime, cheevo_id);
char url[URL_SIZE];
if (AwardAchievement(url, URL_SIZE, cheevo_id, 0, m_hash.c_str()))
{
std::string achievement_url = url;
m_callback(url, cheevo_id);
}
}

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);
}
}

void CCheevos::TestCheevoStatusPerFrame()
{
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<CCheevos*>(ud);
Expand Down
14 changes: 14 additions & 0 deletions src/cheevos/Cheevos.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class CCheevos
void ResetRuntime();
bool GenerateHashFromFile(std::string& hash, unsigned int consoleID, const std::string& filePath);
bool GetGameIDUrl(std::string& url, const std::string& hash);
void SetRetroAchievementsCredentials(const std::string& username, const std::string& token);
bool GetPatchFileUrl(std::string& url,
const std::string& username,
const std::string& token,
Expand All @@ -42,7 +43,13 @@ class CCheevos
const std::string& richPresence);
void EnableRichPresence(const std::string& script);
void EvaluateRichPresence(std::string& evaluation, unsigned int consoleID);
void ActivateAchievement(unsigned cheevo_id, const char* memaddr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the param name to cheevoId to match the code style. This applies for the other methods as well

bool AwardAchievement(
char* url, size_t size, unsigned cheevo_id, int hardcore, const std::string& game_hash);
void DeactivateTriggeredAchievement(unsigned cheevo_id);
void TestCheevoStatusPerFrame();
unsigned int Peek(unsigned int address, unsigned int numBytes);
void GetCheevo_URL_ID(void (*callback)(const char* achievement_url, unsigned cheevo_id));

private:
const uint8_t* FixupFind(unsigned address, CMemoryMap& mmap, int consolecheevos);
Expand All @@ -51,6 +58,9 @@ class CCheevos
size_t Reduce(size_t addr, size_t mask);

static unsigned int PeekInternal(unsigned address, unsigned num_bytes, void* ud);
static void RuntimeEventHandler(const rc_runtime_event_t* runtime_event);

void (*m_callback)(const char* achievement_url, unsigned cheevo_id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why but I remember that we wanted to remove this, I had open this PR Shardul555#1 I just rebased it on this PR.

@garbear can you check it if you remember why we wanted this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want a static function in RetroPlayer.


int m_consoleID;

Expand All @@ -61,5 +71,9 @@ class CCheevos
rc_richpresence_t* m_richPresence = nullptr;
std::string m_richPresenceScript;
std::vector<char> m_richPresenceBuffer;

std::string m_hash;
std::string m_username;
std::string m_token;
};
} // namespace LIBRETRO
22 changes: 22 additions & 0 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ GAME_ERROR CGameLibRetro::RunFrame()

m_client.retro_run();

CCheevos::Get().TestCheevoStatusPerFrame();

CLibretroEnvironment::Get().OnFrameEnd();

return GAME_ERROR_NO_ERROR;
Expand Down Expand Up @@ -502,6 +504,12 @@ GAME_ERROR CGameLibRetro::RCGetPatchFileUrl(std::string& url,
return GAME_ERROR_NO_ERROR;
}

GAME_ERROR CGameLibRetro::SetRetroAchievementsCredentials(const char* username, const char* token)
{
CCheevos::Get().SetRetroAchievementsCredentials(username, token);
return GAME_ERROR_NO_ERROR;
}

GAME_ERROR CGameLibRetro::RCPostRichPresenceUrl(std::string& url,
std::string& postData,
const std::string& username,
Expand Down Expand Up @@ -530,6 +538,20 @@ GAME_ERROR CGameLibRetro::RCGetRichPresenceEvaluation(std::string& evaluation,
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_URL_ID(void (*Callback)(const char* achievement_url,
unsigned cheevo_id))
{
CCheevos::Get().GetCheevo_URL_ID(Callback);
return GAME_ERROR_NO_ERROR;
}

GAME_ERROR CGameLibRetro::RCResetRuntime()
{
CCheevos::Get().ResetRuntime();
Expand Down
4 changes: 4 additions & 0 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class ATTR_DLL_LOCAL CGameLibRetro
const std::string& username,
const std::string& token,
unsigned int gameID) override;
GAME_ERROR SetRetroAchievementsCredentials(const char* username, const char* token);
GAME_ERROR RCPostRichPresenceUrl(std::string& url,
std::string& postData,
const std::string& username,
Expand All @@ -86,7 +87,10 @@ class ATTR_DLL_LOCAL CGameLibRetro
const std::string& richPresence) override;
GAME_ERROR RCEnableRichPresence(const std::string& script) override;
GAME_ERROR RCGetRichPresenceEvaluation(std::string& evaluation, unsigned int consoleID) override;
GAME_ERROR ActivateAchievement(unsigned cheevo_id, const char* memaddr) override;
GAME_ERROR RCResetRuntime() override;
GAME_ERROR GetCheevo_URL_ID(void (*Callback)(const char* achievement_url,
unsigned cheevo_id)) override;

private:
GAME_ERROR AudioAvailable();
Expand Down