-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
AsyncEntityGenerator.cs
397 lines (328 loc) · 19.3 KB
/
AsyncEntityGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
using System;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
namespace AltV.Net.Async.CodeGen
{
internal static class TypeSymbolExtensions
{
internal static string GetGlobalName(this ITypeSymbol symbol)
{
return symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
}
}
internal class SyntaxTreeReceiver : ISyntaxReceiver
{
public readonly List<ClassDeclarationSyntax> Classes = new();
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
if (syntaxNode is ClassDeclarationSyntax classSyntax &&
classSyntax.AttributeLists.Any(l =>
l.Attributes.Any(a => a.Name.ToString() is "AsyncEntity" or "AsyncEntityAttribute")))
{
Classes.Add(classSyntax);
}
}
}
[Generator]
public class AsyncEntityGenerator : ISourceGenerator
{
private static string Indent(string text, int n = 1)
{
return string.Join("\n", text.Split('\n').Select(s => new string(' ', n * 4) + s));
}
private static string FormatAttributes(IEnumerable<AttributeData> attributes)
{
return string.Join(" ", attributes.Select(a => $"[global::{a}]"));
}
private const string AttributeText = @"
using System;
using AltV.Net.Async;
namespace AltV.Net.Async.CodeGen {
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
sealed class AsyncEntityAttribute : Attribute
{
public AsyncEntityAttribute(Type interfaceType)
{
}
}
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false)]
sealed class AsyncPropertyAttribute : Attribute
{
public bool ThreadSafe { get; set; } = false;
public AsyncPropertyAttribute()
{
}
}
}";
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => new SyntaxTreeReceiver());
}
private static IList<INamedTypeSymbol> GetBaseTypes(INamedTypeSymbol @class)
{
var list = new List<INamedTypeSymbol>();
var currentClass = @class;
while (currentClass.BaseType is { } type)
{
list.Add(type);
currentClass = type;
}
return list;
}
public void Execute(GeneratorExecutionContext context)
{
var receiver = (SyntaxTreeReceiver) context.SyntaxReceiver!;
var sourceBuilder = new StringBuilder(AttributeText);
var options = (CSharpParseOptions) ((CSharpCompilation) context.Compilation).SyntaxTrees[0].Options;
Compilation compilation =
context.Compilation.AddSyntaxTrees(
CSharpSyntaxTree.ParseText(SourceText.From(AttributeText, Encoding.UTF8), options));
var attributeSymbol = compilation.GetTypeByMetadataName("AltV.Net.Async.CodeGen.AsyncEntityAttribute");
var propertyAttributeSymbol =
compilation.GetTypeByMetadataName("AltV.Net.Async.CodeGen.AsyncPropertyAttribute");
var validClasses = new List<INamedTypeSymbol>();
foreach (var classSyntax in receiver.Classes)
{
var @class = compilation.GetSemanticModel(classSyntax.SyntaxTree).GetDeclaredSymbol(classSyntax)!;
if (!classSyntax.Modifiers.Any(SyntaxKind.PartialKeyword))
{
context.ReportDiagnostic(Diagnostic.Create(Diagnostics.AsyncEntityClassShouldBePartial,
@class.Locations.FirstOrDefault(), @class.ToString()));
continue;
}
var attribute = @class.GetAttributes().FirstOrDefault(a =>
a.AttributeClass?.Equals(attributeSymbol, SymbolEqualityComparer.Default) == true);
if (attribute is null || attribute.ConstructorArguments.Length != 1)
continue; // that's some other async entity decorator, skipping
var @interface = (INamedTypeSymbol?) attribute.ConstructorArguments[0].Value;
if (@interface is null || [email protected](@interface, SymbolEqualityComparer.Default))
{
context.ReportDiagnostic(Diagnostic.Create(Diagnostics.AsyncEntityShouldImplementInterface,
@class.Locations.FirstOrDefault(), @class.ToString(), @interface?.ToString() ?? ""));
continue;
}
if (@interface.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is InterfaceDeclarationSyntax
interfaceSyntax && !interfaceSyntax.Modifiers.Any(SyntaxKind.PartialKeyword))
{
context.ReportDiagnostic(Diagnostic.Create(Diagnostics.AsyncEntityInterfaceShouldBePartial,
@interface.Locations.FirstOrDefault(), @interface.ToString()));
continue;
}
var baseType = @class.BaseType;
if (baseType is not null)
while (!baseType!.ToString().StartsWith("AltV.Net.Elements.Entities."))
baseType = baseType.BaseType;
if (baseType is null)
{
context.ReportDiagnostic(Diagnostic.Create(Diagnostics.AsyncEntityShouldImplementEntity,
@class.Locations.FirstOrDefault(), @class.ToString()));
continue;
}
validClasses.Add(@class);
var classBaseDeclaration = @class.BaseType!.ToString().StartsWith("AltV.Net.Elements.Entities.")
? ""
: " : " + @class.BaseType;
var members = new List<string>();
foreach (var member in @interface.GetMembers())
{
var classMember = @class.FindImplementationForInterfaceMember(member);
// does member hide base class property (new keyword)
var isNew = classMember?.DeclaringSyntaxReferences.Any(s =>
s.GetSyntax() is MemberDeclarationSyntax declarationSyntax &&
declarationSyntax.Modifiers.Any(SyntaxKind.NewKeyword)) ?? false;
var propertyAttribute = classMember?.GetAttributes().FirstOrDefault(a =>
a.AttributeClass?.Equals(propertyAttributeSymbol, SymbolEqualityComparer.Default) == true);
var propertySettings = propertyAttribute?.NamedArguments
.ToDictionary(e => e.Key, e => e.Value) ??
new Dictionary<string, TypedConstant>();
switch (member)
{
case IPropertySymbol property:
{
if (classMember is not IPropertySymbol classProperty)
{
context.ReportDiagnostic(Diagnostic.Create(
Diagnostics.AsyncEntityInterfacePropertyNotFoundInClass,
member.Locations.FirstOrDefault(), member.Name, @interface.ToString(),
@class.ToString()));
continue;
}
if (property.GetMethod is not null &&
classProperty.GetMethod?.DeclaredAccessibility != Accessibility.Public)
{
context.ReportDiagnostic(Diagnostic.Create(Diagnostics.AsyncEntityGetterNotFound,
member.Locations.FirstOrDefault(), member.Name, @interface.ToString(),
@class.ToString()));
continue;
}
if (property.SetMethod is not null &&
classProperty.SetMethod?.DeclaredAccessibility != Accessibility.Public)
{
context.ReportDiagnostic(Diagnostic.Create(Diagnostics.AsyncEntitySetterNotFound,
member.Locations.FirstOrDefault(), member.Name, @interface.ToString(),
@class.ToString()));
continue;
}
if (property.GetMethod is not null || property.SetMethod is not null)
{
var getter = $"=> CustomBaseObject.{member.Name}; ";
var setter = $"=> CustomBaseObject.{member.Name} = value; ";
if (propertySettings.TryGetValue("ThreadSafe", out var threadSafe) &&
threadSafe.ToCSharpString() == "true")
{
getter = $"{{ lock (CustomBaseObject) return AsyncContext.CheckIfExistsNullable(CustomBaseObject) ? CustomBaseObject.{member.Name} : default; }}";
setter = $"{{ lock (CustomBaseObject) if (AsyncContext.CheckIfExistsNullable(CustomBaseObject)) CustomBaseObject.{member.Name} = value; }}";
}
var propertyValue = "";
if (property.GetMethod is not null)
propertyValue += $"\n get {getter} ";
if (property.SetMethod is not null && !property.SetMethod.IsInitOnly)
propertyValue += $"\n set {setter} ";
else if (property.SetMethod is not null)
propertyValue +=
@$"init => throw new global::System.MemberAccessException(""Manual construction of Async class is prohibited. Property {property.Name} is marked as init-only and therefore cannot be set on the async entity.""); ";
var attributes = classProperty.GetAttributes().Where(a => !a.Equals(propertyAttribute)).ToArray();
var formattedAttributes =
attributes.Length == 0 ? "" : FormatAttributes(attributes) + "\n";
var @new = isNew ? "new " : "";
var newline = property.GetMethod is not null && property.SetMethod is not null
? "\n"
: "";
members.Add(formattedAttributes +
$"public {@new}{property.Type.GetGlobalName()} {member.Name} {{ {propertyValue}{newline}}}");
}
break;
}
case IMethodSymbol {MethodKind: MethodKind.Ordinary}:
{
if (classMember is not IMethodSymbol
{
DeclaredAccessibility: Accessibility.Public
} classMethod)
{
context.ReportDiagnostic(Diagnostic.Create(
Diagnostics.AsyncEntityInterfaceMethodNotFoundInClass,
member.Locations.FirstOrDefault(), member.Name, @interface.ToString(),
@class.ToString()));
continue;
}
// arguments to arguments list
var argumentsList = new string[classMethod.Parameters.Length];
// arguments to provide to call the base method
var callArgumentsList = new string[classMethod.Parameters.Length];
for (var index = 0; index < classMethod.Parameters.Length; index++)
{
var methodParameter = classMethod.Parameters[index];
var parameterAttrs = methodParameter.GetAttributes();
// ToString of methodParameter gives ref/out/in and type, without a name
var modifier = methodParameter.RefKind switch
{
RefKind.Out => "out ",
RefKind.Ref => "ref ",
RefKind.In => "in ",
_ => ""
};
argumentsList[index] =
(parameterAttrs.Length == 0
? ""
: FormatAttributes(methodParameter.GetAttributes()) + " ") + modifier + methodParameter.Type.GetGlobalName() +
" " + methodParameter.Name;
callArgumentsList[index] = modifier + methodParameter.Name;
}
var constraintClauses = "";
foreach (var typeParameter in classMethod.TypeParameters)
{
var constraintDeclarations = new List<string>();
if (typeParameter.HasReferenceTypeConstraint) constraintDeclarations.Add("class");
if (typeParameter.HasValueTypeConstraint) constraintDeclarations.Add("struct");
if (typeParameter.HasUnmanagedTypeConstraint) constraintDeclarations.Add("unmanaged");
if (typeParameter.HasNotNullConstraint) constraintDeclarations.Add("notnull");
constraintDeclarations.AddRange(typeParameter.ConstraintTypes.Select(e => e.GetGlobalName()));
if (typeParameter.HasConstructorConstraint) constraintDeclarations.Add("new()");
if (constraintDeclarations.Count == 0) continue;
constraintClauses +=
$"where {typeParameter.Name} : {string.Join(", ", constraintDeclarations)} ";
}
var genericTypes = "";
if (classMethod.TypeParameters.Length > 0)
{
genericTypes = "<" + string.Join(", ", classMethod.TypeParameters.Select(t => t.ToString())) + ">";
}
var arguments = string.Join(", ", argumentsList);
var callArguments = string.Join(", ", callArgumentsList);
var returnAction = classMethod.ReturnsVoid ? "" : "return ";
var name = member.Name;
var attributes = classMethod.GetAttributes().Where(a => !a.Equals(propertyAttribute)).ToArray();
var formattedAttributes = attributes.Length == 0 ? "" : FormatAttributes(attributes) + "\n";
var @new = isNew ? "new " : "";
var methodCall = $"CustomBaseObject.{name}{genericTypes}({callArguments})";
var methodValue = "";
if (propertySettings.TryGetValue("ThreadSafe", out var threadSafe) &&
threadSafe.ToCSharpString() == "true")
{
if (classMethod.ReturnsVoid)
methodValue =
$"AsyncContext.RunOnMainThreadBlockingAndRunAll(() => {methodCall});";
else
methodValue =
$"{classMethod.ReturnType} res = default; AsyncContext.RunOnMainThreadBlockingAndRunAll(() => res = {methodCall}); return res;";
}
else
{
methodValue = $"{returnAction}{methodCall};";
}
members.Add(formattedAttributes +
$"public {@new}{classMethod.ReturnType.GetGlobalName()} {name}{genericTypes}({arguments}) {constraintClauses}\n{{\n{Indent(methodValue)}\n}}");
break;
}
}
}
var asyncEntityType = "AltV.Net.Async.Elements.Entities.Async" + baseType.Name;
var interfaceDeclaration =
$"public partial interface {@interface.Name} : AltV.Net.Async.IAsyncConvertible<{@interface.Name}> {{ }}";
if (@interface.ContainingNamespace?.ToString() is { } interfaceNamespace)
interfaceDeclaration = $"namespace {interfaceNamespace} {{\n{Indent(interfaceDeclaration)}\n}}";
var classDeclaration = $@"
public partial class {@class.Name}{classBaseDeclaration} {{
public {@interface} ToAsync(AltV.Net.Async.IAsyncContext asyncContext) {{
return new Async(this, asyncContext);
}}
public {@interface} ToAsync() {{
return new Async(this, null);
}}
private class Async : {asyncEntityType}, {@interface} {{
public Async({@interface} player, AltV.Net.Async.IAsyncContext? asyncContext) : base(player, asyncContext) {{
this.CustomBaseObject = player;
}}
private readonly {@interface} CustomBaseObject;
public new {@interface} ToAsync(AltV.Net.Async.IAsyncContext asyncContext) {{
return asyncContext == AsyncContext ? this : new Async(CustomBaseObject, asyncContext);
}}
public {@interface} ToAsync() {{
return this;
}}
{Indent("\n" + string.Join("\n", members), 2)}
}}
}}";
if (@class.ContainingNamespace?.ToString() is { } classNamespace)
classDeclaration = $"\nnamespace {classNamespace} {{{Indent(classDeclaration)}\n}}";
sourceBuilder.Append($"\n\n{interfaceDeclaration}\n{classDeclaration}");
}
foreach (var @class in validClasses)
{
var types = GetBaseTypes(@class);
var invalid = types.FirstOrDefault(e => validClasses.Contains(e, SymbolEqualityComparer.Default));
if (invalid is not { } invalidClass) continue;
context.ReportDiagnostic(Diagnostic.Create(Diagnostics.AsyncEntityCannotBeNested,
@class.Locations.FirstOrDefault(), @class.ToString(), invalidClass.ToString()));
}
context.AddSource("GeneratedAsyncEntities.cs", SourceText.From(sourceBuilder.ToString(), Encoding.UTF8));
}
}
}