From 5eedbf2f1e6738476209149127b0a2bf32d23944 Mon Sep 17 00:00:00 2001 From: anavoi Date: Sun, 13 Jul 2025 23:47:40 +0200 Subject: [PATCH] feat: start/stop server handling The original app volume is also restored when the server is stopped. --- Addons/GBSUGui.cs | 15 ++++++++------- Addons/GBSUServer.cs | 23 +++++++++++++++++++++++ Addons/Helper.cs | 1 + Plugin.cs | 3 +++ 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/Addons/GBSUGui.cs b/Addons/GBSUGui.cs index 2e6cd6e..cac5917 100644 --- a/Addons/GBSUGui.cs +++ b/Addons/GBSUGui.cs @@ -67,20 +67,14 @@ Set CLI arguments: -ip, -port (optionally -maplist) { if (File.Exists(Helper.GameConfigPath)) { - Helper.hosting = true; - - AudioListener.volume = 0; // mute game audio - try { - Plugin.AddServerComp(); // add custom GBSU server component GBSUServer.StartServer(); } catch (Exception e) { PushError("Looks like you've caught a bug! Please send your log file to us :)\n" + e); - Helper.hosting = false; - Plugin.DestroyServerComp(); + GBSUServer.StopServer(); } } else @@ -131,6 +125,13 @@ Make sure to download a file from the examples given and rename it to config.jso { Helper.FlipVSync(); } + if (Helper.hosting) + { + if (GUI.Button(new Rect(195f, 330f, 170f, 30f), "Stop server")) + { + GBSUServer.StopServer(); + } + } GUI.Label(new Rect(20f, 365f, 365f, 400f), $@" diff --git a/Addons/GBSUServer.cs b/Addons/GBSUServer.cs index 7f7302b..2197a47 100644 --- a/Addons/GBSUServer.cs +++ b/Addons/GBSUServer.cs @@ -4,6 +4,7 @@ using GB.Core; using GB.Game; using HarmonyLib; using UnityEngine; +using UnityEngine.SceneManagement; #pragma warning disable IDE0051 // Private member is unused @@ -34,11 +35,33 @@ public class GBSUServer : MonoBehaviour public static void StartServer() { + 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 } + + public static void StopServer() + { + // WIP: At the moment the only way to stop the server is to exit/kill the game + Helper.hosting = false; + + AudioListener.volume = Helper.saved_volume; // restore volume + + // stop network listener + MonoSingleton.Instance.UNetManager.StopServer(); + + // destroy GBSU server comp + Plugin.DestroyServerComp(); + + // go back to main menu + SceneManager.LoadScene("Menu"); + } /* public string GetRemainingRoundTime() { diff --git a/Addons/Helper.cs b/Addons/Helper.cs index 454b68f..16b4a19 100644 --- a/Addons/Helper.cs +++ b/Addons/Helper.cs @@ -11,6 +11,7 @@ public class Helper public static string serverip = null; public static int serverport = 5999; public static bool hosting = false; + public static float saved_volume; public static void DisableAnalytics() { // Try disabling analytics https://docs.unity3d.com/ScriptReference/Analytics.Analytics-deviceStatsEnabled.html diff --git a/Plugin.cs b/Plugin.cs index c4a2a57..ed90b37 100644 --- a/Plugin.cs +++ b/Plugin.cs @@ -67,6 +67,9 @@ public class Plugin : BaseUnityPlugin // "-maplist" CLI Helper.CheckCustomRotationPath(); + + // store app volume in a variable to restore it if needed + Helper.saved_volume = AudioListener.volume; } private static void GBSUCompInit()