diff --git a/Utilities/ReflectionHelper.cs b/Utilities/ReflectionHelper.cs index 36624b1899..b2ffe1acc7 100644 --- a/Utilities/ReflectionHelper.cs +++ b/Utilities/ReflectionHelper.cs @@ -8,56 +8,8 @@ using Terraria.ModLoader.Core; namespace CalamityMod { - public class FastField(FieldInfo fieldInfo) - { - private Action _Setter = ReflectionHelper.CreateFieldSetter(fieldInfo); - private Func _Getter = ReflectionHelper.CreateFieldGetter(fieldInfo); - - public FastField(string fieldName, BindingFlags flags) : this(typeof(T).GetField(fieldName, flags)) - { - - } - - public ValueType Get(T instance) - { - return _Getter(instance); - } - - public void Set(T instance, ValueType newValue) - { - _Setter(instance, newValue); - } - } - public static class ReflectionHelper { - public static Action CreateFieldSetter(FieldInfo fieldInfo) - { - string methodName = fieldInfo.ReflectedType.FullName + ".set_" + fieldInfo.Name; - DynamicMethod setterMethod = new DynamicMethod(methodName, null, [typeof(T), typeof(ValueType)], true); - ILGenerator gen = setterMethod.GetILGenerator(); - - gen.Emit(OpCodes.Ldarg_0); - gen.Emit(OpCodes.Ldarg_1); - gen.Emit(OpCodes.Stfld, fieldInfo); - gen.Emit(OpCodes.Ret); - - return (Action)setterMethod.CreateDelegate(typeof(Action)); - } - - public static Func CreateFieldGetter(FieldInfo fieldInfo) - { - string methodName = fieldInfo.ReflectedType.FullName + ".get_" + fieldInfo.Name; - DynamicMethod getterMethod = new DynamicMethod(methodName, typeof(ValueType), [typeof(T)], true); - ILGenerator gen = getterMethod.GetILGenerator(); - - gen.Emit(OpCodes.Ldarg_0); - gen.Emit(OpCodes.Ldfld, fieldInfo); - gen.Emit(OpCodes.Ret); - - return (Func)getterMethod.CreateDelegate(typeof(Func)); - } - public static IEnumerable GetEveryModsTypes() { return ModLoader.Mods.SelectMany(mod => AssemblyManager.GetLoadableTypes(mod.Code)); @@ -67,21 +19,8 @@ namespace CalamityMod { return type.IsSubclassOf(baseType) && !type.IsAbstract && (!includeBaseType && type != baseType); } - - public static void IterateEveryModsTypesSorted(bool includeBaseType = false, Action action = null) - { - // WHY???? - if (action is null) - return; - - Type baseType = typeof(T); - var types = GetEveryModsTypes().Where(t => IsSubclass(baseType, t, includeBaseType)).OrderBy(t => t.FullName); - foreach (var type in types) - { - action.Invoke(type); - } - } - + + // TODO: Consider replacing with ModType? public static void IterateEveryModsTypes(Action action, bool includeBaseType = false) { // WHY???? @@ -95,26 +34,14 @@ namespace CalamityMod action.Invoke(type); } } - - public static IEnumerable GetCalamityTypes() + + [Obsolete] + private static IEnumerable GetCalamityTypes() { return AssemblyManager.GetLoadableTypes(CalamityMod.Instance.Code); } - - public static void IterateCalamityTypesSorted(bool includeBaseType = false, Action action = null) - { - // WHY???? - if (action is null) - return; - - Type baseType = typeof(T); - var types = GetCalamityTypes().Where(t => IsSubclass(baseType, t, includeBaseType)).OrderBy(t => t.FullName); - foreach (var type in types) - { - action.Invoke(type); - } - } - + + [Obsolete("Please avoid using this; ModType and ILoadable could probably be used instead.")] public static void IterateCalamityTypes(bool includeBaseType = false, Action action = null) { // WHY????