尝试使用源生成器规范NodeModel代码逻辑

This commit is contained in:
fengjiayi
2024-10-20 12:10:57 +08:00
parent 9931fa7436
commit e38833a58c
127 changed files with 5173 additions and 1839 deletions

View File

@@ -0,0 +1,94 @@
using System.Collections.Concurrent;
using System.IO;
using System.Text;
using System.Threading.Channels;
namespace Serein.NodeFlow.Tool
{
/// <summary>
/// 捕获Console输出
/// </summary>
public class LogTextWriter : TextWriter
{
private readonly Action<string> logAction; // 更新日志UI的委托
private readonly StringWriter stringWriter = new(); // 缓存日志内容
private readonly Channel<string> logChannel = Channel.CreateUnbounded<string>(); // 日志管道
//private int writeCount = 0; // 写入计数器
//private const int maxWrites = 500; // 写入最大计数
/// <summary>
/// 定义输出委托与清除输出内容委托
/// </summary>
/// <param name="logAction"></param>
public LogTextWriter(Action<string> logAction)
{
this.logAction = logAction;
// 异步启动日志处理任务,不阻塞主线程
Task.Run(ProcessLogQueueAsync);
}
/// <summary>
/// 编码类型
/// </summary>
public override Encoding Encoding => Encoding.UTF8;
public override void Write(char value)
{
stringWriter.Write(value);
if (value == '\n')
{
EnqueueLog();
}
}
public override void Write(string? value)
{
if (string.IsNullOrWhiteSpace(value)) return;
stringWriter.Write(value);
if (value.Contains('\n'))
{
EnqueueLog();
}
}
public override void WriteLine(string? value)
{
if (string.IsNullOrWhiteSpace(value)) return;
stringWriter.WriteLine(value);
EnqueueLog();
}
/// <summary>
/// 将日志加入通道
/// </summary>
private void EnqueueLog()
{
var log = stringWriter.ToString();
stringWriter.GetStringBuilder().Clear();
if (!logChannel.Writer.TryWrite(log))
{
// 如果写入失败(不太可能),则直接丢弃日志或处理
}
}
/// <summary>
/// 异步处理日志队列
/// </summary>
/// <returns></returns>
private async Task ProcessLogQueueAsync()
{
await foreach (var log in logChannel.Reader.ReadAllAsync()) // 异步读取日志通道
{
logAction?.Invoke(log); // 执行日志写入到UI的委托
//writeCount++;
//if (writeCount >= maxWrites)
//{
// writeCount = 0; // 重置计数器
//}
}
}
}
}

View File

@@ -1,13 +1,8 @@
using Serein.Library.Api;
using Serein.Library.Attributes;
using Serein.Library.Core.NodeFlow;
using Serein.Library.Entity;
using Serein.Library.Utils;
using System;
using Serein.Library;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Reflection;
using System.Text.RegularExpressions;
namespace Serein.NodeFlow.Tool;
@@ -77,7 +72,7 @@ public static class NodeMethodDetailsHelper
Type? returnType;
bool isTask = IsGenericTask(method.ReturnType, out var taskResult);
if (attribute.MethodDynamicType == Library.Enums.NodeType.Flipflop)
if (attribute.MethodDynamicType == Library.NodeType.Flipflop)
{
if (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>))
{
@@ -126,7 +121,7 @@ public static class NodeMethodDetailsHelper
var md = new MethodDetails
var md = new MethodDetails() // 从DLL生成方法描述
{
ActingInstanceType = type,
// ActingInstance = instance,
@@ -137,7 +132,7 @@ public static class NodeMethodDetailsHelper
ParameterDetailss = explicitDataOfParameters,
ReturnType = returnType,
};
var dd = new DelegateDetails( emitMethodType, methodDelegate) ;
var dd = new DelegateDetails(emitMethodType, methodDelegate) ;
return (md, dd);
}
@@ -270,6 +265,7 @@ public static class NodeMethodDetailsHelper
/// <returns></returns>
private static string GetExplicitTypeName(Type type)
{
return type switch
{
Type t when t.IsEnum => "Select",