mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-04-02 14:36:33 +08:00
NodeMVVMManagement不再是静态类,而是运行环境内部的成员。
This commit is contained in:
@@ -42,7 +42,7 @@ namespace Serein.NodeFlow.Env
|
||||
|
||||
};
|
||||
this.FlowLibraryManagement = new FlowLibraryManagement(this); // 实例化类库管理
|
||||
|
||||
this.NodeMVVMManagement = new NodeMVVMManagement();
|
||||
#region 注册基本节点类型
|
||||
NodeMVVMManagement.RegisterModel(NodeControlType.UI, typeof(SingleUINode)); // 动作节点
|
||||
|
||||
@@ -214,6 +214,11 @@ namespace Serein.NodeFlow.Env
|
||||
/// </summary>
|
||||
public UIContextOperation UIContextOperation { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 节点视图模型管理类
|
||||
/// </summary>
|
||||
public NodeMVVMManagement NodeMVVMManagement { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 信息输出等级
|
||||
/// </summary>
|
||||
@@ -654,12 +659,12 @@ namespace Serein.NodeFlow.Env
|
||||
/// <summary>
|
||||
/// 加载本地程序集
|
||||
/// </summary>
|
||||
/// <param name="assembly"></param>
|
||||
public void LoadLibrary(Assembly assembly)
|
||||
/// <param name="flowLibrary"></param>
|
||||
public void LoadLibrary(FlowLibrary flowLibrary)
|
||||
{
|
||||
try
|
||||
{
|
||||
(var libraryInfo, var mdInfos) = FlowLibraryManagement.LoadLibraryOfPath(assembly);
|
||||
(var libraryInfo, var mdInfos) = FlowLibraryManagement.LoadLibraryOfPath(flowLibrary);
|
||||
if (mdInfos.Count > 0)
|
||||
{
|
||||
UIContextOperation?.Invoke(() => OnDllLoad?.Invoke(new LoadDllEventArgs(libraryInfo, mdInfos))); // 通知UI创建dll面板显示
|
||||
|
||||
@@ -82,6 +82,10 @@ namespace Serein.NodeFlow.Env
|
||||
public IFlowEnvironment CurrentEnv { get => currentFlowEnvironment; }
|
||||
|
||||
public UIContextOperation UIContextOperation => currentFlowEnvironment.UIContextOperation;
|
||||
/// <summary>
|
||||
/// 节点视图模型管理类
|
||||
/// </summary>
|
||||
public NodeMVVMManagement NodeMVVMManagement => currentFlowEnvironment.NodeMVVMManagement;
|
||||
|
||||
public ISereinIOC IOC => (ISereinIOC)currentFlowEnvironment;
|
||||
|
||||
|
||||
@@ -83,6 +83,7 @@ namespace Serein.NodeFlow.Env
|
||||
|
||||
public IFlowEnvironment CurrentEnv => this;
|
||||
public UIContextOperation UIContextOperation { get; }
|
||||
public NodeMVVMManagement NodeMVVMManagement { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 标示是否正在加载项目
|
||||
|
||||
@@ -43,7 +43,7 @@ namespace Serein.NodeFlow
|
||||
|
||||
// 尝试获取需要创建的节点类型
|
||||
|
||||
if (!NodeMVVMManagement.TryGetType(nodeControlType, out var nodeMVVM) || nodeMVVM.ModelType == null)
|
||||
if (!env.NodeMVVMManagement.TryGetType(nodeControlType, out var nodeMVVM) || nodeMVVM.ModelType == null)
|
||||
{
|
||||
throw new Exception($"无法创建{nodeControlType}节点,节点类型尚未注册。");
|
||||
}
|
||||
|
||||
@@ -1,110 +0,0 @@
|
||||
using Serein.Library;
|
||||
using Serein.Library.Utils;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Serein.NodeFlow
|
||||
{
|
||||
/// <summary>
|
||||
/// 节点类型
|
||||
/// </summary>
|
||||
public class NodeMVVM
|
||||
{
|
||||
/// <summary>
|
||||
/// 节点类型
|
||||
/// </summary>
|
||||
public required NodeControlType NodeType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 节点Model类型
|
||||
/// </summary>
|
||||
public required Type ModelType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 节点视图控件类型
|
||||
/// </summary>
|
||||
public Type? ControlType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 节点视图VM类型
|
||||
/// </summary>
|
||||
public Type? ViewModelType { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"$[{NodeType}]类型信息 : ModelType->{ModelType};ControlType->{ControlType};ViewModelType->{ViewModelType}";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 节点 数据、视图、VM 管理
|
||||
/// </summary>
|
||||
public static class NodeMVVMManagement
|
||||
{
|
||||
/// <summary>
|
||||
/// 节点对应的控件类型
|
||||
/// </summary>
|
||||
private static ConcurrentDictionary<NodeControlType, NodeMVVM> FlowNodeTypes { get; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// 注册 Model 类型
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="modelType"></param>
|
||||
public static bool RegisterModel(NodeControlType type, Type modelType)
|
||||
{
|
||||
if(FlowNodeTypes.TryGetValue(type,out var nodeMVVM))
|
||||
{
|
||||
SereinEnv.WriteLine(InfoType.WARN, $"无法为节点[{type}]注册Model类型[{modelType}],已经注册的类型为{nodeMVVM}。");
|
||||
return false;
|
||||
}
|
||||
nodeMVVM = new NodeMVVM
|
||||
{
|
||||
NodeType = type,
|
||||
ModelType = modelType
|
||||
};
|
||||
return FlowNodeTypes.TryAdd(type, nodeMVVM);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册 UI 类型
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="controlType"></param>
|
||||
/// <param name="viewModelType"></param>
|
||||
public static bool RegisterUI(NodeControlType type, Type controlType,Type viewModelType)
|
||||
{
|
||||
if (!FlowNodeTypes.TryGetValue(type, out var nodeMVVM))
|
||||
{
|
||||
SereinEnv.WriteLine(InfoType.WARN, $"无法为节点[{type}]注册UI类型[{controlType}][{viewModelType}],当前类型尚未注册。");
|
||||
return false;
|
||||
}
|
||||
nodeMVVM.ControlType = controlType;
|
||||
nodeMVVM.ViewModelType = viewModelType;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取相应的类型
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="nodeMVVM"></param>
|
||||
/// <returns></returns>
|
||||
public static bool TryGetType(NodeControlType type, out NodeMVVM nodeMVVM)
|
||||
{
|
||||
if( FlowNodeTypes.TryGetValue(type, out nodeMVVM))
|
||||
{
|
||||
return nodeMVVM != null;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,13 +101,13 @@ namespace Serein.NodeFlow.Tool
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
ms.Seek(0, SeekOrigin.Begin);
|
||||
var t12 = AppContext.BaseDirectory;
|
||||
var assembly = Assembly.Load(ms.ToArray());
|
||||
var t1 = assembly.Location;
|
||||
var t = assembly.GetType().Assembly.Location;
|
||||
|
||||
|
||||
|
||||
// 保存
|
||||
|
||||
compilation.Emit(savePath);
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace Serein.NodeFlow
|
||||
/// </summary>
|
||||
public class FlowLibrary
|
||||
{
|
||||
private readonly Assembly _assembly;
|
||||
public Assembly Assembly { get; private set; }
|
||||
|
||||
|
||||
|
||||
@@ -36,16 +36,16 @@ namespace Serein.NodeFlow
|
||||
|
||||
public FlowLibrary(Assembly assembly)
|
||||
{
|
||||
this._assembly = assembly;
|
||||
this.FullName = Path.GetFileName(_assembly.Location);
|
||||
this.Assembly = assembly;
|
||||
this.FullName = Path.GetFileName(Assembly.Location);
|
||||
|
||||
this.FilePath = _assembly.Location;
|
||||
this.FilePath = Assembly.Location;
|
||||
}
|
||||
|
||||
public FlowLibrary(Assembly assembly,
|
||||
string filePath)
|
||||
{
|
||||
this._assembly = assembly;
|
||||
this.Assembly = assembly;
|
||||
this.FullName = Path.GetFileName(filePath); ;
|
||||
this.FilePath = filePath;
|
||||
}
|
||||
@@ -92,7 +92,7 @@ namespace Serein.NodeFlow
|
||||
/// <returns></returns>
|
||||
public NodeLibraryInfo ToInfo()
|
||||
{
|
||||
var assemblyName = _assembly.GetName().Name;
|
||||
var assemblyName = Assembly.GetName().Name;
|
||||
return new NodeLibraryInfo
|
||||
{
|
||||
AssemblyName = assemblyName,
|
||||
@@ -111,7 +111,7 @@ namespace Serein.NodeFlow
|
||||
/// <returns></returns>
|
||||
public bool LoadAssembly()
|
||||
{
|
||||
Assembly assembly = this._assembly;
|
||||
Assembly assembly = this.Assembly;
|
||||
#region 检查入参
|
||||
|
||||
// 加载DLL,创建 MethodDetails、实例作用对象、委托方法
|
||||
|
||||
@@ -49,26 +49,31 @@ namespace Serein.NodeFlow.Tool
|
||||
|
||||
var flowAlc = new FlowLibraryAssemblyContext(sereinFlowBaseLibraryPath, Path.GetFileName(libraryfilePath));
|
||||
var assembly = flowAlc.LoadFromAssemblyPath(libraryfilePath); // 加载指定路径的程序集
|
||||
var reulst = LoadDllNodeInfo(assembly);
|
||||
if(reulst.Item1 is null || reulst.Item2.Count == 0)
|
||||
|
||||
var flowLibrary = new FlowLibrary(assembly);
|
||||
try
|
||||
{
|
||||
var reulst = LoadFlowLibrary(flowLibrary);
|
||||
return reulst;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
flowAlc?.Unload(); // 卸载程序集
|
||||
flowAlc = null;
|
||||
GC.Collect(); // 强制触发GC确保卸载成功
|
||||
GC.WaitForPendingFinalizers();
|
||||
throw new Exception("从文件加载DLL失败:"+ libraryfilePath);
|
||||
throw;
|
||||
}
|
||||
return reulst;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载类库
|
||||
/// </summary>
|
||||
/// <param name="assembly"></param>
|
||||
/// <param name="flowLibrary"></param>
|
||||
/// <returns></returns>
|
||||
public (NodeLibraryInfo, List<MethodDetailsInfo>) LoadLibraryOfPath(Assembly assembly)
|
||||
public (NodeLibraryInfo, List<MethodDetailsInfo>) LoadLibraryOfPath(FlowLibrary flowLibrary)
|
||||
{
|
||||
return LoadDllNodeInfo(assembly);
|
||||
return LoadFlowLibrary(flowLibrary);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -235,38 +240,48 @@ namespace Serein.NodeFlow.Tool
|
||||
/// </summary>
|
||||
public readonly static string SereinBaseLibrary = $"{nameof(Serein)}.{nameof(Serein.Library)}.dll";
|
||||
|
||||
private (NodeLibraryInfo, List<MethodDetailsInfo>) LoadDllNodeInfo(Assembly assembly)
|
||||
{
|
||||
//private (NodeLibraryInfo, List<MethodDetailsInfo>) LoadDllNodeInfo(Assembly assembly)
|
||||
//{
|
||||
|
||||
// if (assembly.FullName?.ToString().Equals(typeof(IFlowEnvironment).Assembly.FullName?.ToString()) == true)
|
||||
// {
|
||||
|
||||
// // 加载基础依赖
|
||||
// return LoadAssembly(typeof(IFlowEnvironment).Assembly);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// var assembly_result = LoadAssembly(assembly);
|
||||
// return assembly_result;
|
||||
// }
|
||||
// catch (Exception)
|
||||
// {
|
||||
// return (null,[]);
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
//}
|
||||
|
||||
|
||||
|
||||
private (NodeLibraryInfo, List<MethodDetailsInfo>) LoadFlowLibrary(FlowLibrary flowLibrary)
|
||||
{
|
||||
var assembly = flowLibrary.Assembly;
|
||||
if (assembly.FullName?.ToString().Equals(typeof(IFlowEnvironment).Assembly.FullName?.ToString()) == true)
|
||||
{
|
||||
// 加载基础依赖
|
||||
return LoadAssembly(typeof(IFlowEnvironment).Assembly);
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
var assembly_result = LoadAssembly(assembly);
|
||||
return assembly_result;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return (null,[]);
|
||||
}
|
||||
|
||||
flowLibrary = new FlowLibrary(typeof(IFlowEnvironment).Assembly);
|
||||
}
|
||||
|
||||
}
|
||||
var assmblyName = assembly.GetName().Name;
|
||||
if (!string.IsNullOrEmpty(assmblyName) && _myFlowLibrarys.ContainsKey(assmblyName))
|
||||
{
|
||||
throw new Exception($"程序集[{assembly.GetName().FullName}]已经加载过!");
|
||||
}
|
||||
|
||||
private (NodeLibraryInfo, List<MethodDetailsInfo>) LoadAssembly(Assembly assembly)
|
||||
{
|
||||
if (_myFlowLibrarys.ContainsKey(assembly.GetName().Name))
|
||||
{
|
||||
throw new Exception($"程序集[{assembly.GetName().FullName}]已经加载过!");
|
||||
}
|
||||
|
||||
FlowLibrary flowLibrary = new FlowLibrary(assembly);
|
||||
var loadResult = flowLibrary.LoadAssembly(); // 加载程序集
|
||||
if (loadResult)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user