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