PacketMediator/RaiNote.PacketMediator/PacketMediatorGenerator.cs

304 lines
14 KiB
C#
Raw Normal View History

2025-01-17 08:53:44 +00:00
// Licensed to Timothy Schenk under the Apache 2.0 License.
2025-01-18 16:47:36 +00:00
using System.CodeDom.Compiler;
2025-01-17 08:53:44 +00:00
using System.Globalization;
using System.Text;
using Microsoft.CodeAnalysis;
2025-01-18 16:47:36 +00:00
using Microsoft.CodeAnalysis.CSharp;
2025-01-17 08:53:44 +00:00
using Microsoft.CodeAnalysis.CSharp.Syntax;
2025-01-18 16:47:36 +00:00
using Microsoft.CodeAnalysis.Operations;
2025-01-17 08:53:44 +00:00
using Microsoft.CodeAnalysis.Text;
namespace RaiNote.PacketMediator;
[Generator]
public class PacketMediatorGenerator : IIncrementalGenerator {
private readonly DiagnosticDescriptor _rpmGen001Diagnostic = new(
id: "RPMGen001",
title: "Struct does not implement required interface",
messageFormat:
"The struct '{0}' must implement at least one of: 'IPacket', 'IIncomingPacket', 'IOutgoingPacket', 'IBidirectionalPacket'",
category: "SourceGenerator",
DiagnosticSeverity.Error,
isEnabledByDefault: true);
public void Initialize(IncrementalGeneratorInitializationContext context) {
context.RegisterPostInitializationOutput(ctx => {
ctx.AddSource("PacketMediatorStatic.g.cs", SourceText.From(@"
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace RaiNote.PacketMediator;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false)]
public abstract class PacketIdAttribute<TPacketIdEnum> : Attribute where TPacketIdEnum : Enum
{
protected PacketIdAttribute(TPacketIdEnum code)
{
Code = code;
}
public TPacketIdEnum Code { get; }
}
public interface IPacket;
public interface IIncomingPacket : IPacket
{
public void Deserialize(byte[] data);
}
public interface IOutgoingPacket : IPacket
{
public byte[] Serialize();
}
public interface IBidirectionalPacket : IOutgoingPacket, IIncomingPacket;
public interface IPacketHandler<in TIncomingPacket, in TSession> : IPacketHandler<TSession>
where TIncomingPacket : IIncomingPacket
{
async Task<bool> IPacketHandler<TSession>.TryHandleAsync(IIncomingPacket packet, TSession session,
CancellationToken cancellationToken)
{
if (packet is not TIncomingPacket tPacket)
{
return false;
}
using var activity = new ActivitySource(nameof(PacketMediator)).StartActivity(nameof(HandleAsync));
activity?.AddTag(""Handler"", ToString());
activity?.AddTag(""Packet"", packet.ToString());
await HandleAsync(tPacket, session, cancellationToken);
return true;
}
public Task HandleAsync(TIncomingPacket packet, TSession session, CancellationToken cancellationToken);
}
public interface IPacketHandler<in TSession>
{
Task<bool> TryHandleAsync(IIncomingPacket packet, TSession session, CancellationToken cancellationToken);
}
", Encoding.UTF8));
});
// Find all struct declarations
var structsWithAttributes = context.SyntaxProvider
.CreateSyntaxProvider(
2025-01-18 16:47:36 +00:00
predicate: (node, _) => node is StructDeclarationSyntax { AttributeLists.Count: > 0},
2025-01-17 08:53:44 +00:00
transform: (syntaxContext, cancellationToken) => {
var structDeclaration = (StructDeclarationSyntax)syntaxContext.Node;
var model = syntaxContext.SemanticModel;
var symbol =
2025-01-18 16:47:36 +00:00
ModelExtensions.GetDeclaredSymbol(model, structDeclaration,
cancellationToken: cancellationToken) as
2025-01-17 08:53:44 +00:00
INamedTypeSymbol;
var requiredInterfaces = new[] {
"IPacket", "IIncomingPacket", "IOutgoingPacket", "IBidirectionalPacket"
};
var implementsInterface = symbol != null && symbol.AllInterfaces
.Any(i => requiredInterfaces.Contains(i.Name, StringComparer.Ordinal));
if (!implementsInterface)
return default;
// Check for the marker attribute
var attribute = symbol?.GetAttributes()
.FirstOrDefault(attr => {
var attrClass = attr.AttributeClass;
while (attrClass != null) {
if (string.Equals(attrClass.Name, "PacketIdAttribute", StringComparison.Ordinal) &&
string.Equals(attrClass.ContainingNamespace.ToDisplayString(),
"RaiNote.PacketMediator", StringComparison.Ordinal)) {
return true;
}
attrClass = attrClass.BaseType;
}
return false;
});
if (attribute == null) {
return default;
}
var attributeConstructorArgument = attribute.ConstructorArguments[0];
var enumType = attributeConstructorArgument.Type;
var enumValue = attributeConstructorArgument.Value;
2025-01-18 16:47:36 +00:00
2025-01-17 08:53:44 +00:00
var enumMember = enumType?.GetMembers()
.OfType<IFieldSymbol>()
.FirstOrDefault(f => f.ConstantValue?.Equals(enumValue) == true);
2025-01-18 16:47:36 +00:00
2025-01-17 08:53:44 +00:00
var enumMaxValue = enumType?.GetMembers()
.OfType<IFieldSymbol>().Max(x => x.ConstantValue);
2025-01-18 16:47:36 +00:00
2025-01-17 08:53:44 +00:00
return (symbol, structName: structDeclaration.Identifier.Text, value: enumValue, enumType,
enumMember,
enumMaxValue, implementsInterface);
})
.Where(result => result != default);
2025-01-18 16:47:36 +00:00
2025-01-17 08:53:44 +00:00
var packetHandlers = context.SyntaxProvider.CreateSyntaxProvider(
predicate: (node, _) => node is ClassDeclarationSyntax,
(syntaxContext, cancellationToken) => {
var classDeclaration = (ClassDeclarationSyntax)syntaxContext.Node;
var model = syntaxContext.SemanticModel;
var symbol =
2025-01-18 16:47:36 +00:00
ModelExtensions.GetDeclaredSymbol(model, classDeclaration, cancellationToken: cancellationToken) as
INamedTypeSymbol;
2025-01-17 08:53:44 +00:00
var packetStruct = (symbol?.Interfaces.Select(interfaceSyntax => {
if (!interfaceSyntax.Name.SequenceEqual("IPacketHandler")) {
return null;
}
2025-01-18 16:47:36 +00:00
if (interfaceSyntax.TypeArguments.Length != 2)
2025-01-17 08:53:44 +00:00
return null;
2025-01-18 16:47:36 +00:00
2025-01-17 08:53:44 +00:00
var genericStructArgument = interfaceSyntax.TypeArguments[0];
var genericSessionArgument = interfaceSyntax.TypeArguments[1];
2025-01-18 16:47:36 +00:00
2025-01-17 08:53:44 +00:00
return new IntermediatePacketStructHandlerData(genericStructArgument, genericSessionArgument);
}) ?? throw new InvalidOperationException("1")).FirstOrDefault(x => x != null);
2025-01-18 16:47:36 +00:00
if (packetStruct == null)
return null;
2025-01-17 08:53:44 +00:00
return new IntermediatePacketHandlerData(symbol, classDeclaration.Identifier.Text, packetStruct);
}).Where(result => result != null);
2025-01-18 16:47:36 +00:00
var location =
context.SyntaxProvider.CreateSyntaxProvider(predicate: static (node, _) => InterceptorPredicate(node),
transform: static (context, ct) => InterceptorTransform(context, ct))
.Where(candidate => candidate is not null);
2025-01-17 08:53:44 +00:00
var combinedResults = structsWithAttributes.Collect().Combine(packetHandlers.Collect());
// Collect and generate the dictionary
context.RegisterSourceOutput(combinedResults, (ctx, result) => {
var (packetStructs, packetHandlerDatas) = result;
var combinedInfo = packetHandlerDatas.Select(handler => {
var respectiveStruct = packetStructs.FirstOrDefault(pStruct =>
2025-01-18 16:47:36 +00:00
pStruct.structName.SequenceEqual(handler?.PacketStructHandlerData?.PacketStructSymbol.Name ??
string.Empty));
2025-01-17 08:53:44 +00:00
return (handler, respectiveStruct);
});
2025-01-18 16:47:36 +00:00
var packetHandlerData = packetHandlerDatas.FirstOrDefault();
2025-01-17 08:53:44 +00:00
var usedValues = new List<long>();
var highestValue = long.Parse(packetStructs.FirstOrDefault().enumMaxValue?.ToString());
2025-01-18 16:47:36 +00:00
var ms = new MemoryStream();
var sw = new StreamWriter(ms, Encoding.UTF8);
sw.AutoFlush = true;
2025-01-17 08:53:44 +00:00
var enumTypeString = packetStructs.FirstOrDefault().enumType?.ToDisplayString();
2025-01-18 16:47:36 +00:00
var sessionTypeString = packetHandlerData?.PacketStructHandlerData?.SessionSymbol.ToDisplayString();
sw.WriteLine($$"""
using System;
using System.Threading;
using System.Threading.Tasks;
public static class PacketHandlerMediator
{
public async static Task Handle(IServiceProvider serviceProvider, byte[] data,{{enumTypeString}} opcode ,{{packetHandlerData?.PacketStructHandlerData?.SessionSymbol}} session, CancellationToken cancellationToken){
switch(opcode)
{
""");
foreach (var ((handler, (symbol, structName, value, _, enumMember, _, implementsInterface)), i) in
combinedInfo.Select((value, i)=> (value, i))) {
2025-01-17 08:53:44 +00:00
if (!implementsInterface) {
2025-01-18 16:47:36 +00:00
var diagnostic = Diagnostic.Create(_rpmGen001Diagnostic, symbol?.Locations.FirstOrDefault(),
structName);
2025-01-17 08:53:44 +00:00
ctx.ReportDiagnostic(diagnostic);
continue;
}
2025-01-18 16:47:36 +00:00
var tempVal = long.Parse(value?.ToString()!,
2025-01-17 08:53:44 +00:00
new NumberFormatInfo());
usedValues.Add(tempVal);
2025-01-18 16:47:36 +00:00
sw.WriteLine($"""
case {enumMember}:
var packet = new {handler?.PacketStructHandlerData?.PacketStructSymbol.ToDisplayString()}();
packet.Deserialize(data);
_ = {handler?.Symbol?.ToDisplayString()}.HandleAsync(packet, session, cancellationToken);
return;
""");
2025-01-17 08:53:44 +00:00
}
2025-01-18 16:47:36 +00:00
// Forced jumptable on asm generation
2025-01-17 08:53:44 +00:00
for (long i = 0; i <= highestValue; i++) {
2025-01-18 16:47:36 +00:00
if (!usedValues.Contains(i)) {
sw.WriteLine($"""
case (({enumTypeString}){i}):
_ = StubHandler.HandleAsync(data, opcode, session, cancellationToken);
return;
""");
}
2025-01-17 08:53:44 +00:00
}
2025-01-18 16:47:36 +00:00
// TODO: allow overriding
sw.WriteLine($$"""
}
}
}
public static class StubHandler {
public static async Task HandleAsync(byte[] data,{{enumTypeString}} opcode, {{sessionTypeString}} session, CancellationToken cancellationToken) {
// Stub method
}
}
""");
sw.Flush();
ctx.AddSource("PacketHandlerMediator.g.cs", SourceText.From(sw.BaseStream, Encoding.UTF8, canBeEmbedded: true));
sw.Close();
ms.Close();
var stringWriter = new StringWriter();
var idWriter = new IndentedTextWriter(stringWriter);
idWriter.WriteLine("using Microsoft.Extensions.DependencyInjection;");
idWriter.WriteLine("public static class ServiceExtensions{");
idWriter.Indent++;
idWriter.WriteLine("public static void AddPacketHandlerServices(this IServiceCollection serviceCollection){");
idWriter.Indent++;
idWriter.WriteLine("// PacketHandler Service Generation");
foreach (var handlerData in packetHandlerDatas) {
idWriter.WriteLine($"serviceCollection.AddScoped<{handlerData?.Symbol?.ToDisplayString()}>();");
}
idWriter.Indent--;
idWriter.WriteLine("}");
idWriter.Indent--;
idWriter.WriteLine("}");
ctx.AddSource("ServiceExtensions.g.cs", SourceText.From(stringWriter.ToString(),Encoding.UTF8));
2025-01-17 08:53:44 +00:00
});
}
2025-01-18 16:47:36 +00:00
// https://andrewlock.net/creating-a-source-generator-part-11-implementing-an-interceptor-with-a-source-generator/
private static CandidateInvocation? InterceptorTransform(GeneratorSyntaxContext context, CancellationToken cancellationToken) {
if (context.Node is InvocationExpressionSyntax {
Expression: MemberAccessExpressionSyntax { Name: { } nameSyntax }
} invocation &&
context.SemanticModel.GetOperation(context.Node, cancellationToken) is IInvocationOperation targetOperation &&
targetOperation.TargetMethod is
{ Name: "AddPacketHandlerServices", ContainingNamespace: { Name: "RaiNote.PacketMediator" } }
) {
#pragma warning disable RSEXPERIMENTAL002 // / Experimental interceptable location API
if (context.SemanticModel.GetInterceptableLocation(invocation, cancellationToken: cancellationToken) is { } location) {
// Return the location details and the full type details
return new CandidateInvocation(location);
}
#pragma warning restore RSEXPERIMENTAL002
}
return null;
}
private static bool InterceptorPredicate(SyntaxNode node) {
return node is InvocationExpressionSyntax {
Expression: MemberAccessExpressionSyntax {
Name.Identifier.ValueText: "AddPacketHandlerServices"
}
};
}
#pragma warning disable RSEXPERIMENTAL002 // / Experimental interceptable location API
public record CandidateInvocation(InterceptableLocation Location);
#pragma warning restore RSEXPERIMENTAL002
2025-01-17 08:53:44 +00:00
}