From 5eac25a5418886060eed53c2abe80c39ae3ed3f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=8D=9A=7ENex?= <87421482+NexIsDumb@users.noreply.github.com> Date: Sat, 23 Mar 2024 23:34:52 +0100 Subject: [PATCH] softcoded rpc calls Co-Authored-By: Ne_Eo --- assets/data/discord.hx | 40 +++++++++++++++++++ source/funkin/backend/utils/DiscordUtil.hx | 15 +++++++ .../editors/character/CharacterEditor.hx | 4 +- .../editors/character/CharacterSelection.hx | 2 + source/funkin/editors/charter/Charter.hx | 2 + .../editors/charter/CharterSelection.hx | 2 + source/funkin/game/GameOverSubstate.hx | 3 +- source/funkin/game/PlayState.hx | 2 +- source/funkin/menus/FreeplayState.hx | 2 +- source/funkin/menus/MainMenuState.hx | 2 +- source/funkin/menus/StoryMenuState.hx | 2 +- source/funkin/menus/TitleState.hx | 2 + source/funkin/menus/credits/CreditsMain.hx | 2 + source/funkin/options/OptionsMenu.hx | 2 + 14 files changed, 75 insertions(+), 7 deletions(-) create mode 100644 assets/data/discord.hx diff --git a/assets/data/discord.hx b/assets/data/discord.hx new file mode 100644 index 00000000..d528134b --- /dev/null +++ b/assets/data/discord.hx @@ -0,0 +1,40 @@ +function onGameOver() { + changePresence('Game Over', PlayState.SONG.meta.displayName + " (" + PlayState.difficulty + ")"); +} + +function onPlayStateUpdate() { + // Setting parent to make code cleaner + var old = __script__.interp.scriptObject; + __script__.interp.scriptObject = PlayState.instance; + + DiscordUtil.changeSongPresence(detailsText, (paused ? "Paused - " : "") + SONG.meta.displayName + " (" + difficulty + ")", inst, getIconRPC()); + + __script__.interp.scriptObject = old; +} + +function onMenuLoaded(name:String) { + // Name is either "Main Menu", "Freeplay", "Title Screen", "Options Menu", "Credits Menu" + changePresenceSince("In the Menus", null); +} + +function onEditorTreeLoaded(name:String) { + switch(name) { + case "Character Editor": + changePresenceSince("Choosing a Character", null); + case "Chart Editor": + changePresenceSince("Choosing a Chart", null); + case "Stage Editor": + changePresenceSince("Choosing a Stage", null); + } +} + +function onEditorLoaded(name:String, editingThing:String) { + switch(name) { + case "Character Editor": + changePresenceSince("Editing a Character", editingThing); + case "Chart Editor": + changePresenceSince("Editing a Chart", editingThing); + case "Stage Editor": + changePresenceSince("Editing a Stage", editingThing); + } +} \ No newline at end of file diff --git a/source/funkin/backend/utils/DiscordUtil.hx b/source/funkin/backend/utils/DiscordUtil.hx index c6825b4a..5a85173b 100644 --- a/source/funkin/backend/utils/DiscordUtil.hx +++ b/source/funkin/backend/utils/DiscordUtil.hx @@ -101,6 +101,7 @@ class DiscordUtil script = FlxDestroyUtil.destroy(script); } script = Script.create(Paths.script('data/discord')); + script.setParent(DiscordUtil); script.load(); } @@ -137,6 +138,20 @@ class DiscordUtil #end } + public static function changePresenceSince(details:String, state:String, ?smallImageKey:String, ?offset:Float = 0) + { + #if DISCORD_RPC + var start:Float = Date.now().getTime() + offset; + + changePresenceAdvanced({ + state: state, + details: details, + smallImageKey: smallImageKey, + startTimestamp: Std.int(start / 1000) + }); + #end + } + @:noCompletion public static function fixString(str:String) { return new cpp.ConstCharStar(cast(str, String)); diff --git a/source/funkin/editors/character/CharacterEditor.hx b/source/funkin/editors/character/CharacterEditor.hx index 8c5cf9c7..359ef24d 100644 --- a/source/funkin/editors/character/CharacterEditor.hx +++ b/source/funkin/editors/character/CharacterEditor.hx @@ -224,7 +224,7 @@ class CharacterEditor extends UIState { FlxG.cameras.add(uiCamera); FlxG.cameras.add(animsCamera); - character = new Character(0,0,__character, false, false); + character = new Character(0,0, __character, false, false); character.debugMode = true; character.cameras = [charCamera]; @@ -257,6 +257,8 @@ class CharacterEditor extends UIState { Framerate.memoryCounter.alpha = 0.4; Framerate.codenameBuildField.alpha = 0.4; } + + DiscordUtil.call("onEditorLoaded", ["Character Editor", __character]); } override function destroy() { diff --git a/source/funkin/editors/character/CharacterSelection.hx b/source/funkin/editors/character/CharacterSelection.hx index 9d1a60ed..817acf29 100644 --- a/source/funkin/editors/character/CharacterSelection.hx +++ b/source/funkin/editors/character/CharacterSelection.hx @@ -33,6 +33,8 @@ class CharacterSelection extends EditorTreeMenu })); main = new OptionsScreen("Character Editor", "Select a character to edit", list); + + DiscordUtil.call("onEditorTreeLoaded", ["Character Editor"]); } override function createPost() { diff --git a/source/funkin/editors/charter/Charter.hx b/source/funkin/editors/charter/Charter.hx index 34671758..21c3c2b7 100644 --- a/source/funkin/editors/charter/Charter.hx +++ b/source/funkin/editors/charter/Charter.hx @@ -490,6 +490,8 @@ class Charter extends UIState { Framerate.codenameBuildField.alpha = 0.4; } updateDisplaySprites(); + + DiscordUtil.call("onEditorLoaded", ["Chart Editor", __song + " (" + __diff + ")"]); } override function destroy() { diff --git a/source/funkin/editors/charter/CharterSelection.hx b/source/funkin/editors/charter/CharterSelection.hx index b0957997..6fd97bdc 100644 --- a/source/funkin/editors/charter/CharterSelection.hx +++ b/source/funkin/editors/charter/CharterSelection.hx @@ -46,6 +46,8 @@ class CharterSelection extends EditorTreeMenu { })); main = new OptionsScreen("Chart Editor", "Select a song to modify the charts from.", list); + + DiscordUtil.call("onEditorTreeLoaded", ["Chart Editor"]); } override function createPost() { diff --git a/source/funkin/game/GameOverSubstate.hx b/source/funkin/game/GameOverSubstate.hx index 3d119f02..4868b130 100644 --- a/source/funkin/game/GameOverSubstate.hx +++ b/source/funkin/game/GameOverSubstate.hx @@ -87,8 +87,7 @@ class GameOverSubstate extends MusicBeatSubstate lossSFX = FlxG.sound.play(Paths.sound(lossSFXName)); Conductor.changeBPM(gameOverSongBPM); - DiscordUtil.changePresence('Game Over', PlayState.SONG.meta.displayName + " (" + PlayState.difficulty + ")"); - + DiscordUtil.call("onGameOver", []); gameoverScript.call("postCreate"); } diff --git a/source/funkin/game/PlayState.hx b/source/funkin/game/PlayState.hx index c7364225..91e977a6 100644 --- a/source/funkin/game/PlayState.hx +++ b/source/funkin/game/PlayState.hx @@ -784,7 +784,7 @@ class PlayState extends MusicBeatState * This function is dynamic, which means you can do `updateDiscordPresence = function() {}` in scripts. */ public dynamic function updateDiscordPresence() - DiscordUtil.changeSongPresence(detailsText, (paused ? "Paused - " : "") + SONG.meta.displayName + " (" + difficulty + ")", inst, getIconRPC()); + DiscordUtil.call("onPlayStateUpdate", []); /** * Starts a cutscene. diff --git a/source/funkin/menus/FreeplayState.hx b/source/funkin/menus/FreeplayState.hx index 0a6f3493..7ca66b02 100644 --- a/source/funkin/menus/FreeplayState.hx +++ b/source/funkin/menus/FreeplayState.hx @@ -116,7 +116,7 @@ class FreeplayState extends MusicBeatState } } - DiscordUtil.changePresence("In the Menus", null); + DiscordUtil.call("onMenuLoaded", ["Freeplay"]); super.create(); diff --git a/source/funkin/menus/MainMenuState.hx b/source/funkin/menus/MainMenuState.hx index 03e3bd8b..b17e9db6 100644 --- a/source/funkin/menus/MainMenuState.hx +++ b/source/funkin/menus/MainMenuState.hx @@ -32,7 +32,7 @@ class MainMenuState extends MusicBeatState { super.create(); - DiscordUtil.changePresence("In the Menus", null); + DiscordUtil.call("onMenuLoaded", ["Main Menu"]); CoolUtil.playMenuSong(); diff --git a/source/funkin/menus/StoryMenuState.hx b/source/funkin/menus/StoryMenuState.hx index 6d9e27f2..384118e5 100644 --- a/source/funkin/menus/StoryMenuState.hx +++ b/source/funkin/menus/StoryMenuState.hx @@ -116,7 +116,7 @@ class StoryMenuState extends MusicBeatState { changeWeek(0, true); - DiscordUtil.changePresence("In the Menus", null); + DiscordUtil.call("onMenuLoaded", ["Story Menu"]); CoolUtil.playMenuSong(); } diff --git a/source/funkin/menus/TitleState.hx b/source/funkin/menus/TitleState.hx index 491bc65a..4aca30db 100644 --- a/source/funkin/menus/TitleState.hx +++ b/source/funkin/menus/TitleState.hx @@ -36,6 +36,8 @@ class TitleState extends MusicBeatState startIntro(); super.create(); + + DiscordUtil.call("onMenuLoaded", ["Title Screen"]); } var logoBl:FlxSprite; diff --git a/source/funkin/menus/credits/CreditsMain.hx b/source/funkin/menus/credits/CreditsMain.hx index 5dd16aff..56507274 100644 --- a/source/funkin/menus/credits/CreditsMain.hx +++ b/source/funkin/menus/credits/CreditsMain.hx @@ -43,6 +43,8 @@ class CreditsMain extends TreeMenu { main = new OptionsScreen('Credits', 'The people who made this possible!', selectables); super.create(); + + DiscordUtil.call("onMenuLoaded", ["Credits Menu"]); } /** diff --git a/source/funkin/options/OptionsMenu.hx b/source/funkin/options/OptionsMenu.hx index 5b786e10..f9232e13 100644 --- a/source/funkin/options/OptionsMenu.hx +++ b/source/funkin/options/OptionsMenu.hx @@ -37,6 +37,8 @@ class OptionsMenu extends TreeMenu { CoolUtil.playMenuSong(); + DiscordUtil.call("onMenuLoaded", ["Options Menu"]); + var bg:FlxSprite = new FlxSprite(-80).loadAnimatedGraphic(Paths.image('menus/menuBGBlue')); // bg.scrollFactor.set(); bg.scale.set(1.15, 1.15);