From 57c8ea12ed7cd8669abd101c4628066affecf4d6 Mon Sep 17 00:00:00 2001 From: LillyJadeKatrin Date: Sat, 24 Feb 2024 15:57:06 -0500 Subject: [PATCH] Send Rich Presence to Discord If Rich Presence and Discord Presence are enabled in Achievement Settings, the string generated by rcheevos as the player's current Rich Presence will be sent to the Status field in the Discord Presence object. This will be updated whenever Rich Presence updates. --- Source/Core/Core/AchievementManager.cpp | 3 +++ Source/Core/UICommon/DiscordPresence.cpp | 33 +++++++++++++++++++++--- Source/Core/UICommon/DiscordPresence.h | 3 ++- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/Source/Core/Core/AchievementManager.cpp b/Source/Core/Core/AchievementManager.cpp index c1b7e92c58..b1b63726b3 100644 --- a/Source/Core/Core/AchievementManager.cpp +++ b/Source/Core/Core/AchievementManager.cpp @@ -22,6 +22,7 @@ #include "Core/PowerPC/MMU.h" #include "Core/System.h" #include "DiscIO/Blob.h" +#include "UICommon/DiscordPresence.h" #include "VideoCommon/OnScreenDisplay.h" #include "VideoCommon/VideoEvents.h" @@ -227,6 +228,8 @@ void AchievementManager::DoFrame() m_last_rp_time = current_time; rc_client_get_rich_presence_message(m_client, m_rich_presence.data(), RP_SIZE); m_update_callback(UpdatedItems{.rich_presence = true}); + if (Config::Get(Config::RA_DISCORD_PRESENCE_ENABLED)) + Discord::UpdateDiscordPresence(); } } diff --git a/Source/Core/UICommon/DiscordPresence.cpp b/Source/Core/UICommon/DiscordPresence.cpp index eadc229054..67870999aa 100644 --- a/Source/Core/UICommon/DiscordPresence.cpp +++ b/Source/Core/UICommon/DiscordPresence.cpp @@ -21,6 +21,8 @@ #include "Common/HttpRequest.h" #include "Common/StringUtil.h" +#include "Core/AchievementManager.h" +#include "Core/Config/AchievementSettings.h" #include "Core/System.h" #endif @@ -34,6 +36,9 @@ namespace { Handler* event_handler = nullptr; const char* username = ""; +static int64_t s_start_timestamp = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count(); void HandleDiscordReady(const DiscordUser* user) { @@ -195,7 +200,7 @@ bool UpdateDiscordPresenceRaw(const std::string& details, const std::string& sta } void UpdateDiscordPresence(int party_size, SecretType type, const std::string& secret, - const std::string& current_game) + const std::string& current_game, bool reset_timer) { #ifdef USE_DISCORD_PRESENCE if (!Config::Get(Config::MAIN_USE_DISCORD_PRESENCE)) @@ -224,10 +229,17 @@ void UpdateDiscordPresence(int party_size, SecretType type, const std::string& s discord_presence.smallImageText = "Dolphin is an emulator for the GameCube and the Wii."; } discord_presence.details = title.empty() ? "Not in-game" : title.c_str(); - discord_presence.startTimestamp = std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()) - .count(); + if (reset_timer) + { + s_start_timestamp = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count(); + } + discord_presence.startTimestamp = s_start_timestamp; +#ifdef USE_RETRO_ACHIEVEMENTS + std::string state_string; +#endif // USE_RETRO_ACHIEVEMENTS if (party_size > 0) { if (party_size < 4) @@ -244,6 +256,19 @@ void UpdateDiscordPresence(int party_size, SecretType type, const std::string& s // Note: joining still works without partyMax } } +#ifdef USE_RETRO_ACHIEVEMENTS + else if (Config::Get(Config::RA_ENABLED) && Config::Get(Config::RA_DISCORD_PRESENCE_ENABLED)) + { + state_string = AchievementManager::GetInstance().GetRichPresence().data(); + if (state_string.length() >= 128) + { + // 124 characters + 3 dots + null terminator - thanks to Stenzek for format + state_string.resize(124); + state_string += "..."; + } + discord_presence.state = state_string.c_str(); + } +#endif // USE_RETRO_ACHIEVEMENTS std::string party_id; std::string secret_final; diff --git a/Source/Core/UICommon/DiscordPresence.h b/Source/Core/UICommon/DiscordPresence.h index 67da5fd11e..4e628337d8 100644 --- a/Source/Core/UICommon/DiscordPresence.h +++ b/Source/Core/UICommon/DiscordPresence.h @@ -39,7 +39,8 @@ bool UpdateDiscordPresenceRaw(const std::string& details = {}, const std::string const int64_t start_timestamp = 0, const int64_t end_timestamp = 0, const int party_size = 0, const int party_max = 0); void UpdateDiscordPresence(int party_size = 0, SecretType type = SecretType::Empty, - const std::string& secret = {}, const std::string& current_game = {}); + const std::string& secret = {}, const std::string& current_game = {}, + const bool reset_timer = false); std::string CreateSecretFromIPAddress(const std::string& ip_address, int port); void Shutdown(); void SetDiscordPresenceEnabled(bool enabled);