Handle Achievement Progress Client Events

This is not a 1 to 1 relationship with how the events look primarily because currently achievement
progress messages are in OnScreenDisplay, which currently vanishes messages automatically.
As this covers the last remaining runtime-based event from the old event handler, that handler has been deleted and the new event handler has been renamed to take its place.
This commit is contained in:
LillyJadeKatrin 2024-04-01 14:45:17 -04:00
parent a70733f74f
commit 6b5b7cbd7c
2 changed files with 25 additions and 44 deletions

View File

@ -40,7 +40,7 @@ void AchievementManager::Init()
std::string host_url = Config::Get(Config::RA_HOST_URL);
if (!host_url.empty())
rc_client_set_host(m_client, host_url.c_str());
rc_client_set_event_handler(m_client, EventHandlerV2);
rc_client_set_event_handler(m_client, EventHandler);
rc_client_enable_logging(m_client, RC_CLIENT_LOG_LEVEL_VERBOSE,
[](const char* message, const rc_client_t* client) {
INFO_LOG_FMT(ACHIEVEMENTS, "{}", message);
@ -230,16 +230,6 @@ void AchievementManager::DoFrame()
}
}
void AchievementManager::AchievementEventHandler(const rc_runtime_event_t* runtime_event)
{
switch (runtime_event->type)
{
case RC_RUNTIME_EVENT_ACHIEVEMENT_PROGRESS_UPDATED:
HandleAchievementProgressUpdatedEvent(runtime_event);
break;
}
}
std::recursive_mutex& AchievementManager::GetLock()
{
return m_lock;
@ -740,34 +730,6 @@ void AchievementManager::DisplayWelcomeMessage()
OSD::Duration::VERY_LONG, color);
}
void AchievementManager::HandleAchievementProgressUpdatedEvent(
const rc_runtime_event_t* runtime_event)
{
if (!Config::Get(Config::RA_PROGRESS_ENABLED))
return;
auto it = m_unlock_map.find(runtime_event->id);
if (it == m_unlock_map.end())
{
ERROR_LOG_FMT(ACHIEVEMENTS, "Invalid achievement progress updated event with id {}.",
runtime_event->id);
return;
}
AchievementId game_data_index = it->second.game_data_index;
FormattedValue value{};
if (rc_runtime_format_achievement_measured(&m_runtime, runtime_event->id, value.data(),
FORMAT_SIZE) == 0)
{
ERROR_LOG_FMT(ACHIEVEMENTS, "Failed to format measured data {}.", value.data());
return;
}
OSD::AddMessage(
fmt::format("{} {}", m_game_data.achievements[game_data_index].title, value.data()),
OSD::Duration::SHORT, OSD::Color::GREEN,
(Config::Get(Config::RA_BADGES_ENABLED)) ?
DecodeBadgeToOSDIcon(it->second.unlocked_badge.badge) :
nullptr);
}
void AchievementManager::HandleAchievementTriggeredEvent(const rc_client_event_t* client_event)
{
OSD::AddMessage(fmt::format("Unlocked: {} ({})", client_event->achievement->title,
@ -853,6 +815,19 @@ void AchievementManager::HandleAchievementChallengeIndicatorHideEvent(
client_event->achievement->badge_name);
}
void AchievementManager::HandleAchievementProgressIndicatorShowEvent(
const rc_client_event_t* client_event)
{
OSD::AddMessage(fmt::format("{} {}", client_event->achievement->title,
client_event->achievement->measured_progress),
OSD::Duration::SHORT, OSD::Color::GREEN,
(Config::Get(Config::RA_BADGES_ENABLED)) ?
DecodeBadgeToOSDIcon(AchievementManager::GetInstance()
.m_unlocked_badges[client_event->achievement->id]
.badge) :
nullptr);
}
// Every RetroAchievements API call, with only a partial exception for fetch_image, follows
// the same design pattern (here, X is the name of the call):
// Create a specific rc_api_X_request_t struct and populate with the necessary values
@ -1033,7 +1008,7 @@ void AchievementManager::FetchBadge(AchievementManager::BadgeStatus* badge, u32
});
}
void AchievementManager::EventHandlerV2(const rc_client_event_t* event, rc_client_t* client)
void AchievementManager::EventHandler(const rc_client_event_t* event, rc_client_t* client)
{
switch (event->type)
{
@ -1064,6 +1039,14 @@ void AchievementManager::EventHandlerV2(const rc_client_event_t* event, rc_clien
case RC_CLIENT_EVENT_ACHIEVEMENT_CHALLENGE_INDICATOR_HIDE:
HandleAchievementChallengeIndicatorHideEvent(event);
break;
case RC_CLIENT_EVENT_ACHIEVEMENT_PROGRESS_INDICATOR_SHOW:
case RC_CLIENT_EVENT_ACHIEVEMENT_PROGRESS_INDICATOR_UPDATE:
HandleAchievementProgressIndicatorShowEvent(event);
break;
case RC_CLIENT_EVENT_ACHIEVEMENT_PROGRESS_INDICATOR_HIDE:
// OnScreenDisplay messages disappear over time, so this is unnecessary
// unless the display algorithm changes in the future.
break;
default:
INFO_LOG_FMT(ACHIEVEMENTS, "Event triggered of unhandled type {}", event->type);
break;

View File

@ -128,7 +128,6 @@ public:
void FetchGameBadges();
void DoFrame();
void AchievementEventHandler(const rc_runtime_event_t* runtime_event);
std::recursive_mutex& GetLock();
bool IsHardcoreModeActive() const;
@ -184,8 +183,6 @@ private:
void* userdata);
void DisplayWelcomeMessage();
void HandleAchievementProgressUpdatedEvent(const rc_runtime_event_t* runtime_event);
static void HandleAchievementTriggeredEvent(const rc_client_event_t* client_event);
static void HandleLeaderboardStartedEvent(const rc_client_event_t* client_event);
static void HandleLeaderboardFailedEvent(const rc_client_event_t* client_event);
@ -195,6 +192,7 @@ private:
static void HandleLeaderboardTrackerHideEvent(const rc_client_event_t* client_event);
static void HandleAchievementChallengeIndicatorShowEvent(const rc_client_event_t* client_event);
static void HandleAchievementChallengeIndicatorHideEvent(const rc_client_event_t* client_event);
static void HandleAchievementProgressIndicatorShowEvent(const rc_client_event_t* client_event);
template <typename RcRequest, typename RcResponse>
ResponseType Request(RcRequest rc_request, RcResponse* rc_response,
@ -205,7 +203,7 @@ private:
void* callback_data, rc_client_t* client);
static u32 MemoryPeeker(u32 address, u8* buffer, u32 num_bytes, rc_client_t* client);
void FetchBadge(BadgeStatus* badge, u32 badge_type, const BadgeNameFunction function);
static void EventHandlerV2(const rc_client_event_t* event, rc_client_t* client);
static void EventHandler(const rc_client_event_t* event, rc_client_t* client);
rc_runtime_t m_runtime{};
rc_client_t* m_client{};