From 88af9c52a413b3749b25cac91b92a7a6d98a53eb Mon Sep 17 00:00:00 2001 From: Yoshman29 <81191621+YoshiCrafter29@users.noreply.github.com> Date: Wed, 11 Jan 2023 17:59:35 +0100 Subject: [PATCH] DiscordUtil + Fix for #51 --- buildnumber.txt | 2 +- source/funkin/game/GameOverSubstate.hx | 3 + source/funkin/game/PlayState.hx | 92 ++++--------- source/funkin/menus/FreeplayState.hx | 12 +- source/funkin/menus/MainMenuState.hx | 10 +- source/funkin/menus/ModSwitchMenu.hx | 2 +- source/funkin/menus/PauseSubState.hx | 2 + source/funkin/menus/StoryMenuState.hx | 2 + source/funkin/options/OptionsMenu.hx | 2 +- source/funkin/options/OptionsScreen.hx | 2 +- .../options/keybinds/KeybindsOptions.hx | 2 +- source/funkin/system/Discord.hx | 88 ------------- source/funkin/system/Main.hx | 21 +-- .../funkin/updating/UpdateAvailableScreen.hx | 2 +- source/funkin/utils/DiscordUtil.hx | 124 ++++++++++++++++++ 15 files changed, 169 insertions(+), 197 deletions(-) delete mode 100644 source/funkin/system/Discord.hx create mode 100644 source/funkin/utils/DiscordUtil.hx diff --git a/buildnumber.txt b/buildnumber.txt index 153038f1..6d0f5c2f 100644 --- a/buildnumber.txt +++ b/buildnumber.txt @@ -1 +1 @@ -2003 \ No newline at end of file +2006 \ No newline at end of file diff --git a/source/funkin/game/GameOverSubstate.hx b/source/funkin/game/GameOverSubstate.hx index 5a63b259..2300772f 100644 --- a/source/funkin/game/GameOverSubstate.hx +++ b/source/funkin/game/GameOverSubstate.hx @@ -54,6 +54,9 @@ class GameOverSubstate extends MusicBeatSubstate lossSFX = FlxG.sound.play(Paths.sound(lossSFXName)); Conductor.changeBPM(100); + + + DiscordUtil.changePresence('Game Over', PlayState.SONG.song + " (" + PlayState.difficulty + ")"); } override function update(elapsed:Float) diff --git a/source/funkin/game/PlayState.hx b/source/funkin/game/PlayState.hx index 3e95d414..1b2d6f00 100644 --- a/source/funkin/game/PlayState.hx +++ b/source/funkin/game/PlayState.hx @@ -8,9 +8,6 @@ import flixel.group.FlxSpriteGroup; import funkin.options.Options; import funkin.scripting.Script; import flixel.util.FlxDestroyUtil; -#if DISCORD_RPC -import funkin.system.Discord.DiscordClient; -#end import funkin.system.Section.SwagSection; import funkin.system.Song.SwagSong; import funkin.scripting.ScriptPack; @@ -443,14 +440,9 @@ class PlayState extends MusicBeatState new ComboRating(1, "S++", 0xFF44FFFF), ]; - #if DISCORD_RPC - // Discord RPC variables - public var difficultyText:String = ""; - public var iconRPC:String = ""; - public var songLength:Float = 0; public var detailsText:String = ""; public var detailsPausedText:String = ""; - #end + public function updateRating() { var rating = null; var acc = get_accuracy(); @@ -498,10 +490,6 @@ class PlayState extends MusicBeatState Conductor.setupSong(SONG); - #if DISCORD_RPC - // TODO: Scriptable custom RPC - iconRPC = (dad != null && dad.icon != null) ? dad.icon : SONG.player2; - detailsText = isStoryMode ? ("Story Mode: " + storyWeek.name) : "Freeplay"; // Checks if cutscene files exists @@ -512,10 +500,9 @@ class PlayState extends MusicBeatState // String for when the game is paused detailsPausedText = "Paused - " + detailsText; + // Updating Discord Rich Presence. - DiscordClient.changePresence(detailsText, SONG.song + " (" + difficultyText + ")", iconRPC); - #end dad = new Character(100, 100, SONG.player2); @@ -656,9 +643,16 @@ class PlayState extends MusicBeatState public override function createPost() { startCutscene(); super.createPost(); + + updateDiscordPresence(); + scripts.call("postCreate"); } + public dynamic function updateDiscordPresence() { + DiscordUtil.changeSongPresence(detailsText, (paused ? "Paused - " : "") + SONG.song + " (" + difficulty + ")", inst, getIconRPC()); + } + public function startCutscene() { if (playCutscenes) { var videoCutscene = Paths.video('${PlayState.SONG.song.toLowerCase()}-cutscene'); @@ -805,7 +799,7 @@ class PlayState extends MusicBeatState vocals.play(); inst.play(); - updateDiscordStatus(); + updateDiscordPresence(); } public override function destroy() { @@ -1048,42 +1042,27 @@ class PlayState extends MusicBeatState startTimer.active = true; paused = false; - #if DISCORD_RPC - if (startTimer.finished) - { - DiscordClient.changePresence(detailsText, SONG.song + " (" + difficultyText + ")", iconRPC, true, songLength - Conductor.songPosition); - } - else - { - DiscordClient.changePresence(detailsText, SONG.song + " (" + difficultyText + ")", iconRPC); - } - #end + updateDiscordPresence(); } super.closeSubState(); } + public inline function getIconRPC():String + return (dad != null ? (dad.icon != null ? dad.icon : dad.curCharacter) : SONG.player2); var __songPlaying:Bool = false; var __wasAutoPause:Bool = false; override public function onFocus():Void { scripts.call("onFocus"); - #if DISCORD_RPC - if (health > 0 && !paused) - { - if (Conductor.songPosition > 0.0) - { - DiscordClient.changePresence(detailsText, SONG.song + " (" + difficultyText + ")", iconRPC, true, songLength - Conductor.songPosition); + + + if (__wasAutoPause) { + updateDiscordPresence(); + if (__songPlaying) { + inst.play(); + vocals.play(); } - else - { - DiscordClient.changePresence(detailsText, SONG.song + " (" + difficultyText + ")", iconRPC); - } - } - #end - if (__wasAutoPause && __songPlaying) { - inst.play(); - vocals.play(); } super.onFocus(); @@ -1092,12 +1071,7 @@ class PlayState extends MusicBeatState override public function onFocusLost():Void { scripts.call("onFocusLost"); - #if DISCORD_RPC - if (health > 0 && !paused) - { - DiscordClient.changePresence(detailsPausedText, SONG.song + " (" + difficultyText + ")", iconRPC); - } - #end + updateDiscordPresence(); if (__wasAutoPause = FlxG.autoPause) { __songPlaying = inst.playing; inst.pause(); @@ -1141,20 +1115,7 @@ class PlayState extends MusicBeatState else openSubState(new PauseSubState(boyfriend.getScreenPosition().x, boyfriend.getScreenPosition().y)); - updateDiscordStatus(); - } - - // TODO: Update Discord Status - public function updateDiscordStatus() { - // TODO: Cancellable Discord Update Presence - #if DISCORD_RPC - // Song duration in a float, useful for the time left feature - songLength = inst.length; - - // Updating Discord Rich Presence (with Time Left) - DiscordClient.changePresence(detailsText, SONG.song + " (" + difficultyText + ")", iconRPC, true, songLength); - #end - scripts.call("onDiscordPresenceUpdate"); + updateDiscordPresence(); } override public function update(elapsed:Float) @@ -1185,10 +1146,6 @@ class PlayState extends MusicBeatState if (FlxG.keys.justPressed.SEVEN) { FlxG.switchState(new ChartingState()); - - #if DISCORD_RPC - DiscordClient.changePresence("Chart Editor", null, null, true); - #end } if (FlxG.keys.justPressed.F5) { Logs.trace('Reloading scripts...', WARNING, YELLOW); @@ -1367,11 +1324,6 @@ class PlayState extends MusicBeatState FlxG.sound.music.stop(); openSubState(new GameOverSubstate(boyfriend.x, boyfriend.y, character, gameOverSong, lossSFX, retrySFX)); - - #if DISCORD_RPC - // Game Over doesn't get his own variable because it's only used here - DiscordClient.changePresence("Game Over - " + detailsText, SONG.song + " (" + difficultyText + ")", iconRPC); - #end } function endSong():Void diff --git a/source/funkin/menus/FreeplayState.hx b/source/funkin/menus/FreeplayState.hx index 633bc8c4..87cfc923 100644 --- a/source/funkin/menus/FreeplayState.hx +++ b/source/funkin/menus/FreeplayState.hx @@ -1,9 +1,6 @@ package funkin.menus; import haxe.io.Path; -#if DISCORD_RPC -import funkin.system.Discord.DiscordClient; -#end import flash.text.TextField; import flixel.FlxG; import flixel.FlxSprite; @@ -60,10 +57,7 @@ class FreeplayState extends MusicBeatState songList = FreeplaySonglist.get(); songs = songList.songs; - #if DISCORD_RPC - // Updating Discord Rich Presence - DiscordClient.changePresence("In the Menus", null); - #end + DiscordUtil.changePresence("In the Menus", null); // LOAD CHARACTERS @@ -164,7 +158,7 @@ class FreeplayState extends MusicBeatState if (controls.BACK) { - CoolUtil.playMenuSFX(2, 0.4); + CoolUtil.playMenuSFX(2, 0.7); FlxG.switchState(new MainMenuState()); } @@ -196,7 +190,7 @@ class FreeplayState extends MusicBeatState function changeSelection(change:Int = 0, force:Bool = false) { if (change == 0 && !force) return; - CoolUtil.playMenuSFX(0, 0.4); + CoolUtil.playMenuSFX(0, 0.7); curSelected = FlxMath.wrap(curSelected + change, 0, songs.length-1); diff --git a/source/funkin/menus/MainMenuState.hx b/source/funkin/menus/MainMenuState.hx index 0f97cf77..aca87a4c 100644 --- a/source/funkin/menus/MainMenuState.hx +++ b/source/funkin/menus/MainMenuState.hx @@ -2,9 +2,6 @@ package funkin.menus; import funkin.ui.FunkinText; import funkin.options.Options; -#if DISCORD_RPC -import funkin.system.Discord.DiscordClient; -#end import flixel.FlxG; import funkin.menus.credits.CreditsMain; import flixel.FlxObject; @@ -47,10 +44,7 @@ class MainMenuState extends MusicBeatState { super.create(); - #if DISCORD_RPC - // Updating Discord Rich Presence - DiscordClient.changePresence("In the Menus", null); - #end + DiscordUtil.changePresence("In the Menus", null); CoolUtil.playMenuSong(); @@ -198,7 +192,7 @@ class MainMenuState extends MusicBeatState curSelected = event.value; if (event.playMenuSFX) - CoolUtil.playMenuSFX(0); + CoolUtil.playMenuSFX(0, 0.7); menuItems.forEach(function(spr:FlxSprite) { diff --git a/source/funkin/menus/ModSwitchMenu.hx b/source/funkin/menus/ModSwitchMenu.hx index 5ffde6f4..2e3c7a67 100644 --- a/source/funkin/menus/ModSwitchMenu.hx +++ b/source/funkin/menus/ModSwitchMenu.hx @@ -69,7 +69,7 @@ class ModSwitchMenu extends MusicBeatSubstate { curSelected = FlxMath.wrap(curSelected + change, 0, alphabets.length-1); - CoolUtil.playMenuSFX(); + CoolUtil.playMenuSFX(0, 0.7); for(k=>alphabet in alphabets.members) { alphabet.alpha = 0.6; diff --git a/source/funkin/menus/PauseSubState.hx b/source/funkin/menus/PauseSubState.hx index 7b329cfb..a623667c 100644 --- a/source/funkin/menus/PauseSubState.hx +++ b/source/funkin/menus/PauseSubState.hx @@ -103,6 +103,8 @@ class PauseSubState extends MusicBeatSubstate FlxG.cameras.add(camera, false); pauseScript.call("postCreate"); + + PlayState.instance.updateDiscordPresence(); } override function update(elapsed:Float) diff --git a/source/funkin/menus/StoryMenuState.hx b/source/funkin/menus/StoryMenuState.hx index dcbf17fb..a1159f89 100644 --- a/source/funkin/menus/StoryMenuState.hx +++ b/source/funkin/menus/StoryMenuState.hx @@ -121,6 +121,8 @@ class StoryMenuState extends MusicBeatState { } changeWeek(0, true); + + DiscordUtil.changePresence("In the Menus", null); } var __lastDifficultyTween:FlxTween; diff --git a/source/funkin/options/OptionsMenu.hx b/source/funkin/options/OptionsMenu.hx index 5ff6b85d..a294c323 100644 --- a/source/funkin/options/OptionsMenu.hx +++ b/source/funkin/options/OptionsMenu.hx @@ -103,7 +103,7 @@ class OptionsMenu extends MusicBeatState { public function changeSelection(change:Int) { if (change == 0) return; - CoolUtil.playMenuSFX(0, 0.4); + CoolUtil.playMenuSFX(0, 0.7); curSelected = FlxMath.wrap(curSelected + change, 0, options.length-1); alphabets.forEach(function(e) { e.alpha = 0.6; diff --git a/source/funkin/options/OptionsScreen.hx b/source/funkin/options/OptionsScreen.hx index 2d3e7ca1..6c3adb6c 100644 --- a/source/funkin/options/OptionsScreen.hx +++ b/source/funkin/options/OptionsScreen.hx @@ -107,7 +107,7 @@ class OptionsScreen extends MusicBeatState { public function changeSelection(change:Int) { if (change == 0 && curSelected != -1) return; - CoolUtil.playMenuSFX(0); + CoolUtil.playMenuSFX(0, 0.7); if (curSelected != (curSelected = FlxMath.wrap(curSelected + change, 0, options.length - 1))) { for(e in options) e.selected = false; diff --git a/source/funkin/options/keybinds/KeybindsOptions.hx b/source/funkin/options/keybinds/KeybindsOptions.hx index 42d67230..ed4fbff1 100644 --- a/source/funkin/options/keybinds/KeybindsOptions.hx +++ b/source/funkin/options/keybinds/KeybindsOptions.hx @@ -225,7 +225,7 @@ class KeybindsOptions extends MusicBeatSubstate { if (controls.LEFT_P || controls.RIGHT_P) { if (alphabets.members[curSelected] != null) { - CoolUtil.playMenuSFX(0); + CoolUtil.playMenuSFX(0, 0.7); alphabets.members[curSelected].p2Selected = (p2Selected = !p2Selected); } } diff --git a/source/funkin/system/Discord.hx b/source/funkin/system/Discord.hx deleted file mode 100644 index d58930ee..00000000 --- a/source/funkin/system/Discord.hx +++ /dev/null @@ -1,88 +0,0 @@ -package funkin.system; - -import Sys.sleep; -import discord_rpc.DiscordRpc; - -using StringTools; - -class DiscordClient -{ - public function new() - { - trace("Discord Client starting..."); - DiscordRpc.start({ - // clientID: "814588678700924999", - clientID: "1027994136193810442", - onReady: onReady, - onError: onError, - onDisconnected: onDisconnected - }); - trace("Discord Client started."); - - while (true) - { - DiscordRpc.process(); - sleep(2); - //trace("Discord Client Update"); - } - - DiscordRpc.shutdown(); - } - - public static function shutdown() - { - DiscordRpc.shutdown(); - } - - static function onReady() - { - DiscordRpc.presence({ - details: "In the Menus", - state: null, - largeImageKey: 'icon', - largeImageText: "Friday Night Funkin'" - }); - } - - static function onError(_code:Int, _message:String) - { - trace('Error! $_code : $_message'); - } - - static function onDisconnected(_code:Int, _message:String) - { - trace('Disconnected! $_code : $_message'); - } - - public static function initialize() - { - var DiscordDaemon = sys.thread.Thread.create(() -> - { - new DiscordClient(); - }); - trace("Discord Client initialized"); - } - - public static function changePresence(details:String, state:Null, ?smallImageKey : String, ?hasStartTimestamp : Bool, ?endTimestamp: Float) - { - var startTimestamp:Float = if(hasStartTimestamp) Date.now().getTime() else 0; - - if (endTimestamp > 0) - { - endTimestamp = startTimestamp + endTimestamp; - } - - DiscordRpc.presence({ - details: details, - state: state, - largeImageKey: 'icon', - largeImageText: "Friday Night Funkin'", - smallImageKey : smallImageKey, - // Obtained times are in milliseconds so they are divided so Discord can use it - startTimestamp : Std.int(startTimestamp / 1000), - endTimestamp : Std.int(endTimestamp / 1000) - }); - - //trace('Discord RPC Updated. Arguments: $details, $state, $smallImageKey, $hasStartTimestamp, $endTimestamp'); - } -} diff --git a/source/funkin/system/Main.hx b/source/funkin/system/Main.hx index e7112006..c9752906 100644 --- a/source/funkin/system/Main.hx +++ b/source/funkin/system/Main.hx @@ -24,9 +24,6 @@ import flixel.addons.transition.FlxTransitionSprite.GraphicTransTileDiamond; import flixel.addons.transition.TransitionData; import flixel.math.FlxPoint; import flixel.math.FlxRect; -#if DISCORD_RPC -import funkin.system.Discord.DiscordClient; -#end import lime.app.Application; #if ALLOW_MULTITHREADING @@ -148,9 +145,7 @@ class Main extends Sprite Paths.init(); ModsFolder.init(); DesktopMain.init(); - #if MOD_SUPPORT - ModsFolder.switchMod("introMod"); - #end + DiscordUtil.init(); #if ALLOW_MULTITASKING funkin.multitasking.MultiTaskingHandler.init(); #end @@ -195,19 +190,13 @@ class Main extends Sprite Conductor.init(); AudioSwitchFix.init(); WindowsAPI.setDarkMode(true); - - - #if DISCORD_RPC - DiscordClient.initialize(); - - Application.current.onExit.add (function (exitCode) { - DiscordClient.shutdown(); - }); - #end - EventManager.init(); FlxG.signals.preStateCreate.add(onStateSwitch); + #if MOD_SUPPORT + ModsFolder.switchMod("introMod"); + #end + initTransition(); } diff --git a/source/funkin/updating/UpdateAvailableScreen.hx b/source/funkin/updating/UpdateAvailableScreen.hx index 858adcfe..3cb410a2 100644 --- a/source/funkin/updating/UpdateAvailableScreen.hx +++ b/source/funkin/updating/UpdateAvailableScreen.hx @@ -139,7 +139,7 @@ class UpdateAvailableScreen extends MusicBeatState { public function changeSelection() { - CoolUtil.playMenuSFX(); + CoolUtil.playMenuSFX(0, 0.7); if (installSelected) { installButton.text = "> INSTALL <"; skipButton.text = "SKIP"; diff --git a/source/funkin/utils/DiscordUtil.hx b/source/funkin/utils/DiscordUtil.hx new file mode 100644 index 00000000..30ec025e --- /dev/null +++ b/source/funkin/utils/DiscordUtil.hx @@ -0,0 +1,124 @@ +package funkin.utils; + +#if DISCORD_RPC +import discord_rpc.DiscordRpc; +import sys.thread.Thread; +import Sys; +import lime.app.Application; +import flixel.system.FlxSound; +#end + +class DiscordUtil { + public static var currentID:String = null; + public static var discordThread:#if DISCORD_RPC Thread #else Dynamic #end = null; + public static var ready:Bool = false; + + /** + * CHANGE SETTINGS HERE FOR CUSTOM DISCORD RPC ON SOURCE + */ + public static final defaultClientID:String = "1027994136193810442"; + public static final logoKey:String = "icon"; + + public static function init() { + #if DISCORD_RPC + changeClientID(defaultClientID); + discordThread = Thread.create(function() { + while (true) + { + while(!ready) { + Sys.sleep(1/60); + } + trace("Processing Discord RPC..."); + DiscordRpc.process(); + Sys.sleep(2); + } + }); + + Application.current.onExit.add(function(exitCode) { + shutdown(); + }); + #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 changePresenceAdvanced(data:#if DISCORD_RPC DiscordPresenceOptions #else Dynamic #end) { + #if DISCORD_RPC + if (data == null) return; + + if (data.largeImageKey == null) + data.largeImageKey = logoKey; + if (data.largeImageText == null) + data.largeImageText = Application.current.meta.get('title'); + + DiscordRpc.presence(data); + #end + } + public static function changeClientID(id:String) { + #if DISCORD_RPC + if (currentID != null) { + DiscordRpc.shutdown(); + } + + ready = false; + + DiscordRpc.start({ + clientID: id, + onReady: function() { + Logs.trace('Discord RPC started'); + ready = true; + }, + onError: onError, + onDisconnected: onDisconnected + }); + currentID = id; + + #end + } + + public static function shutdown() { + #if DISCORD_RPC + DiscordRpc.shutdown(); + #end + } + + + // HANDLERS + #if DISCORD_RPC + static function onError(_code:Int, _message:String) { + Logs.trace('Discord RPC Error: ${_message} (Code: $_code)', ERROR); + } + + static function onDisconnected(_code:Int, _message:String) { + Logs.trace('Discord RPC Disconnected: ${_message} (Code: $_code)', WARNING); + } + #end +} \ No newline at end of file