diff --git a/CalamityMod.cs b/CalamityMod.cs index 8984d69317..2444c56c95 100644 --- a/CalamityMod.cs +++ b/CalamityMod.cs @@ -132,8 +132,6 @@ namespace CalamityMod CalamityGlobalItem.UnloadTweaks(); CalamityGlobalProjectile.UnloadTweaks(); - PopupGUIManager.UnloadGUIs(); - Main.QueueMainThreadAction(() => { Main.OnPreDraw -= PrepareRenderTargets; diff --git a/ModSupport/WeakReferenceSupport.cs b/ModSupport/WeakReferenceSupport.cs index ae167ac5f7..7d171a83df 100644 --- a/ModSupport/WeakReferenceSupport.cs +++ b/ModSupport/WeakReferenceSupport.cs @@ -201,7 +201,6 @@ namespace CalamityMod if (!Main.dedServ) { WikiThisSupport(); - PopupGUIManager.LoadGUIs(); } } diff --git a/UI/PopupGUIManager.cs b/UI/PopupGUIManager.cs index 0f472693a9..0e37676da0 100644 --- a/UI/PopupGUIManager.cs +++ b/UI/PopupGUIManager.cs @@ -8,10 +8,10 @@ using Terraria.ModLoader.Core; namespace CalamityMod.UI { - // TODO -- This can be made into a ModSystem with simple OnModLoad and Unload hooks. - public static class PopupGUIManager + [Autoload(Side = ModSide.Client)] + public sealed class PopupGUIManager : ModSystem { - private static readonly List gUIs = new List(); + private static readonly List gUIs = []; public static bool GUIActive(PopupGUI gui) => gui.Active || gui.FadeTime > 0; public static bool AnyGUIsActive => gUIs.Any(GUIActive); public static PopupGUI GetActiveGUI => gUIs.FirstOrDefault(GUIActive); @@ -57,21 +57,18 @@ namespace CalamityMod.UI gUIs.First(gui => gui.GetType() == type).Active = !gUIs.First(gui => gui.GetType() == type).Active; } - public static void LoadGUIs() + public override void OnModLoad() { // Look through every type in the mod, and check if it's derived from PopupGUI. If it is, create a copy and save it in the static list. - foreach (Mod mod in ModLoader.Mods) + ReflectionHelper.IterateEveryModsTypes(action: type => { - foreach (Type type in AssemblyManager.GetLoadableTypes(mod.Code)) - { - // Don't load abstract classes since they cannot have instances. - if (type.IsAbstract) - continue; - if (type.IsSubclassOf(typeof(PopupGUI))) - gUIs.Add(Activator.CreateInstance(type) as PopupGUI); - } - } + gUIs.Add(Activator.CreateInstance(type) as PopupGUI); + }); + } + + public override void Unload() + { + gUIs?.Clear(); } - public static void UnloadGUIs() => gUIs.Clear(); } }