From 0cb71314356bc7f8c0516d5b16033d684b7a6b05 Mon Sep 17 00:00:00 2001 From: anavoi Date: Sun, 13 Jul 2025 16:16:41 +0200 Subject: [PATCH] refactor: wip --- Addons/GBSUClient.cs | 17 +++++++++++++++ Addons/GBSUGui.cs | 50 ++++++++------------------------------------ Addons/GBSUServer.cs | 10 +++++++++ Addons/Helper.cs | 15 +++++++++++++ Plugin.cs | 22 +++++++++++++++---- 5 files changed, 69 insertions(+), 45 deletions(-) create mode 100644 Addons/GBSUClient.cs diff --git a/Addons/GBSUClient.cs b/Addons/GBSUClient.cs new file mode 100644 index 0000000..2fb7562 --- /dev/null +++ b/Addons/GBSUClient.cs @@ -0,0 +1,17 @@ +using GB.Core; +using GB.Platform.Lobby; +using UnityEngine; + +namespace GBSU.Addons; + +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); + } +} \ No newline at end of file diff --git a/Addons/GBSUGui.cs b/Addons/GBSUGui.cs index 432c611..badbf38 100644 --- a/Addons/GBSUGui.cs +++ b/Addons/GBSUGui.cs @@ -1,10 +1,7 @@ using System; using System.IO; using BepInEx; -using CoreNet.Config; -using GB.Config; -using GB.Core; -using GB.Game; +using CoreNet.Utils; using GB.Platform.Lobby; using UnityEngine; @@ -22,20 +19,6 @@ public class GBSUGui : MonoBehaviour private string error_msg = "Unknown error!"; readonly IInputSystem inputSystem = UnityInput.Current; - string serverip = null; - int serverport = 5999; - bool hosting = false; - private void Start() - { - if (CommandLineParser.Instance.KeyExists("-ip")) - { - serverip = CommandLineParser.Instance.GetValueForKey("-ip", true); - } - if (CommandLineParser.Instance.KeyExists("-port")) - { - int.TryParse(CommandLineParser.Instance.GetValueForKey("-port", true), out serverport); - } - } private void Update() { if (inputSystem.GetKeyDown(KeyCode.RightShift)) @@ -77,7 +60,7 @@ Set CLI arguments: -ip, -port { Application.Quit(0); } - if (serverip != null) + if (Helper.serverip != null) { if (GUI.Button(new Rect(20f, 260f, 170f, 30f), "Host")) { @@ -85,18 +68,14 @@ Set CLI arguments: -ip, -port { if (File.Exists(Helper.RotationFolderPath + "config.json")) { - hosting = true; + Helper.hosting = true; AudioListener.volume = 0; // mute game audio - Plugin.AddServerComp(); // add custom GBSU server component - try { - RotationConfig gameConfig = GBConfigLoader.LoadRotationConfig("config.json", true); // load rotation config from Config/Rotation/config.json - 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 + GBSUServer.StartServer(); } catch (Exception e) { @@ -119,13 +98,9 @@ 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 (!hosting) + 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); + GBSUClient.JoinServer(Helper.serverip, Helper.serverport); } else { @@ -153,14 +128,7 @@ Make sure to download a file from the examples given and rename it to config.jso } if (GUI.Button(new Rect(20f, 330f, 170f, 30f), "Toggle VSync")) { - if (QualitySettings.vSyncCount == 0) - { - QualitySettings.vSyncCount = 1; - } - else - { - QualitySettings.vSyncCount = 0; - } + Helper.FlipVSync(); } GUI.Label(new Rect(20f, 365f, 365f, 400f), $@" @@ -195,7 +163,7 @@ Please refer to the documentation for more information."); } private string UpdateScoreDisplay() { - if (hosting) + if (Helper.hosting) { string scoreString = "Score:\n"; foreach (var pair in Plugin.GameScore) diff --git a/Addons/GBSUServer.cs b/Addons/GBSUServer.cs index b2c57b8..2ab58be 100644 --- a/Addons/GBSUServer.cs +++ b/Addons/GBSUServer.cs @@ -1,3 +1,6 @@ +using CoreNet.Config; +using GB.Config; +using GB.Core; using GB.Game; using HarmonyLib; using UnityEngine; @@ -29,6 +32,13 @@ public class GBSUServer : MonoBehaviour localSingleGang?.SetValue(false); } + public static void StartServer() + { + RotationConfig gameConfig = GBConfigLoader.LoadRotationConfig("config.json", true); // load rotation config from Config/Rotation/config.json + 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 string GetRemainingRoundTime() { diff --git a/Addons/Helper.cs b/Addons/Helper.cs index c910d8b..daa4d4b 100644 --- a/Addons/Helper.cs +++ b/Addons/Helper.cs @@ -8,6 +8,9 @@ public class Helper { public static string RotationFolderPath = Application.dataPath + "/Config/Rotation/"; public static string GameConfigPath = RotationFolderPath + "config.json"; + public static string serverip = null; + public static int serverport = 5999; + public static bool hosting = false; public static void DisableAnalytics() { // Try disabling analytics https://docs.unity3d.com/ScriptReference/Analytics.Analytics-deviceStatsEnabled.html @@ -57,4 +60,16 @@ public class Helper return $"There are {number} files: {names}"; } } + + public static void FlipVSync() + { + if (QualitySettings.vSyncCount == 0) + { + QualitySettings.vSyncCount = 1; + } + else + { + QualitySettings.vSyncCount = 0; + } + } } \ No newline at end of file diff --git a/Plugin.cs b/Plugin.cs index 4345841..5b2b533 100644 --- a/Plugin.cs +++ b/Plugin.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Reflection; using BepInEx; using BepInEx.Logging; @@ -54,6 +54,16 @@ public class Plugin : BaseUnityPlugin Helper.CreateRotationFolder(); Log.LogDebug("Server game config should be found at " + Helper.GameConfigPath); + + // Parse CLI arguments + if (CommandLineParser.Instance.KeyExists("-ip")) + { + Helper.serverip = CommandLineParser.Instance.GetValueForKey("-ip", true); + } + if (CommandLineParser.Instance.KeyExists("-port")) + { + int.TryParse(CommandLineParser.Instance.GetValueForKey("-port", true), out Helper.serverport); + } } private static void GBSUCompInit() @@ -66,6 +76,13 @@ public class Plugin : BaseUnityPlugin } public static void AddServerComp() + { + DestroyServerComp(); + Log.LogDebug("Adding GBSUServer component"); + GBSUCompContainer.AddComponent(); + } + + public static void DestroyServerComp() { Component[] comps = GBSUCompContainer.GetComponents(typeof(Component)); foreach (var comp in comps) @@ -77,8 +94,5 @@ public class Plugin : BaseUnityPlugin Destroy(comp); } } - - Log.LogDebug("Adding GBSUServer component"); - GBSUCompContainer.AddComponent(); } }