|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.Immutable; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading; |
| 7 | +using Lagrange.Milky.Generator.Extensions; |
| 8 | +using Lagrange.Milky.Generator.Utilities; |
| 9 | +using Microsoft.CodeAnalysis; |
| 10 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 11 | + |
| 12 | +namespace Lagrange.Milky.Generator.Events; |
| 13 | + |
| 14 | +[Generator(LanguageNames.CSharp)] |
| 15 | +public class EventExtensionGenerator : IIncrementalGenerator |
| 16 | +{ |
| 17 | + private const string EventConverterAttributeTypeName = "Lagrange.Milky.Events.Attributes.EventConverterAttribute"; |
| 18 | + private const string IEventConverterTypeName = "Lagrange.Milky.Events.Converters.IEventConverter`2"; |
| 19 | + |
| 20 | + public void Initialize(IncrementalGeneratorInitializationContext context) |
| 21 | + { |
| 22 | + var converterInfos = context.SyntaxProvider.ForAttributeWithMetadataName( |
| 23 | + EventConverterAttributeTypeName, |
| 24 | + predicate: static (node, _) => node is ClassDeclarationSyntax, |
| 25 | + transform: GetConverterInfo |
| 26 | + ).Collect(); |
| 27 | + |
| 28 | + context.RegisterSourceOutput( |
| 29 | + converterInfos, |
| 30 | + static (context, converterInfos) => |
| 31 | + { |
| 32 | + var validConverterInfos = FilterConverterInfoAndReportDiagnostics(context, converterInfos); |
| 33 | + |
| 34 | + string generatedCode = GenerateSourceCode(validConverterInfos); |
| 35 | + context.AddSource("Lagrange.Milky.Events.Extensions.EventExtension.g.cs", generatedCode); |
| 36 | + } |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + private (string, string?, long, int, Location?) GetConverterInfo(GeneratorAttributeSyntaxContext context, CancellationToken ct) |
| 41 | + { |
| 42 | + var compilation = context.SemanticModel.Compilation; |
| 43 | + var converterSymbol = (INamedTypeSymbol)context.TargetSymbol; |
| 44 | + |
| 45 | + string converterTypeName = converterSymbol.ToDisplayString(); |
| 46 | + |
| 47 | + var namedArguments = context.Attributes.First().NamedArguments; |
| 48 | + long priority = (long?)namedArguments |
| 49 | + .FirstOrDefault(a => a.Key == "Priority") |
| 50 | + .Value |
| 51 | + .Value |
| 52 | + ?? 0; |
| 53 | + int lifetime = (int?)namedArguments |
| 54 | + .FirstOrDefault(a => a.Key == "Lifetime") |
| 55 | + .Value |
| 56 | + .Value |
| 57 | + ?? 0; |
| 58 | + |
| 59 | + var converterInterfaceSymbol = compilation.GetTypeByMetadataName(IEventConverterTypeName); |
| 60 | + string? eventTypeName = converterSymbol.AllInterfaces |
| 61 | + .FirstOrDefault(s => s.OriginalDefinition.DefaultEquals(converterInterfaceSymbol))? |
| 62 | + .TypeArguments |
| 63 | + .FirstOrDefault()? |
| 64 | + .ToDisplayString(); |
| 65 | + |
| 66 | + var location = converterSymbol.Locations.FirstOrDefault(); |
| 67 | + |
| 68 | + return (converterTypeName, eventTypeName, priority, lifetime, location); |
| 69 | + } |
| 70 | + |
| 71 | + private static IEnumerable<(string, string, long, int)> FilterConverterInfoAndReportDiagnostics(SourceProductionContext context, ImmutableArray<(string, string?, long, int, Location?)> converterInfos) |
| 72 | + { |
| 73 | + var eventTypeNames = new HashSet<(string, long)>(StringLongTupleComparer.Default); |
| 74 | + var result = new List<(string, string, long, int)>(); |
| 75 | + |
| 76 | + foreach ((string? converterTypeName, string? eventTypeName, long priority, int lifetime, var location) in converterInfos) |
| 77 | + { |
| 78 | + if (eventTypeName is null) |
| 79 | + { |
| 80 | + context.ReportDiagnostic(Diagnostic.Create( |
| 81 | + EventDiagnosticDescriptors.MustImplementInterfaceError, |
| 82 | + location, |
| 83 | + converterTypeName |
| 84 | + )); |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + if (!eventTypeNames.Add((eventTypeName, priority))) |
| 89 | + { |
| 90 | + context.ReportDiagnostic(Diagnostic.Create( |
| 91 | + EventDiagnosticDescriptors.DuplicateEventSerializerError, |
| 92 | + location, |
| 93 | + eventTypeName, |
| 94 | + converterTypeName, |
| 95 | + priority |
| 96 | + )); |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + result.Add((converterTypeName, eventTypeName, priority, lifetime)); |
| 101 | + } |
| 102 | + |
| 103 | + return result; |
| 104 | + } |
| 105 | + |
| 106 | + private static string GenerateSourceCode(IEnumerable<(string, string, long Priority, int)> converterInfos) |
| 107 | + { |
| 108 | + converterInfos = [.. converterInfos.OrderBy(i => i.Priority)]; |
| 109 | + |
| 110 | + var builder = new StringBuilder(); |
| 111 | + |
| 112 | + builder.AppendLine("// <auto-generated/>"); |
| 113 | + builder.AppendLine(); |
| 114 | + builder.AppendLine("namespace Lagrange.Milky.Events.Extensions;"); |
| 115 | + builder.AppendLine(); |
| 116 | + builder.AppendLine("public static partial class EventExtension"); |
| 117 | + builder.AppendLine("{"); |
| 118 | + builder.AppendLine(" public static partial void RegisterConvertibleEvents(this Lagrange.Core.BotContext lagrange, Lagrange.Milky.Events.IGenericEventHandler handler)"); |
| 119 | + builder.AppendLine(" {"); |
| 120 | + foreach ((_, string? eventTypeName, _, _) in converterInfos) |
| 121 | + { |
| 122 | + builder.AppendLine($" lagrange.EventInvoker.RegisterEvent<{eventTypeName}>(handler.OnEvent);"); |
| 123 | + } |
| 124 | + builder.AppendLine(" }"); |
| 125 | + builder.AppendLine(); |
| 126 | + builder.AppendLine(" public static partial void UnregisterConvertibleEvents(this Lagrange.Core.BotContext lagrange, Lagrange.Milky.Events.IGenericEventHandler handler)"); |
| 127 | + builder.AppendLine(" {"); |
| 128 | + foreach ((_, string? eventTypeName, _, _) in converterInfos) |
| 129 | + { |
| 130 | + builder.AppendLine($" lagrange.EventInvoker.UnregisterEvent<{eventTypeName}>(handler.OnEvent);"); |
| 131 | + } |
| 132 | + builder.AppendLine(" }"); |
| 133 | + builder.AppendLine(); |
| 134 | + builder.AppendLine(" public static partial Microsoft.Extensions.DependencyInjection.IServiceCollection AddEventConverters(this Microsoft.Extensions.DependencyInjection.IServiceCollection services)"); |
| 135 | + builder.AppendLine(" {"); |
| 136 | + foreach (var (converterName, eventName, _, lifetime) in converterInfos) |
| 137 | + { |
| 138 | + builder.AppendLine($" services.Add(new Microsoft.Extensions.DependencyInjection.ServiceDescriptor(typeof(Lagrange.Milky.Events.Converters.IEventConverter<{eventName}>), typeof({converterName}), (Microsoft.Extensions.DependencyInjection.ServiceLifetime){lifetime}));"); |
| 139 | + } |
| 140 | + builder.AppendLine(" return services;"); |
| 141 | + builder.AppendLine(" }"); |
| 142 | + builder.AppendLine("}"); |
| 143 | + |
| 144 | + return builder.ToString(); |
| 145 | + } |
| 146 | +} |
0 commit comments