From 39949bde2b2aebd5927a2a6a6ef66caac86658a3 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Fri, 21 Apr 2023 17:39:06 +0800 Subject: [PATCH] update --- src/Attributes/QueryAttributes.cs | 11 ++++++ src/EcsQuery/EcsQueryDI.cs | 56 ++++++++++++++++++++----------- 2 files changed, 47 insertions(+), 20 deletions(-) create mode 100644 src/Attributes/QueryAttributes.cs diff --git a/src/Attributes/QueryAttributes.cs b/src/Attributes/QueryAttributes.cs new file mode 100644 index 0000000..8f98e53 --- /dev/null +++ b/src/Attributes/QueryAttributes.cs @@ -0,0 +1,11 @@ +using System; + +namespace DCFApixels.DragonECS +{ + [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)] + public sealed class IncAttribute : Attribute { } + + [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)] + public sealed class ExcAttribute : Attribute { } +} + diff --git a/src/EcsQuery/EcsQueryDI.cs b/src/EcsQuery/EcsQueryDI.cs index e866b59..4b9354f 100644 --- a/src/EcsQuery/EcsQueryDI.cs +++ b/src/EcsQuery/EcsQueryDI.cs @@ -3,37 +3,53 @@ using System.Reflection; namespace DCFApixels.DragonECS { - public class EcsQueryDI :EcsQuery where TWorldArhetype : EcsWorld + public abstract class EcsJoinQueryDI : EcsJoinAttachQuery + where TAttachComponent : struct, IEcsAttachComponent { - protected override void Init(Builder b) - { - Type builderType= b.GetType(); - MethodInfo incluedMethod= builderType.GetMethod("Include", BindingFlags.Instance| BindingFlags.Public); - MethodInfo excludeMethod= builderType.GetMethod("Exclude", BindingFlags.Instance| BindingFlags.Public); - MethodInfo optionalMethod= builderType.GetMethod("Optional", BindingFlags.Instance| BindingFlags.Public); + protected override void Init(Builder b) => EcsQueryDIHelper.Fill(this, b); + } + public abstract class EcsQueryDI : EcsQuery + { + protected override void Init(Builder b) => EcsQueryDIHelper.Fill(this, b); + } - Type thisType = GetType(); + internal static class EcsQueryDIHelper + { + public static void Fill(EcsQueryBase q, EcsQueryBase.Builder b) + { + Type builderType = b.GetType(); + MethodInfo incluedMethod = builderType.GetMethod("Include", BindingFlags.Instance | BindingFlags.Public); + MethodInfo excludeMethod = builderType.GetMethod("Exclude", BindingFlags.Instance | BindingFlags.Public); + MethodInfo optionalMethod = builderType.GetMethod("Optional", BindingFlags.Instance | BindingFlags.Public); + + //PropertyInfo componentTypeProp = builderType.GetProperty("ComponentType", BindingFlags.Instance | BindingFlags.Public); + + Type thisType = q.GetType(); FieldInfo[] fieldInfos = thisType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); foreach (FieldInfo fieldInfo in fieldInfos) { - Type fiedlType = fieldInfo.FieldType; - if (fiedlType.IsGenericType == false) + Type fieldType = fieldInfo.FieldType; + if (fieldType.IsSubclassOf(typeof(EcsPoolBase)) == false) continue; - Type fiedlTypeDefinition = fiedlType.GetGenericTypeDefinition(); - Type genericArg = fiedlType.GenericTypeArguments[0]; - if (fiedlTypeDefinition == typeof(inc<>)) + if (fieldType.IsGenericType == false) + continue; + //Type fiedlTypeDefinition = fieldType.GetGenericTypeDefinition(); + + //Type componentType = ((EcsPoolBase)componentTypeProp.GetValue(fieldInfo.GetValue(q))).ComponentType; + Type componentType = fieldType.GenericTypeArguments[0]; + + if (fieldInfo.GetCustomAttribute() != null) { - fieldInfo.SetValue(this, incluedMethod.MakeGenericMethod(genericArg).Invoke(b, null)); + fieldInfo.SetValue(q, incluedMethod.MakeGenericMethod(componentType, fieldType).Invoke(b, null)); + continue; } - if(fiedlTypeDefinition == typeof(exc<>)) + if (fieldInfo.GetCustomAttribute() != null) { - fieldInfo.SetValue(this, excludeMethod.MakeGenericMethod(genericArg).Invoke(b, null)); - } - if (fiedlTypeDefinition == typeof(opt<>)) - { - fieldInfo.SetValue(this, optionalMethod.MakeGenericMethod(genericArg).Invoke(b, null)); + fieldInfo.SetValue(q, excludeMethod.MakeGenericMethod(componentType, fieldType).Invoke(b, null)); + continue; } + fieldInfo.SetValue(q, optionalMethod.MakeGenericMethod(componentType, fieldType).Invoke(b, null)); } } }