2023-05-07 00:50:23 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
|
{
|
2023-06-22 14:30:53 +08:00
|
|
|
|
public abstract class EcsAspectAuto : EcsAspect
|
2023-05-07 00:50:23 +08:00
|
|
|
|
{
|
2023-05-23 01:48:00 +08:00
|
|
|
|
protected sealed override void Init(Builder b)
|
|
|
|
|
{
|
2023-06-22 14:30:53 +08:00
|
|
|
|
EcsAspectAutoHelper.Fill(this, b);
|
2023-05-23 01:48:00 +08:00
|
|
|
|
InitAfterDI(b);
|
|
|
|
|
}
|
|
|
|
|
protected virtual void InitAfterDI(Builder b) { }
|
2023-05-07 00:50:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-22 14:30:53 +08:00
|
|
|
|
internal static class EcsAspectAutoHelper
|
2023-05-07 00:50:23 +08:00
|
|
|
|
{
|
2023-06-22 14:30:53 +08:00
|
|
|
|
public static void Fill(EcsAspect s, EcsAspect.Builder b)
|
2023-05-07 00:50:23 +08:00
|
|
|
|
{
|
|
|
|
|
Type builderType = b.GetType();
|
|
|
|
|
MethodInfo incluedMethod = builderType.GetMethod("Include", BindingFlags.Instance | BindingFlags.Public);
|
|
|
|
|
MethodInfo excludeMethod = builderType.GetMethod("Exclude", BindingFlags.Instance | BindingFlags.Public);
|
2023-06-04 19:42:46 +08:00
|
|
|
|
MethodInfo optionalMethod = builderType.GetMethod("Optional", BindingFlags.Instance | BindingFlags.Public);
|
2023-05-28 22:29:24 +08:00
|
|
|
|
MethodInfo includeImplicitMethod = builderType.GetMethod("IncludeImplicit", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
|
|
|
|
MethodInfo excludeImplicitMethod = builderType.GetMethod("ExcludeImplicit", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
2023-06-05 23:45:25 +08:00
|
|
|
|
MethodInfo combineMethod = builderType.GetMethod("Combine", BindingFlags.Instance | BindingFlags.Public);
|
2023-05-07 00:50:23 +08:00
|
|
|
|
|
2023-06-22 14:30:53 +08:00
|
|
|
|
Type aspectType = s.GetType();
|
2023-05-07 00:50:23 +08:00
|
|
|
|
|
2023-06-22 14:30:53 +08:00
|
|
|
|
foreach (var attribute in aspectType.GetCustomAttributes<ImplicitInjectAttribute>())//TODO убрать дублирование кода - вынести в отедльный метод
|
2023-05-07 00:50:23 +08:00
|
|
|
|
{
|
|
|
|
|
if (attribute is IncImplicitAttribute incImplicit)
|
|
|
|
|
{
|
|
|
|
|
if (incImplicit.isPool)
|
2023-06-04 19:04:22 +08:00
|
|
|
|
incluedMethod.MakeGenericMethod(incImplicit.type).Invoke(b, null);
|
2023-05-07 00:50:23 +08:00
|
|
|
|
else
|
2023-06-04 19:04:22 +08:00
|
|
|
|
includeImplicitMethod.Invoke(b, new object[] { incImplicit.type });
|
2023-05-07 00:50:23 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (attribute is ExcImplicitAttribute excImplicit)
|
|
|
|
|
{
|
|
|
|
|
if (excImplicit.isPool)
|
2023-06-04 19:04:22 +08:00
|
|
|
|
excludeMethod.MakeGenericMethod(excImplicit.type).Invoke(b, null);
|
2023-05-07 00:50:23 +08:00
|
|
|
|
else
|
2023-06-04 19:04:22 +08:00
|
|
|
|
excludeImplicitMethod.Invoke(b, new object[] { excImplicit.type });
|
2023-05-07 00:50:23 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}//TODO КОНЕЦ убрать дублирование кода - вынести в отедльный метод
|
|
|
|
|
|
|
|
|
|
|
2023-06-22 14:30:53 +08:00
|
|
|
|
FieldInfo[] fieldInfos = aspectType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
2023-05-07 00:50:23 +08:00
|
|
|
|
foreach (FieldInfo fieldInfo in fieldInfos)
|
|
|
|
|
{
|
|
|
|
|
Type fieldType = fieldInfo.FieldType;
|
|
|
|
|
|
|
|
|
|
foreach (var attribute in fieldInfo.GetCustomAttributes<ImplicitInjectAttribute>())//TODO убрать дублирование кода - вынести в отедльный метод
|
|
|
|
|
{
|
2023-06-05 23:45:25 +08:00
|
|
|
|
if (attribute is IncImplicitAttribute incImplicit)
|
2023-05-07 00:50:23 +08:00
|
|
|
|
{
|
2023-06-05 23:45:25 +08:00
|
|
|
|
if (incImplicit.isPool)
|
2023-06-04 19:04:22 +08:00
|
|
|
|
incluedMethod.MakeGenericMethod(incImplicit.type).Invoke(b, null);
|
2023-05-07 00:50:23 +08:00
|
|
|
|
else
|
2023-06-04 19:04:22 +08:00
|
|
|
|
includeImplicitMethod.Invoke(b, new object[] { incImplicit.type });
|
2023-05-07 00:50:23 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (attribute is ExcImplicitAttribute excImplicit)
|
|
|
|
|
{
|
|
|
|
|
if (excImplicit.isPool)
|
2023-06-04 19:04:22 +08:00
|
|
|
|
excludeMethod.MakeGenericMethod(excImplicit.type).Invoke(b, null);
|
2023-05-07 00:50:23 +08:00
|
|
|
|
else
|
2023-06-04 19:04:22 +08:00
|
|
|
|
excludeImplicitMethod.Invoke(b, new object[] { excImplicit.type });
|
2023-05-07 00:50:23 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}//TODO КОНЕЦ убрать дублирование кода - вынести в отедльный метод
|
|
|
|
|
|
2023-12-24 18:17:43 +08:00
|
|
|
|
if (!fieldInfo.TryGetCustomAttribute(out InjectAttribute injectAttribute))
|
2023-05-07 00:50:23 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2023-06-05 23:45:25 +08:00
|
|
|
|
if (injectAttribute is IncAttribute)
|
2023-05-07 00:50:23 +08:00
|
|
|
|
{
|
2023-06-04 19:04:22 +08:00
|
|
|
|
fieldInfo.SetValue(s, incluedMethod.MakeGenericMethod(fieldType).Invoke(b, null));
|
2023-05-07 00:50:23 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-06-05 23:45:25 +08:00
|
|
|
|
if (injectAttribute is ExcAttribute)
|
2023-05-07 00:50:23 +08:00
|
|
|
|
{
|
2023-06-04 19:04:22 +08:00
|
|
|
|
fieldInfo.SetValue(s, excludeMethod.MakeGenericMethod(fieldType).Invoke(b, null));
|
2023-05-07 00:50:23 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-06-05 23:45:25 +08:00
|
|
|
|
if (injectAttribute is OptAttribute)
|
2023-05-07 00:50:23 +08:00
|
|
|
|
{
|
2023-06-04 19:04:22 +08:00
|
|
|
|
fieldInfo.SetValue(s, optionalMethod.MakeGenericMethod(fieldType).Invoke(b, null));
|
2023-05-07 00:50:23 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-06-05 23:45:25 +08:00
|
|
|
|
if (injectAttribute is CombineAttribute combAttribute)
|
|
|
|
|
{
|
|
|
|
|
fieldInfo.SetValue(s, combineMethod.MakeGenericMethod(fieldType).Invoke(b, new object[] { combAttribute.order }));
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-05-07 00:50:23 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|