mirror of
https://github.com/DCFApixels/DragonECS-AutoInjections.git
synced 2025-09-17 20:34:34 +08:00
Compare commits
3 Commits
443a69615a
...
f50021007b
Author | SHA1 | Date | |
---|---|---|---|
![]() |
f50021007b | ||
![]() |
6ab2c8321a | ||
![]() |
69075891af |
@ -8,7 +8,7 @@
|
||||
"displayName": "DragonECS-AutoInjections",
|
||||
"description": "Auto Injections for DragonECS",
|
||||
"unity": "2020.3",
|
||||
"version": "0.9.17",
|
||||
"version": "0.9.18",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/DCFApixels/DragonECS-AutoInjections.git"
|
||||
|
@ -24,26 +24,19 @@ namespace DCFApixels.DragonECS
|
||||
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;
|
||||
private const BindingFlags REFL_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||
static EcsAspectAutoHelper()
|
||||
{
|
||||
const BindingFlags REFL_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||
|
||||
Type builderType = typeof(EcsAspect.Builder);
|
||||
|
||||
_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))
|
||||
{
|
||||
if (fieldInfo.GetCustomAttribute<MaskAttribute>() == null)
|
||||
@ -63,9 +56,6 @@ namespace DCFApixels.DragonECS
|
||||
}
|
||||
public static void FillFields(object aspect, EcsAspect.Builder builder)
|
||||
{
|
||||
const BindingFlags REFL_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
|
||||
|
||||
|
||||
Type aspectType = aspect.GetType();
|
||||
|
||||
var implicitInjectAttributes = (IEnumerable<ImplicitInjectAttribute>)aspectType.GetCustomAttributes<ImplicitInjectAttribute>();
|
||||
@ -81,24 +71,47 @@ namespace DCFApixels.DragonECS
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
IEcsPool pool;
|
||||
switch (injectAttribute)
|
||||
{
|
||||
case IncAttribute atr:
|
||||
var x1 = _incluedMethod;
|
||||
fieldInfo.SetValue(aspect, _incluedMethod.MakeGenericMethod(fieldType).Invoke(builder, null));
|
||||
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);
|
||||
break;
|
||||
case ExcAttribute atr:
|
||||
var x2 = _excludeMethod;
|
||||
fieldInfo.SetValue(aspect, _excludeMethod.MakeGenericMethod(fieldType).Invoke(builder, null));
|
||||
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);
|
||||
break;
|
||||
case OptAttribute atr:
|
||||
var x3 = _optionalMethod;
|
||||
fieldInfo.SetValue(aspect, _optionalMethod.MakeGenericMethod(fieldType).Invoke(builder, null));
|
||||
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);
|
||||
break;
|
||||
case CombineAttribute atr:
|
||||
var x4 = _combineMethod;
|
||||
fieldInfo.SetValue(aspect, _combineMethod.MakeGenericMethod(fieldType).Invoke(builder, new object[] { atr.Order }));
|
||||
case CombineAttribute combineAtr:
|
||||
pool = builder.World.FindPoolInstance(fieldType);
|
||||
fieldInfo.SetValue(aspect, _combineMethod.MakeGenericMethod(fieldType).Invoke(builder, new object[] { combineAtr.Order }));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -106,27 +119,16 @@ namespace DCFApixels.DragonECS
|
||||
}
|
||||
|
||||
|
||||
void Inject(ImplicitInjectAttribute atr_, MethodInfo method_, MethodInfo implicitMethod_)
|
||||
{
|
||||
if (atr_.IsPool)
|
||||
{
|
||||
method_.MakeGenericMethod(atr_.Type).Invoke(builder, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
implicitMethod_.Invoke(builder, new object[] { atr_.Type });
|
||||
}
|
||||
}
|
||||
foreach (var attribute in implicitInjectAttributes)
|
||||
{
|
||||
if (attribute is IncImplicitAttribute incImplicit)
|
||||
if (attribute is IncImplicitAttribute incImplicitAtr)
|
||||
{
|
||||
Inject(incImplicit, _incluedMethod, _includeImplicitMethod);
|
||||
builder.SetMaskInclude(incImplicitAtr.ComponentType);
|
||||
continue;
|
||||
}
|
||||
if (attribute is ExcImplicitAttribute excImplicit)
|
||||
if (attribute is ExcImplicitAttribute excImplicitAtr)
|
||||
{
|
||||
Inject(excImplicit, _excludeMethod, _excludeImplicitMethod);
|
||||
builder.SetMaskExclude(excImplicitAtr.ComponentType);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
@ -26,12 +26,10 @@ namespace DCFApixels.DragonECS
|
||||
|
||||
public abstract class ImplicitInjectAttribute : Attribute
|
||||
{
|
||||
public readonly Type Type;
|
||||
public readonly bool IsPool;
|
||||
public ImplicitInjectAttribute(Type type)
|
||||
public readonly Type ComponentType;
|
||||
public ImplicitInjectAttribute(Type componentType)
|
||||
{
|
||||
Type = type;
|
||||
IsPool = type.GetInterfaces().Any(o => o == typeof(IEcsPoolImplementation));
|
||||
ComponentType = componentType;
|
||||
}
|
||||
}
|
||||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
|
||||
|
@ -101,10 +101,7 @@ namespace DCFApixels.DragonECS
|
||||
private Type[] _relatedTypesBuffer;
|
||||
public void Inject(Type fieldType, object obj)
|
||||
{
|
||||
if (_isPreInitInjectionComplete == false)
|
||||
{
|
||||
_notInjected.Remove(fieldType);
|
||||
}
|
||||
|
||||
if (_relatedTypesBuffer == null || _relatedTypesBuffer.Length < _injectedTypeToPropertiesMap.Count)
|
||||
{
|
||||
_relatedTypesBuffer = new Type[_injectedTypeToPropertiesMap.Count];
|
||||
@ -132,6 +129,10 @@ namespace DCFApixels.DragonECS
|
||||
string propertyName = item.Attribute.NamedInjection;
|
||||
if (string.IsNullOrEmpty(propertyName) || propertyName == name)
|
||||
{
|
||||
if (_isPreInitInjectionComplete == false)
|
||||
{
|
||||
_notInjected.Remove(item.property.PropertyType);
|
||||
}
|
||||
item.property.Inject(item.target, obj);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user