diff --git a/CalamityMod.cs b/CalamityMod.cs index 39a6be1dcc..dffe7863ae 100644 --- a/CalamityMod.cs +++ b/CalamityMod.cs @@ -158,8 +158,6 @@ namespace CalamityMod { Main.OnPreDraw += PrepareRenderTargets; }); - - InvasionProgressUIManager.LoadGUIs(); } #endregion @@ -177,7 +175,6 @@ namespace CalamityMod CalamityGlobalProjectile.UnloadTweaks(); PopupGUIManager.UnloadGUIs(); - InvasionProgressUIManager.UnloadGUIs(); SchematicManager.Unload(); PlayerDashManager.Unload(); diff --git a/UI/InvasionProgressUIManager.cs b/UI/InvasionProgressUIManager.cs index 4c037c5a74..73d9af3532 100644 --- a/UI/InvasionProgressUIManager.cs +++ b/UI/InvasionProgressUIManager.cs @@ -2,14 +2,15 @@ using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework.Graphics; +using Terraria.ModLoader; using Terraria.ModLoader.Core; namespace CalamityMod.UI { - // TODO -- This can be made into a ModSystem with simple OnModLoad and Unload hooks. - public static class InvasionProgressUIManager + [Autoload(Side = ModSide.Client)] + public sealed class InvasionProgressUIManager : ModSystem { - private static readonly List gUIs = new List(); + private static readonly List gUIs = []; public static int TotalGUIsActive => gUIs.Count(gui => gui.IsActive); public static bool AnyGUIsActive => TotalGUIsActive > 0; public static InvasionProgressUI GetActiveGUI => gUIs.FirstOrDefault(gui => gui.IsActive); @@ -23,19 +24,18 @@ namespace CalamityMod.UI } } - public static void LoadGUIs() + public override void OnModLoad() { // Look through every type in the mod, and check if it's derived from InvasionProgressUI. If it is, create a copy and save it in the static list. - Type[] types = AssemblyManager.GetLoadableTypes(CalamityMod.Instance.Code); - foreach (Type type in types) + ReflectionHelper.IterateCalamityModTypes(includeBaseType: false, type => { - // Don't load abstract classes since they cannot have instances. - if (type.IsAbstract) - continue; - if (type.IsSubclassOf(typeof(InvasionProgressUI))) - gUIs.Add(Activator.CreateInstance(type) as InvasionProgressUI); - } + gUIs.Add(Activator.CreateInstance(type) as InvasionProgressUI); + }); + } + + public override void Unload() + { + gUIs?.Clear(); } - public static void UnloadGUIs() => gUIs.Clear(); } }