Skip to content

Commit c69ecf4

Browse files
committed
feat:同步ET更新
1 parent 057557b commit c69ecf4

File tree

12 files changed

+300
-89
lines changed

12 files changed

+300
-89
lines changed

Robot/Model/Robot.Model.csproj

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
<Compile Include="..\..\Server\Model\Base\StructBsonSerialize.cs">
3232
<Link>Base\StructBsonSerialize.cs</Link>
3333
</Compile>
34+
<Compile Include="..\..\Server\Model\Config\ConfigLoader.cs">
35+
<Link>Config\ConfigLoader.cs</Link>
36+
</Compile>
3437
<Compile Include="..\..\Unity\Codes\Model\Module\Config\**\*.cs">
3538
<Link>Module\Config\%(RecursiveDir)%(FileName)%(Extension)</Link>
3639
</Compile>
@@ -119,7 +122,7 @@
119122
<ProjectReference Include="..\..\ThirdParty\ETTask\ETTask.csproj" />
120123
<ProjectReference Include="..\..\ThirdParty\ShareLib\ShareLib.csproj" />
121124
<ProjectReference Include="..\..\ThirdParty\UnityEngine\UnityEngine.csproj" />
122-
<ProjectReference Include="..\..\Tools\Analyzer\Analyzer.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
125+
<ProjectReference Include="..\..\Tools\Analyzer\Analyzer.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
123126
</ItemGroup>
124127

125128
</Project>
File renamed without changes.

Tools/Analyzer/Analyzer/AddChildTypeAnalyzer.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class AddChildTypeAnalyzer: DiagnosticAnalyzer
1818

1919
private const string Description = "请使用被允许的ChildType 或添加该类型至ChildType.";
2020

21-
private static readonly string[] AddChildMethods = new[] { "AddChild", "AddChildWithId" };
21+
private static readonly string[] AddChildMethods = { "AddChild", "AddChildWithId" };
2222

2323
private static readonly DiagnosticDescriptor Rule =
2424
new DiagnosticDescriptor(DiagnosticIds.AddChildTypeAnalyzerRuleId,
@@ -56,7 +56,7 @@ private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context)
5656
}
5757

5858
// 筛选出 AddChild函数syntax
59-
var methodName = memberAccessExpressionSyntax.Name.Identifier.Text;
59+
string methodName = memberAccessExpressionSyntax.Name.Identifier.Text;
6060
if (!AddChildMethods.Contains(methodName))
6161
{
6262
return;
@@ -68,7 +68,7 @@ private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context)
6868
return;
6969
}
7070

71-
var identifierSyntax = memberAccessExpressionSyntax.GetFirstChild<IdentifierNameSyntax>();
71+
IdentifierNameSyntax? identifierSyntax = memberAccessExpressionSyntax.GetFirstChild<IdentifierNameSyntax>();
7272
if (identifierSyntax == null)
7373
{
7474
return;
@@ -149,7 +149,7 @@ private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context)
149149
// addChild为非泛型调用
150150
else
151151
{
152-
var firstArgumentSyntax = invocationExpressionSyntax.GetFirstChild<ArgumentListSyntax>()?.GetFirstChild<ArgumentSyntax>()
152+
SyntaxNode? firstArgumentSyntax = invocationExpressionSyntax.GetFirstChild<ArgumentListSyntax>()?.GetFirstChild<ArgumentSyntax>()
153153
?.ChildNodes().First();
154154
if (firstArgumentSyntax == null)
155155
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System.Collections.Immutable;
2+
using Microsoft.CodeAnalysis;
3+
using Microsoft.CodeAnalysis.Diagnostics;
4+
5+
namespace ET.Analyzer
6+
{
7+
[DiagnosticAnalyzer(LanguageNames.CSharp)]
8+
public class ClassDeclarationInHotfixAnalyzer: DiagnosticAnalyzer
9+
{
10+
private const string Title = "Hotfix程序集中 只能声明含有BaseAttribute子类特性的类或静态类";
11+
12+
private const string MessageFormat = "Hotfix程序集中 只能声明含有BaseAttribute子类特性的类或静态类 类: {0}";
13+
14+
private const string Description = "Hotfix程序集中 只能声明含有BaseAttribute子类特性的类或静态类.";
15+
16+
private const string BaseAttribute = "ET.BaseAttribute";
17+
18+
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticIds.ClassDeclarationInHotfixAnalyzerRuleId,
19+
Title,
20+
MessageFormat,
21+
DiagnosticCategories.Hotfix,
22+
DiagnosticSeverity.Error, true, Description);
23+
24+
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
25+
26+
public override void Initialize(AnalysisContext context)
27+
{
28+
if (!AnalyzerGlobalSetting.EnableAnalyzer)
29+
{
30+
return;
31+
}
32+
33+
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
34+
context.EnableConcurrentExecution();
35+
context.RegisterSymbolAction(this.Analyzer, SymbolKind.NamedType);
36+
}
37+
38+
private void Analyzer(SymbolAnalysisContext context)
39+
{
40+
if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName, AnalyzeAssembly.AllHotfix))
41+
{
42+
return;
43+
}
44+
45+
if (!(context.Symbol is INamedTypeSymbol namedTypeSymbol))
46+
{
47+
return;
48+
}
49+
50+
if (namedTypeSymbol.IsStatic)
51+
{
52+
return;
53+
}
54+
55+
if (!this.CheckIsTypeOrBaseTypeHasBaseAttributeInherit(namedTypeSymbol))
56+
{
57+
foreach (SyntaxReference? declaringSyntaxReference in namedTypeSymbol.DeclaringSyntaxReferences)
58+
{
59+
Diagnostic diagnostic = Diagnostic.Create(Rule, declaringSyntaxReference.GetSyntax()?.GetLocation(), namedTypeSymbol.Name);
60+
context.ReportDiagnostic(diagnostic);
61+
}
62+
}
63+
}
64+
65+
/// <summary>
66+
/// 检查该类或其基类是否有BaseAttribute的子类特性标记
67+
/// </summary>
68+
private bool CheckIsTypeOrBaseTypeHasBaseAttributeInherit(INamedTypeSymbol namedTypeSymbol)
69+
{
70+
INamedTypeSymbol? typeSymbol = namedTypeSymbol;
71+
while (typeSymbol != null)
72+
{
73+
if (typeSymbol.HasBaseAttribute(BaseAttribute))
74+
{
75+
return true;
76+
}
77+
78+
typeSymbol = typeSymbol.BaseType;
79+
}
80+
81+
return false;
82+
}
83+
}
84+
}

Tools/Analyzer/Analyzer/EntityClassDeclarationAnalyzer.cs

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,56 @@
11
using System.Collections.Immutable;
2-
using System.Linq;
32
using Microsoft.CodeAnalysis;
4-
using Microsoft.CodeAnalysis.CSharp;
53
using Microsoft.CodeAnalysis.Diagnostics;
64

75
namespace ET.Analyzer
86
{
97
[DiagnosticAnalyzer(LanguageNames.CSharp)]
10-
public class EntityClassDeclarationAnalyzer : DiagnosticAnalyzer
8+
public class EntityClassDeclarationAnalyzer: DiagnosticAnalyzer
119
{
1210
private const string Title = "实体类限制多层继承";
13-
11+
1412
private const string MessageFormat = "类: {0} 不能继承Entiy的子类 请直接继承Entity";
15-
13+
1614
private const string Description = "实体类限制多层继承.";
1715

1816
private const string EntityType = "ET.Entity";
19-
17+
2018
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticIds.EntityClassDeclarationAnalyzerRuleId,
2119
Title,
2220
MessageFormat,
2321
DiagnosticCategories.All,
2422
DiagnosticSeverity.Error, true, Description);
2523

2624
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
27-
25+
2826
public override void Initialize(AnalysisContext context)
2927
{
3028
if (!AnalyzerGlobalSetting.EnableAnalyzer)
3129
{
3230
return;
3331
}
32+
3433
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
3534
context.EnableConcurrentExecution();
3635
context.RegisterSymbolAction(this.Analyzer, SymbolKind.NamedType);
3736
}
38-
37+
3938
private void Analyzer(SymbolAnalysisContext context)
4039
{
41-
4240
if (!(context.Symbol is INamedTypeSymbol namedTypeSymbol))
4341
{
4442
return;
4543
}
46-
47-
44+
4845
if (namedTypeSymbol.BaseType?.BaseType?.ToString() != EntityType)
4946
{
5047
return;
5148
}
5249

53-
foreach (var declaringSyntaxReference in namedTypeSymbol.DeclaringSyntaxReferences)
50+
foreach (SyntaxReference? declaringSyntaxReference in namedTypeSymbol.DeclaringSyntaxReferences)
5451
{
55-
var classSyntax = declaringSyntaxReference.GetSyntax();
56-
Diagnostic diagnostic =Diagnostic.Create(Rule, classSyntax.GetLocation(), namedTypeSymbol.Name, context.Compilation.AssemblyName);
52+
SyntaxNode classSyntax = declaringSyntaxReference.GetSyntax();
53+
Diagnostic diagnostic = Diagnostic.Create(Rule, classSyntax.GetLocation(), namedTypeSymbol.Name, context.Compilation.AssemblyName);
5754
context.ReportDiagnostic(diagnostic);
5855
}
5956
}

Tools/Analyzer/Analyzer/EntityFiledAccessAnalyzer.cs

+40-37
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System;
2-
using System.Collections.Immutable;
1+
using System.Collections.Immutable;
32
using System.Linq;
4-
using System.Reflection.Metadata.Ecma335;
53
using Microsoft.CodeAnalysis;
64
using Microsoft.CodeAnalysis.CSharp;
75
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -10,18 +8,18 @@
108
namespace ET.Analyzer
119
{
1210
[DiagnosticAnalyzer(LanguageNames.CSharp)]
13-
public class EntityFiledAccessAnalyzer : DiagnosticAnalyzer
11+
public class EntityFiledAccessAnalyzer: DiagnosticAnalyzer
1412
{
1513
private const string Title = "实体字段访问错误";
16-
14+
1715
private const string MessageFormat = "实体: {0} 字段: {1} 只能在实体类生命周期组件或友元类(含有FriendClassAttribute)中访问";
18-
16+
1917
private const string Description = "请使用实体类属性或方法访问其他实体字段.";
20-
18+
2119
private const string EntityType = "ET.Entity";
22-
20+
2321
private const string ObjectSystemAttribute = "ET.ObjectSystemAttribute";
24-
22+
2523
private const string ISystemType = "ET.ISystemType";
2624

2725
private const string FriendClassAttribute = "ET.FriendClassAttribute";
@@ -31,37 +29,37 @@ public class EntityFiledAccessAnalyzer : DiagnosticAnalyzer
3129
MessageFormat,
3230
DiagnosticCategories.Hotfix,
3331
DiagnosticSeverity.Error, true, Description);
34-
32+
3533
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
36-
37-
34+
3835
public override void Initialize(AnalysisContext context)
3936
{
4037
if (!AnalyzerGlobalSetting.EnableAnalyzer)
4138
{
4239
return;
4340
}
41+
4442
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
4543
context.EnableConcurrentExecution();
4644
context.RegisterSyntaxNodeAction(this.AnalyzeMemberAccessExpression, SyntaxKind.SimpleMemberAccessExpression);
4745
}
4846

4947
private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context)
5048
{
51-
if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName,AnalyzeAssembly.AllHotfix))
49+
if (!AnalyzerHelper.IsAssemblyNeedAnalyze(context.Compilation.AssemblyName, AnalyzeAssembly.AllHotfix))
5250
{
5351
return;
5452
}
55-
53+
5654
if (!(context.Node is MemberAccessExpressionSyntax memberAccessExpressionSyntax))
5755
{
5856
return;
5957
}
60-
58+
6159
// -----筛选出实体类的字段symbol-----
62-
63-
var filedSymbol = context.SemanticModel.GetSymbolInfo(memberAccessExpressionSyntax).Symbol;
64-
if (filedSymbol ==null || !(filedSymbol is IFieldSymbol))
60+
61+
ISymbol? filedSymbol = context.SemanticModel.GetSymbolInfo(memberAccessExpressionSyntax).Symbol;
62+
if (filedSymbol == null || !(filedSymbol is IFieldSymbol))
6563
{
6664
return;
6765
}
@@ -71,20 +69,21 @@ private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context)
7169
return;
7270
}
7371

74-
if (filedSymbol.ContainingType.BaseType?.ToString()!= EntityType)
72+
if (filedSymbol.ContainingType.BaseType?.ToString() != EntityType)
7573
{
7674
return;
7775
}
78-
76+
7977
// -----筛选出在实体类和实体System外部字段访问-----
8078
// 实体System包括awakeSystem updateSystem等生命周期类和 componentSystem静态方法类
8179

82-
var accessFieldClassDeclaretion = memberAccessExpressionSyntax.GetParentClassDeclaration();
83-
if (accessFieldClassDeclaretion==null)
80+
ClassDeclarationSyntax? accessFieldClassDeclaretion = memberAccessExpressionSyntax.GetParentClassDeclaration();
81+
if (accessFieldClassDeclaretion == null)
8482
{
8583
return;
8684
}
87-
var accessFieldClassSymbol = context.SemanticModel.GetDeclaredSymbol(accessFieldClassDeclaretion);
85+
86+
INamedTypeSymbol? accessFieldClassSymbol = context.SemanticModel.GetDeclaredSymbol(accessFieldClassDeclaretion);
8887

8988
if (accessFieldClassSymbol == null)
9089
{
@@ -96,51 +95,55 @@ private void AnalyzeMemberAccessExpression(SyntaxNodeAnalysisContext context)
9695
{
9796
return;
9897
}
99-
98+
10099
//判断是否在实体类生命周期System中
101-
if(CheckIsEntityLifecycleSystem(accessFieldClassSymbol, filedSymbol.ContainingType))
100+
if (this.CheckIsEntityLifecycleSystem(accessFieldClassSymbol, filedSymbol.ContainingType))
102101
{
103102
return;
104103
}
105104

106105
//判断是否在实体类的友元类中
107-
if (CheckIsEntityFriendClass(accessFieldClassSymbol,filedSymbol.ContainingType))
106+
if (this.CheckIsEntityFriendClass(accessFieldClassSymbol, filedSymbol.ContainingType))
108107
{
109108
return;
110109
}
111-
112-
Diagnostic diagnostic =Diagnostic.Create(Rule, memberAccessExpressionSyntax.GetLocation(), filedSymbol.ContainingType.Name, filedSymbol.Name);
110+
111+
Diagnostic diagnostic = Diagnostic.Create(Rule, memberAccessExpressionSyntax.GetLocation(), filedSymbol.ContainingType.Name,
112+
filedSymbol.Name);
113113
context.ReportDiagnostic(diagnostic);
114114
}
115-
115+
116116
private bool CheckIsEntityLifecycleSystem(INamedTypeSymbol accessFieldClassSymbol, INamedTypeSymbol entityTypeSymbol)
117117
{
118-
if (accessFieldClassSymbol.BaseType==null ||!accessFieldClassSymbol.BaseType.IsGenericType)
118+
if (accessFieldClassSymbol.BaseType == null || !accessFieldClassSymbol.BaseType.IsGenericType)
119119
{
120120
return false;
121121
}
122+
122123
// 判断是否含有 ObjectSystem Attribute 且继承了接口 ISystemType
123124
if (accessFieldClassSymbol.BaseType.HasAttribute(ObjectSystemAttribute) && accessFieldClassSymbol.HasInterface(ISystemType))
124125
{
125126
// 获取 accessFieldClassSymbol 父类的实体类型参数
126-
var entityTypeArgumentSymbol = accessFieldClassSymbol.BaseType.TypeArguments.FirstOrDefault();
127+
ITypeSymbol? entityTypeArgumentSymbol = accessFieldClassSymbol.BaseType.TypeArguments.FirstOrDefault();
127128
if (entityTypeArgumentSymbol == null)
128129
{
129130
return false;
130131
}
131-
// 判断 accessFieldClassSymbol 父类的实体类型参数是否为 entityTypeSymbol
132-
if (entityTypeArgumentSymbol.ToString() == entityTypeSymbol.ToString())
133-
{
134-
return true;
135-
}
132+
133+
// 判断 accessFieldClassSymbol 父类的实体类型参数是否为 entityTypeSymbol
134+
if (entityTypeArgumentSymbol.ToString() == entityTypeSymbol.ToString())
135+
{
136+
return true;
137+
}
136138
}
139+
137140
return false;
138141
}
139142

140143
private bool CheckIsEntityFriendClass(INamedTypeSymbol accessFieldTypeSymbol, INamedTypeSymbol entityTypeSymbol)
141144
{
142145
var attributes = accessFieldTypeSymbol.GetAttributes();
143-
foreach (var attributeData in attributes)
146+
foreach (AttributeData? attributeData in attributes)
144147
{
145148
if (attributeData.AttributeClass?.ToString() != FriendClassAttribute)
146149
{

0 commit comments

Comments
 (0)