diff --git a/Cooldowns/CooldownHandler.cs b/Cooldowns/CooldownHandler.cs index 9943d9d676..2b7ee32f27 100644 --- a/Cooldowns/CooldownHandler.cs +++ b/Cooldowns/CooldownHandler.cs @@ -9,11 +9,16 @@ using Terraria.ModLoader; namespace CalamityMod.Cooldowns { - public abstract class CooldownHandler + public abstract class CooldownHandler : ModType { public static string ID => null; public CooldownInstance instance; + protected sealed override void Register() + { + ModTypeLookup.Register(this); + } + #region Gameplay Behavior /// /// This method runs once every frame while the cooldown instance is active. diff --git a/Cooldowns/CooldownRegistry.cs b/Cooldowns/CooldownRegistry.cs index d74cf6cd11..b697488611 100644 --- a/Cooldowns/CooldownRegistry.cs +++ b/Cooldowns/CooldownRegistry.cs @@ -1,8 +1,8 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Reflection; using Terraria.ModLoader; -using Terraria.ModLoader.Core; namespace CalamityMod.Cooldowns { @@ -12,18 +12,16 @@ namespace CalamityMod.Cooldowns // Indexed by ushort netID. Contains every registered cooldown. // Cooldowns are given netIDs when they are registered. // Cooldowns are useless until they are registered. - public static Cooldown[] registry; + public static Cooldown[] registry = new Cooldown[defaultSize]; private const ushort defaultSize = 256; private static ushort nextCDNetID = 0; - private static Dictionary nameToNetID = null; + private static Dictionary nameToNetID = new Dictionary(defaultSize); - public override void Load() + #region Unused manual registration left here as backup + /*public override void Load() { - registry = new Cooldown[defaultSize]; - nameToNetID = new Dictionary(defaultSize); - - #region Unused manual registration left here as backup + // Vanilla cooldowns represented by the interface //var potionSickness = Register(PotionSickness.ID); //var chaosState = Register(ChaosState.ID); @@ -57,33 +55,30 @@ namespace CalamityMod.Cooldowns //var tarragonCloak = Register(TarragonCloak.ID); //var tarragonImmunity = Register(TarragonImmunity.ID); //var universeSplitter = Register(UniverseSplitter.ID); - #endregion - } + + }*/ + #endregion - public override void OnModLoad() + public override void ResizeArrays() { - Type baseHandlerType = typeof(CooldownHandler); - foreach (Mod mod in ModLoader.Mods) + var cooldowns = ModContent.GetContent(); + var count = cooldowns.Count(); + Array.Resize(ref registry, count); + + MethodInfo registerBaseMethod = typeof(CooldownRegistry).GetMethod(nameof(Register), BindingFlags.Public | BindingFlags.Static); + foreach (var cooldown in cooldowns) { - foreach (Type type in AssemblyManager.GetLoadableTypes(mod.Code)) - { - if (type.IsSubclassOf(baseHandlerType) && !type.IsAbstract && type != baseHandlerType) - { - //Get the static property ID of the handler - string handlerID = (string)type.GetProperty("ID").GetValue(null); + var type = cooldown.GetType(); - //If for whatever reason the ID is not set, create an ID from the mod and handler name - if (handlerID == null) - handlerID = mod.Name + "_" + type.Name; + //Get the static property ID of the handler + string handlerID = (string)type.GetProperty("ID").GetValue(null); - //Use reflection to call the method. You can't use the type as the generic type argument of register here - MethodInfo methodInfo = typeof(CooldownRegistry).GetMethod("Register", BindingFlags.Public | BindingFlags.Static); - Type[] genericArguments = new Type[] { type }; + //If for whatever reason the ID is not set, create an ID from the mod and handler name + handlerID ??= cooldown.FullName; - MethodInfo genericRegister = methodInfo.MakeGenericMethod(genericArguments); - genericRegister.Invoke(null, new object[] { handlerID }); - } - } + //Use reflection to call the method. You can't use the type as the generic type argument of register here + MethodInfo genericRegister = registerBaseMethod.MakeGenericMethod(typeArguments: [type]); + genericRegister.Invoke(null, [handlerID]); } } @@ -118,6 +113,7 @@ namespace CalamityMod.Cooldowns registry[cd.netID] = cd; ++nextCDNetID; + // TODO: Cooldown resizes are not really needed now. Leaving it here just in case. // If the end of the array is reached, double its size. if (nextCDNetID == currentMaxID && currentMaxID < ushort.MaxValue) {