-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencj_discord.cpp
300 lines (261 loc) · 8.34 KB
/
opencj_discord.cpp
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// TODO this class is currently Linux-specific. A Windows port needs to be added
#include "shared.hpp"
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <errno.h>
#ifdef COD4
#define SOCKET_PATH "/tmp/opencj_events_cod4"
#else
#define SOCKET_PATH "/tmp/opencj_events_cod2"
#endif
// These events are from the game to Discord
typedef enum
{
PLAYER_MESSAGE = 0,
MAP_STARTED = 1,
PLAYER_COUNT_CHANGED = 2,
PLAYER_JOINED = 3,
PLAYER_LEFT = 4,
RUN_FINISHED = 5,
PLAYER_RENAMED = 6,
} eGameEvent_t;
// These events are from Discord to the game
typedef enum
{
DISCORD_MESSAGE = 0,
} eDiscordEvent_t;
static int g_fileDescriptor = -1;
// Currently, just set latest Discord event, if one comes too quick, overwrite it.
// In future it may be an idea to queue them, but there'd have to be overflow guards
static bool g_hasLogged = false;
static int setupAndConnect()
{
extern cvar_t *net_port;
if (net_port->integer != 28960)
{
// Don't allow any other server than main to connect for now
return -1;
}
// Check if we need to close the socket first
if (g_fileDescriptor != -1)
{
// Socket error, disconnect
close(g_fileDescriptor);
g_fileDescriptor = -1;
Com_PrintError(CON_CHANNEL_ERROR, "Lost connection with Discord socket\n");
g_hasLogged = false;
}
g_fileDescriptor = socket(PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (g_fileDescriptor >= 0)
{
struct sockaddr_un server;
server.sun_family = AF_UNIX;
strcpy(server.sun_path, SOCKET_PATH);
if (connect(g_fileDescriptor, (struct sockaddr *)&server, sizeof(struct sockaddr_un)) >= 0)
{
Com_Printf(CON_CHANNEL_SYSTEM, "Successfully connected to Discord socket\n");
g_hasLogged = false;
}
else
{
close(g_fileDescriptor);
g_fileDescriptor = -1;
if (!g_hasLogged)
{
Com_PrintWarning(CON_CHANNEL_SYSTEM, "Could not connect to Discord socket\n");
g_hasLogged = true;
}
}
}
else
{
g_fileDescriptor = -1;
Com_PrintError(CON_CHANNEL_ERROR, "Could not setup Discord socket\n");
}
return g_fileDescriptor;
}
void Gsc_Discord_Connect()
{
if (g_fileDescriptor == -1)
{
(void)setupAndConnect();
}
else
{
// Check if connection is alive
int error = 0;
socklen_t len = sizeof(error);
int result = getsockopt(g_fileDescriptor, SOL_SOCKET, SO_ERROR, &error, &len);
if (result == 0)
{
stackPushInt(g_fileDescriptor);
return;
}
else
{
// Try to re-connect
(void)setupAndConnect();
}
}
if (g_fileDescriptor == -1)
{
stackPushUndefined();
}
else
{
stackPushInt(g_fileDescriptor);
}
}
void Gsc_Discord_GetEvent() // Check if there are events from Discord
{
char buf[512] = {0};
if (g_fileDescriptor != -1)
{
int result = read(g_fileDescriptor, buf, sizeof(buf));
int error = errno;
if (result > 0)
{
//Com_Printf(CON_CHANNEL_SYSTEM, "Discord event '%s' -> game\n", buf);
stackPushString(buf);
return;
}
else if ((result == 0) || ((result == -1) && (error != EAGAIN) && (error != EWOULDBLOCK)))
{
// Read returned EOF or another error
// Connection lost. Try to re-connect
(void)setupAndConnect();
}
}
stackPushUndefined();
}
void Gsc_Discord_OnEvent() // Push an event to Discord
{
// Only process game events if Discord socket is connected
if (g_fileDescriptor == -1)
{
return;
}
if ((Scr_GetNumParam() < 1) || (stackGetParamType(0) != STACK_INT))
{
stackError("Expected at least 1 argument: eventType (int)");
return;
}
int gameEventType = -1;
stackGetParamInt(0, &gameEventType);
char txBuf[512] = {0};
switch (gameEventType)
{
case PLAYER_MESSAGE:
{
if ((Scr_GetNumParam() != 3) || (stackGetParamType(1) != STACK_STRING) || (stackGetParamType(2) != STACK_STRING))
{
stackError("Expected 3 arguments: eventType (int), playerName (string), message (string)");
return;
}
char *playerName = NULL;
stackGetParamString(1, &playerName);
char *message = NULL;
stackGetParamString(2, &message);
snprintf(txBuf, sizeof(txBuf), "%d %s;%s\n", gameEventType, playerName, message);
} break;
case MAP_STARTED:
{
if ((Scr_GetNumParam() != 2) || (stackGetParamType(1) != STACK_STRING))
{
stackError("Expected 2 arguments: eventType (int), mapName (string)");
return;
}
char *mapName = NULL;
stackGetParamString(1, &mapName);
snprintf(txBuf, sizeof(txBuf), "%d %s\n", gameEventType, mapName);
} break;
case PLAYER_COUNT_CHANGED:
{
if ((Scr_GetNumParam() != 2) || (stackGetParamType(1) != STACK_INT))
{
stackError("Expected 2 arguments: eventType (int), playerCount (int)");
return;
}
int playerCount = 0;
stackGetParamInt(1, &playerCount);
snprintf(txBuf, sizeof(txBuf), "%d %d\n", gameEventType, playerCount);
} break;
case PLAYER_JOINED:
case PLAYER_LEFT:
{
if ((Scr_GetNumParam() != 2) || (stackGetParamType(1) != STACK_STRING))
{
stackError("Expected 2 arguments: eventType (int), playerName (string)");
return;
}
char *playerName = NULL;
stackGetParamString(1, &playerName);
if (playerName != NULL)
{
snprintf(txBuf, sizeof(txBuf), "%d %s\n", gameEventType, playerName);
}
} break;
case RUN_FINISHED:
{
if ((Scr_GetNumParam() != 6) || (stackGetParamType(1) != STACK_STRING) ||
(stackGetParamType(2) != STACK_INT) ||
(stackGetParamType(3) != STACK_STRING) ||
(stackGetParamType(4) != STACK_STRING) ||
(stackGetParamType(5) != STACK_STRING))
{
stackError("Expected 6 arguments: event (int), name (string), runID (int), time (string), mapName (string), route (string)");
return;
}
char *playerName = NULL;
stackGetParamString(1, &playerName);
if (playerName == NULL)
{
return;
}
int runID = -1;
stackGetParamInt(2, &runID);
if (runID == -1)
{
return;
}
char *timeStr = NULL;
stackGetParamString(3, &timeStr);
if (timeStr == NULL)
{
return;
}
char *mapName = NULL;
stackGetParamString(4, &mapName);
if (mapName == NULL)
{
return;
}
char *routeName = NULL;
stackGetParamString(5, &routeName);
if (routeName == NULL)
{
return;
}
snprintf(txBuf, sizeof(txBuf), "%d %s;%d;%s;%s;%s\n", gameEventType, playerName, runID, timeStr, mapName, routeName);
} break;
}
// Arriving here means some data is ready to be transmitted
int result = write(g_fileDescriptor, txBuf, strlen(txBuf));
int error = errno;
if (result > 0)
{
//Com_Printf(CON_CHANNEL_SYSTEM, "Game event '%s' -> Discord\n", txBuf);
}
else if ((result == -1) && (error != EAGAIN) && (error != EWOULDBLOCK))
{
// Write returned an error
// Connection lost. Try to re-connect
(void)setupAndConnect();
}
else
{
Com_PrintWarning(CON_CHANNEL_SYSTEM, "Could not transmit event (full)\n");
}
}