Skip to content

Commit

Permalink
DiscordRPC: Add game icon support.
Browse files Browse the repository at this point in the history
  • Loading branch information
kamfretoz authored and F0bes committed Aug 21, 2024
1 parent 31b5672 commit ffe8d16
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
17 changes: 17 additions & 0 deletions pcsx2/Achievements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ namespace Achievements
static std::string s_game_hash;
static std::string s_game_title;
static std::string s_game_icon;
static std::string s_game_icon_url;
static u32 s_game_crc;
static rc_client_user_game_summary_t s_game_summary;
static u32 s_game_id = 0;
Expand Down Expand Up @@ -401,6 +402,10 @@ const std::string& Achievements::GetRichPresenceString()
return s_rich_presence_string;
}

const std::string& Achievements::GetGameIconURL()
{
return s_game_icon_url;
}

bool Achievements::Initialize()
{
Expand Down Expand Up @@ -945,6 +950,7 @@ void Achievements::ClientLoadGameCallback(int result, const char* error_message,
s_has_leaderboards = has_leaderboards;
s_has_rich_presence = rc_client_has_rich_presence(client);
s_game_icon = {};
s_game_icon_url = {};

// ensure fullscreen UI is ready for notifications
MTGS::RunOnGSThread(&ImGuiManager::InitializeFullscreenUI);
Expand All @@ -966,6 +972,16 @@ void Achievements::ClientLoadGameCallback(int result, const char* error_message,
}
}

char icon_url[64];
if (int err = rc_client_game_get_image_url(info, icon_url, std::size(icon_url)); err == RC_OK)
{
s_game_icon_url = icon_url;
}
else
{
ReportRCError(err, "rc_client_game_get_image_url() failed: ");
}

UpdateGameSummary();
DisplayAchievementSummary();

Expand All @@ -990,6 +1006,7 @@ void Achievements::ClearGameInfo()
s_game_id = 0;
s_game_title = {};
s_game_icon = {};
s_game_icon_url = {};
s_has_achievements = false;
s_has_leaderboards = false;
s_has_rich_presence = false;
Expand Down
4 changes: 4 additions & 0 deletions pcsx2/Achievements.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ namespace Achievements
/// Should be called with the lock held.
const std::string& GetRichPresenceString();

/// Returns the current game icon url.
/// Should be called with the lock held.
const std::string& GetGameIconURL();

/// Returns the RetroAchievements title for the current game.
/// Should be called with the lock held.
const std::string& GetGameTitle();
Expand Down
8 changes: 7 additions & 1 deletion pcsx2/VMManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3608,15 +3608,21 @@ void VMManager::UpdateDiscordPresence(bool update_session_time)
// https://discord.com/developers/docs/rich-presence/how-to#updating-presence-update-presence-payload-fields
DiscordRichPresence rp = {};
rp.largeImageKey = "4k-pcsx2";
rp.largeImageText = "PCSX2 Emulator";
rp.largeImageText = "PCSX2 PS2 Emulator";
rp.startTimestamp = s_discord_presence_time_epoch;
rp.details = s_title.empty() ? TRANSLATE("VMManager","No Game Running") : s_title.c_str();

std::string state_string;

if (Achievements::HasRichPresence())
{
auto lock = Achievements::GetLock();

state_string = StringUtil::Ellipsise(Achievements::GetRichPresenceString(), 128);
rp.state = state_string.c_str();

rp.largeImageKey = Achievements::GetGameIconURL().c_str();
rp.largeImageText = s_title.c_str();
}

Discord_UpdatePresence(&rp);
Expand Down

0 comments on commit ffe8d16

Please sign in to comment.