project64/Source/Project64/UserInterface/DiscordRPC.cpp

94 lines
3.7 KiB
C++
Raw Normal View History

2019-10-23 21:12:12 +00:00
/****************************************************************************
* *
* Project64 - A Nintendo 64 emulator. *
* http://www.pj64-emu.com/ *
* Copyright (C) 2012 Project64. All rights reserved. *
* *
* License: *
* GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html *
* *
****************************************************************************/
#include "stdafx.h"
#include <3rdParty\discord-rpc\include\discord_rpc.h>
#include "DiscordRPC.h"
#define UNIX_TIME_START 0x019DB1DED53E8000
#define TICKS_PER_SECOND 10000000
//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()
{
DiscordEventHandlers handlers = {};
Discord_Initialize(PJ64_DISCORD_APPID, &handlers, 1, NULL);
}
void CDiscord::Shutdown()
{
Discord_Shutdown();
}
void CDiscord::Update(bool bHaveGame)
{
2019-12-22 21:23:55 +00:00
//Variables we use later
//szState uses the Rdb_GoodName to display a proper game name over DiscordRPC
//keyState uses the Header of the rom to easily add game pictures through the discord developer panel using the ID above
char szState[256];
char keyState[256];
2019-12-22 23:51:22 +00:00
sprintf(szState, "%s", g_Settings->LoadStringVal(Rdb_GoodName).c_str()); //rdb_GoodName in a variable for use later
sprintf(keyState, "%s", g_Settings->LoadStringVal(Rdb_RPCKey).c_str()); //Game Image Key in a variable for use later
2019-10-23 21:12:12 +00:00
2019-12-22 21:23:55 +00:00
//Load Game Into DiscordRPC
DiscordRichPresence discordPresence = {}; //activates DiscordRPC
if (bHaveGame && g_Settings->LoadStringVal(Game_File).length() != 0)
2019-10-23 21:12:12 +00:00
{
2019-12-22 21:23:55 +00:00
//DiscordRPC Game Name - Playing <game>
discordPresence.state = szState; //sets the state of the DiscordRPC to the Rdb_GoodName file
2019-10-23 21:12:12 +00:00
2019-12-22 21:23:55 +00:00
//Play Time over DiscordRPC
discordPresence.startTimestamp = Timestamp(); //sets the time on the DiscordRPC
2019-10-23 21:12:12 +00:00
2019-12-22 21:23:55 +00:00
//Large Image Text over DiscordRPC
discordPresence.largeImageText = szState; //sets the RDB_GoodName Variable as the large image text
2019-10-23 21:12:12 +00:00
2019-12-22 21:23:55 +00:00
//Large Image File Name over DiscordRPC
2019-12-22 23:51:22 +00:00
discordPresence.largeImageKey = keyState; //sets the Rom Header Variable as the large image key (the file you upload to discord)
2019-10-23 21:12:12 +00:00
2019-12-22 21:23:55 +00:00
//Small Image over DiscordRPC
discordPresence.smallImageKey = "icon"; //Project 64 Logo in bottom right corner
discordPresence.smallImageText = "Project64"; //Name of the Project64 Logo
discordPresence.instance = 1; //Instance of Active DiscordRPC
2019-10-23 21:12:12 +00:00
}
2019-12-22 21:23:55 +00:00
else
2019-10-23 21:12:12 +00:00
{
2019-12-22 21:23:55 +00:00
//Show when you are not playing a game over DiscordRPC.
// This is not perfect due to Project64's method of loading
// ROM's into the filesystem before emulation starts.
2019-12-22 23:51:22 +00:00
discordPresence.details = "Not in-game"; //shows "Not in-game" on the active DiscordRPC text
2019-12-22 21:23:55 +00:00
discordPresence.largeImageKey = "icon"; //Shows the Project64 logo on the large image box
discordPresence.largeImageText = "Project64"; //Name of the Project64 Logo
discordPresence.smallImageKey = NULL; //Safety Measure to force unload the smallImageKey
discordPresence.smallImageText = NULL; //Safety Measure to force unload the smallImageText
discordPresence.endTimestamp = NULL; //Safety Measure to force unload the TimeStamp
2019-10-23 21:12:12 +00:00
}
2019-12-22 21:23:55 +00:00
Discord_UpdatePresence(&discordPresence); //end DiscordRPC
2019-10-23 21:12:12 +00:00
}
int64_t CDiscord::Timestamp()
{
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
LARGE_INTEGER li;
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
return (li.QuadPart - UNIX_TIME_START) / TICKS_PER_SECOND;
}