CLre/CLre_server/API/Utility/Reflection.cs

119 lines
4.9 KiB
C#
Raw Normal View History

using System;
using System.Reflection;
using GameNetworkLayer.Shared;
using HarmonyLib;
using NetworkFramework.Shared;
using Svelto.DataStructures;
using Svelto.ECS;
namespace CLre_server.API.Utility
{
public static class Reflection
{
// useful function & method prototypes
public delegate T Getter<T>();
public delegate bool ExistsV1(EGID egid);
public delegate bool ExistsV2(int id, int groupid);
public delegate T QueryEntityViewV1<T>(EGID egid);
public delegate T QueryEntityViewV2<T>(int id, ExclusiveGroup.ExclusiveGroupStruct groupid);
public delegate ReadOnlyCollectionStruct<T> QueryEntityViews<T>(int group) where T : class, IEntityViewStruct;
public delegate T[] QueryEntitiesV2<T>(int group, out int count) where T : IEntityStruct;
public delegate T[] QueryEntitiesV1<T>(ExclusiveGroup.ExclusiveGroupStruct group, out int count) where T : IEntityStruct;
public delegate object[] SendMessage<T>(NetworkDispatcherCode dispatcherCode, ref T value) where T : struct, ISerializedNetData;
// useful reflection functions
public static TFuncProto BuildDelegate<TFuncProto>(MethodInfo method) where TFuncProto : Delegate
{
return (TFuncProto) Delegate.CreateDelegate(typeof(TFuncProto), method);
}
public static Delegate BuildDelegateRaw(MethodInfo method, Type TFuncProto)
{
return Delegate.CreateDelegate(TFuncProto, method);
}
public static TFuncProto BuildDelegate<TFuncProto>(MethodInfo method, object instance) where TFuncProto : Delegate
{
return (TFuncProto) Delegate.CreateDelegate(typeof(TFuncProto), instance, method, true);
}
public static Delegate BuildDelegateRaw(MethodInfo method, object instance, Type TFuncProto)
{
return Delegate.CreateDelegate(TFuncProto, instance, method, true);
}
public static TFuncProto MethodAsDelegate<TFuncProto>(Type class_, string methodName, Type[] parameters = null, Type[] generics = null, object instance = null) where TFuncProto : Delegate
{
MethodInfo method = AccessTools.Method(class_, methodName, parameters, generics);
if (instance != null)
{
return BuildDelegate<TFuncProto>(method, instance);
}
return BuildDelegate<TFuncProto>(method);
}
public static Delegate MethodAsDelegateRaw(Type class_, Type TFuncProto, string methodName, Type[] parameters = null, Type[] generics = null, object instance = null)
{
MethodInfo method = AccessTools.Method(class_, methodName, parameters, generics);
if (instance != null)
{
return BuildDelegateRaw(method, instance, TFuncProto);
}
return BuildDelegateRaw(method, TFuncProto);
}
public static TFuncProto MethodAsDelegate<TFuncProto>(string typeColonName, Type[] parameters = null, Type[] generics = null, object instance = null) where TFuncProto : Delegate
{
MethodInfo method = AccessTools.Method(typeColonName, parameters, generics);
if (instance != null)
{
return BuildDelegate<TFuncProto>(method, instance);
}
return BuildDelegate<TFuncProto>(method);
}
public static Delegate MethodAsDelegateRaw(string typeColonName, Type TFuncProto, Type[] parameters = null, Type[] generics = null, object instance = null)
{
MethodInfo method = AccessTools.Method(typeColonName, parameters, generics);
if (instance != null)
{
return BuildDelegateRaw(method, instance, TFuncProto);
}
return BuildDelegateRaw(method, TFuncProto);
}
public static PropertyInfo GetIndexer(this Type type, Type[] arguments = null, BindingFlags bindingFlags = BindingFlags.GetField | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.SetField | BindingFlags.SetProperty | BindingFlags.Static)
{
foreach (PropertyInfo p in type.GetProperties(bindingFlags))
{
if (arguments == null && p.GetIndexParameters().Length != 0) return p;
if (arguments != null)
{
uint count = 0;
foreach (ParameterInfo param in p.GetIndexParameters())
{
if (param.ParameterType != arguments[count])
{
break;
}
count++;
}
if (count == arguments.Length)
{
return p;
}
}
}
return null;
}
}
}