forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord.c
249 lines (203 loc) · 8.03 KB
/
discord.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* RetroArch - A frontend for libretro.
* Copyright (C) 2018 - Andrés Suárez
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <file/file_path.h>
#include "discord.h"
#include "discord_register.h"
#include "../retroarch.h"
#include "../configuration.h"
#include "../core.h"
#include "../core_info.h"
#include "../paths.h"
#include "../playlist.h"
#include "../msg_hash.h"
#ifdef HAVE_NETWORKING
#include "../../network/netplay/netplay.h"
#include "../../network/netplay/netplay_discovery.h"
#include "../../tasks/tasks_internal.h"
#endif
#ifdef HAVE_CHEEVOS
#include "../cheevos/cheevos.h"
#endif
static int FrustrationLevel = 0;
static int64_t start_time = 0;
static int64_t pause_time = 0;
static int64_t ellapsed_time = 0;
static bool discord_ready = false;
static unsigned discord_status = 0;
struct netplay_room *room;
DiscordRichPresence discord_presence;
static void handle_discord_ready(const DiscordUser* connectedUser)
{
RARCH_LOG("[Discord] connected to user: %s#%s - avatar id: %s\n",
connectedUser->username,
connectedUser->discriminator,
connectedUser->userId);
}
static void handle_discord_disconnected(int errcode, const char* message)
{
RARCH_LOG("[Discord] disconnected (%d: %s)\n", errcode, message);
}
static void handle_discord_error(int errcode, const char* message)
{
RARCH_LOG("[Discord] error (%d: %s)\n", errcode, message);
}
static void handle_discord_join(const char* secret)
{
RARCH_LOG("[Discord] join (%s)\n", secret);
static struct string_list *list = NULL;
list = string_split(secret, "|");
char tmp_hostname[32];
snprintf(tmp_hostname,
sizeof(tmp_hostname),
"%s|%s", list->elems[0].data, list->elems[1].data);
if (netplay_driver_ctl(RARCH_NETPLAY_CTL_IS_DATA_INITED, NULL))
deinit_netplay();
netplay_driver_ctl(RARCH_NETPLAY_CTL_ENABLE_CLIENT, NULL);
task_push_netplay_crc_scan(atoi(list->elems[3].data),
list->elems[2].data,
tmp_hostname, list->elems[4].data);
}
static void handle_discord_spectate(const char* secret)
{
RARCH_LOG("[Discord] spectate (%s)\n", secret);
}
static void handle_discord_join_request(const DiscordUser* request)
{
int response = -1;
char yn[4];
RARCH_LOG("[Discord] join request from %s#%s - %s\n",
request->username,
request->discriminator,
request->userId);
}
void discord_update(enum discord_presence presence)
{
core_info_t *core_info = NULL;
core_info_get_current_core(&core_info);
if (!discord_ready)
return;
if (presence == discord_status)
return;
if (presence == DISCORD_PRESENCE_NONE || presence == DISCORD_PRESENCE_MENU)
memset(&discord_presence, 0, sizeof(discord_presence));
switch (presence)
{
case DISCORD_PRESENCE_MENU:
discord_presence.details = msg_hash_to_str(MENU_ENUM_LABEL_VALUE_DISCORD_IN_MENU);
discord_presence.largeImageKey = "base";
discord_presence.largeImageText = msg_hash_to_str(MENU_ENUM_LABEL_VALUE_NO_CORE);
discord_presence.instance = 0;
break;
case DISCORD_PRESENCE_GAME_PAUSED:
discord_presence.smallImageKey = "paused";
discord_presence.smallImageText = msg_hash_to_str(MENU_ENUM_LABEL_VALUE_DISCORD_STATUS_PAUSED);
discord_presence.details = msg_hash_to_str(MENU_ENUM_LABEL_VALUE_DISCORD_IN_GAME_PAUSED);
pause_time = time(0);
ellapsed_time = difftime(time(0), start_time);
discord_presence.startTimestamp = pause_time;
break;
case DISCORD_PRESENCE_GAME:
if (core_info)
{
const char *system_id = core_info->system_id ? core_info->system_id : "core";
char *label = NULL;
playlist_t *current_playlist = playlist_get_cached();
if (current_playlist)
playlist_get_index_by_path(
current_playlist, path_get(RARCH_PATH_CONTENT), NULL, &label, NULL, NULL, NULL, NULL);
if (!label)
label = (char *)path_basename(path_get(RARCH_PATH_BASENAME));
#if 0
RARCH_LOG("[Discord] current core: %s\n", system_id);
RARCH_LOG("[Discord] current content: %s\n", label);
#endif
discord_presence.largeImageKey = system_id;
if (core_info->display_name)
discord_presence.largeImageText = core_info->display_name;
start_time = time(0);
if (pause_time != 0)
start_time = time(0) - ellapsed_time;
pause_time = 0;
ellapsed_time = 0;
discord_presence.smallImageKey = "playing";
discord_presence.smallImageText = msg_hash_to_str(MENU_ENUM_LABEL_VALUE_DISCORD_STATUS_PLAYING);
discord_presence.startTimestamp = start_time;
discord_presence.details = msg_hash_to_str(MENU_ENUM_LABEL_VALUE_DISCORD_IN_GAME);
discord_presence.state = label;
discord_presence.instance = 0;
}
break;
case DISCORD_PRESENCE_NETPLAY_HOSTING:
room = netplay_get_host_room();
if (room->id == 0)
return;
RARCH_LOG("[Discord] netplay room details: id=%d, nick=%s IP=%s port=%d\n",
room->id, room->nickname,
room->host_method == NETPLAY_HOST_METHOD_MITM ? room->mitm_address : room->address,
room->host_method == NETPLAY_HOST_METHOD_MITM ? room->mitm_port : room->port);
char party_id[128];
snprintf(party_id, sizeof(party_id), "%d|%s", room->id, room->nickname);
char join_secret[128];
snprintf(join_secret, sizeof(join_secret), "%s|%d|%s|%u|%s",
room->host_method == NETPLAY_HOST_METHOD_MITM ? room->mitm_address : room->address,
room->host_method == NETPLAY_HOST_METHOD_MITM ? room->mitm_port : room->port,
room->gamename, room->gamecrc, room->corename);
RARCH_LOG("%s\n", join_secret);
discord_presence.joinSecret = strdup(join_secret);
discord_presence.spectateSecret = "SPECSPECSPEC";
discord_presence.partyId = party_id;
discord_presence.partyMax = 0;
discord_presence.partySize = 0;
break;
case DISCORD_PRESENCE_NETPLAY_HOSTING_STOPPED:
case DISCORD_PRESENCE_NETPLAY_CLIENT:
default:
discord_presence.joinSecret = NULL;
break;
}
RARCH_LOG("[Discord] updating (%d)\n", presence);
Discord_UpdatePresence(&discord_presence);
discord_status = presence;
}
void discord_init(void)
{
settings_t *settings = config_get_ptr();
DiscordEventHandlers handlers;
RARCH_LOG("[Discord] initializing ..\n");
start_time = time(0);
memset(&handlers, 0, sizeof(handlers));
handlers.ready = handle_discord_ready;
handlers.disconnected = handle_discord_disconnected;
handlers.errored = handle_discord_error;
handlers.joinGame = handle_discord_join;
handlers.spectateGame = handle_discord_spectate;
handlers.joinRequest = handle_discord_join_request;
/* To-Do: Add the arguments RetroArch was started with to the register URI*/
const char command[256] = "retroarch";
Discord_Initialize(settings->arrays.discord_app_id, &handlers, 0, NULL);
Discord_Register(settings->arrays.discord_app_id, NULL);
discord_ready = true;
}
void discord_shutdown(void)
{
RARCH_LOG("[Discord] shutting down ..\n");
Discord_ClearPresence();
Discord_Shutdown();
discord_ready = false;
}
void discord_run_callbacks()
{
Discord_RunCallbacks();
}