add: MessageBus
This commit is contained in:
@@ -113,6 +113,41 @@ public abstract class AssetProcessGenerator : ISourceGenerator
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected IEnumerable<ClassInfo> GetClassesWithInterface(GeneratorExecutionContext context, INamedTypeSymbol? ifc, bool direct = true)
|
||||||
|
{
|
||||||
|
if(context.SyntaxReceiver is not ClassSyntaxReceiver receiver)
|
||||||
|
yield break;
|
||||||
|
Compilation compilation = context.Compilation;
|
||||||
|
foreach (ClassDeclarationSyntax? declaration in receiver.Classes)
|
||||||
|
{
|
||||||
|
SemanticModel model = compilation.GetSemanticModel(declaration.SyntaxTree);
|
||||||
|
INamedTypeSymbol? sClassSymbol = model.GetDeclaredSymbol(declaration) as INamedTypeSymbol;
|
||||||
|
string filePath = declaration.SyntaxTree.FilePath;
|
||||||
|
string className = Path.GetFileNameWithoutExtension(filePath);
|
||||||
|
bool interfaceFound = false;
|
||||||
|
if (sClassSymbol is null)
|
||||||
|
yield break;
|
||||||
|
if (direct)
|
||||||
|
interfaceFound = sClassSymbol.Interfaces.Any(i => SymbolEqualityComparer.Default.Equals(i, ifc));
|
||||||
|
else
|
||||||
|
interfaceFound = sClassSymbol.AllInterfaces.Any(i => SymbolEqualityComparer.Default.Equals(i, ifc));
|
||||||
|
|
||||||
|
if (interfaceFound)
|
||||||
|
{
|
||||||
|
yield return new ClassInfo
|
||||||
|
{
|
||||||
|
DeclarationSyntax = declaration,
|
||||||
|
ClassName = className,
|
||||||
|
ClassFullName = sClassSymbol.ToDisplayString(),
|
||||||
|
Path = filePath,
|
||||||
|
Symbol = sClassSymbol,
|
||||||
|
Namespace = sClassSymbol.ContainingNamespace.ToDisplayString()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected static bool HasAttribute(INamedTypeSymbol? type, INamedTypeSymbol? attr)
|
protected static bool HasAttribute(INamedTypeSymbol? type, INamedTypeSymbol? attr)
|
||||||
{
|
{
|
||||||
if (type is null)
|
if (type is null)
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ public class PoloniumFactoryGenerator : AssetProcessGenerator
|
|||||||
.AppendLine("using Polonium.Attributes;")
|
.AppendLine("using Polonium.Attributes;")
|
||||||
.AppendLine("using Polonium.Interfaces;")
|
.AppendLine("using Polonium.Interfaces;")
|
||||||
.AppendLine("using Polonium.Resources;")
|
.AppendLine("using Polonium.Resources;")
|
||||||
|
.AppendLine("using Polonium.MessageManager;")
|
||||||
.AppendLine("[AutoRegister]")
|
.AppendLine("[AutoRegister]")
|
||||||
.AppendLine("public class PoloniumFactoryRegister")
|
.AppendLine("public class PoloniumFactoryRegister")
|
||||||
.AppendLine("{")
|
.AppendLine("{")
|
||||||
@@ -25,10 +26,10 @@ public class PoloniumFactoryGenerator : AssetProcessGenerator
|
|||||||
.AppendLine(" }")
|
.AppendLine(" }")
|
||||||
.AppendLine(" public class PoloniumFactory : IPoloniumFactory")
|
.AppendLine(" public class PoloniumFactory : IPoloniumFactory")
|
||||||
.AppendLine(" {")
|
.AppendLine(" {")
|
||||||
.AppendLine(" public SignalHeader CreateSignalHeader()")
|
.AppendLine(" public MessageHeader CreateMessageHeader()")
|
||||||
.AppendLine(" {");
|
.AppendLine(" {");
|
||||||
Compilation compilation = context.Compilation;
|
Compilation compilation = context.Compilation;
|
||||||
INamedTypeSymbol? signalHeader = compilation.GetTypeByMetadataName("Polonium.Resources.SignalHeader");
|
INamedTypeSymbol? signalHeader = compilation.GetTypeByMetadataName("Polonium.MessageManager.MessageHeader");
|
||||||
|
|
||||||
bool founded = false;
|
bool founded = false;
|
||||||
foreach (ClassDeclarationSyntax cls in receiver.Classes)
|
foreach (ClassDeclarationSyntax cls in receiver.Classes)
|
||||||
|
|||||||
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