2024-11-03 21:17:45 +08:00
|
|
|
|
using Serein.Library;
|
|
|
|
|
|
using Serein.Library.Api;
|
|
|
|
|
|
using Serein.Library.FlowNode;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
using System.Runtime.Loader;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2024-11-03 23:51:18 +08:00
|
|
|
|
using System.Xml.Linq;
|
2024-11-03 21:17:45 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Serein.NodeFlow.Tool
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 管理加载在运行环境中的外部程序集
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class FlowLibraryManagement
|
|
|
|
|
|
{
|
|
|
|
|
|
public FlowLibraryManagement(IFlowEnvironment flowEnvironment)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.flowEnvironment = flowEnvironment;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private readonly IFlowEnvironment flowEnvironment;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 缓存所有加载了的程序集
|
|
|
|
|
|
/// </summary>
|
2024-11-03 23:51:18 +08:00
|
|
|
|
private readonly ConcurrentDictionary<string, FlowLibrary> _myFlowLibrarys = new ConcurrentDictionary<string, FlowLibrary>();
|
2024-11-03 21:17:45 +08:00
|
|
|
|
|
2024-11-03 23:51:18 +08:00
|
|
|
|
public (NodeLibraryInfo, List<MethodDetailsInfo>) LoadLibrary(string libraryfilePath)
|
2024-11-03 21:17:45 +08:00
|
|
|
|
{
|
2024-11-03 23:51:18 +08:00
|
|
|
|
|
2024-11-03 21:17:45 +08:00
|
|
|
|
return LoadDllNodeInfo(libraryfilePath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool UnloadLibrary(string libraryName)
|
|
|
|
|
|
{
|
2024-11-03 23:51:18 +08:00
|
|
|
|
libraryName = libraryName.Split(',')[0];
|
|
|
|
|
|
if (_myFlowLibrarys.Remove(libraryName, out var flowLibrary))
|
2024-11-03 21:17:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
flowLibrary.Upload();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"尝试卸载程序集[{libraryName}]发生错误:{ex}");
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2024-11-03 23:51:18 +08:00
|
|
|
|
|
2024-11-03 21:17:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取方法描述
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="assemblyName">程序集名称</param>
|
|
|
|
|
|
/// <param name="methodName">方法名称</param>
|
|
|
|
|
|
/// <param name="md">返回的方法描述</param>
|
|
|
|
|
|
/// <returns>是否获取成功</returns>
|
2024-11-03 23:51:18 +08:00
|
|
|
|
public bool TryGetMethodDetails(string assemblyName, string methodName, [MaybeNullWhen(false)] out MethodDetails md)
|
2024-11-03 21:17:45 +08:00
|
|
|
|
{
|
2024-11-03 23:51:18 +08:00
|
|
|
|
if (_myFlowLibrarys.TryGetValue(assemblyName, out var flowLibrary)
|
|
|
|
|
|
&& flowLibrary.MethodDetailss.TryGetValue(methodName, out md))
|
2024-11-03 21:17:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
md = null;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-11-03 23:51:18 +08:00
|
|
|
|
|
2024-11-03 21:17:45 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取方法调用的委托
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="assemblyName">程序集名称</param>
|
|
|
|
|
|
/// <param name="methodName">方法名称</param>
|
|
|
|
|
|
/// <param name="dd">返回的委托调用封装类</param>
|
|
|
|
|
|
/// <returns>是否获取成功</returns>
|
2024-11-03 23:51:18 +08:00
|
|
|
|
public bool TryGetDelegateDetails(string assemblyName, string methodName, [MaybeNullWhen(false)] out DelegateDetails dd)
|
2024-11-03 21:17:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (_myFlowLibrarys.TryGetValue(assemblyName, out var flowLibrary)
|
|
|
|
|
|
&& flowLibrary.DelegateDetailss.TryGetValue(methodName, out dd))
|
|
|
|
|
|
{
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
dd = null;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取(初始化/加载时/退出后)相应的节点方法
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeType"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public List<MethodDetails> GetMdsOnFlowStart(NodeType nodeType)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<MethodDetails> mds = [];
|
|
|
|
|
|
|
2024-11-03 23:51:18 +08:00
|
|
|
|
foreach (var library in _myFlowLibrarys.Values)
|
2024-11-03 21:17:45 +08:00
|
|
|
|
{
|
2024-11-03 23:51:18 +08:00
|
|
|
|
var t_mds = library.MethodDetailss.Values.Where(it => it.MethodDynamicType == nodeType).ToList();
|
2024-11-03 21:17:45 +08:00
|
|
|
|
mds.AddRange(t_mds);
|
|
|
|
|
|
}
|
|
|
|
|
|
return mds;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取流程启动时在不同时间点需要自动实例化的类型
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public Dictionary<RegisterSequence, List<Type>> GetaAutoRegisterType()
|
|
|
|
|
|
{
|
|
|
|
|
|
Dictionary<RegisterSequence, List<Type>> rsTypes = new Dictionary<RegisterSequence, List<Type>>();
|
2024-11-03 23:51:18 +08:00
|
|
|
|
foreach (var library in _myFlowLibrarys.Values)
|
2024-11-03 21:17:45 +08:00
|
|
|
|
{
|
2024-11-03 23:51:18 +08:00
|
|
|
|
foreach (var kv in library.RegisterTypes)
|
2024-11-03 21:17:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
var @class = kv.Key;
|
|
|
|
|
|
var type = kv.Value;
|
2024-11-03 23:51:18 +08:00
|
|
|
|
if (!rsTypes.TryGetValue(@class, out var tmpTypes))
|
2024-11-03 21:17:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
tmpTypes = new List<Type>();
|
|
|
|
|
|
rsTypes.Add(@class, tmpTypes);
|
|
|
|
|
|
}
|
|
|
|
|
|
tmpTypes.AddRange(type);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return rsTypes;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取所有方法信息,用于保存项目时调用
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public List<LibraryMds> GetAllLibraryMds()
|
|
|
|
|
|
{
|
|
|
|
|
|
List<LibraryMds> mds = new List<LibraryMds>();
|
2024-11-03 23:51:18 +08:00
|
|
|
|
foreach (FlowLibrary library in _myFlowLibrarys.Values)
|
2024-11-03 21:17:45 +08:00
|
|
|
|
{
|
2024-11-03 23:51:18 +08:00
|
|
|
|
var tmp = new LibraryMds
|
|
|
|
|
|
{
|
|
|
|
|
|
AssemblyName = library.FullName,
|
2024-11-03 21:17:45 +08:00
|
|
|
|
Mds = library.MethodDetailss.Values.Select(md => md.ToInfo()).ToArray()
|
|
|
|
|
|
};
|
|
|
|
|
|
mds.Add(tmp);
|
|
|
|
|
|
}
|
|
|
|
|
|
return mds;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 序列化当前项目的依赖信息、节点信息,用于远程登录的场景,需要将依赖信息从本地(受控端)发送到远程(主控端)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public List<NodeLibraryInfo> GetAllLibraryInfo()
|
|
|
|
|
|
{
|
|
|
|
|
|
return _myFlowLibrarys.Values.Select(library => library.ToInfo()).ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region 功能性方法
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 从文件路径中加载程序集,返回相应的信息
|
|
|
|
|
|
/// </summary>
|
2024-11-03 23:51:18 +08:00
|
|
|
|
/// <param name="dllFilePath"></param>
|
2024-11-03 21:17:45 +08:00
|
|
|
|
/// <returns></returns>
|
2024-11-03 23:51:18 +08:00
|
|
|
|
private (NodeLibraryInfo, List<MethodDetailsInfo>) LoadDllNodeInfo(string dllFilePath)
|
2024-11-03 21:17:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
2024-11-03 23:51:18 +08:00
|
|
|
|
#if true
|
|
|
|
|
|
var fileName = Path.GetFileName(dllFilePath); // 获取文件名
|
|
|
|
|
|
var dir = Path.GetDirectoryName(dllFilePath); // 获取目录路径
|
|
|
|
|
|
var sereinFlowLibraryPath = Path.Combine(dir, $"{nameof(Serein)}.{nameof(Serein.Library)}.dll");
|
|
|
|
|
|
// 每个类库下面至少需要有“Serein.Library.dll”类库依赖
|
|
|
|
|
|
var flowAlc = new FlowLibraryAssemblyContext(sereinFlowLibraryPath, fileName);
|
|
|
|
|
|
Action actionUnload = () =>
|
|
|
|
|
|
{
|
|
|
|
|
|
flowAlc?.Unload(); // 卸载程序集
|
|
|
|
|
|
flowAlc = null;
|
|
|
|
|
|
GC.Collect(); // 强制触发GC确保卸载成功
|
|
|
|
|
|
GC.WaitForPendingFinalizers();
|
|
|
|
|
|
};
|
|
|
|
|
|
var assembly = flowAlc.LoadFromAssemblyPath(dllFilePath); // 加载指定路径的程序集
|
|
|
|
|
|
if (_myFlowLibrarys.ContainsKey(assembly.GetName().Name))
|
|
|
|
|
|
{
|
|
|
|
|
|
actionUnload.Invoke();
|
|
|
|
|
|
throw new Exception($"程序集[{assembly.GetName().FullName}]已经加载过!");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FlowLibrary flowLibrary = new FlowLibrary(dllFilePath, assembly, actionUnload);
|
|
|
|
|
|
if (flowLibrary.LoadAssembly(assembly))
|
|
|
|
|
|
{
|
|
|
|
|
|
_myFlowLibrarys.TryAdd(assembly.GetName().Name, flowLibrary);
|
|
|
|
|
|
(NodeLibraryInfo, List<MethodDetailsInfo>) result = (flowLibrary.ToInfo(),
|
|
|
|
|
|
flowLibrary.MethodDetailss.Values.Select(md => md.ToInfo()).ToList());
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"程序集[{assembly.GetName().FullName}]加载失败");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
var fileName = Path.GetFileName(dllFilePath); // 获取文件名
|
|
|
|
|
|
Assembly assembly = Assembly.LoadFrom(dllFilePath); // 加载程序集
|
|
|
|
|
|
FlowLibrary flowLibrary = new FlowLibrary(dllFilePath, assembly, () =>
|
2024-11-03 21:17:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("暂未实现卸载程序集");
|
|
|
|
|
|
//flowAlc.Unload(); // 卸载程序集
|
|
|
|
|
|
//flowAlc = null;
|
|
|
|
|
|
//GC.Collect(); // 强制触发GC确保卸载成功
|
|
|
|
|
|
//GC.WaitForPendingFinalizers();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
_myFlowLibrarys.TryAdd(assembly.GetName().Name, flowLibrary);
|
|
|
|
|
|
|
|
|
|
|
|
(NodeLibraryInfo, List<MethodDetailsInfo>) result = (flowLibrary.ToInfo(),
|
|
|
|
|
|
flowLibrary.MethodDetailss.Values.Select(md => md.ToInfo()).ToList());
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
2024-11-03 23:51:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
2024-11-03 21:17:45 +08:00
|
|
|
|
|
2024-11-03 23:51:18 +08:00
|
|
|
|
}
|
2024-11-03 21:17:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-11-03 23:51:18 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
2024-11-03 21:17:45 +08:00
|
|
|
|
|
2024-11-03 23:51:18 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 流程依赖加载
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class FlowLibraryAssemblyContext : AssemblyLoadContext
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly AssemblyDependencyResolver _resolver;
|
2024-11-03 21:17:45 +08:00
|
|
|
|
|
2024-11-03 23:51:18 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 创建新的加载上下文
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sereinFlowLibraryPath">类库主</param>
|
|
|
|
|
|
/// <param name="name"></param>
|
|
|
|
|
|
public FlowLibraryAssemblyContext(string sereinFlowLibraryPath, string name) : base(name, isCollectible: true)
|
|
|
|
|
|
{
|
|
|
|
|
|
_resolver = new AssemblyDependencyResolver(sereinFlowLibraryPath);
|
2024-11-03 21:17:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-03 23:51:18 +08:00
|
|
|
|
protected override Assembly? Load(AssemblyName assemblyName)
|
|
|
|
|
|
{
|
|
|
|
|
|
string? assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
|
|
|
|
|
|
if (!string.IsNullOrEmpty(assemblyPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);
|
|
|
|
|
|
//var assembly = LoadFromAssemblyPath(assemblyPath);
|
|
|
|
|
|
return assembly;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return Default.Assemblies.FirstOrDefault(x => x.FullName == assemblyName.FullName);
|
|
|
|
|
|
}
|
2024-11-03 21:17:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
2024-11-03 23:51:18 +08:00
|
|
|
|
// 构建依赖项的路径
|
|
|
|
|
|
//string assemblyPath = Path.Combine(AppContext.BaseDirectory, assemblyName.Name + ".dll");
|
|
|
|
|
|
//if (File.Exists(assemblyPath))
|
|
|
|
|
|
//{
|
|
|
|
|
|
// return LoadFromAssemblyPath(assemblyPath);
|
|
|
|
|
|
//}
|
|
|
|
|
|
//assemblyPath = Path.Combine(filePath, assemblyName.Name + ".dll");
|
|
|
|
|
|
//if (File.Exists(assemblyPath))
|
|
|
|
|
|
//{
|
|
|
|
|
|
// return LoadFromAssemblyPath(assemblyPath);
|
|
|
|
|
|
//}
|
2024-11-03 21:17:45 +08:00
|
|
|
|
|
2024-11-03 23:51:18 +08:00
|
|
|
|
//return null; // 如果没有找到,返回 null
|
|
|
|
|
|
}
|
2024-11-03 21:17:45 +08:00
|
|
|
|
}
|
2024-11-03 23:51:18 +08:00
|
|
|
|
public static class PluginAssemblyContextExtensions
|
|
|
|
|
|
{
|
2024-11-03 21:17:45 +08:00
|
|
|
|
|
2024-11-03 23:51:18 +08:00
|
|
|
|
public static Assembly FromAssemblyPath(this AssemblyLoadContext context, string path)
|
|
|
|
|
|
{
|
2024-11-03 21:17:45 +08:00
|
|
|
|
|
2024-11-03 23:51:18 +08:00
|
|
|
|
return context.LoadFromAssemblyPath(path);
|
2024-11-03 21:17:45 +08:00
|
|
|
|
|
2024-11-03 23:51:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-11-03 21:17:45 +08:00
|
|
|
|
}
|