Skip to content

Commit

Permalink
Add Rich Presence to Achievement Dialog Header
Browse files Browse the repository at this point in the history
This refactors the Rich Presence generation to store to a member field that can be exposed to the UI to display the Rich Presence in the achievement header. It still updates at its original rate of once per two minutes, but calls an update on the dialog when that ticks.
  • Loading branch information
LillyJadeKatrin committed Aug 16, 2023
1 parent 54d3a22 commit 5890e6f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
20 changes: 14 additions & 6 deletions Source/Core/Core/AchievementManager.cpp
Expand Up @@ -290,9 +290,11 @@ void AchievementManager::DoFrame()
time_t current_time = std::time(nullptr);
if (difftime(current_time, m_last_ping_time) > 120)
{
RichPresence rp = GenerateRichPresence();
m_queue.EmplaceItem([this, rp] { PingRichPresence(rp); });
GenerateRichPresence();
m_queue.EmplaceItem([this] { PingRichPresence(m_rich_presence); });
m_last_ping_time = current_time;
if (m_update_callback)
m_update_callback();
}
}

Expand Down Expand Up @@ -411,6 +413,13 @@ void AchievementManager::GetAchievementProgress(AchievementId achievement_id, u3
rc_runtime_get_achievement_measured(&m_runtime, achievement_id, value, target);
}

RichPresence AchievementManager::GetRichPresence()
{
std::lock_guard lg{m_lock};
RichPresence rich_presence = m_rich_presence;
return rich_presence;
}

void AchievementManager::CloseGame()
{
{
Expand Down Expand Up @@ -585,18 +594,17 @@ void AchievementManager::ActivateDeactivateAchievement(AchievementId id, bool en
rc_runtime_deactivate_achievement(&m_runtime, id);
}

RichPresence AchievementManager::GenerateRichPresence()
void AchievementManager::GenerateRichPresence()
{
RichPresence rp_buffer;
Core::RunAsCPUThread([&] {
std::lock_guard lg{m_lock};
rc_runtime_get_richpresence(
&m_runtime, rp_buffer.data(), RP_SIZE,
&m_runtime, m_rich_presence.data(), RP_SIZE,
[](unsigned address, unsigned num_bytes, void* ud) {
return static_cast<AchievementManager*>(ud)->MemoryPeeker(address, num_bytes, ud);
},
this, nullptr);
});
return rp_buffer;
}

AchievementManager::ResponseType AchievementManager::AwardAchievement(AchievementId achievement_id)
Expand Down
4 changes: 3 additions & 1 deletion Source/Core/Core/AchievementManager.h
Expand Up @@ -92,6 +92,7 @@ class AchievementManager
rc_api_fetch_game_data_response_t* GetGameData();
UnlockStatus GetUnlockStatus(AchievementId achievement_id) const;
void GetAchievementProgress(AchievementId achievement_id, u32* value, u32* target);
RichPresence GetRichPresence();

void CloseGame();
void Logout();
Expand All @@ -109,7 +110,7 @@ class AchievementManager
ResponseType FetchUnlockData(bool hardcore);

void ActivateDeactivateAchievement(AchievementId id, bool enabled, bool unofficial, bool encore);
RichPresence GenerateRichPresence();
void GenerateRichPresence();

ResponseType AwardAchievement(AchievementId achievement_id);
ResponseType SubmitLeaderboard(AchievementId leaderboard_id, int value);
Expand All @@ -135,6 +136,7 @@ class AchievementManager
u32 m_game_id = 0;
rc_api_fetch_game_data_response_t m_game_data{};
bool m_is_game_loaded = false;
RichPresence m_rich_presence;
time_t m_last_ping_time = 0;

std::unordered_map<AchievementId, UnlockStatus> m_unlock_map;
Expand Down
Expand Up @@ -20,6 +20,7 @@
#include <rcheevos/include/rc_runtime.h>

#include "Core/AchievementManager.h"
#include "Core/Config/AchievementSettings.h"
#include "Core/Core.h"

#include "DolphinQt/QtUtils/ModalMessageBox.h"
Expand Down Expand Up @@ -101,9 +102,9 @@ void AchievementHeaderWidget::UpdateData()
m_game_progress_soft->setValue(point_spread.hard_unlocks);
m_game_progress_soft->setRange(0, point_spread.total_count);
m_game_progress_soft->setValue(point_spread.hard_unlocks + point_spread.soft_unlocks);
// TODO: RP needs a minor refactor to work here, will be a future PR
// m_rich_presence->setText(QString::fromStdString(AchievementManager::GetInstance()->GenerateRichPresence()));
// m_rich_presence->setVisible(Config::Get(Config::RA_RICH_PRESENCE_ENABLED));
m_rich_presence->setText(
QString::fromUtf8(AchievementManager::GetInstance()->GetRichPresence().data()));
m_rich_presence->setVisible(Config::Get(Config::RA_RICH_PRESENCE_ENABLED));
m_rich_presence->setText(QString{});
m_rich_presence->setVisible(false);

Expand Down

0 comments on commit 5890e6f

Please sign in to comment.