using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Reflection.Emit; using Terraria.ModLoader; 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)); } public static bool IsSubclass(Type baseType, Type type, bool includeBaseType) { 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); } } public static void IterateEveryModsTypes(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)); foreach (var type in types) { action.Invoke(type); } } public 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); } } public static void IterateCalamityTypes(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)); foreach (var type in types) { action.Invoke(type); } } } }