From b9a7a1c4797153af5c4465578d6451c71a360cfe Mon Sep 17 00:00:00 2001 From: anavoi Date: Sun, 13 Jul 2025 23:55:54 +0200 Subject: [PATCH 1/3] fix: catch more edge cases --- Addons/GBSUClient.cs | 17 ++++++++++++----- Addons/GBSUGui.cs | 13 +------------ Addons/GBSUServer.cs | 19 +++++++++++-------- Plugin.cs | 4 ++++ 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/Addons/GBSUClient.cs b/Addons/GBSUClient.cs index 2fb7562..6b05a36 100644 --- a/Addons/GBSUClient.cs +++ b/Addons/GBSUClient.cs @@ -8,10 +8,17 @@ public class GBSUClient : MonoBehaviour { public static void JoinServer(string serverip, int serverport) { - LobbyManager.Instance.LobbyStates.IP = serverip; - LobbyManager.Instance.LobbyStates.Port = serverport; - LobbyManager.Instance.LobbyStates.CurrentState = LobbyState.State.Ready | LobbyState.State.InGame; - LobbyManager.Instance.LocalBeasts.SetupNetMemberContext(true); - MonoSingleton.Instance.UNetManager.LaunchClient(serverip, serverport); + if (!Helper.hosting) + { + LobbyManager.Instance.LobbyStates.IP = serverip; + LobbyManager.Instance.LobbyStates.Port = serverport; + LobbyManager.Instance.LobbyStates.CurrentState = LobbyState.State.Ready | LobbyState.State.InGame; + LobbyManager.Instance.LocalBeasts.SetupNetMemberContext(true); + MonoSingleton.Instance.UNetManager.LaunchClient(serverip, serverport); + } + else + { + GBSUGui.PushError("You are currently hosting a game! Please stop your server before attempting to join."); + } } } \ No newline at end of file diff --git a/Addons/GBSUGui.cs b/Addons/GBSUGui.cs index cac5917..24c080f 100644 --- a/Addons/GBSUGui.cs +++ b/Addons/GBSUGui.cs @@ -93,14 +93,7 @@ Make sure to download a file from the examples given and rename it to config.jso { if (LobbyManager.Instance.LobbyStates.SelfState == LobbyState.Game.Online) { - if (!Helper.hosting) - { - GBSUClient.JoinServer(Helper.serverip, Helper.serverport); - } - else - { - PushError("You are currently hosting a match! Please restart the game before attempting to join."); - } + GBSUClient.JoinServer(Helper.serverip, Helper.serverport); } else { @@ -108,10 +101,6 @@ Make sure to download a file from the examples given and rename it to config.jso } } } - else - { - PushError("Couldn't find the -ip CLI argument. Please refer to the documentation."); - } if (GUI.Button(new Rect(20f, 295f, 170f, 30f), "Cap FPS to 60")) { diff --git a/Addons/GBSUServer.cs b/Addons/GBSUServer.cs index 2197a47..157f6de 100644 --- a/Addons/GBSUServer.cs +++ b/Addons/GBSUServer.cs @@ -35,15 +35,18 @@ public class GBSUServer : MonoBehaviour public static void StartServer() { - Helper.hosting = true; - AudioListener.volume = 0; // mute game audio + if (!Helper.hosting) + { + Helper.hosting = true; + AudioListener.volume = 0; // mute game audio - Plugin.AddServerComp(); // add custom GBSU server component - - RotationConfig gameConfig = GBConfigLoader.LoadRotationConfig(null); // the argument doesn't matter as we do stuff in the method - ServerConfig serverConfig = NetConfigLoader.LoadServerConfig(); // load default server config, because it can be overridden by args like -ip and -port - MonoSingleton.Instance.UNetManager.LaunchServer(serverConfig); // launch the server with the server config - MonoSingleton.Instance.UNetManager.GetComponent().ChangeRotationConfig(gameConfig, 0); // set server's rotationconfig + Plugin.AddServerComp(); // add custom GBSU server component + + RotationConfig gameConfig = GBConfigLoader.LoadRotationConfig(null); // the argument doesn't matter as we do stuff in the method + ServerConfig serverConfig = NetConfigLoader.LoadServerConfig(); // load default server config, because it can be overridden by args like -ip and -port + MonoSingleton.Instance.UNetManager.LaunchServer(serverConfig); // launch the server with the server config + MonoSingleton.Instance.UNetManager.GetComponent().ChangeRotationConfig(gameConfig, 0); // set server's rotationconfig + } } public static void StopServer() diff --git a/Plugin.cs b/Plugin.cs index ed90b37..98947a4 100644 --- a/Plugin.cs +++ b/Plugin.cs @@ -60,6 +60,10 @@ public class Plugin : BaseUnityPlugin { Helper.serverip = CommandLineParser.Instance.GetValueForKey("-ip", false); } + else + { + GBSUGui.PushError("Couldn't find the -ip CLI argument. Please refer to the documentation."); + } if (CommandLineParser.Instance.KeyExists("-port")) { int.TryParse(CommandLineParser.Instance.GetValueForKey("-port", false), out Helper.serverport); From 34d9e9261e3b5aea2831522e3c9aa1d8500fda5a Mon Sep 17 00:00:00 2001 From: anavoi Date: Mon, 14 Jul 2025 00:14:11 +0200 Subject: [PATCH 2/3] feat: added "menu" panic button This avoids restarting the game when encountering an unknown issue. --- Addons/GBSUClient.cs | 8 ++++++++ Addons/GBSUGui.cs | 4 ++++ Addons/GBSUServer.cs | 5 ++--- Addons/Helper.cs | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/Addons/GBSUClient.cs b/Addons/GBSUClient.cs index 6b05a36..a800c62 100644 --- a/Addons/GBSUClient.cs +++ b/Addons/GBSUClient.cs @@ -1,4 +1,5 @@ using GB.Core; +using GB.Networking.Utils; using GB.Platform.Lobby; using UnityEngine; @@ -21,4 +22,11 @@ public class GBSUClient : MonoBehaviour GBSUGui.PushError("You are currently hosting a game! Please stop your server before attempting to join."); } } + + // this method should be used when something has went wrong + // because players can just use the escape menu to exit + public static void DisconnectFromServer() + { + GBNetUtils.DisconnectSelf(false); + } } \ No newline at end of file diff --git a/Addons/GBSUGui.cs b/Addons/GBSUGui.cs index 24c080f..3053d5d 100644 --- a/Addons/GBSUGui.cs +++ b/Addons/GBSUGui.cs @@ -59,6 +59,10 @@ Set CLI arguments: -ip, -port (optionally -maplist) { Application.Quit(0); } + if (GUI.Button(new Rect(320f, 45f, 55f, 30f), "Menu")) + { + Helper.SwitchToMenu(); + } if (Helper.serverip != null) { if (GUI.Button(new Rect(20f, 260f, 170f, 30f), "Host")) diff --git a/Addons/GBSUServer.cs b/Addons/GBSUServer.cs index 157f6de..8df60df 100644 --- a/Addons/GBSUServer.cs +++ b/Addons/GBSUServer.cs @@ -4,7 +4,6 @@ using GB.Core; using GB.Game; using HarmonyLib; using UnityEngine; -using UnityEngine.SceneManagement; #pragma warning disable IDE0051 // Private member is unused @@ -41,7 +40,7 @@ public class GBSUServer : MonoBehaviour AudioListener.volume = 0; // mute game audio Plugin.AddServerComp(); // add custom GBSU server component - + RotationConfig gameConfig = GBConfigLoader.LoadRotationConfig(null); // the argument doesn't matter as we do stuff in the method ServerConfig serverConfig = NetConfigLoader.LoadServerConfig(); // load default server config, because it can be overridden by args like -ip and -port MonoSingleton.Instance.UNetManager.LaunchServer(serverConfig); // launch the server with the server config @@ -63,7 +62,7 @@ public class GBSUServer : MonoBehaviour Plugin.DestroyServerComp(); // go back to main menu - SceneManager.LoadScene("Menu"); + Helper.SwitchToMenu(); } /* public string GetRemainingRoundTime() diff --git a/Addons/Helper.cs b/Addons/Helper.cs index 16b4a19..0139ddc 100644 --- a/Addons/Helper.cs +++ b/Addons/Helper.cs @@ -1,7 +1,11 @@ using System; using System.IO; +using GB.Core; +using GB.Networking.Utils; +using GB.Platform.Lobby; using UnityEngine; using UnityEngine.Analytics; +using UnityEngine.SceneManagement; namespace GBSU.Addons; public class Helper @@ -83,4 +87,32 @@ public class Helper Plugin.Log.LogInfo("Set custom rotation config path at " + GameConfigPath); } } + + public static void SwitchToMenu() + { + if (hosting) + { + GBSUGui.PushError("Please press the Stop server button instead!"); + } + else + { + // uhm, time to panic! + + // let's begin by disconnecting if theres an active connection + try + { + GBNetUtils.DisconnectSelf(false); + } + catch (Exception e) + { + Plugin.Log.LogWarning("Couldn't disconnect from server:\n" + e); + } + + // go back to main menu + SceneManager.LoadScene(Global.MENU_SCENE_NAME); + + // make sure our lobby state is menu + LobbyManager.Instance.LobbyStates.SelfState = LobbyState.Game.Menu; + } + } } \ No newline at end of file From 60706fb255f8cbf67ad0d07a3750a5cf2d4a0b9a Mon Sep 17 00:00:00 2001 From: anavoi Date: Mon, 14 Jul 2025 00:15:04 +0200 Subject: [PATCH 3/3] fix: make sure volume is set to 0 when entering server mode --- Addons/GBSUServer.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Addons/GBSUServer.cs b/Addons/GBSUServer.cs index 8df60df..06ee5e8 100644 --- a/Addons/GBSUServer.cs +++ b/Addons/GBSUServer.cs @@ -26,6 +26,8 @@ public class GBSUServer : MonoBehaviour //_gmInit = AccessTools.Method(typeof(GameMode), "Init"); localSingleGang = Traverse.Create(nameof(GameMode)).Field("localSingleGang"); gameTimer = Traverse.Create(nameof(GameMode)).Field("timer"); + + AudioListener.volume = 0; // mute game audio } void SetLocalGangToOff() { @@ -37,7 +39,6 @@ public class GBSUServer : MonoBehaviour if (!Helper.hosting) { Helper.hosting = true; - AudioListener.volume = 0; // mute game audio Plugin.AddServerComp(); // add custom GBSU server component