Add Discord RPC (#161)

* Add discord-rpc submodule

* Add Discord RPC

* Fix up Discord status

* Fix CMake because MacOS sucks

* Slightly less hacky fix
This commit is contained in:
wheremyfoodat 2023-08-08 00:23:39 +03:00 committed by GitHub
parent 32ad43cb8e
commit 1c11e2df40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 145 additions and 8 deletions

View file

@ -29,6 +29,15 @@ void EmulatorConfig::load(const std::filesystem::path& path) {
return;
}
if (data.contains("General")) {
auto generalResult = toml::expect<toml::value>(data.at("General"));
if (generalResult.is_ok()) {
auto general = generalResult.unwrap();
discordRpcEnabled = toml::find_or<toml::boolean>(general, "EnableDiscordRPC", false);
}
}
if (data.contains("GPU")) {
auto gpuResult = toml::expect<toml::value>(data.at("GPU"));
if (gpuResult.is_ok()) {
@ -68,6 +77,7 @@ void EmulatorConfig::save(const std::filesystem::path& path) {
printf("Saving new configuration file %s\n", path.string().c_str());
}
data["General"]["EnableDiscordRPC"] = discordRpcEnabled;
data["GPU"]["EnableShaderJIT"] = shaderJitEnabled;
data["GPU"]["Renderer"] = std::string(Renderer::typeToString(rendererType));

41
src/discord_rpc.cpp Normal file
View file

@ -0,0 +1,41 @@
#ifdef PANDA3DS_ENABLE_DISCORD_RPC
#include "discord_rpc.hpp"
#include <cstring>
#include <ctime>
void Discord::RPC::init() {
DiscordEventHandlers handlers{};
Discord_Initialize("1138176975865909360", &handlers, 1, nullptr);
startTimestamp = time(nullptr);
enabled = true;
}
void Discord::RPC::update(Discord::RPCStatus status, const std::string& game) {
DiscordRichPresence rpc{};
if (status == Discord::RPCStatus::Playing) {
rpc.details = "Playing a game";
rpc.state = game.c_str();
} else {
rpc.details = "Idle";
}
rpc.largeImageKey = "pand";
rpc.largeImageText = "Panda3DS is a 3DS emulator for Windows, MacOS and Linux";
rpc.startTimestamp = startTimestamp;
Discord_UpdatePresence(&rpc);
}
void Discord::RPC::stop() {
if (enabled) {
enabled = false;
Discord_ClearPresence();
Discord_Shutdown();
}
}
#endif

View file

@ -13,7 +13,7 @@ __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 1;
Emulator::Emulator()
: config(std::filesystem::current_path() / "config.toml"), kernel(cpu, memory, gpu), cpu(memory, kernel), gpu(memory, config),
memory(cpu.getTicksRef()), cheats(memory, kernel.getServiceManager().getHID())
memory(cpu.getTicksRef()), cheats(memory, kernel.getServiceManager().getHID()), running(false), programRunning(false)
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
, httpServer(this)
#endif
@ -34,6 +34,13 @@ Emulator::Emulator()
needOpenGL = needOpenGL || (config.rendererType == RendererType::OpenGL);
#endif
#ifdef PANDA3DS_ENABLE_DISCORD_RPC
if (config.discordRpcEnabled) {
discordRpc.init();
updateDiscord();
}
#endif
if (needOpenGL) {
// Demand 3.3 core for software renderer, or 4.1 core for OpenGL renderer (max available on MacOS)
// MacOS gets mad if we don't explicitly demand a core profile
@ -75,12 +82,16 @@ Emulator::Emulator()
}
}
running = false;
programRunning = false;
reset(ReloadOption::NoReload);
}
Emulator::~Emulator() { config.save(std::filesystem::current_path() / "config.toml"); }
Emulator::~Emulator() {
config.save(std::filesystem::current_path() / "config.toml");
#ifdef PANDA3DS_ENABLE_DISCORD_RPC
discordRpc.stop();
#endif
}
void Emulator::reset(ReloadOption reload) {
cpu.reset();
@ -121,6 +132,7 @@ void Emulator::run() {
#ifdef PANDA3DS_ENABLE_HTTP_SERVER
httpServer.processActions();
#endif
runFrame();
HIDService& hid = kernel.getServiceManager().getHID();
@ -431,6 +443,9 @@ bool Emulator::loadROM(const std::filesystem::path& path) {
if (success) {
romPath = path;
#ifdef PANDA3DS_ENABLE_DISCORD_RPC
updateDiscord();
#endif
} else {
romPath = std::nullopt;
romType = ROMType::None;
@ -487,3 +502,18 @@ bool Emulator::loadELF(std::ifstream& file) {
// Reset our graphics context and initialize the GPU's graphics context
void Emulator::initGraphicsContext() { gpu.initGraphicsContext(window); }
#ifdef PANDA3DS_ENABLE_DISCORD_RPC
void Emulator::updateDiscord() {
if (config.discordRpcEnabled) {
if (romType != ROMType::None) {
const auto name = romPath.value().stem();
discordRpc.update(Discord::RPCStatus::Playing, name.string());
} else {
discordRpc.update(Discord::RPCStatus::Idling, "");
}
}
}
#else
void Emulator::updateDiscord() {}
#endif