261 lines
6.6 KiB
Haxe
261 lines
6.6 KiB
Haxe
package;
|
|
|
|
#if DISCORD_ALLOWED
|
|
#if cpp
|
|
import cpp.ConstCharStar;
|
|
import cpp.Function;
|
|
import cpp.RawConstPointer;
|
|
#end
|
|
|
|
import sys.thread.Thread;
|
|
import lime.app.Application;
|
|
|
|
import hxdiscord_rpc.Discord;
|
|
import hxdiscord_rpc.Types;
|
|
|
|
import flixel.util.FlxStringUtil;
|
|
|
|
class DiscordClient
|
|
{
|
|
public static var isInitialized:Bool = false;
|
|
private inline static final _defaultID:String = "1192736165472784445";
|
|
public static var clientID(default, set):String = _defaultID;
|
|
private static var presence:DiscordPresence = #if (hxdiscord_rpc > "1.2.4") new DiscordPresence(); #else DiscordPresence.create(); #end
|
|
// hides this field from scripts and reflection in general
|
|
@:unreflective private static var __thread:Thread;
|
|
|
|
public static function check()
|
|
{
|
|
if(ClientPrefs.discordRPC) initialize();
|
|
else if(isInitialized) shutdown();
|
|
}
|
|
|
|
public static function prepare()
|
|
{
|
|
if (!isInitialized && ClientPrefs.discordRPC)
|
|
initialize();
|
|
|
|
Application.current.window.onClose.add(function() {
|
|
if(isInitialized) shutdown();
|
|
});
|
|
}
|
|
|
|
public dynamic static function shutdown()
|
|
{
|
|
isInitialized = false;
|
|
Discord.Shutdown();
|
|
}
|
|
|
|
private static function onReady(request:RawConstPointer<DiscordUser>):Void
|
|
{
|
|
final user = cast (request[0].username, String);
|
|
final discriminator = cast (request[0].discriminator, String);
|
|
|
|
var message = '(Discord) Connected to User ';
|
|
if (discriminator != '0') //Old discriminators
|
|
message += '($user#$discriminator)';
|
|
else //New Discord IDs/Discriminator system
|
|
message += '($user)';
|
|
|
|
trace(message);
|
|
changePresence();
|
|
}
|
|
|
|
private static function onError(errorCode:Int, message:ConstCharStar):Void
|
|
{
|
|
trace('Discord: Error ($errorCode: ${cast(message, String)})');
|
|
}
|
|
|
|
private static function onDisconnected(errorCode:Int, message:ConstCharStar):Void
|
|
{
|
|
trace('Discord: Disconnected ($errorCode: ${cast(message, String)})');
|
|
}
|
|
|
|
public static function initialize()
|
|
{
|
|
final discordHandlers:DiscordEventHandlers = #if (hxdiscord_rpc > "1.2.4") new DiscordEventHandlers(); #else DiscordEventHandlers.create(); #end
|
|
discordHandlers.ready = Function.fromStaticFunction(onReady);
|
|
discordHandlers.disconnected = Function.fromStaticFunction(onDisconnected);
|
|
discordHandlers.errored = Function.fromStaticFunction(onError);
|
|
Discord.Initialize(clientID, cpp.RawPointer.addressOf(discordHandlers), #if (hxdiscord_rpc > "1.2.4") false #else 1 #end, null);
|
|
|
|
if(!isInitialized) trace("Discord Client initialized");
|
|
|
|
if (__thread == null)
|
|
{
|
|
__thread = Thread.create(() ->
|
|
{
|
|
while (true)
|
|
{
|
|
if (isInitialized)
|
|
{
|
|
#if DISCORD_DISABLE_IO_THREAD
|
|
Discord.UpdateConnection();
|
|
#end
|
|
Discord.RunCallbacks();
|
|
}
|
|
|
|
// Wait 1 second until the next loop...
|
|
Sys.sleep(1.0);
|
|
}
|
|
});
|
|
}
|
|
isInitialized = true;
|
|
}
|
|
|
|
public static function changePresence(details:String = 'In the Menus', ?state:String, ?smallImageKey:String, ?hasStartTimestamp:Bool, ?endTimestamp:Float, largeImageKey:String = 'icon')
|
|
{
|
|
var startTimestamp:Float = 0;
|
|
if (hasStartTimestamp) startTimestamp = Date.now().getTime();
|
|
if (endTimestamp > 0) endTimestamp = startTimestamp + endTimestamp;
|
|
|
|
presence.state = state;
|
|
presence.details = details;
|
|
presence.smallImageKey = smallImageKey;
|
|
presence.largeImageKey = largeImageKey;
|
|
presence.largeImageText = "Engine Version: " + MainMenuState.psychEngineJSVersion;
|
|
// Obtained times are in milliseconds so they are divided so Discord can use it
|
|
presence.startTimestamp = Std.int(startTimestamp / 1000);
|
|
presence.endTimestamp = Std.int(endTimestamp / 1000);
|
|
updatePresence();
|
|
}
|
|
|
|
public static function updatePresence()
|
|
{
|
|
Discord.UpdatePresence(RawConstPointer.addressOf(presence.__presence));
|
|
}
|
|
|
|
inline public static function resetClientID()
|
|
{
|
|
clientID = _defaultID;
|
|
}
|
|
|
|
private static function set_clientID(newID:String)
|
|
{
|
|
var change:Bool = (clientID != newID);
|
|
clientID = newID;
|
|
|
|
if(change && isInitialized)
|
|
{
|
|
shutdown();
|
|
initialize();
|
|
updatePresence();
|
|
}
|
|
return newID;
|
|
}
|
|
|
|
#if LUA_ALLOWED
|
|
public static function addLuaCallbacks(lua:State)
|
|
{
|
|
Lua_helper.add_callback(lua, "changeDiscordPresence", changePresence);
|
|
Lua_helper.add_callback(lua, "changeDiscordClientID", function(?newID:String) {
|
|
if(newID == null) newID = _defaultID;
|
|
clientID = newID;
|
|
});
|
|
}
|
|
#end
|
|
}
|
|
|
|
@:allow(DiscordClient)
|
|
final class DiscordPresence
|
|
{
|
|
public var state(get, set):String;
|
|
public var details(get, set):String;
|
|
public var smallImageKey(get, set):String;
|
|
public var largeImageKey(get, set):String;
|
|
public var largeImageText(get, set):String;
|
|
public var startTimestamp(get, set):Int;
|
|
public var endTimestamp(get, set):Int;
|
|
|
|
@:noCompletion private var __presence:DiscordRichPresence;
|
|
|
|
function new()
|
|
{
|
|
__presence = new DiscordRichPresence();
|
|
}
|
|
|
|
public function toString():String
|
|
{
|
|
return FlxStringUtil.getDebugString([
|
|
LabelValuePair.weak("state", state),
|
|
LabelValuePair.weak("details", details),
|
|
LabelValuePair.weak("smallImageKey", smallImageKey),
|
|
LabelValuePair.weak("largeImageKey", largeImageKey),
|
|
LabelValuePair.weak("largeImageText", largeImageText),
|
|
LabelValuePair.weak("startTimestamp", startTimestamp),
|
|
LabelValuePair.weak("endTimestamp", endTimestamp)
|
|
]);
|
|
}
|
|
|
|
@:noCompletion inline function get_state():String
|
|
{
|
|
return __presence.state;
|
|
}
|
|
|
|
@:noCompletion inline function set_state(value:String):String
|
|
{
|
|
return __presence.state = value;
|
|
}
|
|
|
|
@:noCompletion inline function get_details():String
|
|
{
|
|
return __presence.details;
|
|
}
|
|
|
|
@:noCompletion inline function set_details(value:String):String
|
|
{
|
|
return __presence.details = value;
|
|
}
|
|
|
|
@:noCompletion inline function get_smallImageKey():String
|
|
{
|
|
return __presence.smallImageKey;
|
|
}
|
|
|
|
@:noCompletion inline function set_smallImageKey(value:String):String
|
|
{
|
|
return __presence.smallImageKey = value;
|
|
}
|
|
|
|
@:noCompletion inline function get_largeImageKey():String
|
|
{
|
|
return __presence.largeImageKey;
|
|
}
|
|
|
|
@:noCompletion inline function set_largeImageKey(value:String):String
|
|
{
|
|
return __presence.largeImageKey = value;
|
|
}
|
|
|
|
@:noCompletion inline function get_largeImageText():String
|
|
{
|
|
return __presence.largeImageText;
|
|
}
|
|
|
|
@:noCompletion inline function set_largeImageText(value:String):String
|
|
{
|
|
return __presence.largeImageText = value;
|
|
}
|
|
|
|
@:noCompletion inline function get_startTimestamp():Int
|
|
{
|
|
return __presence.startTimestamp;
|
|
}
|
|
|
|
@:noCompletion inline function set_startTimestamp(value:Int):Int
|
|
{
|
|
return __presence.startTimestamp = value;
|
|
}
|
|
|
|
@:noCompletion inline function get_endTimestamp():Int
|
|
{
|
|
return __presence.endTimestamp;
|
|
}
|
|
|
|
@:noCompletion inline function set_endTimestamp(value:Int):Int
|
|
{
|
|
return __presence.endTimestamp = value;
|
|
}
|
|
}
|
|
#end
|