2025-03-14 16:53:33 +08:00
|
|
|
|
#if DISABLE_DEBUG
|
|
|
|
|
|
#undef DEBUG
|
|
|
|
|
|
#endif
|
|
|
|
|
|
using DCFApixels.DragonECS.AutoInjections.Internal;
|
2024-03-07 03:34:00 +08:00
|
|
|
|
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
|
|
|
|
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 _combineMethod;
|
2025-03-17 11:07:06 +08:00
|
|
|
|
private const BindingFlags REFL_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
2025-03-13 13:38:58 +08:00
|
|
|
|
static EcsAspectAutoHelper()
|
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);
|
|
|
|
|
|
_combineMethod = builderType.GetMethod("Combine", REFL_FLAGS);
|
|
|
|
|
|
}
|
|
|
|
|
|
public static void FillMaskFields(object aspect, EcsMask mask)
|
|
|
|
|
|
{
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
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
|
|
|
|
}
|
2025-03-17 11:07:06 +08:00
|
|
|
|
IEcsPool pool;
|
2025-03-13 13:38:58 +08:00
|
|
|
|
switch (injectAttribute)
|
2023-05-07 00:50:23 +08:00
|
|
|
|
{
|
2025-03-17 11:07:06 +08:00
|
|
|
|
case IncAttribute incAtr:
|
|
|
|
|
|
if (builder.World.TryFindPoolInstance(fieldType, out pool))
|
|
|
|
|
|
{
|
|
|
|
|
|
builder.SetMaskInclude(fieldType);
|
|
|
|
|
|
fieldInfo.SetValue(aspect, pool);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
pool = (IEcsPool)_incluedMethod.MakeGenericMethod(fieldType).Invoke(builder, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
fieldInfo.SetValue(aspect, pool);
|
2025-03-13 13:38:58 +08:00
|
|
|
|
break;
|
2025-03-17 11:07:06 +08:00
|
|
|
|
case ExcAttribute extAtr:
|
|
|
|
|
|
if (builder.World.TryFindPoolInstance(fieldType, out pool))
|
|
|
|
|
|
{
|
|
|
|
|
|
builder.SetMaskExclude(fieldType);
|
|
|
|
|
|
fieldInfo.SetValue(aspect, pool);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
pool = (IEcsPool)_excludeMethod.MakeGenericMethod(fieldType).Invoke(builder, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
fieldInfo.SetValue(aspect, pool);
|
2025-03-13 13:38:58 +08:00
|
|
|
|
break;
|
2025-03-17 11:07:06 +08:00
|
|
|
|
case OptAttribute optAtr:
|
|
|
|
|
|
if (builder.World.TryFindPoolInstance(fieldType, out pool))
|
|
|
|
|
|
{
|
|
|
|
|
|
fieldInfo.SetValue(aspect, pool);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
pool = (IEcsPool)_optionalMethod.MakeGenericMethod(fieldType).Invoke(builder, null);
|
|
|
|
|
|
}
|
|
|
|
|
|
fieldInfo.SetValue(aspect, pool);
|
2025-03-13 13:38:58 +08:00
|
|
|
|
break;
|
2025-03-17 11:07:06 +08:00
|
|
|
|
case CombineAttribute combineAtr:
|
|
|
|
|
|
pool = builder.World.FindPoolInstance(fieldType);
|
|
|
|
|
|
fieldInfo.SetValue(aspect, _combineMethod.MakeGenericMethod(fieldType).Invoke(builder, new object[] { combineAtr.Order }));
|
2025-03-13 13:38:58 +08:00
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
2023-05-07 00:50:23 +08:00
|
|
|
|
}
|
2025-03-13 13:38:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var attribute in implicitInjectAttributes)
|
|
|
|
|
|
{
|
2025-03-17 11:07:06 +08:00
|
|
|
|
if (attribute is IncImplicitAttribute incImplicitAtr)
|
2025-03-13 13:38:58 +08:00
|
|
|
|
{
|
2025-03-17 11:07:06 +08:00
|
|
|
|
builder.SetMaskInclude(incImplicitAtr.ComponentType);
|
2023-05-07 00:50:23 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2025-03-17 11:07:06 +08:00
|
|
|
|
if (attribute is ExcImplicitAttribute excImplicitAtr)
|
2023-06-05 23:45:25 +08:00
|
|
|
|
{
|
2025-03-17 11:07:06 +08:00
|
|
|
|
builder.SetMaskExclude(excImplicitAtr.ComponentType);
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|