31 lines
1.3 KiB
C#
31 lines
1.3 KiB
C#
using System.Text;
|
|
using Microsoft.CodeAnalysis;
|
|
|
|
namespace Polonium.Generators.Generators;
|
|
[Generator]
|
|
public class SendMessageMethodGenerator : AssetProcessGenerator
|
|
{
|
|
public override void Execute(GeneratorExecutionContext context)
|
|
{
|
|
if (context.SyntaxReceiver is not ClassSyntaxReceiver receiver)
|
|
return;
|
|
Compilation compilation = context.Compilation;
|
|
INamedTypeSymbol? ifc = compilation.GetTypeByMetadataName("Polonium.Interfaces.IMessageClient");
|
|
foreach (ClassInfo cls in GetClassesWithInterface(context, ifc))
|
|
{
|
|
StringBuilder sb = new();
|
|
sb
|
|
.AppendLine("using Polonium.Interfaces;")
|
|
.AppendLine("using System;")
|
|
.AppendLine("using Polonium.Resources;")
|
|
.AppendLine("using Polonium.MessageManager;")
|
|
.AppendLine($"namespace {cls.Namespace};")
|
|
.AppendLine($"public partial class {cls.ClassName}")
|
|
.AppendLine("{")
|
|
.AppendLine(" public event IMessageClient.MessageSentEventHandler MessageSent;")
|
|
.AppendLine(" public void SendMessage(PoloniumMessage msg) => MessageSent?.Invoke(msg); ")
|
|
.AppendLine("}");
|
|
context.AddSource($"{cls.ClassName}_MessageClient.g.cs", sb.ToString());
|
|
}
|
|
}
|
|
} |