NodeMVVMManagement不再是静态类,而是运行环境内部的成员。

This commit is contained in:
fengjiayi
2025-03-18 12:33:54 +08:00
parent fffb22b3a8
commit 87402ec7ea
14 changed files with 110 additions and 77 deletions

View File

@@ -705,7 +705,10 @@ namespace Serein.Library.Api
/// </summary>
UIContextOperation UIContextOperation { get; }
/// <summary>
/// 节点视图模型管理类
/// </summary>
NodeMVVMManagement NodeMVVMManagement { get; }
#endregion
#region

View File

@@ -40,6 +40,7 @@ namespace Serein.Library
public IFlowEnvironment CurrentEnv => this;
public UIContextOperation UIContextOperation { get; set; }
public NodeMVVMManagement NodeMVVMManagement { get; set; }
/// <summary>
/// 设置在UI线程操作的线程上下文

View File

@@ -7,32 +7,34 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Serein.NodeFlow
namespace Serein.Library
{
/// <summary>
/// 节点类型
/// </summary>
public class NodeMVVM
{
/// <summary>
/// 节点类型
/// </summary>
public required NodeControlType NodeType { get; set; }
public NodeControlType NodeType { get; set; }
/// <summary>
/// 节点Model类型
/// </summary>
public required Type ModelType { get; set; }
public Type ModelType { get; set; }
/// <summary>
/// 节点视图控件类型
/// </summary>
public Type? ControlType { get; set; }
public Type ControlType { get; set; }
/// <summary>
/// 节点视图VM类型
/// </summary>
public Type? ViewModelType { get; set; }
public Type ViewModelType { get; set; }
public override string ToString()
{
@@ -43,19 +45,19 @@ namespace Serein.NodeFlow
/// <summary>
/// 节点 数据、视图、VM 管理
/// </summary>
public static class NodeMVVMManagement
public class NodeMVVMManagement
{
/// <summary>
/// 节点对应的控件类型
/// </summary>
private static ConcurrentDictionary<NodeControlType, NodeMVVM> FlowNodeTypes { get; } = [];
private ConcurrentDictionary<NodeControlType, NodeMVVM> FlowNodeTypes { get; } = [];
/// <summary>
/// 注册 Model 类型
/// </summary>
/// <param name="type"></param>
/// <param name="modelType"></param>
public static bool RegisterModel(NodeControlType type, Type modelType)
public bool RegisterModel(NodeControlType type, Type modelType)
{
if(FlowNodeTypes.TryGetValue(type,out var nodeMVVM))
{
@@ -76,7 +78,7 @@ namespace Serein.NodeFlow
/// <param name="type"></param>
/// <param name="controlType"></param>
/// <param name="viewModelType"></param>
public static bool RegisterUI(NodeControlType type, Type controlType,Type viewModelType)
public bool RegisterUI(NodeControlType type, Type controlType,Type viewModelType)
{
if (!FlowNodeTypes.TryGetValue(type, out var nodeMVVM))
{
@@ -94,7 +96,7 @@ namespace Serein.NodeFlow
/// <param name="type"></param>
/// <param name="nodeMVVM"></param>
/// <returns></returns>
public static bool TryGetType(NodeControlType type, out NodeMVVM nodeMVVM)
public bool TryGetType(NodeControlType type, out NodeMVVM nodeMVVM)
{
if( FlowNodeTypes.TryGetValue(type, out nodeMVVM))
{

View File

@@ -12,7 +12,8 @@
<RepositoryUrl>https://github.com/fhhyyp/serein-flow</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<LangVersion>latest</LangVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>.\obj\g</CompilerGeneratedFilesOutputPath>

View File

@@ -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面板显示

View File

@@ -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;

View File

@@ -83,6 +83,7 @@ namespace Serein.NodeFlow.Env
public IFlowEnvironment CurrentEnv => this;
public UIContextOperation UIContextOperation { get; }
public NodeMVVMManagement NodeMVVMManagement { get; }
/// <summary>
/// 标示是否正在加载项目

View File

@@ -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}节点,节点类型尚未注册。");
}

View File

@@ -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);

View File

@@ -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、实例作用对象、委托方法

View File

@@ -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)
{

View File

@@ -164,15 +164,15 @@ namespace Serein.Workbench
IOCObjectViewer.SelectObj += ViewObjectViewer.LoadObjectInformation; // 使选择 IOC容器视图 的某项(对象)时,可以在 数据视图 呈现数据
#region NodeControlType Control类型 ViewModel类型
NodeMVVMManagement.RegisterUI(NodeControlType.UI, typeof(UINodeControl), typeof(UINodeControlViewModel));
NodeMVVMManagement.RegisterUI(NodeControlType.Action, typeof(ActionNodeControl), typeof(ActionNodeControlViewModel));
NodeMVVMManagement.RegisterUI(NodeControlType.Flipflop, typeof(FlipflopNodeControl), typeof(FlipflopNodeControlViewModel));
NodeMVVMManagement.RegisterUI(NodeControlType.ExpOp, typeof(ExpOpNodeControl), typeof(ExpOpNodeControlViewModel));
NodeMVVMManagement.RegisterUI(NodeControlType.ExpCondition, typeof(ConditionNodeControl), typeof(ConditionNodeControlViewModel));
NodeMVVMManagement.RegisterUI(NodeControlType.ConditionRegion, typeof(ConditionRegionControl), typeof(ConditionRegionNodeControlViewModel));
NodeMVVMManagement.RegisterUI(NodeControlType.GlobalData, typeof(GlobalDataControl), typeof(GlobalDataNodeControlViewModel));
NodeMVVMManagement.RegisterUI(NodeControlType.Script, typeof(ScriptNodeControl), typeof(ScriptNodeControlViewModel));
NodeMVVMManagement.RegisterUI(NodeControlType.NetScript, typeof(NetScriptNodeControl), typeof(NetScriptNodeControlViewModel));
EnvDecorator.NodeMVVMManagement.RegisterUI(NodeControlType.UI, typeof(UINodeControl), typeof(UINodeControlViewModel));
EnvDecorator.NodeMVVMManagement.RegisterUI(NodeControlType.Action, typeof(ActionNodeControl), typeof(ActionNodeControlViewModel));
EnvDecorator.NodeMVVMManagement.RegisterUI(NodeControlType.Flipflop, typeof(FlipflopNodeControl), typeof(FlipflopNodeControlViewModel));
EnvDecorator.NodeMVVMManagement.RegisterUI(NodeControlType.ExpOp, typeof(ExpOpNodeControl), typeof(ExpOpNodeControlViewModel));
EnvDecorator.NodeMVVMManagement.RegisterUI(NodeControlType.ExpCondition, typeof(ConditionNodeControl), typeof(ConditionNodeControlViewModel));
EnvDecorator.NodeMVVMManagement.RegisterUI(NodeControlType.ConditionRegion, typeof(ConditionRegionControl), typeof(ConditionRegionNodeControlViewModel));
EnvDecorator.NodeMVVMManagement.RegisterUI(NodeControlType.GlobalData, typeof(GlobalDataControl), typeof(GlobalDataNodeControlViewModel));
EnvDecorator.NodeMVVMManagement.RegisterUI(NodeControlType.Script, typeof(ScriptNodeControl), typeof(ScriptNodeControlViewModel));
EnvDecorator.NodeMVVMManagement.RegisterUI(NodeControlType.NetScript, typeof(NetScriptNodeControl), typeof(NetScriptNodeControlViewModel));
#endregion
@@ -733,7 +733,7 @@ namespace Serein.Workbench
PositionOfUI position = eventArgs.Position;
if(!NodeMVVMManagement.TryGetType(nodeModel.ControlType, out var nodeMVVM))
if(!EnvDecorator.NodeMVVMManagement.TryGetType(nodeModel.ControlType, out var nodeMVVM))
{
SereinEnv.WriteLine(InfoType.INFO, $"无法创建{nodeModel.ControlType}节点,节点类型尚未注册。");
return;
@@ -2707,11 +2707,11 @@ public class FlowLibrary
DynamicCompilerView dynamicCompilerView = new DynamicCompilerView();
dynamicCompilerView.ScriptCode = script;
dynamicCompilerView.OnCompileComplete = (assembly) =>
dynamicCompilerView.OnCompileComplete = (flowLibrary) =>
{
if(EnvDecorator.CurrentEnv is FlowEnvironment environment)
{
environment.LoadLibrary(assembly);
environment.LoadLibrary(flowLibrary);
}
//EnvDecorator.LoadLibrary
};

View File

@@ -71,9 +71,8 @@ public class FlowLibrary
});
}
private static void OnCompileComplete(System.Reflection.Assembly assembly)
private static void OnCompileComplete(FlowLibrary flowLibrary)
{
FlowLibrary flowLibrary = new FlowLibrary(assembly);
var loadResult = flowLibrary.LoadAssembly(); // 动态编译完成后加载程序集
if (!loadResult)
{

View File

@@ -1,4 +1,5 @@
using Microsoft.Win32;
using Serein.NodeFlow;
using Serein.NodeFlow.Tool;
using System;
using System.Collections.Generic;
@@ -33,7 +34,7 @@ namespace Serein.Workbench.Themes
/// <summary>
/// 编译成功回调
/// </summary>
public Action<Assembly> OnCompileComplete { get; set; }
public Action<FlowLibrary> OnCompileComplete { get; set; }
public DynamicCompilerView()
{
InitializeComponent();
@@ -127,16 +128,17 @@ namespace Serein.Workbench.Themes
try
{
txtErrors.Clear();
string code = codeEditor.Text;
Assembly assembly = _compiler.Compile(code, textboxAssemblyName.Text);
var code = codeEditor.Text;
var path = textboxAssemblyName.Text;
Assembly assembly = _compiler.Compile(code, path);
FlowLibrary flowLibrary = new FlowLibrary(assembly, path);
if (assembly != null)
{
txtErrors.Text = "编译成功!";
txtErrors.Background = System.Windows.Media.Brushes.LightGreen;
OnCompileComplete.Invoke(assembly);
OnCompileComplete.Invoke(flowLibrary);
count++;
}
}