using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Terraria.ModLoader; namespace CalamityMod.Cooldowns { public sealed class CooldownRegistry : ModSystem { // 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 = new Cooldown[defaultSize]; private const ushort defaultSize = 256; private static ushort nextCDNetID = 0; private static Dictionary nameToNetID = new Dictionary(defaultSize); #region Unused manual registration left here as backup /*public override void Load() { // Vanilla cooldowns represented by the interface //var potionSickness = Register(PotionSickness.ID); //var chaosState = Register(ChaosState.ID); //var globalDodge = Register(GlobalDodge.ID); // Calamity cooldowns //var aquaticHeartIceShield = Register(AquaticHeartIceShield.ID); //var bloodflareFrenzy = Register(BloodflareFrenzy.ID); //var bloodflareRanged = Register(BloodflareRangedSet.ID); //var brimflameFrenzy = Register(BrimflameFrenzy.ID); //var counterScarf = Register(CounterScarf.ID); //var divineBless = Register(DivineBless.ID); //var divingPlatesBreaking = Register(DivingPlatesBreaking.ID); //var divingPlatesBroken = Register(DivingPlatesBroken.ID); //var draconicElixir = Register(DraconicElixir.ID); //var evasionScarf = Register(EvasionScarf.ID); //var fleshTotem = Register(FleshTotem.ID); //var godSlayerDash = Register(GodSlayerDash.ID); //var inkBomb = Register(InkBomb.ID); //var lionHeartShield = Register(LionHeartShield.ID); //var nebulousCore = Register(NebulousCore.ID); //var omegaBlue = Register(OmegaBlue.ID); //var permafrostConcoction = Register(PermafrostConcoction.ID); //var plagueBlackout = Register(PlagueBlackout.ID); //var prismaticLaser = Register(PrismaticLaser.ID); //var profanedSoulArtifact = Register(ProfanedSoulShield.ID); //var relicOfResilience = Register(RelicOfResilience.ID); //var rogueBooster = Register(RogueBooster.ID); //var sandCloak = Register(SandCloak.ID); //var silvaRevive = Register(SilvaRevive.ID); //var tarragonCloak = Register(TarragonCloak.ID); //var tarragonImmunity = Register(TarragonImmunity.ID); //var universeSplitter = Register(UniverseSplitter.ID); }*/ #endregion public override void ResizeArrays() { 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) { var type = cooldown.GetType(); //Get the static property ID of the handler string handlerID = (string)type.GetProperty("ID").GetValue(null); //If for whatever reason the ID is not set, create an ID from the mod and handler name handlerID ??= cooldown.FullName; //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]); } } public override void Unload() { registry = null; nameToNetID?.Clear(); nameToNetID = null; } public static Cooldown Get(string id) { bool hasValue = nameToNetID.TryGetValue(id, out ushort netID); return hasValue ? registry[netID] : null; } /// /// Registers a CooldownHandler for use in netcode, assigning it a Cooldown and thus a netID. Cooldowns are useless until this has been done. /// /// The registered Cooldown. public static Cooldown Register(string id) where HandlerT : CooldownHandler { int currentMaxID = registry.Length; // This case only happens when you cap out at 65,536 cooldown registrations (which should never occur). // It just stops you from registering more cooldowns. if (nextCDNetID == currentMaxID) return null; Cooldown cd = new Cooldown(id, nextCDNetID); nameToNetID[cd.ID] = cd.netID; 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) { Cooldown[] largerArray = new Cooldown[currentMaxID * 2]; for (int i = 0; i < currentMaxID; ++i) largerArray[i] = registry[i]; registry = largerArray; } return cd; } } }