project64/Source/Project64/UserInterface/DiscordRPC.cpp

81 lines
2.2 KiB
C++
Raw Normal View History

2019-10-23 21:12:12 +00:00
#include "stdafx.h"
2022-09-21 05:16:07 +00:00
2019-10-23 21:12:12 +00:00
#include "DiscordRPC.h"
2022-09-21 05:16:07 +00:00
#include <3rdParty\discord-rpc\include\discord_rpc.h>
2019-10-23 21:12:12 +00:00
2022-09-21 05:16:07 +00:00
#define UNIX_TIME_START 0x019DB1DED53E8000
#define TICKS_PER_SECOND 10000000
2019-10-23 21:12:12 +00:00
// Discord Project64 app ID
2020-04-28 20:45:54 +00:00
#define PJ64_DISCORD_APPID "704794684387491891"
2019-10-23 21:12:12 +00:00
void CDiscord::Init()
{
2022-09-21 05:16:07 +00:00
DiscordEventHandlers handlers = {};
2019-10-23 21:12:12 +00:00
2022-09-21 05:16:07 +00:00
Discord_Initialize(PJ64_DISCORD_APPID, &handlers, 1, nullptr);
2019-10-23 21:12:12 +00:00
}
void CDiscord::Shutdown()
{
2022-09-21 05:16:07 +00:00
Discord_ClearPresence();
Discord_Shutdown();
2019-10-23 21:12:12 +00:00
}
2022-09-21 05:16:07 +00:00
static stdstr GetTitle()
{
2022-09-21 05:16:07 +00:00
stdstr Default = "";
bool existsInRdb = g_Settings->LoadStringVal(Rdb_GoodName, Default);
if (existsInRdb)
return g_Settings->LoadStringVal(Rdb_GoodName);
else
{
Default = CPath(g_Settings->LoadStringVal(Game_File)).GetName().c_str();
if (strstr(const_cast<char *>(Default.c_str()), "?") != nullptr)
{
return Default.substr(Default.find("?") + 1);
}
return Default;
}
}
2019-10-23 21:12:12 +00:00
void CDiscord::Update(bool bHaveGame)
{
2022-09-21 05:16:07 +00:00
// Variables we use later
// Title uses the Rdb_GoodName to display a proper game name over DiscordRPC
// Artwork uses the header of the ROM to easily add game images through the Discord developer panel using the ID above
stdstr title = bHaveGame ? GetTitle() : "";
stdstr artwork = bHaveGame ? g_Settings->LoadStringVal(Rdb_RPCKey) : "";
2019-10-23 21:12:12 +00:00
2022-09-21 05:16:07 +00:00
// Load game into DiscordRPC
DiscordRichPresence discordPresence = {}; // Activates DiscordRPC
if (artwork.empty())
{
discordPresence.largeImageKey = "pj64_icon";
discordPresence.largeImageText = "Project64";
}
else
{
discordPresence.largeImageKey = artwork.c_str();
discordPresence.largeImageText = title.c_str();
discordPresence.smallImageKey = "pj64_icon";
discordPresence.smallImageText = "Project64";
}
discordPresence.details = title.empty() ? "Not in-game" : title.c_str();
discordPresence.startTimestamp = Timestamp();
2019-10-23 21:12:12 +00:00
2022-09-21 05:16:07 +00:00
Discord_UpdatePresence(&discordPresence); // End DiscordRPC
2019-10-23 21:12:12 +00:00
}
int64_t CDiscord::Timestamp()
{
2022-09-21 05:16:07 +00:00
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
2019-10-23 21:12:12 +00:00
2022-09-21 05:16:07 +00:00
LARGE_INTEGER li;
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
2019-10-23 21:12:12 +00:00
2022-09-21 05:16:07 +00:00
return (li.QuadPart - UNIX_TIME_START) / TICKS_PER_SECOND;
}