Răsfoiți Sursa

添加分析器禁止LSEntity类声明浮点数成员, 添加实体类分析器对LSEntity支持

wenchao 2 ani în urmă
părinte
comite
500a4ba965

+ 2 - 2
Share/Analyzer/Analyzer/AddChildTypeAnalyzer.cs

@@ -60,14 +60,14 @@ namespace ET.Analyzer
             
             
             
             
             // 对于Entity基类会报错 除非标记了EnableAccessEntiyChild
             // 对于Entity基类会报错 除非标记了EnableAccessEntiyChild
-            if (parentTypeSymbol.ToString()==Definition.EntityType)
+            if (parentTypeSymbol.ToString() is Definition.EntityType or Definition.LSEntityType)
             {
             {
                 HandleAcessEntityChild(context);
                 HandleAcessEntityChild(context);
                 return;
                 return;
             }
             }
 
 
             // 非Entity的子类 跳过
             // 非Entity的子类 跳过
-            if (parentTypeSymbol.BaseType?.ToString()!= Definition.EntityType)
+            if (parentTypeSymbol.BaseType?.ToString()!= Definition.EntityType  && parentTypeSymbol.BaseType?.ToString()!=Definition.LSEntityType)
             {
             {
                 return;
                 return;
             }
             }

+ 3 - 3
Share/Analyzer/Analyzer/EntityComponentAnalyzer.cs

@@ -58,14 +58,14 @@ namespace ET.Analyzer
             }
             }
             
             
             // 对于Entity基类会报错 除非标记了EnableAccessEntiyChild
             // 对于Entity基类会报错 除非标记了EnableAccessEntiyChild
-            if (parentTypeSymbol.ToString()==Definition.EntityType)
+            if (parentTypeSymbol.ToString() is Definition.EntityType or Definition.LSEntityType)
             {
             {
                 HandleAcessEntityChild(context);
                 HandleAcessEntityChild(context);
                 return;
                 return;
             }
             }
 
 
             // 非Entity的子类 跳过
             // 非Entity的子类 跳过
-            if (parentTypeSymbol.BaseType?.ToString()!= Definition.EntityType)
+            if (parentTypeSymbol.BaseType?.ToString()!= Definition.EntityType && parentTypeSymbol.BaseType?.ToString()!= Definition.LSEntityType)
             {
             {
                 return;
                 return;
             }
             }
@@ -172,7 +172,7 @@ namespace ET.Analyzer
             }
             }
 
 
             // 组件类型为Entity时 忽略检查
             // 组件类型为Entity时 忽略检查
-            if (componentTypeSymbol.ToString()== Definition.EntityType)
+            if (componentTypeSymbol.ToString() is Definition.EntityType or Definition.LSEntityType)
             {
             {
                 return;
                 return;
             }
             }

+ 2 - 2
Share/Analyzer/Analyzer/EntityFiledAccessAnalyzer.cs

@@ -61,7 +61,7 @@ namespace ET.Analyzer
                 return;
                 return;
             }
             }
 
 
-            if (filedSymbol.ContainingType.BaseType?.ToString() != Definition.EntityType)
+            if (filedSymbol.ContainingType.BaseType?.ToString() != Definition.EntityType && filedSymbol.ContainingType.BaseType?.ToString() != Definition.LSEntityType)
             {
             {
                 return;
                 return;
             }
             }
@@ -83,7 +83,7 @@ namespace ET.Analyzer
             }
             }
 
 
             // 实体基类忽略处理
             // 实体基类忽略处理
-            if (accessFieldClassSymbol.ToString() == Definition.EntityType)
+            if (accessFieldClassSymbol.ToString() is Definition.EntityType or Definition.LSEntityType)
             {
             {
                 return;
                 return;
             }
             }

+ 49 - 7
Share/Analyzer/Analyzer/EntityMemberDeclarationAnalyzer.cs

@@ -9,7 +9,8 @@ namespace ET.Analyzer
     [DiagnosticAnalyzer(LanguageNames.CSharp)]
     [DiagnosticAnalyzer(LanguageNames.CSharp)]
     public class EntityMemberDeclarationAnalyzer: DiagnosticAnalyzer
     public class EntityMemberDeclarationAnalyzer: DiagnosticAnalyzer
     {
     {
-        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>ImmutableArray.Create(EntityDelegateDeclarationAnalyzerRule.Rule,EntityFieldDeclarationInEntityAnalyzerRule.Rule);
+        public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>ImmutableArray.Create(EntityDelegateDeclarationAnalyzerRule.Rule,
+            EntityFieldDeclarationInEntityAnalyzerRule.Rule, LSEntityFloatMemberAnalyzer.Rule);
         
         
         public override void Initialize(AnalysisContext context)
         public override void Initialize(AnalysisContext context)
         {
         {
@@ -34,14 +35,18 @@ namespace ET.Analyzer
                 return;
                 return;
             }
             }
 
 
+            var baseType = namedTypeSymbol.BaseType?.ToString();
             // 筛选出实体类
             // 筛选出实体类
-            if (namedTypeSymbol.BaseType?.ToString() != Definition.EntityType)
+            if (baseType== Definition.EntityType)
             {
             {
-                return;
+                AnalyzeDelegateMember(context, namedTypeSymbol);
+                AnalyzeEntityMember(context, namedTypeSymbol);
+            }else if (baseType == Definition.LSEntityType)
+            {
+                AnalyzeDelegateMember(context, namedTypeSymbol);
+                AnalyzeEntityMember(context, namedTypeSymbol);
+                AnalyzeFloatMemberInLSEntity(context,namedTypeSymbol);
             }
             }
-
-            AnalyzeDelegateMember(context, namedTypeSymbol);
-            AnalyzeEntityMember(context, namedTypeSymbol);
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -95,7 +100,7 @@ namespace ET.Analyzer
                 {
                 {
                     continue;
                     continue;
                 }
                 }
-                if (fieldSymbol.Type.ToString()== Definition.EntityType || fieldSymbol.Type.BaseType?.ToString()== Definition.EntityType)
+                if (fieldSymbol.Type.ToString()is Definition.EntityType or Definition.LSEntityType || fieldSymbol.Type.BaseType?.ToString()is Definition.EntityType or Definition.LSEntityType)
                 {
                 {
                     var syntaxReference = fieldSymbol.DeclaringSyntaxReferences.FirstOrDefault();
                     var syntaxReference = fieldSymbol.DeclaringSyntaxReferences.FirstOrDefault();
                     if (syntaxReference==null)
                     if (syntaxReference==null)
@@ -107,6 +112,43 @@ namespace ET.Analyzer
                 }
                 }
             }
             }
         }
         }
+
+        /// <summary>
+        /// 检查LSEntity中 是否有浮点数字段
+        /// </summary>
+        private void AnalyzeFloatMemberInLSEntity(SymbolAnalysisContext context, INamedTypeSymbol namedTypeSymbol)
+        {
+            foreach (var member in namedTypeSymbol.GetMembers())
+            {
+                ITypeSymbol? memberType = null;
+                
+                if (member is IFieldSymbol fieldSymbol)
+                {
+                    memberType = fieldSymbol.Type;
+                }
+
+                if (member is IPropertySymbol propertySymbol)
+                {
+                    memberType = propertySymbol.Type;
+                }
+
+                if (memberType==null)
+                {
+                    continue;
+                }
+                
+                if (memberType.SpecialType is  SpecialType.System_Single or SpecialType.System_Double)
+                {
+                    var syntaxReference = member.DeclaringSyntaxReferences.FirstOrDefault();
+                    if (syntaxReference==null)
+                    {
+                        continue;
+                    }
+                    Diagnostic diagnostic = Diagnostic.Create(LSEntityFloatMemberAnalyzer.Rule, syntaxReference.GetSyntax().GetLocation(),namedTypeSymbol.Name,member.Name);
+                    context.ReportDiagnostic(diagnostic);
+                }
+            }
+        }
     }
     }
 }
 }
 
 

+ 1 - 1
Share/Analyzer/Analyzer/EntityMethodDeclarationAnalyzer.cs

@@ -54,7 +54,7 @@ namespace ET.Analyzer
             }
             }
 
 
             // 筛选出实体类
             // 筛选出实体类
-            if (namedTypeSymbol.BaseType?.ToString() != Definition.EntityType)
+            if (namedTypeSymbol.BaseType?.ToString() != Definition.EntityType && namedTypeSymbol.BaseType?.ToString() != Definition.LSEntityType)
             {
             {
                 return;
                 return;
             }
             }

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

@@ -3,6 +3,8 @@
     public static class Definition
     public static class Definition
     {
     {
         public const string EntityType = "ET.Entity";
         public const string EntityType = "ET.Entity";
+
+        public const string LSEntityType = "ET.LSEntity";
         
         
         public const string ETTask = "ETTask";
         public const string ETTask = "ETTask";
 
 

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

@@ -46,5 +46,7 @@
 
 
         public const string ClientClassInServerAnalyzerRuleId = "ET0022";
         public const string ClientClassInServerAnalyzerRuleId = "ET0022";
 
 
+        public const string LSEntityFloatMemberAnalyzerRuleId = "ET0023";
+
     }
     }
 }
 }

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

@@ -305,4 +305,22 @@ namespace ET.Analyzer
                     true,
                     true,
                     Description);
                     Description);
     }
     }
+
+    public static class LSEntityFloatMemberAnalyzer
+    {
+        private const string Title = "LSEntity类禁止声明浮点数字段或属性";
+
+        private const string MessageFormat = "LSEntity类: {0} 禁止声明浮点数字段或属性: {1}";
+
+        private const string Description = "LSEntity类禁止声明浮点数字段或属性.";
+
+        public static readonly DiagnosticDescriptor Rule =
+                new DiagnosticDescriptor(DiagnosticIds.LSEntityFloatMemberAnalyzerRuleId,
+                    Title,
+                    MessageFormat,
+                    DiagnosticCategories.Model,
+                    DiagnosticSeverity.Error,
+                    true,
+                    Description);
+    }
 }
 }

+ 4 - 0
Unity/Assets/Scripts/Model/Share/LockStep/LSEntity.cs

@@ -25,21 +25,25 @@ namespace ET
             return this.AddComponentWithId<K, P1, P2, P3>(this.GetId(), p1, p2, p3, isFromPool);
             return this.AddComponentWithId<K, P1, P2, P3>(this.GetId(), p1, p2, p3, isFromPool);
         }
         }
 
 
+        [EnableAccessEntiyChild]
         public new T AddChild<T>(bool isFromPool = false) where T : LSEntity, IAwake
         public new T AddChild<T>(bool isFromPool = false) where T : LSEntity, IAwake
         {
         {
             return this.AddChildWithId<T>(this.GetId(), isFromPool);
             return this.AddChildWithId<T>(this.GetId(), isFromPool);
         }
         }
 
 
+        [EnableAccessEntiyChild]
         public new T AddChild<T, A>(A a, bool isFromPool = false) where T : LSEntity, IAwake<A>
         public new T AddChild<T, A>(A a, bool isFromPool = false) where T : LSEntity, IAwake<A>
         {
         {
             return this.AddChildWithId<T, A>(this.GetId(), a, isFromPool);
             return this.AddChildWithId<T, A>(this.GetId(), a, isFromPool);
         }
         }
 
 
+        [EnableAccessEntiyChild]
         public new T AddChild<T, A, B>(A a, B b, bool isFromPool = false) where T : LSEntity, IAwake<A, B>
         public new T AddChild<T, A, B>(A a, B b, bool isFromPool = false) where T : LSEntity, IAwake<A, B>
         {
         {
             return this.AddChildWithId<T, A, B>(this.GetId(), a, b, isFromPool);
             return this.AddChildWithId<T, A, B>(this.GetId(), a, b, isFromPool);
         }
         }
 
 
+        [EnableAccessEntiyChild]
         public new T AddChild<T, A, B, C>(A a, B b, C c, bool isFromPool = false) where T : LSEntity, IAwake<A, B, C>
         public new T AddChild<T, A, B, C>(A a, B b, C c, bool isFromPool = false) where T : LSEntity, IAwake<A, B, C>
         {
         {
             return this.AddChildWithId<T, A, B, C>(this.GetId(), a, b, c, isFromPool);
             return this.AddChildWithId<T, A, B, C>(this.GetId(), a, b, c, isFromPool);

+ 1 - 0
Unity/Assets/Scripts/Model/Share/LockStep/LSInputComponent.cs

@@ -1,5 +1,6 @@
 namespace ET
 namespace ET
 {
 {
+    [ComponentOf(typeof(LSUnit))]
     public class LSInputComponent: LSEntity, ILSUpdate, IAwake, ISerializeToEntity
     public class LSInputComponent: LSEntity, ILSUpdate, IAwake, ISerializeToEntity
     {
     {
         public LSInput LSInput { get; set; }
         public LSInput LSInput { get; set; }

+ 1 - 0
Unity/Assets/Scripts/Model/Share/LockStep/LSUnit.cs

@@ -1,3 +1,4 @@
+using System;
 using MongoDB.Bson.Serialization.Attributes;
 using MongoDB.Bson.Serialization.Attributes;
 using TrueSync;
 using TrueSync;
 
 

+ 1 - 1
Unity/Assets/Scripts/Model/Share/LockStep/LSUnitComponent.cs

@@ -1,6 +1,6 @@
 namespace ET
 namespace ET
 {
 {
-	[ComponentOf(typeof(Scene))]
+	[ComponentOf(typeof(LSWorld))]
 	public class LSUnitComponent: LSEntity, IAwake, ISerializeToEntity
 	public class LSUnitComponent: LSEntity, IAwake, ISerializeToEntity
 	{
 	{
 	}
 	}