Просмотр исходного кода

更新分析器 禁止直接访问Entity基类的child,component. (#398)

* 禁止直接访问Entity基类的child,component. 如特殊需求, 要在方法体添加EnableAccessEntiyChildAttribute
susices 3 лет назад
Родитель
Сommit
666bc24e18
23 измененных файлов с 250 добавлено и 114 удалено
  1. 55 30
      Share/Analyzer/Analyzer/AddChildTypeAnalyzer.cs
  2. 1 3
      Share/Analyzer/Analyzer/ClassDeclarationInHotfixAnalyzer.cs
  3. 1 3
      Share/Analyzer/Analyzer/ETTaskAnalyzer.cs
  4. 1 3
      Share/Analyzer/Analyzer/EntityClassDeclarationAnalyzer.cs
  5. 53 34
      Share/Analyzer/Analyzer/EntityComponentAnalyzer.cs
  6. 2 4
      Share/Analyzer/Analyzer/EntityDelegateDeclarationAnalyzer.cs
  7. 4 12
      Share/Analyzer/Analyzer/EntityFiledAccessAnalyzer.cs
  8. 3 5
      Share/Analyzer/Analyzer/EntityMethodDeclarationAnalyzer.cs
  9. 1 8
      Share/Analyzer/Analyzer/UniqueIdAnalyzer.cs
  10. 32 0
      Share/Analyzer/Config/Definition.cs
  11. 2 0
      Share/Analyzer/Config/DiagnosticIds.cs
  12. 56 0
      Share/Analyzer/Config/DiagnosticRules.cs
  13. 1 1
      Unity/Assets/Scripts/Codes/Hotfix/Client/Demo/Unit/UnitHelper.cs
  14. 2 2
      Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Gate/SessionPlayerComponentSystem.cs
  15. 3 2
      Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Helper/InnerMessageDispatcherHelper.cs
  16. 1 1
      Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Map/AOI/ChangePosition_NotifyAOI.cs
  17. 2 2
      Unity/Assets/Scripts/Codes/Hotfix/Server/Module/AOI/AOIEntitySystem.cs
  18. 1 1
      Unity/Assets/Scripts/Codes/HotfixView/Client/Demo/UI/UIHelp/UIHelpEvent.cs
  19. 1 1
      Unity/Assets/Scripts/Codes/HotfixView/Client/Demo/UI/UILobby/UILobbyEvent.cs
  20. 1 1
      Unity/Assets/Scripts/Codes/HotfixView/Client/Demo/UI/UILogin/UILoginEvent.cs
  21. 1 1
      Unity/Assets/Scripts/Codes/HotfixView/Client/Demo/Unit/AnimatorComponentSystem.cs
  22. 15 0
      Unity/Assets/Scripts/Core/Object/EnableAccessEntiyChildAttribute.cs
  23. 11 0
      Unity/Assets/Scripts/Core/Object/EnableAccessEntiyChildAttribute.cs.meta

+ 55 - 30
Share/Analyzer/Analyzer/AddChildTypeAnalyzer.cs

@@ -11,26 +11,7 @@ namespace ET.Analyzer
     [DiagnosticAnalyzer(LanguageNames.CSharp)]
     public class AddChildTypeAnalyzer: DiagnosticAnalyzer
     {
-        private const string Title = "AddChild方法类型约束错误";
-
-        private const string MessageFormat = "Type: {0} 不允许作为实体: {1} 的AddChild函数参数类型! 若要允许该类型作为参数,请使用ChildOfAttribute对child实体类标记父级类型";
-
-        private const string Description = "AddChild方法类型约束错误.";
-
-        private static readonly string[] AddChildMethods = { "AddChild", "AddChildWithId" };
-        
-        private const string EntityType = "ET.Entity";
-
-        private static readonly DiagnosticDescriptor Rule =
-                new DiagnosticDescriptor(DiagnosticIds.AddChildTypeAnalyzerRuleId,
-                    Title,
-                    MessageFormat,
-                    DiagnosticCategories.Hotfix,
-                    DiagnosticSeverity.Error,
-                    true,
-                    Description);
-
-        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
+        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(AddChildTypeAnalyzerRule.Rule,DisableAccessEntityChildAnalyzerRule.Rule);
 
         public override void Initialize(AnalysisContext context)
         {
@@ -59,7 +40,7 @@ namespace ET.Analyzer
             // 筛选出 AddChild函数syntax
             string methodName = memberAccessExpressionSyntax.Name.Identifier.Text;
 
-            if (!AddChildMethods.Contains(methodName))
+            if (!Definition.AddChildMethods.Contains(methodName))
             {
                 return;
             }
@@ -76,13 +57,22 @@ namespace ET.Analyzer
             {
                 return;
             }
-
-            // 只检查Entity的子类
-            if (parentTypeSymbol.BaseType?.ToString()!= EntityType)
+            
+            
+            // 对于Entity基类会报错 除非标记了EnableAccessEntiyChild
+            if (parentTypeSymbol.ToString()==Definition.EntityType)
             {
+                HandleAcessEntityChild(context);
                 return;
             }
 
+            // 非Entity的子类 跳过
+            if (parentTypeSymbol.BaseType?.ToString()!= Definition.EntityType)
+            {
+                return;
+            }
+            
+            
             // 获取 child实体类型
             ISymbol? childTypeSymbol = null;
             // addChild为泛型调用
@@ -96,7 +86,7 @@ namespace ET.Analyzer
 
                 if (childTypeSyntax == null)
                 {
-                    Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
+                    Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
                     context.ReportDiagnostic(diagnostic);
                     throw new Exception("childTypeSyntax==null");
                 }
@@ -110,7 +100,7 @@ namespace ET.Analyzer
                         ?.ChildNodes().First();
                 if (firstArgumentSyntax == null)
                 {
-                    Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
+                    Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
                     context.ReportDiagnostic(diagnostic);
                     return;
                 }
@@ -139,14 +129,14 @@ namespace ET.Analyzer
                 }
                 else if (firstArgumentSymbol != null)
                 {
-                    Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(),
+                    Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(),
                         firstArgumentSymbol.Name, parentTypeSymbol.Name);
                     context.ReportDiagnostic(diagnostic);
                     return;
                 }
                 else
                 {
-                    Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(),
+                    Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(),
                         firstArgumentSyntax.GetText(), parentTypeSymbol.Name);
                     context.ReportDiagnostic(diagnostic);
                     return;
@@ -176,7 +166,7 @@ namespace ET.Analyzer
 
             foreach (AttributeData? attributeData in childType.GetAttributes())
             {
-                if (attributeData.AttributeClass?.Name == "ChildOfAttribute")
+                if (attributeData.AttributeClass?.ToString() == Definition.ChildOfAttribute)
                 {
                     hasAttribute = true;
                     availableParentType = attributeData.ConstructorArguments[0].Value as INamedTypeSymbol;
@@ -196,10 +186,45 @@ namespace ET.Analyzer
             }
 
             {
-                Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), childTypeSymbol?.Name,
+                Diagnostic diagnostic = Diagnostic.Create(AddChildTypeAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), childTypeSymbol?.Name,
                     parentTypeSymbol?.Name);
                 context.ReportDiagnostic(diagnostic);
             }
         }
+
+        private void HandleAcessEntityChild(SyntaxNodeAnalysisContext context)
+        {
+            var memberAccessExpressionSyntax = context.Node as MemberAccessExpressionSyntax;
+            //在方法体内
+            var methodDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor<MethodDeclarationSyntax>();
+            if (methodDeclarationSyntax!=null)
+            {
+                var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodDeclarationSyntax);
+
+                bool? enableAccessEntiyChild = methodSymbol?.GetAttributes().Any(x => x.AttributeClass?.ToString() == Definition.EnableAccessEntiyChildAttribute);
+                if (enableAccessEntiyChild == null || !enableAccessEntiyChild.Value)
+                {
+                    Diagnostic diagnostic = Diagnostic.Create(DisableAccessEntityChildAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
+                    context.ReportDiagnostic(diagnostic);
+                }
+                return;
+            }
+                
+            //在属性内
+            var propertyDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor<PropertyDeclarationSyntax>();
+            if (propertyDeclarationSyntax!=null)
+            {
+                var propertySymbol = context.SemanticModel.GetDeclaredSymbol(propertyDeclarationSyntax);
+                
+                bool? enableAccessEntiyChild = propertySymbol?.GetAttributes().Any(x => x.AttributeClass?.ToString() == Definition.EnableAccessEntiyChildAttribute);
+                if (enableAccessEntiyChild == null || !enableAccessEntiyChild.Value)
+                {
+                    Diagnostic diagnostic = Diagnostic.Create(DisableAccessEntityChildAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
+                    context.ReportDiagnostic(diagnostic);
+                }
+
+                return;
+            }
+        }
     }
 }

+ 1 - 3
Share/Analyzer/Analyzer/ClassDeclarationInHotfixAnalyzer.cs

@@ -13,8 +13,6 @@ namespace ET.Analyzer
 
         private const string Description = "Hotfix程序集中 只能声明含有BaseAttribute子类特性的类或静态类.";
 
-        private const string BaseAttribute = "ET.BaseAttribute";
-
         private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticIds.ClassDeclarationInHotfixAnalyzerRuleId,
             Title,
             MessageFormat,
@@ -70,7 +68,7 @@ namespace ET.Analyzer
             INamedTypeSymbol? typeSymbol = namedTypeSymbol;
             while (typeSymbol != null)
             {
-                if (typeSymbol.HasBaseAttribute(BaseAttribute))
+                if (typeSymbol.HasBaseAttribute(Definition.BaseAttribute))
                 {
                     return true;
                 }

+ 1 - 3
Share/Analyzer/Analyzer/ETTaskAnalyzer.cs

@@ -10,8 +10,6 @@ namespace ET.Analyzer
     [DiagnosticAnalyzer(LanguageNames.CSharp)]
     public class ETTaskAnalyzer:DiagnosticAnalyzer
     {
-        private const string ETTask = "ETTask";
-
         public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => 
                 ImmutableArray.Create(ETTaskInSyncMethodAnalyzerRule.Rule,ETTaskInAsyncMethodAnalyzerRule.Rule);
         
@@ -57,7 +55,7 @@ namespace ET.Analyzer
             }
             
             // 筛选出返回值为ETTask 和ETTask<T>的函数
-            if (namedTypeSymbol.Name!=ETTask)
+            if (namedTypeSymbol.Name!=Definition.ETTask)
             {
                 return;
             }

+ 1 - 3
Share/Analyzer/Analyzer/EntityClassDeclarationAnalyzer.cs

@@ -13,8 +13,6 @@ namespace ET.Analyzer
 
         private const string Description = "实体类限制多层继承.";
 
-        private const string EntityType = "ET.Entity";
-
         private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticIds.EntityClassDeclarationAnalyzerRuleId,
             Title,
             MessageFormat,
@@ -42,7 +40,7 @@ namespace ET.Analyzer
                 return;
             }
 
-            if (namedTypeSymbol.BaseType?.BaseType?.ToString() != EntityType)
+            if (namedTypeSymbol.BaseType?.BaseType?.ToString() != Definition.EntityType)
             {
                 return;
             }

+ 53 - 34
Share/Analyzer/Analyzer/EntityComponentAnalyzer.cs

@@ -11,27 +11,7 @@ namespace ET.Analyzer
     [DiagnosticAnalyzer(LanguageNames.CSharp)]
     public class EntityComponentAnalyzer:DiagnosticAnalyzer
     {
-        private const string Title = "实体类添加或获取组件类型错误";
-
-        private const string MessageFormat = "组件类型: {0} 不允许作为实体: {1} 的组件类型! 若要允许该类型作为参数,请使用ComponentOfAttribute对组件类标记父级实体类型";
-
-        private const string Description = "实体类添加或获取组件类型错误.";
-
-        private static readonly string[] ComponentMethod = {"AddComponent","GetComponent"};
-        
-        private const string EntityType = "ET.Entity";
-        
-        private static readonly DiagnosticDescriptor Rule =
-                new DiagnosticDescriptor(DiagnosticIds.EntityComponentAnalyzerRuleId,
-                    Title,
-                    MessageFormat,
-                    DiagnosticCategories.Hotfix,
-                    DiagnosticSeverity.Error,
-                    true,
-                    Description);
-
-        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
-
+        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(EntityComponentAnalyzerRule.Rule,DisableAccessEntityChildAnalyzerRule.Rule);
         
         public override void Initialize(AnalysisContext context)
         {
@@ -59,7 +39,7 @@ namespace ET.Analyzer
             // 筛选出 Component函数syntax
             string methodName = memberAccessExpressionSyntax.Name.Identifier.Text;
 
-            if (!ComponentMethod.Contains(methodName))
+            if (!Definition.ComponentMethod.Contains(methodName))
             {
                 return;
             }
@@ -77,14 +57,19 @@ namespace ET.Analyzer
                 return;
             }
             
-            // 只检查Entity的子类
-            if (parentTypeSymbol.BaseType?.ToString()!= EntityType)
+            // 对于Entity基类会报错 除非标记了EnableAccessEntiyChild
+            if (parentTypeSymbol.ToString()==Definition.EntityType)
             {
+                HandleAcessEntityChild(context);
                 return;
             }
-            
-            
-            
+
+            // 非Entity的子类 跳过
+            if (parentTypeSymbol.BaseType?.ToString()!= Definition.EntityType)
+            {
+                return;
+            }
+
             // 获取 component实体类型
             ISymbol? componentTypeSymbol = null;
             
@@ -99,7 +84,7 @@ namespace ET.Analyzer
                 
                 if (componentTypeSyntax == null)
                 {
-                    Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
+                    Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
                     context.ReportDiagnostic(diagnostic);
                     throw new Exception("componentTypeSyntax==null");
                 }
@@ -113,7 +98,7 @@ namespace ET.Analyzer
                         ?.ChildNodes().First();
                 if (firstArgumentSyntax == null)
                 {
-                    Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
+                    Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
                     context.ReportDiagnostic(diagnostic);
                     return;
                 }
@@ -155,14 +140,14 @@ namespace ET.Analyzer
                 }
                 else if (firstArgumentSymbol != null)
                 {
-                    Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(),
+                    Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(),
                         firstArgumentSymbol.Name, parentTypeSymbol.Name);
                     context.ReportDiagnostic(diagnostic);
                     return;
                 }
                 else
                 {
-                    Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(),
+                    Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(),
                         firstArgumentSyntax.GetText(), parentTypeSymbol.Name);
                     context.ReportDiagnostic(diagnostic);
                     return;
@@ -187,7 +172,7 @@ namespace ET.Analyzer
             }
 
             // 组件类型为Entity时 忽略检查
-            if (componentTypeSymbol.ToString()== EntityType)
+            if (componentTypeSymbol.ToString()== Definition.EntityType)
             {
                 return;
             }
@@ -200,7 +185,7 @@ namespace ET.Analyzer
             foreach (AttributeData? attributeData in componentTypeSymbol.GetAttributes())
             {
 
-                if (attributeData.AttributeClass?.Name == "ComponentOfAttribute")
+                if (attributeData.AttributeClass?.ToString() == Definition.ComponentOfAttribute)
                 {
                     hasParentTypeAttribute = true;
                     if (attributeData.ConstructorArguments[0].Value is INamedTypeSymbol typeSymbol)
@@ -223,10 +208,44 @@ namespace ET.Analyzer
             }
 
             {
-                Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), componentTypeSymbol?.Name,
+                Diagnostic diagnostic = Diagnostic.Create(EntityComponentAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation(), componentTypeSymbol?.Name,
                     parentTypeSymbol?.Name);
                 context.ReportDiagnostic(diagnostic);
             }
         }
+        
+        private void HandleAcessEntityChild(SyntaxNodeAnalysisContext context)
+        {
+            var memberAccessExpressionSyntax = context.Node as MemberAccessExpressionSyntax;
+            //在方法体内
+            var methodDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor<MethodDeclarationSyntax>();
+            if (methodDeclarationSyntax!=null)
+            {
+                var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodDeclarationSyntax);
+
+                bool? enableAccessEntiyChild = methodSymbol?.GetAttributes().Any(x => x.AttributeClass?.ToString() == Definition.EnableAccessEntiyChildAttribute);
+                if (enableAccessEntiyChild == null || !enableAccessEntiyChild.Value)
+                {
+                    Diagnostic diagnostic = Diagnostic.Create(DisableAccessEntityChildAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
+                    context.ReportDiagnostic(diagnostic);
+                }
+                return;
+            }
+                
+            //在属性内
+            var propertyDeclarationSyntax = memberAccessExpressionSyntax?.GetNeareastAncestor<PropertyDeclarationSyntax>();
+            if (propertyDeclarationSyntax!=null)
+            {
+                var propertySymbol = context.SemanticModel.GetDeclaredSymbol(propertyDeclarationSyntax);
+                
+                bool? enableAccessEntiyChild = propertySymbol?.GetAttributes().Any(x => x.AttributeClass?.ToString() == Definition.EnableAccessEntiyChildAttribute);
+                if (enableAccessEntiyChild == null || !enableAccessEntiyChild.Value)
+                {
+                    Diagnostic diagnostic = Diagnostic.Create(DisableAccessEntityChildAnalyzerRule.Rule, memberAccessExpressionSyntax?.Name.Identifier.GetLocation());
+                    context.ReportDiagnostic(diagnostic);
+                }
+                return;
+            }
+        }
     }
 }

+ 2 - 4
Share/Analyzer/Analyzer/EntityDelegateDeclarationAnalyzer.cs

@@ -14,9 +14,7 @@ namespace ET.Analyzer
         private const string MessageFormat = "实体类: {0} 不能在类内部声明委托字段或属性: {1}";
 
         private const string Description = "实体类禁止声明委托字段或属性.";
-        
-        private const string EntityType = "ET.Entity";
-        
+
         private static readonly DiagnosticDescriptor Rule =
                 new DiagnosticDescriptor(DiagnosticIds.DelegateAnalyzerRuleId,
                     Title,
@@ -52,7 +50,7 @@ namespace ET.Analyzer
             }
 
             // 筛选出实体类
-            if (namedTypeSymbol.BaseType?.ToString() != EntityType)
+            if (namedTypeSymbol.BaseType?.ToString() != Definition.EntityType)
             {
                 return;
             }

+ 4 - 12
Share/Analyzer/Analyzer/EntityFiledAccessAnalyzer.cs

@@ -16,14 +16,6 @@ namespace ET.Analyzer
 
         private const string Description = "请使用实体类属性或方法访问其他实体字段.";
 
-        private const string EntityType = "ET.Entity";
-
-        private const string ObjectSystemAttribute = "ET.ObjectSystemAttribute";
-
-        private const string ISystemType = "ET.ISystemType";
-
-        private const string FriendOfAttribute = "ET.FriendOfAttribute";
-
         private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticIds.EntityFiledAccessAnalyzerRuleId,
             Title,
             MessageFormat,
@@ -69,7 +61,7 @@ namespace ET.Analyzer
                 return;
             }
 
-            if (filedSymbol.ContainingType.BaseType?.ToString() != EntityType)
+            if (filedSymbol.ContainingType.BaseType?.ToString() != Definition.EntityType)
             {
                 return;
             }
@@ -91,7 +83,7 @@ namespace ET.Analyzer
             }
 
             // 实体基类忽略处理
-            if (accessFieldClassSymbol.ToString() == EntityType)
+            if (accessFieldClassSymbol.ToString() == Definition.EntityType)
             {
                 return;
             }
@@ -129,7 +121,7 @@ namespace ET.Analyzer
             }
 
             // 判断是否含有 ObjectSystem Attribute 且继承了接口 ISystemType
-            if (accessFieldClassSymbol.BaseType.HasAttribute(ObjectSystemAttribute) && accessFieldClassSymbol.HasInterface(ISystemType))
+            if (accessFieldClassSymbol.BaseType.HasAttribute(Definition.ObjectSystemAttribute) && accessFieldClassSymbol.HasInterface(Definition.ISystemType))
             {
                 // 获取 accessFieldClassSymbol 父类的实体类型参数
                 ITypeSymbol? entityTypeArgumentSymbol = accessFieldClassSymbol.BaseType.TypeArguments.FirstOrDefault();
@@ -153,7 +145,7 @@ namespace ET.Analyzer
             var attributes = accessFieldTypeSymbol.GetAttributes();
             foreach (AttributeData? attributeData in attributes)
             {
-                if (attributeData.AttributeClass?.ToString() != FriendOfAttribute)
+                if (attributeData.AttributeClass?.ToString() != Definition.FriendOfAttribute)
                 {
                     continue;
                 }

+ 3 - 5
Share/Analyzer/Analyzer/EntityMethodDeclarationAnalyzer.cs

@@ -15,10 +15,8 @@ namespace ET.Analyzer
         private const string MessageFormat = "实体类: {0} 不能在类内部声明方法: {1}";
 
         private const string Description = "实体类禁止声明方法.";
-        
-        private const string EntityType = "ET.Entity";
 
-        private const string EnableMethodAttribute = "ET.EnableMethodAttribute";
+        
         
         private static readonly DiagnosticDescriptor Rule =
                 new DiagnosticDescriptor(DiagnosticIds.EntityMethodDeclarationAnalyzerRuleId,
@@ -56,13 +54,13 @@ namespace ET.Analyzer
             }
 
             // 筛选出实体类
-            if (namedTypeSymbol.BaseType?.ToString() != EntityType)
+            if (namedTypeSymbol.BaseType?.ToString() != Definition.EntityType)
             {
                 return;
             }
 
             // 忽略含有EnableMethod标签的实体类
-            if (namedTypeSymbol.HasAttribute(EnableMethodAttribute))
+            if (namedTypeSymbol.HasAttribute(Definition.EnableMethodAttribute))
             {
                 return;
             }

+ 1 - 8
Share/Analyzer/Analyzer/UniqueIdAnalyzer.cs

@@ -9,15 +9,8 @@ namespace ET.Analyzer
     [DiagnosticAnalyzer(LanguageNames.CSharp)]
     public class UniqueIdAnalyzer : DiagnosticAnalyzer
     {
-
-
-        private const string UniqueIdAttribute = "ET.UniqueIdAttribute";
-        
-
-
         public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>ImmutableArray.Create(UniqueIdRangeAnaluzerRule.Rule,UniqueIdDuplicateAnalyzerRule.Rule);
         
-        
         public override void Initialize(AnalysisContext context)
         {
             if (!AnalyzerGlobalSetting.EnableAnalyzer)
@@ -42,7 +35,7 @@ namespace ET.Analyzer
             }
 
             // 筛选出含有UniqueId标签的类
-            var attr = namedTypeSymbol.GetFirstAttribute(UniqueIdAttribute);
+            var attr = namedTypeSymbol.GetFirstAttribute(Definition.UniqueIdAttribute);
             if (attr==null)
             {
                 return;

+ 32 - 0
Share/Analyzer/Config/Definition.cs

@@ -0,0 +1,32 @@
+namespace ET.Analyzer
+{
+    public static class Definition
+    {
+        public const string EntityType = "ET.Entity";
+        
+        public const string ETTask = "ETTask";
+
+        public static readonly string[] AddChildMethods = { "AddChild", "AddChildWithId" };
+
+        public static readonly string[] ComponentMethod = {"AddComponent","GetComponent"};
+
+        public const string ISystemType = "ET.ISystemType";
+        
+        public const string BaseAttribute = "ET.BaseAttribute";
+        
+        public const string ObjectSystemAttribute = "ET.ObjectSystemAttribute";
+
+        public const string EnableMethodAttribute = "ET.EnableMethodAttribute";
+        
+        public const string FriendOfAttribute = "ET.FriendOfAttribute";
+        
+        public const string UniqueIdAttribute = "ET.UniqueIdAttribute";
+
+        public const string ChildOfAttribute = "ET.ChildOfAttribute";
+
+        public const string ComponentOfAttribute = "ET.ComponentOfAttribute";
+        
+        public const string EnableAccessEntiyChildAttribute = "ET.EnableAccessEntiyChildAttribute";
+    }
+}
+

+ 2 - 0
Share/Analyzer/Config/DiagnosticIds.cs

@@ -28,5 +28,7 @@
 
         public const string StaticClassCircularDedendencyAnalyzerRuleId = "ET0013";
 
+        public const string DisableUseChildComponentInEntityAnalyzerRuleId = "ET0014";
+
     }
 }

+ 56 - 0
Share/Analyzer/Config/DiagnosticRules.cs

@@ -73,4 +73,60 @@ namespace ET.Analyzer
                     true,
                     Description);
     }
+
+    public static class AddChildTypeAnalyzerRule
+    {
+        private const string Title = "AddChild方法类型约束错误";
+
+        private const string MessageFormat = "Type: {0} 不允许作为实体: {1} 的AddChild函数参数类型! 若要允许该类型作为参数,请使用ChildOfAttribute对child实体类标记父级类型";
+
+        private const string Description = "AddChild方法类型约束错误.";
+        
+        public static readonly DiagnosticDescriptor Rule =
+                new DiagnosticDescriptor(DiagnosticIds.AddChildTypeAnalyzerRuleId,
+                    Title,
+                    MessageFormat,
+                    DiagnosticCategories.Hotfix,
+                    DiagnosticSeverity.Error,
+                    true,
+                    Description);
+    }
+    
+    public static class DisableAccessEntityChildAnalyzerRule
+    {
+        private const string Title = "禁止在Entity类中直接调用Child和Component";
+
+        private const string MessageFormat = "禁止在Entity类中直接调用Child和Component";
+
+        private const string Description = "禁止在Entity类中直接调用Child和Component.";
+        
+        public static readonly DiagnosticDescriptor Rule =
+                new DiagnosticDescriptor(DiagnosticIds.DisableUseChildComponentInEntityAnalyzerRuleId,
+                    Title,
+                    MessageFormat,
+                    DiagnosticCategories.Hotfix,
+                    DiagnosticSeverity.Error,
+                    true,
+                    Description);
+    }
+
+    public static class EntityComponentAnalyzerRule
+    {
+        private const string Title = "实体类添加或获取组件类型错误";
+
+        private const string MessageFormat = "组件类型: {0} 不允许作为实体: {1} 的组件类型! 若要允许该类型作为参数,请使用ComponentOfAttribute对组件类标记父级实体类型";
+
+        private const string Description = "实体类添加或获取组件类型错误.";
+        
+        public static readonly DiagnosticDescriptor Rule =
+                new DiagnosticDescriptor(DiagnosticIds.EntityComponentAnalyzerRuleId,
+                    Title,
+                    MessageFormat,
+                    DiagnosticCategories.Hotfix,
+                    DiagnosticSeverity.Error,
+                    true,
+                    Description);
+    }
+    
+    
 }

+ 1 - 1
Unity/Assets/Scripts/Codes/Hotfix/Client/Demo/Unit/UnitHelper.cs

@@ -11,7 +11,7 @@
         
         public static Unit GetMyUnitFromCurrentScene(Scene currentScene)
         {
-            PlayerComponent playerComponent = currentScene.Parent.Parent.GetComponent<PlayerComponent>();
+            PlayerComponent playerComponent = currentScene.Parent.GetParent<Scene>().GetComponent<PlayerComponent>();
             return currentScene.GetComponent<UnitComponent>().Get(playerComponent.MyId);
         }
     }

+ 2 - 2
Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Gate/SessionPlayerComponentSystem.cs

@@ -10,13 +10,13 @@ namespace ET.Server
 			{
 				// 发送断线消息
 				ActorLocationSenderComponent.Instance.Send(self.PlayerId, new G2M_SessionDisconnect());
-				self.Domain.GetComponent<PlayerComponent>()?.Remove(self.PlayerId);
+				self.DomainScene().GetComponent<PlayerComponent>()?.Remove(self.PlayerId);
 			}
 		}
 
 		public static Player GetMyPlayer(this SessionPlayerComponent self)
 		{
-			return self.Domain.GetComponent<PlayerComponent>().Get(self.PlayerId);
+			return self.DomainScene().GetComponent<PlayerComponent>().Get(self.PlayerId);
 		}
 	}
 }

+ 3 - 2
Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Helper/InnerMessageDispatcherHelper.cs

@@ -10,6 +10,7 @@ namespace ET.Server
             ActorMessageSenderComponent.Instance.RunMessage(actorId, iActorResponse);
         }
 
+        [EnableAccessEntiyChild]
         public static void HandleIActorRequest(ushort opcode, long actorId, IActorRequest iActorRequest, Action<IActorResponse> reply)
         {
             Entity entity = Game.EventSystem.Get(actorId);
@@ -62,8 +63,8 @@ namespace ET.Server
             IActorResponse response = ActorHelper.CreateResponse(iActorRequest, error);
             reply.Invoke(response);
         }
-
-
+        
+        [EnableAccessEntiyChild]
         public static void HandleIActorMessage(ushort opcode, long actorId, IActorMessage iActorMessage)
         {
             OpcodeHelper.LogMsg(opcode, actorId, iActorMessage);

+ 1 - 1
Unity/Assets/Scripts/Codes/Hotfix/Server/Demo/Map/AOI/ChangePosition_NotifyAOI.cs

@@ -24,7 +24,7 @@ namespace ET.Server
                 return;
             }
 
-            unit.Domain.GetComponent<AOIManagerComponent>().Move(aoiEntity, newCellX, newCellY);
+            unit.DomainScene().GetComponent<AOIManagerComponent>().Move(aoiEntity, newCellX, newCellY);
             await ETTask.CompletedTask;
         }
     }

+ 2 - 2
Unity/Assets/Scripts/Codes/Hotfix/Server/Module/AOI/AOIEntitySystem.cs

@@ -13,7 +13,7 @@ namespace ET.Server
             protected override void Awake(AOIEntity self, int distance, Vector3 pos)
             {
                 self.ViewDistance = distance;
-                self.Domain.GetComponent<AOIManagerComponent>().Add(self, pos.x, pos.z);
+                self.DomainScene().GetComponent<AOIManagerComponent>().Add(self, pos.x, pos.z);
             }
         }
 
@@ -22,7 +22,7 @@ namespace ET.Server
         {
             protected override void Destroy(AOIEntity self)
             {
-                self.Domain.GetComponent<AOIManagerComponent>()?.Remove(self);
+                self.DomainScene().GetComponent<AOIManagerComponent>()?.Remove(self);
                 self.ViewDistance = 0;
                 self.SeeUnits.Clear();
                 self.SeePlayers.Clear();

+ 1 - 1
Unity/Assets/Scripts/Codes/HotfixView/Client/Demo/UI/UIHelp/UIHelpEvent.cs

@@ -10,7 +10,7 @@ namespace ET.Client
         {
 	        try
 	        {
-		        await uiComponent.Domain.GetComponent<ResourcesLoaderComponent>().LoadAsync(UIType.UIHelp.StringToAB());
+		        await uiComponent.DomainScene().GetComponent<ResourcesLoaderComponent>().LoadAsync(UIType.UIHelp.StringToAB());
 		        GameObject bundleGameObject = (GameObject) ResourcesComponent.Instance.GetAsset(UIType.UIHelp.StringToAB(), UIType.UIHelp);
 		        GameObject gameObject = UnityEngine.Object.Instantiate(bundleGameObject, UIEventComponent.Instance.GetLayer((int)uiLayer));
 		        UI ui = uiComponent.AddChild<UI, string, GameObject>(UIType.UIHelp, gameObject);

+ 1 - 1
Unity/Assets/Scripts/Codes/HotfixView/Client/Demo/UI/UILobby/UILobbyEvent.cs

@@ -8,7 +8,7 @@ namespace ET.Client
         public override async ETTask<UI> OnCreate(UIComponent uiComponent, UILayer uiLayer)
         {
             await ETTask.CompletedTask;
-            await uiComponent.Domain.GetComponent<ResourcesLoaderComponent>().LoadAsync(UIType.UILobby.StringToAB());
+            await uiComponent.DomainScene().GetComponent<ResourcesLoaderComponent>().LoadAsync(UIType.UILobby.StringToAB());
             GameObject bundleGameObject = (GameObject) ResourcesComponent.Instance.GetAsset(UIType.UILobby.StringToAB(), UIType.UILobby);
             GameObject gameObject = UnityEngine.Object.Instantiate(bundleGameObject, UIEventComponent.Instance.GetLayer((int)uiLayer));
             UI ui = uiComponent.AddChild<UI, string, GameObject>(UIType.UILobby, gameObject);

+ 1 - 1
Unity/Assets/Scripts/Codes/HotfixView/Client/Demo/UI/UILogin/UILoginEvent.cs

@@ -8,7 +8,7 @@ namespace ET.Client
     {
         public override async ETTask<UI> OnCreate(UIComponent uiComponent, UILayer uiLayer)
         {
-            await uiComponent.Domain.GetComponent<ResourcesLoaderComponent>().LoadAsync(UIType.UILogin.StringToAB());
+            await uiComponent.DomainScene().GetComponent<ResourcesLoaderComponent>().LoadAsync(UIType.UILogin.StringToAB());
             GameObject bundleGameObject = (GameObject) ResourcesComponent.Instance.GetAsset(UIType.UILogin.StringToAB(), UIType.UILogin);
             GameObject gameObject = UnityEngine.Object.Instantiate(bundleGameObject, UIEventComponent.Instance.GetLayer((int)uiLayer));
             UI ui = uiComponent.AddChild<UI, string, GameObject>(UIType.UILogin, gameObject);

+ 1 - 1
Unity/Assets/Scripts/Codes/HotfixView/Client/Demo/Unit/AnimatorComponentSystem.cs

@@ -37,7 +37,7 @@ namespace ET.Client
 		
 		public static void Awake(this AnimatorComponent self)
 		{
-			Animator animator = self.Parent.GetComponent<GameObjectComponent>().GameObject.GetComponent<Animator>();
+			Animator animator = self.GetParent<Unit>().GetComponent<GameObjectComponent>().GameObject.GetComponent<Animator>();
 
 			if (animator == null)
 			{

+ 15 - 0
Unity/Assets/Scripts/Core/Object/EnableAccessEntiyChildAttribute.cs

@@ -0,0 +1,15 @@
+using System;
+
+namespace ET
+{
+    /// <summary>
+    /// 当方法或属性内需要访问Entity类的child和component时 使用此标签
+    /// 仅供必要时使用 大多数情况推荐通过Entity的子类访问
+    /// </summary>
+    [AttributeUsage(AttributeTargets.Method|AttributeTargets.Property)]
+    public class EnableAccessEntiyChildAttribute : Attribute
+    {
+        
+    }
+}
+

+ 11 - 0
Unity/Assets/Scripts/Core/Object/EnableAccessEntiyChildAttribute.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 97842853560d56c42a4e149134fef910
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: