2019-09-30 07:56:57 +00:00
|
|
|
|
2018-08-12 22:08:56 +00:00
|
|
|
#include <ctime>
|
2022-01-30 23:49:02 +00:00
|
|
|
#include <string>
|
2018-08-12 22:08:56 +00:00
|
|
|
|
|
|
|
#include "ppsspp_config.h"
|
|
|
|
#include "Common/Log.h"
|
|
|
|
#include "Core/Config.h"
|
|
|
|
#include "DiscordIntegration.h"
|
2020-10-01 11:05:04 +00:00
|
|
|
#include "Common/Data/Text/I18n.h"
|
2018-08-12 22:08:56 +00:00
|
|
|
|
2018-12-10 13:55:07 +00:00
|
|
|
#if (PPSSPP_PLATFORM(WINDOWS) || PPSSPP_PLATFORM(MAC) || PPSSPP_PLATFORM(LINUX)) && !PPSSPP_PLATFORM(ANDROID) && !PPSSPP_PLATFORM(UWP)
|
2018-08-12 22:08:56 +00:00
|
|
|
|
2018-11-10 15:39:27 +00:00
|
|
|
#ifdef _MSC_VER
|
2018-08-12 22:25:08 +00:00
|
|
|
#define ENABLE_DISCORD
|
2018-11-10 15:39:27 +00:00
|
|
|
#elif USE_DISCORD
|
|
|
|
#define ENABLE_DISCORD
|
|
|
|
#endif
|
2018-08-12 22:08:56 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2018-08-13 18:14:34 +00:00
|
|
|
// TODO
|
2018-08-12 22:25:08 +00:00
|
|
|
|
2018-08-13 18:14:34 +00:00
|
|
|
#endif
|
2018-08-12 22:25:08 +00:00
|
|
|
|
|
|
|
#ifdef ENABLE_DISCORD
|
|
|
|
#include "ext/discord-rpc/include/discord_rpc.h"
|
2018-08-12 22:08:56 +00:00
|
|
|
#endif
|
|
|
|
|
2018-08-12 22:25:08 +00:00
|
|
|
// TODO: Enable on more platforms. Make optional.
|
2018-08-12 22:08:56 +00:00
|
|
|
|
|
|
|
Discord g_Discord;
|
|
|
|
|
|
|
|
static const char *ppsspp_app_id = "423397985041383434";
|
|
|
|
|
2019-06-22 20:01:47 +00:00
|
|
|
#ifdef ENABLE_DISCORD
|
2018-08-12 22:08:56 +00:00
|
|
|
// No context argument? What?
|
|
|
|
static void handleDiscordError(int errCode, const char *message) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::System, "Discord error code %d: '%s'", errCode, message);
|
2018-08-12 22:08:56 +00:00
|
|
|
}
|
2019-06-22 20:01:47 +00:00
|
|
|
#endif
|
2018-08-12 22:08:56 +00:00
|
|
|
|
|
|
|
Discord::~Discord() {
|
2019-09-30 07:56:57 +00:00
|
|
|
if (initialized_) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::System, "Discord destructor running though g_Discord.Shutdown() has not been called.");
|
2019-09-30 07:56:57 +00:00
|
|
|
}
|
2018-08-12 22:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Discord::IsEnabled() const {
|
2018-08-12 22:25:08 +00:00
|
|
|
return g_Config.bDiscordPresence;
|
2018-08-12 22:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Discord::Init() {
|
2020-08-15 22:38:55 +00:00
|
|
|
_assert_(IsEnabled());
|
|
|
|
_assert_(!initialized_);
|
2018-08-12 22:08:56 +00:00
|
|
|
|
2018-08-12 22:25:08 +00:00
|
|
|
#ifdef ENABLE_DISCORD
|
2018-08-12 22:08:56 +00:00
|
|
|
DiscordEventHandlers eventHandlers{};
|
|
|
|
eventHandlers.errored = &handleDiscordError;
|
|
|
|
Discord_Initialize(ppsspp_app_id, &eventHandlers, 0, nullptr);
|
2024-07-14 12:42:59 +00:00
|
|
|
INFO_LOG(Log::System, "Discord connection initialized");
|
2018-08-12 22:25:08 +00:00
|
|
|
#endif
|
2018-08-12 22:08:56 +00:00
|
|
|
|
|
|
|
initialized_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Discord::Shutdown() {
|
2019-11-18 20:46:51 +00:00
|
|
|
if (initialized_) {
|
2018-08-12 22:25:08 +00:00
|
|
|
#ifdef ENABLE_DISCORD
|
2019-11-18 20:46:51 +00:00
|
|
|
Discord_Shutdown();
|
2018-08-12 22:25:08 +00:00
|
|
|
#endif
|
2019-11-18 20:46:51 +00:00
|
|
|
initialized_ = false;
|
|
|
|
}
|
2018-08-12 22:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Discord::Update() {
|
|
|
|
if (!IsEnabled()) {
|
|
|
|
if (initialized_) {
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (!initialized_) {
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-12 22:25:08 +00:00
|
|
|
#ifdef ENABLE_DISCORD
|
2018-08-12 22:08:56 +00:00
|
|
|
#ifdef DISCORD_DISABLE_IO_THREAD
|
|
|
|
Discord_UpdateConnection();
|
|
|
|
#endif
|
|
|
|
Discord_RunCallbacks();
|
2018-08-12 22:25:08 +00:00
|
|
|
#endif
|
2018-08-12 22:08:56 +00:00
|
|
|
}
|
|
|
|
|
2024-01-19 12:44:49 +00:00
|
|
|
void Discord::SetPresenceGame(std::string_view gameTitle) {
|
2018-08-12 22:08:56 +00:00
|
|
|
if (!IsEnabled())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!initialized_) {
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
2018-08-12 22:25:08 +00:00
|
|
|
#ifdef ENABLE_DISCORD
|
2023-04-05 22:34:50 +00:00
|
|
|
auto sc = GetI18NCategory(I18NCat::SCREEN);
|
2024-01-19 12:44:49 +00:00
|
|
|
std::string title(gameTitle);
|
2018-08-12 22:08:56 +00:00
|
|
|
DiscordRichPresence discordPresence{};
|
2024-01-19 12:44:49 +00:00
|
|
|
discordPresence.state = title.c_str();
|
|
|
|
discordPresence.details = sc->T_cstr("Playing");
|
2018-08-12 22:08:56 +00:00
|
|
|
discordPresence.startTimestamp = time(0);
|
2018-12-09 13:29:58 +00:00
|
|
|
discordPresence.largeImageText = "PPSSPP is the best PlayStation Portable emulator around!";
|
2018-08-12 22:08:56 +00:00
|
|
|
#ifdef GOLD
|
|
|
|
discordPresence.largeImageKey = "icon_gold_png";
|
|
|
|
#else
|
|
|
|
discordPresence.largeImageKey = "icon_regular_png";
|
|
|
|
#endif
|
|
|
|
Discord_UpdatePresence(&discordPresence);
|
2018-08-12 22:25:08 +00:00
|
|
|
#endif
|
2018-08-12 22:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Discord::SetPresenceMenu() {
|
|
|
|
if (!IsEnabled())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!initialized_) {
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
2018-08-12 22:25:08 +00:00
|
|
|
#ifdef ENABLE_DISCORD
|
2023-04-05 22:34:50 +00:00
|
|
|
auto sc = GetI18NCategory(I18NCat::SCREEN);
|
2018-08-13 18:14:34 +00:00
|
|
|
|
2018-08-12 22:08:56 +00:00
|
|
|
DiscordRichPresence discordPresence{};
|
2024-01-19 12:44:49 +00:00
|
|
|
discordPresence.state = sc->T_cstr("In menu");
|
2018-08-12 22:08:56 +00:00
|
|
|
discordPresence.details = "";
|
|
|
|
discordPresence.startTimestamp = time(0);
|
2018-12-09 13:29:58 +00:00
|
|
|
discordPresence.largeImageText = "PPSSPP is the best PlayStation Portable emulator around!";
|
2018-08-12 22:08:56 +00:00
|
|
|
#ifdef GOLD
|
|
|
|
discordPresence.largeImageKey = "icon_gold_png";
|
|
|
|
#else
|
|
|
|
discordPresence.largeImageKey = "icon_regular_png";
|
|
|
|
#endif
|
|
|
|
Discord_UpdatePresence(&discordPresence);
|
2018-08-12 22:25:08 +00:00
|
|
|
#endif
|
2018-08-12 22:08:56 +00:00
|
|
|
}
|
2018-11-10 15:48:04 +00:00
|
|
|
|
|
|
|
void Discord::ClearPresence() {
|
|
|
|
if (!IsEnabled() || !initialized_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
#ifdef ENABLE_DISCORD
|
|
|
|
Discord_ClearPresence();
|
|
|
|
#endif
|
|
|
|
}
|