redesign: Agents
This commit is contained in:
57
src/Generators/PoloniumFactoryGenerator.cs
Normal file
57
src/Generators/PoloniumFactoryGenerator.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
|
||||
namespace Polonium.Generators.Generators;
|
||||
[Generator]
|
||||
public class PoloniumFactoryGenerator : AssetProcessGenerator
|
||||
{
|
||||
public override void Execute(GeneratorExecutionContext context)
|
||||
{
|
||||
if (!(context.SyntaxReceiver is ClassSyntaxReceiver receiver))
|
||||
return;
|
||||
StringBuilder sb = new();
|
||||
sb
|
||||
.AppendLine("using System;")
|
||||
.AppendLine("using Polonium.Attributes;")
|
||||
.AppendLine("using Polonium.Interfaces;")
|
||||
.AppendLine("using Polonium.Resources;")
|
||||
.AppendLine("[AutoRegister]")
|
||||
.AppendLine("public class PoloniumFactoryRegister")
|
||||
.AppendLine("{")
|
||||
.AppendLine(" public static void Register()")
|
||||
.AppendLine(" {")
|
||||
.AppendLine(" GlobalRegistry.PoloniumFactory = new PoloniumFactory();")
|
||||
.AppendLine(" }")
|
||||
.AppendLine(" public class PoloniumFactory : IPoloniumFactory")
|
||||
.AppendLine(" {")
|
||||
.AppendLine(" public SignalHeader CreateSignalHeader()")
|
||||
.AppendLine(" {");
|
||||
Compilation compilation = context.Compilation;
|
||||
INamedTypeSymbol? signalHeader = compilation.GetTypeByMetadataName("Polonium.Resources.SignalHeader");
|
||||
|
||||
bool founded = false;
|
||||
foreach (ClassDeclarationSyntax cls in receiver.Classes)
|
||||
{
|
||||
SemanticModel semanticModel = compilation.GetSemanticModel(cls.SyntaxTree);
|
||||
INamedTypeSymbol? nCls = semanticModel.GetDeclaredSymbol(cls) as INamedTypeSymbol;
|
||||
if(nCls is null)
|
||||
continue;
|
||||
if (IsDerivedFrom(nCls, signalHeader) && !nCls.IsAbstract)
|
||||
{
|
||||
sb.AppendLine($" return new {nCls.ToDisplayString()}();");
|
||||
founded = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!founded)
|
||||
sb.AppendLine($" throw new NotImplementedException();");
|
||||
|
||||
sb
|
||||
.AppendLine(" }")
|
||||
.AppendLine(" }")
|
||||
.AppendLine("}");
|
||||
|
||||
context.AddSource("PoloniumFactoryRegistry.g.cs", sb.ToString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user