-
-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathDiscordUtil.hx
More file actions
544 lines (467 loc) · 14.1 KB
/
DiscordUtil.hx
File metadata and controls
544 lines (467 loc) · 14.1 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package funkin.backend.utils;
import flixel.util.FlxSignal.FlxTypedSignal;
import flixel.sound.FlxSound;
import flixel.util.typeLimit.OneOfTwo;
import funkin.backend.scripting.*; // lazy
import funkin.backend.scripting.events.CancellableEvent;
import funkin.backend.scripting.events.discord.DiscordPresenceUpdateEvent;
import funkin.backend.system.macros.Utils;
import haxe.Json;
import openfl.display.BitmapData;
#if DISCORD_RPC
import Sys;
import hxdiscord_rpc.Discord;
import hxdiscord_rpc.Types;
import lime.app.Application;
import sys.thread.Thread;
#end
final class DiscordUtil
{
public static var currentID(default, set):String = null;
public static var discordThread:#if DISCORD_RPC Thread #else Dynamic #end = null;
public static var ready:Bool = false;
public static var initialized:Bool = false;
private static var stopThread:Bool = false;
public static var user:#if DISCORD_RPC DUser #else Dynamic #end = null;
public static var lastPresence:#if DISCORD_RPC DPresence #else Dynamic #end = null;
public static var config:#if DISCORD_RPC DiscordJson #else Dynamic #end = null;
public static var script:Script;
// Constants
#if DISCORD_RPC
public static var REPLY_NO:Int = Discord.REPLY_NO;
public static var REPLY_YES:Int = Discord.REPLY_YES;
public static var REPLY_IGNORE:Int = Discord.REPLY_IGNORE;
public static var PARTY_PRIVATE:Int = Discord.PARTY_PRIVATE;
public static var PARTY_PUBLIC:Int = Discord.PARTY_PUBLIC;
#end
public static function init()
{
#if DISCORD_RPC
reloadJsonData();
if (initialized)
return;
initialized = true;
discordThread = Thread.create(function()
{
while (true)
{
while (!stopThread)
{
#if DISCORD_DISABLE_IO_THREAD
Discord.UpdateConnection();
#end
Discord.RunCallbacks();
Sys.sleep(2);
}
Sys.sleep(1); // to reduce cpu
}
});
Application.current.onExit.add(function(_) shutdown());
#end
}
public static function reloadJsonData()
{
#if DISCORD_RPC
config = null;
var jsonPath = Paths.json("config/discord");
if (Assets.exists(jsonPath))
{
try
config = Json.parse(Assets.getText(jsonPath))
catch (e)
Logs.error('Couldn\'t load Discord RPC configuration: ${e.toString()}');
}
if (config == null)
config = {};
config.logoKey = config.logoKey.getDefault(Flags.DEFAULT_DISCORD_LOGO_KEY);
config.logoText = config.logoText.getDefault(Flags.DEFAULT_DISCORD_LOGO_TEXT);
config.clientID = config.clientID.getDefault(Flags.DEFAULT_DISCORD_CLIENT_ID);
if (Flags.MOD_DISCORD_CLIENT_ID.length > 0) config.clientID = Flags.MOD_DISCORD_CLIENT_ID;
if (Flags.MOD_DISCORD_LOGO_KEY.length > 0) config.logoKey = Flags.MOD_DISCORD_LOGO_KEY;
if (Flags.MOD_DISCORD_LOGO_TEXT.length > 0) config.logoText = Flags.MOD_DISCORD_LOGO_TEXT;
currentID = config.clientID;
#end
}
public static function event<T:CancellableEvent>(name:String, event:T):T
{
if (script != null)
script.call(name, [event]);
return event;
}
public static function call(name:String, ?args:Array<Dynamic>)
{
if (script != null)
script.call(name, args);
}
public static function loadScript()
{
#if DISCORD_RPC
if (script != null)
{
call("destroy");
script = FlxDestroyUtil.destroy(script);
}
script = Script.create(Paths.script('data/discord'));
script.set("ActivityType", {
Playing: ActivityType.Playing,
PLAYING: ActivityType.Playing,
Streaming: ActivityType.Streaming,
STREAMING: ActivityType.Streaming,
Watching: ActivityType.Watching,
WATCHING: ActivityType.Watching,
Listening: ActivityType.Listening,
LISTENING: ActivityType.Listening,
Competing: ActivityType.Competing,
COMPETING: ActivityType.Competing
});
// script.setParent(DiscordUtil);
script.load();
#end
}
public static function changePresence(details:String, state:String, ?smallImageKey:String)
{
#if DISCORD_RPC
changePresenceAdvanced({
state: state,
details: details,
smallImageKey: smallImageKey
});
#end
}
public static function changeSongPresence(details:String, state:String, audio:FlxSound, ?smallImageKey:String)
{
#if DISCORD_RPC
var start:Float = 0;
var end:Float = 0;
if (audio != null && audio.playing)
{
start = Date.now().getTime();
end = start + (audio.length - audio.time);
}
changePresenceAdvanced({
state: state,
details: details,
smallImageKey: smallImageKey,
startTimestamp: Std.int(start / 1000),
endTimestamp: Std.int(end / 1000)
});
#end
}
public static function changePresenceSince(details:String, state:String, ?smallImageKey:String, ?time:Float)
{
#if DISCORD_RPC
if (time == null)
time = Date.now().getTime();
changePresenceAdvanced({
state: state,
details: details,
smallImageKey: smallImageKey,
startTimestamp: Std.int(time / 1000)
});
#end
}
#if cpp
@:noCompletion public static function fixString(str:String)
{
return new cpp.ConstCharStar(cast(str, String));
}
@:noCompletion public static function toString(str:cpp.ConstCharStar)
{
return cast(str, String);
}
#end
public static function changePresenceAdvanced(data:DPresence)
{
#if DISCORD_RPC
if (data == null)
return;
// copy last presence
if (data.largeImageKey == null)
data.largeImageKey = config.logoKey;
if (data.largeImageText == null)
data.largeImageText = config.logoText;
var evt = EventManager.get(DiscordPresenceUpdateEvent).recycle(data);
#if GLOBAL_SCRIPT
// kept for "backwards compat"
funkin.backend.scripting.GlobalScript.event("onDiscordPresenceUpdate", evt);
#end
event("onDiscordPresenceUpdate", evt);
if (evt.cancelled)
return;
data = evt.presence;
lastPresence = data;
var dp:DiscordRichPresence = DiscordRichPresence.create();
// TODO: make this use a reflection-like macro
Utils.safeSetWrapper(dp.state, data.state, fixString);
Utils.safeSetWrapper(dp.details, data.details, fixString);
Utils.safeSet(dp.startTimestamp, data.startTimestamp);
Utils.safeSet(dp.endTimestamp, data.endTimestamp);
Utils.safeSetWrapper(dp.largeImageKey, data.largeImageKey, fixString);
Utils.safeSetWrapper(dp.largeImageText, data.largeImageText, fixString);
Utils.safeSetWrapper(dp.smallImageKey, data.smallImageKey, fixString);
Utils.safeSetWrapper(dp.smallImageText, data.smallImageText, fixString);
Utils.safeSetWrapper(dp.partyId, data.partyId, fixString);
Utils.safeSet(dp.partySize, data.partySize);
Utils.safeSet(dp.partyMax, data.partyMax);
Utils.safeSet(dp.partyPrivacy, data.partyPrivacy);
Utils.safeSetWrapper(dp.matchSecret, data.matchSecret, fixString);
Utils.safeSetWrapper(dp.joinSecret, data.joinSecret, fixString);
Utils.safeSetWrapper(dp.spectateSecret, data.spectateSecret, fixString);
Utils.safeSet(dp.instance, data.instance);
Utils.safeSet(dp.activityType, data.activityType);
Utils.safeSetWrapper(dp.streamUrl, data.streamUrl, fixString);
if (data.matchSecret == null && data.joinSecret == null && data.spectateSecret == null)
{
Utils.safeSetWrapper(dp.button1Label, data.button1Label, fixString);
Utils.safeSetWrapper(dp.button1Url, data.button1Url, fixString);
Utils.safeSetWrapper(dp.button2Label, data.button2Label, fixString);
Utils.safeSetWrapper(dp.button2Url, data.button2Url, fixString);
}
Discord.UpdatePresence(cpp.RawConstPointer.addressOf(dp));
#end
}
public static function clearPresence()
{
#if DISCORD_RPC
Discord.ClearPresence();
#end
}
private static function set_currentID(id:String):String
{
if (currentID == id)
return id;
#if DISCORD_RPC
if (currentID != null)
shutdown();
var handlers:DiscordEventHandlers = DiscordEventHandlers.create();
handlers.ready = cpp.Function.fromStaticFunction(onReady);
handlers.disconnected = cpp.Function.fromStaticFunction(onDisconnected);
handlers.errored = cpp.Function.fromStaticFunction(onError);
handlers.joinGame = cpp.Function.fromStaticFunction(onJoin);
handlers.joinRequest = cpp.Function.fromStaticFunction(onJoinReq);
handlers.spectateGame = cpp.Function.fromStaticFunction(onSpectate);
handlers.anyResponse = cpp.Function.fromStaticFunction(onAnyResponse);
Discord.Initialize(id, cpp.RawPointer.addressOf(handlers), 1, null);
stopThread = false;
loadScript();
#end
return currentID = id;
}
public static function shutdown()
{
ready = false;
stopThread = true;
#if DISCORD_RPC
Discord.Shutdown();
#end
call("destroy");
script = FlxDestroyUtil.destroy(script);
}
public static function respond(userId:String, reply:Int)
{
#if DISCORD_RPC
Discord.Respond(fixString(userId), reply);
#end
}
// HANDLERS
#if DISCORD_RPC
private static function onReady(request:cpp.RawConstPointer<DiscordUser>):Void
{
user = DUser.initRaw(request);
Logs.traceColored([
Logs.getPrefix("Discord"),
Logs.logText("Connected to User " + user.globalName + " ("),
Logs.logText(user.handle, GRAY),
Logs.logText(")")
], INFO);
ready = true;
call("onReady", [user]);
}
private static function onDisconnected(errorCode:Int, message:cpp.ConstCharStar):Void
{
var finalMsg:String = cast(message, String);
Logs.traceColored([
Logs.getPrefix("Discord"),
Logs.logText("Disconnected ("),
Logs.logText('$errorCode: $finalMsg', RED),
Logs.logText(")")
], INFO);
call("onDisconnected", [errorCode, cast(finalMsg, String)]);
}
private static function onError(errorCode:Int, message:cpp.ConstCharStar):Void
{
var finalMsg:String = cast(message, String);
Logs.traceColored([
Logs.getPrefix("Discord"),
Logs.logText('Error ($errorCode: $finalMsg)', RED)
], ERROR);
call("onError", [errorCode, cast(finalMsg, String)]);
}
private static function onJoin(joinSecret:cpp.ConstCharStar):Void
{
Logs.traceColored([Logs.getPrefix("Discord"), Logs.logText("Someone has just joined", GREEN)], INFO);
call("onJoinGame", [cast(joinSecret, String)]);
}
private static function onSpectate(spectateSecret:cpp.ConstCharStar):Void
{
Logs.traceColored([
Logs.getPrefix("Discord"),
Logs.logText("Someone started spectating your game", YELLOW)
], INFO);
call("onSpectateGame", [cast(spectateSecret, String)]);
}
private static function onJoinReq(request:cpp.RawConstPointer<DiscordUser>):Void
{
Logs.traceColored([
Logs.getPrefix("Discord"),
Logs.logText("Someone has just requested to join", YELLOW)
], INFO);
var req:DUser = DUser.initRaw(request);
call("onJoinRequest", [req]);
}
public static var anyResponse:FlxTypedSignal<String->Void> = new FlxTypedSignal<String->Void>();
private static function onAnyResponse(data:cpp.ConstCharStar):Void
{
call("onAnyResponse", [data]);
anyResponse.dispatch(data);
}
#end
private static function getUUID():String {
var uuid = new StringBuf();
for (i in 0...16) {
uuid.add(StringTools.hex(Math.floor(Math.random() * 16), 1));
}
return uuid.toString();
}
public static function sendCustomCommand(data:Dynamic) {
#if DISCORD_RPC
if(data == null) return;
if(data.nonce == null) data.nonce = getUUID();
var json = Json.stringify(data);
Discord.SendCustomCommand(json);
#end
}
public static function setDebugMode(mode:Bool) {
#if DISCORD_RPC
Discord.SetDebugMode(mode);
#end
}
}
typedef DiscordJson =
{
var ?clientID:String;
var ?logoKey:String;
var ?logoText:String;
}
@:noCustomClass
final class DUser
{
/**
* The username + discriminator if they have it
**/
public var handle:String;
/**
* The user id, aka 860561967383445535
**/
public var userId:String;
/**
* The user's username
**/
public var username:String;
/**
* The #number from before discord changed to usernames only, if the user has changed to a username them its just a 0
**/
public var discriminator:Int;
/**
* The user's avatar filename
**/
public var avatar:String;
/**
* The user's display name
**/
public var globalName:String;
/**
* If the user is a bot or not
**/
public var bot:Bool;
/**
* Idk check discord docs
**/
public var flags:Int;
/**
* If the user has nitro
**/
public var premiumType:NitroType;
private function new()
{
}
#if DISCORD_RPC
public static function initRaw(req:cpp.RawConstPointer<DiscordUser>)
{
return init(cpp.ConstPointer.fromRaw(req).ptr);
}
public static function init(userData:cpp.Star<DiscordUser>)
{
var d = new DUser();
d.userId = userData.userId;
d.username = userData.username;
d.discriminator = Std.parseInt(userData.discriminator);
d.avatar = userData.avatar;
d.globalName = userData.globalName;
d.bot = userData.bot;
d.flags = userData.flags;
d.premiumType = userData.premiumType;
if (d.discriminator != 0)
d.handle = '${d.username}#${d.discriminator}';
else
d.handle = '${d.username}';
return d;
}
#end
/**
* Calling this function gets the BitmapData of the user
**/
public function getAvatar(size:Int = 256):BitmapData
return BitmapData.fromBytes(HttpUtil.requestBytes('https://cdn.discordapp.com/avatars/$userId/$avatar.png?size=$size'));
}
enum abstract NitroType(Int) to Int from Int
{
var NONE = 0;
var NITRO_CLASSIC = 1;
var NITRO = 2;
var NITRO_BASIC = 3;
}
typedef DPresence =
{
var ?state:String; /* max 128 bytes */
var ?details:String; /* max 128 bytes */
var ?startTimestamp:OneOfTwo<Int, haxe.Int64>;
var ?endTimestamp:OneOfTwo<Int, haxe.Int64>;
var ?largeImageKey:String; /* max 32 bytes */
var ?largeImageText:String; /* max 128 bytes */
var ?smallImageKey:String; /* max 32 bytes */
var ?smallImageText:String; /* max 128 bytes */
var ?partyId:String; /* max 128 bytes */
var ?partySize:Int;
var ?partyMax:Int;
var ?partyPrivacy:Int;
var ?matchSecret:String; /* max 128 bytes */
var ?joinSecret:String; /* max 128 bytes */
var ?spectateSecret:String; /* max 128 bytes */
var ?instance:#if cpp OneOfTwo<Int, cpp.Int8> #else Int #end;
var ?button1Label:String; /* max 32 bytes */
var ?button1Url:String; /* max 512 bytes */
var ?button2Label:String; /* max 32 bytes */
var ?button2Url:String; /* max 512 bytes */
var ?activityType:#if DISCORD_RPC ActivityType #else Dynamic #end;
var ?streamUrl:String; /* max 512 bytes */
}
typedef DEvents =
{
var ?ready:DUser->Void;
var ?disconnected:(errorCode:Int, message:String) -> Void;
var ?errored:(errorCode:Int, message:String) -> Void;
var ?joinGame:String->Void;
var ?spectateGame:String->Void;
var ?joinRequest:DUser->Void;
}