Skip to content

Commit

Permalink
[QOL] Discord Client Rewrite (kinda...)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackx2 committed Jan 24, 2025
1 parent e7ff287 commit f229190
Show file tree
Hide file tree
Showing 12 changed files with 367 additions and 250 deletions.
2 changes: 1 addition & 1 deletion Project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<!-- Lua -->
<haxelib name="linc_luajit" if="LUA_ALLOWED"/>
<!-- HScript -->
<haxelib name="hscript-iris" if="HSCRIPT_ALLOWED" version="1.0.2"/>
<haxelib name="hscript-iris" if="HSCRIPT_ALLOWED" version="1.1.3"/>
<!-- Hxvlc -->
<haxelib name="hxvlc" if="VIDEOS_ALLOWED"/>
<!-- FlxAnimate -->
Expand Down
2 changes: 1 addition & 1 deletion assets/BASE_GAME/shared/data/stress/stress-psych.json
Original file line number Diff line number Diff line change
Expand Up @@ -3027,7 +3027,7 @@
"stage": "tank",
"needsVoices": true,
"validScore": true,
"format": "psych_v1_convert",
"format": "astro_v1_convert",
"bpm": 178,
"speed": 1.75
}
Expand Down
4 changes: 2 additions & 2 deletions commandline/libraries.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
<lib name="hscript"/>

<!-- HScript -->
<lib name="hscript-iris" version="1.0.2" />
<lib name="hscript-iris" version="1.1.3" />

<!-- Json -->
<lib name="tjson" version="1.4.0" />

<!-- Hx Discord RPC -->
<git name="hxdiscord_rpc" url="https://github.com/CodenameCrew/cne-hxdiscord_rpc" />
<git name="hxdiscord_rpc" url="https://github.com/AstroEngineDevs/hxdiscord_rpc" />

<!-- FlxAnimate -->
<git name="flxanimate" url="https://github.com/AstroEngineDevs/flxanimate" />
Expand Down
16 changes: 8 additions & 8 deletions source/Constants.hx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import funkin.backend.utils.Paths;

@:final class Constants {
@:final class Constants
{
public static inline final DEFAULT_DISCORD_ID:String = "1095422496473358356";

public static inline final SOUND_EXT:String = #if web "mp3" #else "ogg" #end;
public static inline final VIDEO_EXT:String = "mp4";

public static inline final DEFAULT_CHARACTER:String = 'bf'; // if the character is missing.

public static var DEFAULT_FONT(get, null):String = null;
@:noCompletion private inline static function get_DEFAULT_FONT():String {
if (DEFAULT_FONT == null)
DEFAULT_FONT = Paths.font("vcr.ttf");
return DEFAULT_FONT;
}
}
public static var DEFAULT_FONT(get, null):String;
@:noCompletion private inline static function get_DEFAULT_FONT():String
return DEFAULT_FONT == null ? Paths.font("vcr.ttf") : DEFAULT_FONT;
}
138 changes: 76 additions & 62 deletions source/funkin/backend/client/Discord.hx
Original file line number Diff line number Diff line change
@@ -1,51 +1,77 @@
package funkin.backend.client;

import Sys.sleep;

import funkin.game.Init;
import funkin.game.Config;

import funkin.backend.data.EngineData;
import funkin.backend.utils.ClientPrefs;
import Sys.sleep;

import lime.app.Application;

import hxdiscord_rpc.Discord;
import hxdiscord_rpc.Types;

#if LUA_ALLOWED
import llua.*;
import llua.Lua;
#end

enum ButtonType
{
FIRST;
SECOND;
}

class DiscordClient
{
public static var isInitialized:Bool = false;

private static final _defaultID:String = Config.discordID;
public static var clientID(default, set):String;
private static var presence:DiscordRichPresence = DiscordRichPresence.create();

@:isVar
public static var clientID(default, set):String;
@:noCompletion private static inline function set_clientID(newID:String)
{
if (newID == null) return clientID = _defaultID;

final change:Bool = (clientID != newID);
clientID = newID;

if (change && isInitialized)
{
shutdown();
initialize();
updatePresence();
}

return newID;
}

@:isVar
public static var clientName(default, set):String = null;

private static function set_clientName(owo:String)
@:noCompletion private static inline function set_clientName(owo:String)
return clientName = owo;

@:isVar
public static var clientDiscrim(default, set):String = null;

private static function set_clientDiscrim(owo:String)
@:noCompletion private static inline function set_clientDiscrim(owo:String)
return clientDiscrim = owo;

// discriminator

public static function check()
public static function check():Void
{
if (Config.discordID == '')
clientID = cast(EngineData.coreGame.coreDiscordID, String); // uhm astro engine shiz
else
clientID = _defaultID;

clientID = Config.discordID == '' ? cast(Constants.DEFAULT_DISCORD_ID, String) : _defaultID;
if (ClientPrefs.data.discordRPC)
initialize();
else if (isInitialized)
shutdown();
}

public static function prepare()
public static function prepare():Void
{
if (!isInitialized && ClientPrefs.data.discordRPC)
initialize();
Expand All @@ -65,27 +91,24 @@ class DiscordClient

private static function onReady(request:cpp.RawConstPointer<DiscordUser>):Void
{
var requestPtr:cpp.Star<DiscordUser> = cpp.ConstPointer.fromRaw(request).ptr;
final requestPtr:cpp.Star<DiscordUser> = cpp.ConstPointer.fromRaw(request).ptr;

clientName = '${cast (requestPtr.username, String)}';
clientDiscrim = cast(requestPtr.discriminator, String);

if (Std.parseInt(cast(requestPtr.discriminator, String)) != 0)
{ // Old discriminators
final userDiscriminator = '${cast (requestPtr.username, String)}#${cast (requestPtr.discriminator, String)}';
#if WATERMARK
Init.watermark.text += '\n$userDiscriminator\n${HashUtils.hash(cast(requestPtr.username, String), MD5)}';
#end
traceFr('Connected to User ($userDiscriminator)');
#if WATERMARK Init.watermark.text += '\n$userDiscriminator\n${HashUtils.hash(cast(requestPtr.username, String), MD5)}'; #end
trace('Connected to User ($userDiscriminator)');
}
else
{ // New Discord IDs/Discriminator system
final user = cast(requestPtr.username, String);
#if WATERMARK
Init.watermark.text += '\n@$user\n${HashUtils.hash(user, MD5)}';
#end
traceFr('Connected to User ($user)');
#if WATERMARK Init.watermark.text += '\n@$user\n${HashUtils.hash(user, MD5)}'; #end
trace('Connected to User ($user)');
}

#if WATERMARK
final userID = cast(requestPtr.userId, String).trim();
trace(userID);
Expand All @@ -97,45 +120,45 @@ class DiscordClient
}

private static function onError(errorCode:Int, message:cpp.ConstCharStar):Void
traceFr('Error ($errorCode: ${cast (message, String)})');
trace('[Discord Client]: Error ($errorCode: ${cast (message, String)})');

private static function onDisconnected(errorCode:Int, message:cpp.ConstCharStar):Void
traceFr('Disconnected ($errorCode: ${cast (message, String)})');
trace('[Discord Client]: Disconnected ($errorCode: ${cast (message, String)})');

public static function initialize()
public static function initialize():Void
{
#if desktop
var discordHandlers:DiscordEventHandlers = DiscordEventHandlers.create();
final discordHandlers:DiscordEventHandlers = DiscordEventHandlers.create();
discordHandlers.ready = cpp.Function.fromStaticFunction(onReady);
discordHandlers.disconnected = cpp.Function.fromStaticFunction(onDisconnected);
discordHandlers.errored = cpp.Function.fromStaticFunction(onError);
Discord.Initialize(clientID, cpp.RawPointer.addressOf(discordHandlers), 1, null);

if (!isInitialized)
traceFr("Initialized");
trace('[Discord Client]: Initialized');

sys.thread.Thread.create(() ->
{
var localID:String = clientID;
final localID:String = clientID;
while (localID == clientID)
{
#if DISCORD_DISABLE_IO_THREAD
Discord.UpdateConnection();
#end
Discord.RunCallbacks();

// Wait 0.5 seconds until the next loop...
Sys.sleep(0.5);
Sys.sleep(1);
}
});

isInitialized = true;
#else
FlxG.log.add('Isn\' desktop thingy');
FlxG.log.error('Desktop Support Only.')
#end
}

public static function changePresence(?details:String = 'In the Menus', ?state:Null<String>, ?smallImageKey:String, ?hasStartTimestamp:Bool,
?endTimestamp:Float, largeImageKey:String = 'icon')
?endTimestamp:Float, largeImageKey:String = 'icon'):Void
{
var startTimestamp:Float = 0;
if (hasStartTimestamp)
Expand All @@ -149,56 +172,52 @@ class DiscordClient
presence.largeImageText = "Engine Version: " + cast(EngineData.engineData.coreVersion, String);
presence.smallImageKey = smallImageKey;

// 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);

presence.button1Label = "Astro Engine Github";
presence.button1Url = "https://github.com/AstroEngineDevs/FNF-AstroEngine";

updatePresence();
}

public static function updatePresence()
Discord.UpdatePresence(cpp.RawConstPointer.addressOf(presence));
public static function updatePresence():Void
{
setButton({label: "Github Page", url: "https://github.com/AstroEngineDevs"}, FIRST);
setButton({label: "X Page", url: "https://x.com/YourFriendOrbl"}, SECOND);

public static function resetClientID()
clientID = _defaultID;
Discord.UpdatePresence(cpp.RawConstPointer.addressOf(presence));
}

private static function set_clientID(newID:String)
public dynamic static function setButton(data:{label:String, url:String}, type:ButtonType):Void
{
var change:Bool = (clientID != newID);
clientID = newID;

if (change && isInitialized)
switch (type)
{
shutdown();
initialize();
updatePresence();
case FIRST:
if (presence.button1Label == null)
presence.button1Label = cast(data.label, String);
if (presence.button1Url == null)
presence.button1Url = cast(data.url, String);
case SECOND:
if (presence.button2Label == null)
presence.button2Label = cast(data.label, String);
if (presence.button2Url == null)
presence.button2Url = cast(data.url, String);
}
return newID;
}

#if MODS_ALLOWED
public static function loadModRPC()
{
var pack:Dynamic = Mods.getPack();
final pack:Dynamic = Mods.getPack();
if (pack != null && pack.discordRPC != null && pack.discordRPC != clientID)
{
clientID = pack.discordRPC;
// trace('Changing clientID! $clientID, $_defaultID');
}
}
#end

#if LUA_ALLOWED
public static function addLuaCallbacks(lua:State)
{
Lua_helper.add_callback(lua, "changePresence",
function(details:String, state:Null<String>, ?smallImageKey:String, ?hasStartTimestamp:Bool, ?endTimestamp:Float)
{
changePresence(details, state, smallImageKey, hasStartTimestamp, endTimestamp);
});
function(details:String, state:Null<String>, ?smallImageKey:String, ?hasStartTimestamp:Bool,
?endTimestamp:Float) changePresence(details, state, smallImageKey, hasStartTimestamp, endTimestamp));

Lua_helper.add_callback(lua, "changeClientID", function(?newID:String = null)
{
Expand All @@ -208,9 +227,4 @@ class DiscordClient
});
}
#end

private static function traceFr(fr:String)
{
trace('RPC ${fr}');
}
}
1 change: 0 additions & 1 deletion source/funkin/backend/data/EngineData.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ final class EngineData
{
//** Customization (Source Code Only) **/
static final coreGame = {
coreDiscordID: "1095422496473358356",
menuColor: 0xff525252,
}

Expand Down
4 changes: 3 additions & 1 deletion source/funkin/backend/funkinLua/FunkinLua.hx
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ class FunkinLua {
else
MusicBeatState.switchState(new FreeplayState());

#if DISCORD_ALLOWED DiscordClient.resetClientID(); #end
#if DISCORD_ALLOWED DiscordClient.clientID = null; #end

FlxG.sound.playMusic(Paths.music('freakyMenu'));
PlayState.changedDifficulty = false;
Expand Down Expand Up @@ -1568,6 +1568,8 @@ class FunkinLua {

Lua_helper.add_callback(lua, "debugPrint", function(text:Dynamic = '', color:String = 'WHITE') PlayState.instance.addTextToDebug(text, CoolUtil.colorFromString(color)));

Lua_helper.add_callback(lua, "print", function(text:Dynamic = '') trace(text));

addLocalCallback("close", function() {
closed = true;
trace('Closing script $scriptName');
Expand Down
Loading

0 comments on commit f229190

Please sign in to comment.