2024-03-07 03:34:00 +08:00
|
|
|
|
using DCFApixels.DragonECS.AutoInjections.Internal;
|
|
|
|
|
|
using System;
|
2025-03-13 13:38:58 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2023-05-07 00:50:23 +08:00
|
|
|
|
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)
|
|
|
|
|
|
{
|
2025-03-13 13:38:58 +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
|
|
|
|
{
|
2025-03-13 13:38:58 +08:00
|
|
|
|
private static readonly MethodInfo _incluedMethod;
|
|
|
|
|
|
private static readonly MethodInfo _excludeMethod;
|
|
|
|
|
|
private static readonly MethodInfo _optionalMethod;
|
|
|
|
|
|
private static readonly MethodInfo _includeImplicitMethod;
|
|
|
|
|
|
private static readonly MethodInfo _excludeImplicitMethod;
|
|
|
|
|
|
private static readonly MethodInfo _combineMethod;
|
|
|
|
|
|
static EcsAspectAutoHelper()
|
2023-05-07 00:50:23 +08:00
|
|
|
|
{
|
2025-03-13 13:38:58 +08:00
|
|
|
|
const BindingFlags REFL_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
2023-05-07 00:50:23 +08:00
|
|
|
|
|
2025-03-13 13:38:58 +08:00
|
|
|
|
Type builderType = typeof(EcsAspect.Builder);
|
2023-05-07 00:50:23 +08:00
|
|
|
|
|
2025-03-13 13:38:58 +08:00
|
|
|
|
_incluedMethod = builderType.GetMethod("IncludePool", REFL_FLAGS);
|
|
|
|
|
|
_excludeMethod = builderType.GetMethod("ExcludePool", REFL_FLAGS);
|
|
|
|
|
|
_optionalMethod = builderType.GetMethod("OptionalPool", REFL_FLAGS);
|
|
|
|
|
|
_includeImplicitMethod = builderType.GetMethod("IncludeImplicit", REFL_FLAGS);
|
|
|
|
|
|
_excludeImplicitMethod = builderType.GetMethod("ExcludeImplicit", REFL_FLAGS);
|
|
|
|
|
|
_combineMethod = builderType.GetMethod("Combine", REFL_FLAGS);
|
|
|
|
|
|
}
|
|
|
|
|
|
public static void FillMaskFields(object aspect, EcsMask mask)
|
|
|
|
|
|
{
|
|
|
|
|
|
const BindingFlags REFL_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
|
|
|
|
|
|
|
|
|
|
|
foreach (FieldInfo fieldInfo in aspect.GetType().GetFields(REFL_FLAGS))
|
2023-05-07 00:50:23 +08:00
|
|
|
|
{
|
2025-03-13 13:38:58 +08:00
|
|
|
|
if (fieldInfo.GetCustomAttribute<MaskAttribute>() == null)
|
2023-05-07 00:50:23 +08:00
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2025-03-13 13:38:58 +08:00
|
|
|
|
|
|
|
|
|
|
if (fieldInfo.FieldType == typeof(EcsMask))
|
2023-05-07 00:50:23 +08:00
|
|
|
|
{
|
2025-03-13 13:38:58 +08:00
|
|
|
|
fieldInfo.SetValue(aspect, mask);
|
2023-05-07 00:50:23 +08:00
|
|
|
|
}
|
2025-03-13 13:38:58 +08:00
|
|
|
|
else if (fieldInfo.FieldType == typeof(EcsStaticMask))
|
|
|
|
|
|
{
|
|
|
|
|
|
fieldInfo.SetValue(aspect, mask.ToStatic());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public static void FillFields(object aspect, EcsAspect.Builder builder)
|
|
|
|
|
|
{
|
|
|
|
|
|
const BindingFlags REFL_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
2023-05-07 00:50:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-03-13 13:38:58 +08:00
|
|
|
|
Type aspectType = aspect.GetType();
|
|
|
|
|
|
|
|
|
|
|
|
var implicitInjectAttributes = (IEnumerable<ImplicitInjectAttribute>)aspectType.GetCustomAttributes<ImplicitInjectAttribute>();
|
|
|
|
|
|
|
|
|
|
|
|
FieldInfo[] fieldInfos = aspectType.GetFields(REFL_FLAGS);
|
2023-05-07 00:50:23 +08:00
|
|
|
|
foreach (FieldInfo fieldInfo in fieldInfos)
|
|
|
|
|
|
{
|
|
|
|
|
|
Type fieldType = fieldInfo.FieldType;
|
|
|
|
|
|
|
2025-03-13 13:38:58 +08:00
|
|
|
|
implicitInjectAttributes = implicitInjectAttributes.Concat(fieldInfo.GetCustomAttributes<ImplicitInjectAttribute>());
|
|
|
|
|
|
|
|
|
|
|
|
if (fieldInfo.TryGetCustomAttribute(out InjectAspectMemberAttribute injectAttribute) == false)
|
2024-03-07 03:34:00 +08:00
|
|
|
|
{
|
2023-05-07 00:50:23 +08:00
|
|
|
|
continue;
|
2024-03-07 03:34:00 +08:00
|
|
|
|
}
|
2023-05-07 00:50:23 +08:00
|
|
|
|
|
2025-03-13 13:38:58 +08:00
|
|
|
|
switch (injectAttribute)
|
2023-05-07 00:50:23 +08:00
|
|
|
|
{
|
2025-03-13 13:38:58 +08:00
|
|
|
|
case IncAttribute atr:
|
|
|
|
|
|
var x1 = _incluedMethod;
|
|
|
|
|
|
fieldInfo.SetValue(aspect, _incluedMethod.MakeGenericMethod(fieldType).Invoke(builder, null));
|
|
|
|
|
|
break;
|
|
|
|
|
|
case ExcAttribute atr:
|
|
|
|
|
|
var x2 = _excludeMethod;
|
|
|
|
|
|
fieldInfo.SetValue(aspect, _excludeMethod.MakeGenericMethod(fieldType).Invoke(builder, null));
|
|
|
|
|
|
break;
|
|
|
|
|
|
case OptAttribute atr:
|
|
|
|
|
|
var x3 = _optionalMethod;
|
|
|
|
|
|
fieldInfo.SetValue(aspect, _optionalMethod.MakeGenericMethod(fieldType).Invoke(builder, null));
|
|
|
|
|
|
break;
|
|
|
|
|
|
case CombineAttribute atr:
|
|
|
|
|
|
var x4 = _combineMethod;
|
|
|
|
|
|
fieldInfo.SetValue(aspect, _combineMethod.MakeGenericMethod(fieldType).Invoke(builder, new object[] { atr.Order }));
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
2023-05-07 00:50:23 +08:00
|
|
|
|
}
|
2025-03-13 13:38:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Inject(ImplicitInjectAttribute atr_, MethodInfo method_, MethodInfo implicitMethod_)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (atr_.IsPool)
|
2023-05-07 00:50:23 +08:00
|
|
|
|
{
|
2025-03-13 13:38:58 +08:00
|
|
|
|
method_.MakeGenericMethod(atr_.Type).Invoke(builder, null);
|
2023-05-07 00:50:23 +08:00
|
|
|
|
}
|
2025-03-13 13:38:58 +08:00
|
|
|
|
else
|
2023-05-07 00:50:23 +08:00
|
|
|
|
{
|
2025-03-13 13:38:58 +08:00
|
|
|
|
implicitMethod_.Invoke(builder, new object[] { atr_.Type });
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
foreach (var attribute in implicitInjectAttributes)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (attribute is IncImplicitAttribute incImplicit)
|
|
|
|
|
|
{
|
|
|
|
|
|
Inject(incImplicit, _incluedMethod, _includeImplicitMethod);
|
2023-05-07 00:50:23 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2025-03-13 13:38:58 +08:00
|
|
|
|
if (attribute is ExcImplicitAttribute excImplicit)
|
2023-06-05 23:45:25 +08:00
|
|
|
|
{
|
2025-03-13 13:38:58 +08:00
|
|
|
|
Inject(excImplicit, _excludeMethod, _excludeImplicitMethod);
|
2023-06-05 23:45:25 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2023-05-07 00:50:23 +08:00
|
|
|
|
}
|
2025-03-13 13:38:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
2023-05-07 00:50:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|