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.
This commit is contained in:
Pierre Bourdon 2018-08-19 14:43:39 +02:00
parent 0fdb6f4267
commit 4ecee3cb99

View file

@ -13,23 +13,28 @@
#ifdef USE_DISCORD_PRESENCE
#include <algorithm>
#include <cctype>
#include <ctime>
#include <discord-rpc/include/discord_rpc.h>
#include <string>
#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<std::string> 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);