From 4ecee3cb9900f7f1fc34b806965cc399f43d7f28 Mon Sep 17 00:00:00 2001 From: Pierre Bourdon Date: Sun, 19 Aug 2018 14:43:39 +0200 Subject: [PATCH] DiscordPresence: use game-specific artwork if available Since we don't have a way (AFAIK) to dynamically collect the list of available art assets, we hardcode a list of gameids with available artwork inside Dolphin. It's not great, but I don't think it's a terrible solution either. Art has to be manually uploaded to our Discord app configuration, and we have a limit of ~150 assets, so most likely we'll limit ourselves to a small set of popular games. --- Source/Core/UICommon/DiscordPresence.cpp | 49 ++++++++++++++++++++---- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/Source/Core/UICommon/DiscordPresence.cpp b/Source/Core/UICommon/DiscordPresence.cpp index 5fb7ee676b..b39d372fa2 100644 --- a/Source/Core/UICommon/DiscordPresence.cpp +++ b/Source/Core/UICommon/DiscordPresence.cpp @@ -13,23 +13,28 @@ #ifdef USE_DISCORD_PRESENCE +#include +#include #include #include +#include #endif namespace Discord { #ifdef USE_DISCORD_PRESENCE -static Handler* event_handler = nullptr; -static const char* username = ""; +namespace +{ +Handler* event_handler = nullptr; +const char* username = ""; -static void HandleDiscordReady(const DiscordUser* user) +void HandleDiscordReady(const DiscordUser* user) { username = user->username; } -static void HandleDiscordJoinRequest(const DiscordUser* user) +void HandleDiscordJoinRequest(const DiscordUser* user) { if (event_handler == nullptr) return; @@ -38,7 +43,7 @@ static void HandleDiscordJoinRequest(const DiscordUser* user) event_handler->DiscordJoinRequest(user->userId, discord_tag, user->avatar); } -static void HandleDiscordJoin(const char* join_secret) +void HandleDiscordJoin(const char* join_secret) { if (event_handler == nullptr) return; @@ -82,6 +87,25 @@ static void HandleDiscordJoin(const char* join_secret) event_handler->DiscordJoin(); } + +std::string ArtworkForGameId(const std::string& gameid) +{ + static const std::set REGISTERED_GAMES{ + "GAL", // Super Smash Bros. Melee + }; + + std::string region_neutral_gameid = gameid.substr(0, 3); + if (REGISTERED_GAMES.count(region_neutral_gameid) != 0) + { + // Discord asset keys can only be lowercase. + std::transform(region_neutral_gameid.begin(), region_neutral_gameid.end(), + region_neutral_gameid.begin(), tolower); + return "game_" + region_neutral_gameid; + } + return ""; +} + +} // namespace #endif Discord::Handler::~Handler() = default; @@ -130,10 +154,21 @@ void UpdateDiscordPresence(int party_size, SecretType type, const std::string& s const std::string& title = current_game.empty() ? SConfig::GetInstance().GetTitleDescription() : current_game; + std::string game_artwork = ArtworkForGameId(SConfig::GetInstance().GetGameID()); DiscordRichPresence discord_presence = {}; - discord_presence.largeImageKey = "dolphin_logo"; - discord_presence.largeImageText = "Dolphin is an emulator for the GameCube and the Wii."; + if (game_artwork.empty()) + { + discord_presence.largeImageKey = "dolphin_logo"; + discord_presence.largeImageText = "Dolphin is an emulator for the GameCube and the Wii."; + } + else + { + discord_presence.largeImageKey = game_artwork.c_str(); + discord_presence.largeImageText = title.c_str(); + discord_presence.smallImageKey = "dolphin_logo"; + 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::time(nullptr);