From 05256ecd8ab9fa27f2715f5332c8d30467829af4 Mon Sep 17 00:00:00 2001 From: anavoi Date: Sat, 12 Jul 2025 23:28:33 +0200 Subject: [PATCH] fix: keep only one server comp --- Plugin.cs | 154 +++++++++++++++++++++++++++++------------------------- 1 file changed, 83 insertions(+), 71 deletions(-) diff --git a/Plugin.cs b/Plugin.cs index b98969d..81e98a0 100644 --- a/Plugin.cs +++ b/Plugin.cs @@ -1,71 +1,83 @@ -using System.Collections.Generic; -using System.Reflection; -using BepInEx; -using BepInEx.Logging; -using GBSU.Addons; -using HarmonyLib; -using HarmonyLib.Tools; -using UnityEngine; - -#pragma warning disable IDE0051 // Private member is unused - -namespace GBSU; - -[BepInPlugin(PluginGUID, PluginName, PluginVersion)] -[BepInProcess("Gang Beasts.exe")] -public class Plugin : BaseUnityPlugin -{ - public static Dictionary GameScore = []; - private static GameObject _gbsuCompContainer; - internal static ManualLogSource Log; - - public static GameObject GBSUCompContainer - { - get - { - if (_gbsuCompContainer == null) - { - _gbsuCompContainer = new GameObject("GBSUSingletons"); - } - - return _gbsuCompContainer; - } - set - { - Destroy(_gbsuCompContainer); - _gbsuCompContainer = value; - } - } - private void Awake() - { - // Plugin startup logic - Log = base.Logger; - Log.LogInfo($"\n------\nPlugin {PluginName} [{PluginVersion}] is loaded!\n------\n"); - - HarmonyFileLog.Enabled = true; - var harmony = new Harmony(PluginGUID); - harmony.PatchAll(Assembly.GetExecutingAssembly()); - - GBSUCompInit(); - - Helper.DisableAnalytics(); // thank me later - } - - private static void GBSUCompInit() - { - // Create a container that wont lose our objects - GBSUCompContainer = new GameObject("GBSUSingletons"); - DontDestroyOnLoad(GBSUCompContainer); - GBSUCompContainer.hideFlags = HideFlags.DontUnloadUnusedAsset; - GBSUCompContainer.AddComponent(); - } - - public static void AddServerComp() - { - GBSUCompContainer.AddComponent(); - } - - public const string PluginGUID = "com.gaboule.plugins.gbsu"; - public const string PluginName = "Gang Beasts Server Utility"; - public const string PluginVersion = "1.0.2"; -} +using System.Collections.Generic; +using System.Reflection; +using BepInEx; +using BepInEx.Logging; +using GBSU.Addons; +using HarmonyLib; +using HarmonyLib.Tools; +using UnityEngine; + +#pragma warning disable IDE0051 // Private member is unused + +namespace GBSU; + +[BepInPlugin(PluginGUID, PluginName, PluginVersion)] +[BepInProcess("Gang Beasts.exe")] +public class Plugin : BaseUnityPlugin +{ + public static Dictionary GameScore = []; + private static GameObject _gbsuCompContainer; + internal static ManualLogSource Log; + + public static GameObject GBSUCompContainer + { + get + { + if (_gbsuCompContainer == null) + { + _gbsuCompContainer = new GameObject("GBSUSingletons"); + } + + return _gbsuCompContainer; + } + set + { + Destroy(_gbsuCompContainer); + _gbsuCompContainer = value; + } + } + private void Awake() + { + // Plugin startup logic + Log = base.Logger; + Log.LogInfo($"\n------\nPlugin {PluginName} [{PluginVersion}] is loaded!\n------\n"); + + HarmonyFileLog.Enabled = true; + var harmony = new Harmony(PluginGUID); + harmony.PatchAll(Assembly.GetExecutingAssembly()); + + GBSUCompInit(); + + Helper.DisableAnalytics(); // thank me later + } + + private static void GBSUCompInit() + { + // Create a container that wont lose our objects + GBSUCompContainer = new GameObject("GBSUSingletons"); + DontDestroyOnLoad(GBSUCompContainer); + GBSUCompContainer.hideFlags = HideFlags.DontUnloadUnusedAsset; + GBSUCompContainer.AddComponent(); + } + + public static void AddServerComp() + { + Component[] comps = GBSUCompContainer.GetComponents(typeof(Component)); + foreach (var comp in comps) + { + Log.LogDebug("iterating thru singleton comps"); + if(comp is GBSUServer) + { + Log.LogDebug("GBSUServer component was found! Destroying it"); + Destroy(comp); + } + } + + Log.LogDebug("Adding GBSUServer component"); + GBSUCompContainer.AddComponent(); + } + + public const string PluginGUID = "com.gaboule.plugins.gbsu"; + public const string PluginName = "Gang Beasts Server Utility"; + public const string PluginVersion = "1.0.2"; +}