فهرست منبع

AddChild必须指定一个ChildType,不能有多个

tanghai 3 سال پیش
والد
کامیت
5bdf4a7b7f

+ 1 - 0
Apps/Model/Module/ActorLocation/ActorLocationSenderComponent.cs

@@ -2,6 +2,7 @@
 
 namespace ET
 {
+    [ChildType(typeof(ActorLocationSender))]
     public class ActorLocationSenderComponent: Entity, IAwake, IDestroy
     {
         public const long TIMEOUT_TIME = 60 * 1000;

+ 1 - 0
Apps/Model/Module/DB/DBManagerComponent.cs

@@ -1,5 +1,6 @@
 namespace ET
 {
+    [ChildType(typeof(DBComponent))]
     public class DBManagerComponent: Entity, IAwake, IDestroy
     {
         public static DBManagerComponent Instance;

+ 1 - 0
Apps/Model/Module/MessageInner/NetInnerComponent.cs

@@ -19,6 +19,7 @@ namespace ET
         }
     }
 
+    [ChildType(typeof(Session))]
     public class NetInnerComponent: Entity, IAwake<IPEndPoint, int>, IAwake<int>, IDestroy
     {
         public AService Service;

+ 1 - 0
Apps/Model/Module/RobotCase/RobotCaseComponent.cs

@@ -2,6 +2,7 @@
 
 namespace ET
 {
+    [ChildType(typeof(RobotCase))]
     public class RobotCaseComponent: Entity, IAwake, IDestroy
     {
         public static RobotCaseComponent Instance;

+ 1 - 0
Apps/Model/Server/AOI/AOIManagerComponent.cs

@@ -1,5 +1,6 @@
 namespace ET.Server
 {
+    [ChildType(typeof(Cell))]
     public class AOIManagerComponent: Entity, IAwake
     {
         public static int CellSize = 10 * 1000;

+ 1 - 0
Apps/Model/Server/PlayerComponent.cs

@@ -3,6 +3,7 @@ using System.Linq;
 
 namespace ET.Server
 {
+	[ChildType(typeof(Player))]
 	public class PlayerComponent : Entity, IAwake, IDestroy
 	{
 		public readonly Dictionary<long, Player> idPlayers = new Dictionary<long, Player>();

+ 13 - 19
Share/Analyzer/Analyzer/AddChildTypeAnalyzer.cs

@@ -101,31 +101,20 @@ namespace ET.Analyzer
             }
 
             // 获取实体类 ChildType标签的约束类型
-            List<INamedTypeSymbol>? availableChildTypeSymbols = null;
+            INamedTypeSymbol? availableChildTypeSymbol = null;
             foreach (AttributeData? attributeData in parentTypeSymbol.GetAttributes())
             {
                 if (attributeData.AttributeClass?.Name == "ChildTypeAttribute")
                 {
-                    if (!(attributeData.ConstructorArguments[0].Value is INamedTypeSymbol availableChildTypeSymbol))
+                    if (!(attributeData.ConstructorArguments[0].Value is INamedTypeSymbol s))
                     {
                         continue;
                     }
 
-                    if (availableChildTypeSymbols == null)
-                    {
-                        availableChildTypeSymbols = new List<INamedTypeSymbol>();
-                    }
-
-                    availableChildTypeSymbols.Add(availableChildTypeSymbol);
+                    availableChildTypeSymbol = s;
                 }
             }
 
-            // 没有ChildType标签的不做约束
-            if (availableChildTypeSymbols == null)
-            {
-                return;
-            }
-
             // 获取 child实体类型
             ISymbol? childTypeSymbol = null;
             // addChild为泛型调用
@@ -203,13 +192,18 @@ namespace ET.Analyzer
                 throw new Exception("childTypeSymbol==null");
             }
 
+            if (availableChildTypeSymbol == null)
+            {
+                Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), childTypeSymbol?.Name,
+                    parentTypeSymbol?.Name);
+                context.ReportDiagnostic(diagnostic);
+                return;
+            }
+
             // 判断child类型是否属于约束类型
-            foreach (INamedTypeSymbol? availableChildTypeSymbol in availableChildTypeSymbols)
+            if (availableChildTypeSymbol.ToString() == childTypeSymbol.ToString())
             {
-                if (availableChildTypeSymbol?.ToString() == childTypeSymbol.ToString())
-                {
-                    return;
-                }
+                return;
             }
 
             {

+ 1 - 0
Unity/Codes/Model/Client/Unit/UnitComponent.cs

@@ -1,5 +1,6 @@
 namespace ET
 {
+	[ChildType(typeof(Unit))]
 	public class UnitComponent: Entity, IAwake, IDestroy
 	{
 	}

+ 15 - 0
Unity/Codes/Model/Core/Object/ChildTypeAttribute.cs

@@ -0,0 +1,15 @@
+using System;
+
+namespace ET
+{
+    [AttributeUsage(AttributeTargets.Class)]
+    public class ChildTypeAttribute : Attribute
+    {
+        public Type type;
+
+        public ChildTypeAttribute(Type type)
+        {
+            this.type = type;
+        }
+    }
+}

+ 1 - 0
Unity/Codes/Model/Module/Message/NetKcpComponent.cs

@@ -2,6 +2,7 @@
 
 namespace ET
 {
+    [ChildType(typeof(Session))]
     public class NetKcpComponent: Entity, IAwake<int>, IAwake<IPEndPoint, int>, IDestroy
     {
         public AService Service;

+ 1 - 0
Unity/Codes/ModelView/Module/UI/UIComponent.cs

@@ -5,6 +5,7 @@ namespace ET
 	/// <summary>
 	/// 管理Scene上的UI
 	/// </summary>
+	[ChildType(typeof(UI))]
 	public class UIComponent: Entity, IAwake
 	{
 		public Dictionary<string, UI> UIs = new Dictionary<string, UI>();