add: MessageBus
This commit is contained in:
31
src/Generators/SendMessageMethodGenerator.cs
Normal file
31
src/Generators/SendMessageMethodGenerator.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user