Compare commits

...

3 Commits

Author SHA1 Message Date
DCFApixels
f50021007b up version to 0.9.18 2025-03-29 16:14:54 +08:00
DCFApixels
6ab2c8321a fix 2025-03-24 19:30:52 +08:00
DCFApixels
69075891af reflection optimization 2025-03-17 11:07:06 +08:00
4 changed files with 50 additions and 49 deletions

View File

@ -8,7 +8,7 @@
"displayName": "DragonECS-AutoInjections", "displayName": "DragonECS-AutoInjections",
"description": "Auto Injections for DragonECS", "description": "Auto Injections for DragonECS",
"unity": "2020.3", "unity": "2020.3",
"version": "0.9.17", "version": "0.9.18",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/DCFApixels/DragonECS-AutoInjections.git" "url": "https://github.com/DCFApixels/DragonECS-AutoInjections.git"

View File

@ -24,26 +24,19 @@ namespace DCFApixels.DragonECS
private static readonly MethodInfo _incluedMethod; private static readonly MethodInfo _incluedMethod;
private static readonly MethodInfo _excludeMethod; private static readonly MethodInfo _excludeMethod;
private static readonly MethodInfo _optionalMethod; private static readonly MethodInfo _optionalMethod;
private static readonly MethodInfo _includeImplicitMethod;
private static readonly MethodInfo _excludeImplicitMethod;
private static readonly MethodInfo _combineMethod; private static readonly MethodInfo _combineMethod;
private const BindingFlags REFL_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
static EcsAspectAutoHelper() static EcsAspectAutoHelper()
{ {
const BindingFlags REFL_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
Type builderType = typeof(EcsAspect.Builder); Type builderType = typeof(EcsAspect.Builder);
_incluedMethod = builderType.GetMethod("IncludePool", REFL_FLAGS); _incluedMethod = builderType.GetMethod("IncludePool", REFL_FLAGS);
_excludeMethod = builderType.GetMethod("ExcludePool", REFL_FLAGS); _excludeMethod = builderType.GetMethod("ExcludePool", REFL_FLAGS);
_optionalMethod = builderType.GetMethod("OptionalPool", 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); _combineMethod = builderType.GetMethod("Combine", REFL_FLAGS);
} }
public static void FillMaskFields(object aspect, EcsMask mask) 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)) foreach (FieldInfo fieldInfo in aspect.GetType().GetFields(REFL_FLAGS))
{ {
if (fieldInfo.GetCustomAttribute<MaskAttribute>() == null) if (fieldInfo.GetCustomAttribute<MaskAttribute>() == null)
@ -63,9 +56,6 @@ namespace DCFApixels.DragonECS
} }
public static void FillFields(object aspect, EcsAspect.Builder builder) public static void FillFields(object aspect, EcsAspect.Builder builder)
{ {
const BindingFlags REFL_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
Type aspectType = aspect.GetType(); Type aspectType = aspect.GetType();
var implicitInjectAttributes = (IEnumerable<ImplicitInjectAttribute>)aspectType.GetCustomAttributes<ImplicitInjectAttribute>(); var implicitInjectAttributes = (IEnumerable<ImplicitInjectAttribute>)aspectType.GetCustomAttributes<ImplicitInjectAttribute>();
@ -81,24 +71,47 @@ namespace DCFApixels.DragonECS
{ {
continue; continue;
} }
IEcsPool pool;
switch (injectAttribute) switch (injectAttribute)
{ {
case IncAttribute atr: case IncAttribute incAtr:
var x1 = _incluedMethod; if (builder.World.TryFindPoolInstance(fieldType, out pool))
fieldInfo.SetValue(aspect, _incluedMethod.MakeGenericMethod(fieldType).Invoke(builder, null)); {
builder.SetMaskInclude(fieldType);
fieldInfo.SetValue(aspect, pool);
}
else
{
pool = (IEcsPool)_incluedMethod.MakeGenericMethod(fieldType).Invoke(builder, null);
}
fieldInfo.SetValue(aspect, pool);
break; break;
case ExcAttribute atr: case ExcAttribute extAtr:
var x2 = _excludeMethod; if (builder.World.TryFindPoolInstance(fieldType, out pool))
fieldInfo.SetValue(aspect, _excludeMethod.MakeGenericMethod(fieldType).Invoke(builder, null)); {
builder.SetMaskExclude(fieldType);
fieldInfo.SetValue(aspect, pool);
}
else
{
pool = (IEcsPool)_excludeMethod.MakeGenericMethod(fieldType).Invoke(builder, null);
}
fieldInfo.SetValue(aspect, pool);
break; break;
case OptAttribute atr: case OptAttribute optAtr:
var x3 = _optionalMethod; if (builder.World.TryFindPoolInstance(fieldType, out pool))
fieldInfo.SetValue(aspect, _optionalMethod.MakeGenericMethod(fieldType).Invoke(builder, null)); {
fieldInfo.SetValue(aspect, pool);
}
else
{
pool = (IEcsPool)_optionalMethod.MakeGenericMethod(fieldType).Invoke(builder, null);
}
fieldInfo.SetValue(aspect, pool);
break; break;
case CombineAttribute atr: case CombineAttribute combineAtr:
var x4 = _combineMethod; pool = builder.World.FindPoolInstance(fieldType);
fieldInfo.SetValue(aspect, _combineMethod.MakeGenericMethod(fieldType).Invoke(builder, new object[] { atr.Order })); fieldInfo.SetValue(aspect, _combineMethod.MakeGenericMethod(fieldType).Invoke(builder, new object[] { combineAtr.Order }));
break; break;
default: default:
break; 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) foreach (var attribute in implicitInjectAttributes)
{ {
if (attribute is IncImplicitAttribute incImplicit) if (attribute is IncImplicitAttribute incImplicitAtr)
{ {
Inject(incImplicit, _incluedMethod, _includeImplicitMethod); builder.SetMaskInclude(incImplicitAtr.ComponentType);
continue; continue;
} }
if (attribute is ExcImplicitAttribute excImplicit) if (attribute is ExcImplicitAttribute excImplicitAtr)
{ {
Inject(excImplicit, _excludeMethod, _excludeImplicitMethod); builder.SetMaskExclude(excImplicitAtr.ComponentType);
continue; continue;
} }
} }

View File

@ -26,12 +26,10 @@ namespace DCFApixels.DragonECS
public abstract class ImplicitInjectAttribute : Attribute public abstract class ImplicitInjectAttribute : Attribute
{ {
public readonly Type Type; public readonly Type ComponentType;
public readonly bool IsPool; public ImplicitInjectAttribute(Type componentType)
public ImplicitInjectAttribute(Type type)
{ {
Type = type; ComponentType = componentType;
IsPool = type.GetInterfaces().Any(o => o == typeof(IEcsPoolImplementation));
} }
} }
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Class, Inherited = false, AllowMultiple = true)] [AttributeUsage(AttributeTargets.Field | AttributeTargets.Class, Inherited = false, AllowMultiple = true)]

View File

@ -101,10 +101,7 @@ namespace DCFApixels.DragonECS
private Type[] _relatedTypesBuffer; private Type[] _relatedTypesBuffer;
public void Inject(Type fieldType, object obj) public void Inject(Type fieldType, object obj)
{ {
if (_isPreInitInjectionComplete == false)
{
_notInjected.Remove(fieldType);
}
if (_relatedTypesBuffer == null || _relatedTypesBuffer.Length < _injectedTypeToPropertiesMap.Count) if (_relatedTypesBuffer == null || _relatedTypesBuffer.Length < _injectedTypeToPropertiesMap.Count)
{ {
_relatedTypesBuffer = new Type[_injectedTypeToPropertiesMap.Count]; _relatedTypesBuffer = new Type[_injectedTypeToPropertiesMap.Count];
@ -132,6 +129,10 @@ namespace DCFApixels.DragonECS
string propertyName = item.Attribute.NamedInjection; string propertyName = item.Attribute.NamedInjection;
if (string.IsNullOrEmpty(propertyName) || propertyName == name) if (string.IsNullOrEmpty(propertyName) || propertyName == name)
{ {
if (_isPreInitInjectionComplete == false)
{
_notInjected.Remove(item.property.PropertyType);
}
item.property.Inject(item.target, obj); item.property.Inject(item.target, obj);
} }
} }