CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/UI/DiscordIntegration.cpp
Views: 1401
1
2
#include <ctime>
3
#include <string>
4
5
#include "ppsspp_config.h"
6
#include "Common/Log.h"
7
#include "Core/Config.h"
8
#include "DiscordIntegration.h"
9
#include "Common/Data/Text/I18n.h"
10
11
#if (PPSSPP_PLATFORM(WINDOWS) || PPSSPP_PLATFORM(MAC) || PPSSPP_PLATFORM(LINUX)) && !PPSSPP_PLATFORM(ANDROID) && !PPSSPP_PLATFORM(UWP)
12
13
#ifdef _MSC_VER
14
#define ENABLE_DISCORD
15
#elif USE_DISCORD
16
#define ENABLE_DISCORD
17
#endif
18
19
#else
20
21
// TODO
22
23
#endif
24
25
#ifdef ENABLE_DISCORD
26
#include "ext/discord-rpc/include/discord_rpc.h"
27
#endif
28
29
// TODO: Enable on more platforms. Make optional.
30
31
Discord g_Discord;
32
33
static const char *ppsspp_app_id = "423397985041383434";
34
35
#ifdef ENABLE_DISCORD
36
// No context argument? What?
37
static void handleDiscordError(int errCode, const char *message) {
38
ERROR_LOG(Log::System, "Discord error code %d: '%s'", errCode, message);
39
}
40
#endif
41
42
Discord::~Discord() {
43
if (initialized_) {
44
ERROR_LOG(Log::System, "Discord destructor running though g_Discord.Shutdown() has not been called.");
45
}
46
}
47
48
bool Discord::IsEnabled() const {
49
return g_Config.bDiscordPresence;
50
}
51
52
void Discord::Init() {
53
_assert_(IsEnabled());
54
_assert_(!initialized_);
55
56
#ifdef ENABLE_DISCORD
57
DiscordEventHandlers eventHandlers{};
58
eventHandlers.errored = &handleDiscordError;
59
Discord_Initialize(ppsspp_app_id, &eventHandlers, 0, nullptr);
60
INFO_LOG(Log::System, "Discord connection initialized");
61
#endif
62
63
initialized_ = true;
64
}
65
66
void Discord::Shutdown() {
67
if (initialized_) {
68
#ifdef ENABLE_DISCORD
69
Discord_Shutdown();
70
#endif
71
initialized_ = false;
72
}
73
}
74
75
void Discord::Update() {
76
if (!IsEnabled()) {
77
if (initialized_) {
78
Shutdown();
79
}
80
return;
81
} else {
82
if (!initialized_) {
83
Init();
84
}
85
}
86
87
#ifdef ENABLE_DISCORD
88
#ifdef DISCORD_DISABLE_IO_THREAD
89
Discord_UpdateConnection();
90
#endif
91
Discord_RunCallbacks();
92
#endif
93
}
94
95
void Discord::SetPresenceGame(std::string_view gameTitle) {
96
if (!IsEnabled())
97
return;
98
99
if (!initialized_) {
100
Init();
101
}
102
103
#ifdef ENABLE_DISCORD
104
auto sc = GetI18NCategory(I18NCat::SCREEN);
105
std::string title(gameTitle);
106
DiscordRichPresence discordPresence{};
107
discordPresence.state = title.c_str();
108
discordPresence.details = sc->T_cstr("Playing");
109
discordPresence.startTimestamp = time(0);
110
discordPresence.largeImageText = "PPSSPP is the best PlayStation Portable emulator around!";
111
#ifdef GOLD
112
discordPresence.largeImageKey = "icon_gold_png";
113
#else
114
discordPresence.largeImageKey = "icon_regular_png";
115
#endif
116
Discord_UpdatePresence(&discordPresence);
117
#endif
118
}
119
120
void Discord::SetPresenceMenu() {
121
if (!IsEnabled())
122
return;
123
124
if (!initialized_) {
125
Init();
126
}
127
128
#ifdef ENABLE_DISCORD
129
auto sc = GetI18NCategory(I18NCat::SCREEN);
130
131
DiscordRichPresence discordPresence{};
132
discordPresence.state = sc->T_cstr("In menu");
133
discordPresence.details = "";
134
discordPresence.startTimestamp = time(0);
135
discordPresence.largeImageText = "PPSSPP is the best PlayStation Portable emulator around!";
136
#ifdef GOLD
137
discordPresence.largeImageKey = "icon_gold_png";
138
#else
139
discordPresence.largeImageKey = "icon_regular_png";
140
#endif
141
Discord_UpdatePresence(&discordPresence);
142
#endif
143
}
144
145
void Discord::ClearPresence() {
146
if (!IsEnabled() || !initialized_)
147
return;
148
149
#ifdef ENABLE_DISCORD
150
Discord_ClearPresence();
151
#endif
152
}
153
154