Mark several reflection helpers obsolete
This commit is contained in:
1 parent
895d8c07d7
commit
5a102d443f
1 file changed
+7
-80
@@ -8,56 +8,8 @@ using Terraria.ModLoader.Core;
|
||||
|
||||
namespace CalamityMod
|
||||
{
|
||||
public class FastField<T, ValueType>(FieldInfo fieldInfo)
|
||||
{
|
||||
private Action<T, ValueType> _Setter = ReflectionHelper.CreateFieldSetter<T, ValueType>(fieldInfo);
|
||||
private Func<T, ValueType> _Getter = ReflectionHelper.CreateFieldGetter<T, ValueType>(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<T, ValueType> CreateFieldSetter<T, ValueType>(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<T, ValueType>)setterMethod.CreateDelegate(typeof(Action<T, ValueType>));
|
||||
}
|
||||
|
||||
public static Func<T, ValueType> CreateFieldGetter<T, ValueType>(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<T, ValueType>)getterMethod.CreateDelegate(typeof(Func<T, ValueType>));
|
||||
}
|
||||
|
||||
public static IEnumerable<Type> 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<T>(bool includeBaseType = false, Action<Type> 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<T>(Action<Type> action, bool includeBaseType = false)
|
||||
{
|
||||
// WHY????
|
||||
@@ -95,26 +34,14 @@ namespace CalamityMod
|
||||
action.Invoke(type);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<Type> GetCalamityTypes()
|
||||
|
||||
[Obsolete]
|
||||
private static IEnumerable<Type> GetCalamityTypes()
|
||||
{
|
||||
return AssemblyManager.GetLoadableTypes(CalamityMod.Instance.Code);
|
||||
}
|
||||
|
||||
public static void IterateCalamityTypesSorted<T>(bool includeBaseType = false, Action<Type> 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<T>(bool includeBaseType = false, Action<Type> action = null)
|
||||
{
|
||||
// WHY????
|
||||
|
||||
Reference in new issue
Block a user