using Serein.Library.Api;
using Serein.Library.Utils;
using Serein.Library;
using System.Collections.Concurrent;
using System.Reflection;
using Serein.Library.FlowNode;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace Serein.NodeFlow.Tool;
public static class NodeMethodDetailsHelper
{
///
/// 获取处理方法
///
public static IEnumerable GetMethodsToProcess(Type type)
{
return type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
.Where(m => m.GetCustomAttribute()?.Scan == true);
}
///
/// 创建方法信息/委托信息
///
/// 方法所属的类型
/// 方法信息
/// 方法所属的程序集名称
/// 创建的方法描述,用来生成节点信息
/// 方法对应的Emit动态委托
/// 指示是否创建成功
public static bool TryCreateDetails(Type type,
MethodInfo methodInfo,
string assemblyName,
[MaybeNullWhen(false)] out MethodDetails methodDetails,
[MaybeNullWhen(false)] out DelegateDetails delegateDetails)
{
var attribute = methodInfo.GetCustomAttribute();
if(attribute is null || attribute.Scan == false)
{
methodDetails = null;
delegateDetails = null;
return false;
}
var methodName = $"{assemblyName}.{type.Name}.{methodInfo.Name}";
SereinEnv.WriteLine(InfoType.INFO, "loading method : " + methodName);
// 创建参数信息
var explicitDataOfParameters = GetExplicitDataOfParameters(methodInfo.GetParameters());
//// 通过表达式树生成委托
//var methodDelegate = GenerateMethodDelegate(type, // 方法所在的对象类型
// method, // 方法信息
// method.GetParameters(),// 方法参数
// method.ReturnType);// 返回值
Type? returnType;
bool isTask = IsGenericTask(methodInfo.ReturnType, out var taskResult);
if (attribute.MethodDynamicType == Library.NodeType.Flipflop)
{
if (methodInfo.ReturnType.IsGenericType && methodInfo.ReturnType.GetGenericTypeDefinition() == typeof(Task<>))
{
// 获取 Task<> 的泛型参数类型
var innerType = methodInfo.ReturnType.GetGenericArguments()[0];
if (innerType.IsGenericType && innerType.GetGenericTypeDefinition() == typeof(IFlipflopContext<>))
{
var flipflopType = innerType.GetGenericArguments()[0];
returnType = flipflopType;
}
else
{
SereinEnv.WriteLine(InfoType.WARN, $"[{methodName}]跳过创建,返回类型非预期的Task>。");
methodDetails = null;
delegateDetails = null;
return false;
}
}
else
{
SereinEnv.WriteLine(InfoType.WARN, $"[{methodName}]跳过创建,因为触发器方法的返回值并非Task<>,将无法等待。");
methodDetails = null;
delegateDetails = null;
return false;
}
//if (!isTask || taskResult != typeof(IFlipflopContext