2024-12-20 23:39:29 +08:00
|
|
|
|
using Serein.Library;
|
|
|
|
|
|
using Serein.Library.Api;
|
|
|
|
|
|
using Serein.Library.Utils;
|
|
|
|
|
|
using Serein.Script;
|
2025-07-13 17:34:03 +08:00
|
|
|
|
using Serein.Script.Node.FlowControl;
|
2024-12-20 23:39:29 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2024-12-21 20:47:31 +08:00
|
|
|
|
using System.Dynamic;
|
2024-12-20 23:39:29 +08:00
|
|
|
|
using System.Linq;
|
2024-12-21 20:47:31 +08:00
|
|
|
|
using System.Linq.Expressions;
|
2024-12-20 23:39:29 +08:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Serein.NodeFlow.Model
|
|
|
|
|
|
{
|
2024-12-23 23:19:10 +08:00
|
|
|
|
|
2024-12-20 23:39:29 +08:00
|
|
|
|
[NodeProperty(ValuePath = NodeValuePath.Node)]
|
|
|
|
|
|
public partial class SingleScriptNode : NodeModelBase
|
|
|
|
|
|
{
|
|
|
|
|
|
[PropertyInfo(IsNotification = true)]
|
|
|
|
|
|
private string _script;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 流程脚本节点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public partial class SingleScriptNode : NodeModelBase
|
|
|
|
|
|
{
|
2024-12-24 22:23:53 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 脚本节点是基础节点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public override bool IsBase => true;
|
|
|
|
|
|
|
2025-05-30 15:42:59 +08:00
|
|
|
|
private bool IsScriptChanged = false;
|
|
|
|
|
|
|
2025-07-16 16:16:19 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 脚本解释器
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private readonly SereinScript sereinScript;
|
|
|
|
|
|
|
2024-12-20 23:39:29 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 构建流程脚本节点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="environment"></param>
|
2025-07-16 16:16:19 +08:00
|
|
|
|
public SingleScriptNode(IFlowEnvironment environment) : base(environment)
|
2024-12-20 23:39:29 +08:00
|
|
|
|
{
|
2025-07-16 16:16:19 +08:00
|
|
|
|
sereinScript = new SereinScript();
|
2024-12-21 20:47:31 +08:00
|
|
|
|
}
|
2024-12-20 23:39:29 +08:00
|
|
|
|
|
2024-12-21 20:47:31 +08:00
|
|
|
|
static SingleScriptNode()
|
|
|
|
|
|
{
|
2024-12-20 23:39:29 +08:00
|
|
|
|
// 挂载静态方法
|
2025-07-11 23:43:27 +08:00
|
|
|
|
var tempMethods = typeof(ScriptBaseFunc).GetMethods().Where(method =>
|
2024-12-20 23:39:29 +08:00
|
|
|
|
!(method.Name.Equals("GetHashCode")
|
|
|
|
|
|
|| method.Name.Equals("Equals")
|
|
|
|
|
|
|| method.Name.Equals("ToString")
|
|
|
|
|
|
|| method.Name.Equals("GetType")
|
|
|
|
|
|
)).Select(method => (method.Name, method)).ToArray();
|
2024-12-21 20:47:31 +08:00
|
|
|
|
// 加载基础方法
|
2024-12-20 23:39:29 +08:00
|
|
|
|
foreach ((string name, MethodInfo method) item in tempMethods)
|
|
|
|
|
|
{
|
2025-07-16 16:16:19 +08:00
|
|
|
|
SereinScript.AddStaticFunction(item.name, item.method);
|
2024-12-20 23:39:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-30 15:42:59 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 代码改变后
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
|
|
partial void OnScriptChanged(string value)
|
|
|
|
|
|
{
|
|
|
|
|
|
IsScriptChanged = true;
|
|
|
|
|
|
}
|
2025-03-14 21:38:07 +08:00
|
|
|
|
|
2025-05-30 15:42:59 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 节点创建时
|
|
|
|
|
|
/// </summary>
|
2024-12-21 20:47:31 +08:00
|
|
|
|
public override void OnCreating()
|
|
|
|
|
|
{
|
|
|
|
|
|
var md = MethodDetails;
|
|
|
|
|
|
var pd = md.ParameterDetailss ??= new ParameterDetails[1];
|
|
|
|
|
|
md.ParamsArgIndex = 0;
|
|
|
|
|
|
pd[0] = new ParameterDetails
|
|
|
|
|
|
{
|
|
|
|
|
|
Index = 0,
|
2025-07-17 22:46:40 +08:00
|
|
|
|
Name = "string",
|
2024-12-21 20:47:31 +08:00
|
|
|
|
IsExplicitData = true,
|
|
|
|
|
|
DataValue = string.Empty,
|
2025-07-17 22:46:40 +08:00
|
|
|
|
DataType = typeof(string),
|
|
|
|
|
|
ExplicitType = typeof(string),
|
2024-12-21 20:47:31 +08:00
|
|
|
|
ArgDataSourceNodeGuid = string.Empty,
|
|
|
|
|
|
ArgDataSourceType = ConnectionArgSourceType.GetPreviousNodeData,
|
|
|
|
|
|
NodeModel = this,
|
2025-03-15 15:43:42 +08:00
|
|
|
|
InputType = ParameterValueInputType.Input,
|
2024-12-21 20:47:31 +08:00
|
|
|
|
Items = null,
|
|
|
|
|
|
IsParams = true,
|
2025-05-30 15:42:59 +08:00
|
|
|
|
//Description = "脚本节点入参"
|
2024-12-21 20:47:31 +08:00
|
|
|
|
};
|
2025-07-17 22:46:40 +08:00
|
|
|
|
md.ReturnType = typeof(void); // 默认无返回
|
2024-12-21 20:47:31 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 导出脚本代码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="nodeInfo"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public override NodeInfo SaveCustomData(NodeInfo nodeInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
dynamic data = new ExpandoObject();
|
|
|
|
|
|
data.Script = Script ?? "";
|
|
|
|
|
|
nodeInfo.CustomData = data;
|
|
|
|
|
|
return nodeInfo;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-20 23:39:29 +08:00
|
|
|
|
/// <summary>
|
2024-12-21 20:47:31 +08:00
|
|
|
|
/// 加载自定义数据
|
2024-12-20 23:39:29 +08:00
|
|
|
|
/// </summary>
|
2024-12-21 20:47:31 +08:00
|
|
|
|
/// <param name="nodeInfo"></param>
|
|
|
|
|
|
public override void LoadCustomData(NodeInfo nodeInfo)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.Script = nodeInfo.CustomData?.Script ?? "";
|
2025-03-14 21:38:07 +08:00
|
|
|
|
|
|
|
|
|
|
// 更新变量名
|
|
|
|
|
|
for (int i = 0; i < Math.Min(this.MethodDetails.ParameterDetailss.Length, nodeInfo.ParameterData.Length); i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.MethodDetails.ParameterDetailss[i].Name = nodeInfo.ParameterData[i].ArgName;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-17 22:46:40 +08:00
|
|
|
|
//ReloadScript();// 加载时重新解析
|
2025-07-06 14:34:49 +08:00
|
|
|
|
IsScriptChanged = false; // 重置脚本改变标志
|
2025-03-14 21:38:07 +08:00
|
|
|
|
|
2024-12-21 20:47:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 重新加载脚本代码
|
|
|
|
|
|
/// </summary>
|
2025-07-17 22:46:40 +08:00
|
|
|
|
public bool ReloadScript()
|
2024-12-20 23:39:29 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-03-14 21:38:07 +08:00
|
|
|
|
HashSet<string> varNames = new HashSet<string>();
|
|
|
|
|
|
foreach (var pd in MethodDetails.ParameterDetailss)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (varNames.Contains(pd.Name))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"脚本节点重复的变量名称:{pd.Name} - {Guid}");
|
|
|
|
|
|
}
|
|
|
|
|
|
varNames.Add(pd.Name);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-16 16:16:19 +08:00
|
|
|
|
|
2025-07-17 22:46:40 +08:00
|
|
|
|
var argTypes = MethodDetails.ParameterDetailss
|
|
|
|
|
|
.Select(pd =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Env.TryGetNodeModel(pd.ArgDataSourceNodeGuid, out var node) &&
|
|
|
|
|
|
node.MethodDetails?.ReturnType is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
pd.DataType = node.MethodDetails.ReturnType;
|
|
|
|
|
|
return (pd.Name, node.MethodDetails.ReturnType);
|
|
|
|
|
|
}
|
|
|
|
|
|
return default;
|
|
|
|
|
|
})
|
|
|
|
|
|
.Where(x => x != default)
|
|
|
|
|
|
.ToDictionary(x => x.Name, x => x.ReturnType); // 准备预定义类型
|
2025-07-06 14:34:49 +08:00
|
|
|
|
|
2025-07-17 22:46:40 +08:00
|
|
|
|
|
|
|
|
|
|
var returnType = sereinScript.ParserScript(Script, argTypes); // 开始解析获取程序主节点
|
|
|
|
|
|
MethodDetails.ReturnType = returnType;
|
|
|
|
|
|
return true;
|
2024-12-20 23:39:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-07-17 22:46:40 +08:00
|
|
|
|
SereinEnv.WriteLine(ex);
|
|
|
|
|
|
return false; // 解析失败
|
2024-12-20 23:39:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 执行脚本
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-03-21 18:26:01 +08:00
|
|
|
|
public override async Task<FlowResult> ExecutingAsync(IDynamicContext context, CancellationToken token)
|
2025-05-30 15:42:59 +08:00
|
|
|
|
{
|
2025-07-11 20:52:21 +08:00
|
|
|
|
var result = await ExecutingAsync(this, context, token);
|
|
|
|
|
|
return result;
|
2025-05-30 15:42:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 流程接口提供参数进行调用脚本节点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="flowCallNode"></param>
|
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<FlowResult> ExecutingAsync(NodeModelBase flowCallNode, IDynamicContext context, CancellationToken token)
|
2024-12-20 23:39:29 +08:00
|
|
|
|
{
|
2025-07-06 14:34:49 +08:00
|
|
|
|
if (token.IsCancellationRequested) return new FlowResult(this.Guid, context);
|
2025-05-30 15:42:59 +08:00
|
|
|
|
var @params = await flowCallNode.GetParametersAsync(context, token);
|
2025-07-06 14:34:49 +08:00
|
|
|
|
if (token.IsCancellationRequested) return new FlowResult(this.Guid, context);
|
2025-03-14 21:38:07 +08:00
|
|
|
|
|
|
|
|
|
|
//context.AddOrUpdate($"{context.Guid}_{this.Guid}_Params", @params[0]); // 后面再改
|
2025-05-30 15:42:59 +08:00
|
|
|
|
|
|
|
|
|
|
if (IsScriptChanged)
|
|
|
|
|
|
{
|
|
|
|
|
|
lock (@params) {
|
|
|
|
|
|
if (IsScriptChanged)
|
|
|
|
|
|
{
|
2025-07-17 22:46:40 +08:00
|
|
|
|
ReloadScript();// 执行时检查是否需要重新解析
|
2025-07-06 14:34:49 +08:00
|
|
|
|
IsScriptChanged = false;
|
|
|
|
|
|
context.Env.WriteLine(InfoType.INFO, $"[{Guid}]脚本解析完成");
|
2025-05-30 15:42:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-03-14 21:38:07 +08:00
|
|
|
|
|
2024-12-26 22:24:44 +08:00
|
|
|
|
IScriptInvokeContext scriptContext = new ScriptInvokeContext(context);
|
|
|
|
|
|
|
2025-03-14 21:38:07 +08:00
|
|
|
|
if (@params[0] is object[] agrDatas)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (int i = 0; i < agrDatas.Length; i++)
|
|
|
|
|
|
{
|
2025-05-30 15:42:59 +08:00
|
|
|
|
var argName = flowCallNode.MethodDetails.ParameterDetailss[i].Name;
|
2025-03-14 21:38:07 +08:00
|
|
|
|
var argData = agrDatas[i];
|
|
|
|
|
|
scriptContext.SetVarValue(argName, argData);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-03-15 14:02:12 +08:00
|
|
|
|
|
|
|
|
|
|
FlowRunCompleteHandler onFlowStop = (e) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
scriptContext.OnExit();
|
|
|
|
|
|
};
|
2025-03-14 21:38:07 +08:00
|
|
|
|
|
2025-06-22 21:53:37 +08:00
|
|
|
|
var envEvent = context.Env.Event;
|
2025-06-02 19:17:30 +08:00
|
|
|
|
envEvent.FlowRunComplete += onFlowStop; // 防止运行后台流程
|
2025-03-20 22:54:10 +08:00
|
|
|
|
|
|
|
|
|
|
if (token.IsCancellationRequested) return null;
|
|
|
|
|
|
|
2025-07-16 16:16:19 +08:00
|
|
|
|
var result = await sereinScript.InterpreterAsync(scriptContext); // 从入口节点执行
|
2025-06-02 19:17:30 +08:00
|
|
|
|
envEvent.FlowRunComplete -= onFlowStop;
|
2025-07-06 14:34:49 +08:00
|
|
|
|
return new FlowResult(this.Guid, context, result);
|
2024-12-20 23:39:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-21 20:47:31 +08:00
|
|
|
|
#region 挂载的方法
|
|
|
|
|
|
|
2025-07-13 17:34:03 +08:00
|
|
|
|
/*public IScriptFlowApi GetFlowApi()
|
2024-12-21 20:47:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
return ScriptFlowApi;
|
2025-07-13 17:34:03 +08:00
|
|
|
|
}*/
|
2024-12-20 23:39:29 +08:00
|
|
|
|
|
2025-07-11 23:43:27 +08:00
|
|
|
|
private static class ScriptBaseFunc
|
2024-12-20 23:39:29 +08:00
|
|
|
|
{
|
2025-07-16 16:16:19 +08:00
|
|
|
|
public static DateTime now() => DateTime.Now;
|
2025-05-30 23:31:31 +08:00
|
|
|
|
|
|
|
|
|
|
#region 常用的类型转换
|
2025-07-11 23:43:27 +08:00
|
|
|
|
public static bool @bool(object value)
|
2025-05-30 23:31:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
return ConvertHelper.ValueParse<bool>(value);
|
|
|
|
|
|
}
|
2025-07-11 23:43:27 +08:00
|
|
|
|
public static decimal @decimal(object value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ConvertHelper.ValueParse<decimal>(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
public static float @float(object value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ConvertHelper.ValueParse<float>(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
public static double @double(object value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ConvertHelper.ValueParse<double>(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
public static int @int(object value)
|
2025-05-30 23:31:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
return ConvertHelper.ValueParse<int>(value);
|
|
|
|
|
|
}
|
2025-07-11 23:43:27 +08:00
|
|
|
|
public static int @long(object value)
|
2025-05-30 23:31:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
return ConvertHelper.ValueParse<int>(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2025-07-11 23:43:27 +08:00
|
|
|
|
public static int len(object target)
|
2024-12-20 23:39:29 +08:00
|
|
|
|
{
|
2025-07-11 23:43:27 +08:00
|
|
|
|
// 获取数组或集合对象
|
|
|
|
|
|
// 访问数组或集合中的指定索引
|
|
|
|
|
|
if (target is Array array)
|
|
|
|
|
|
{
|
|
|
|
|
|
return array.Length;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (target is string chars)
|
|
|
|
|
|
{
|
|
|
|
|
|
return chars.Length;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (target is IDictionary<object, object> dict)
|
|
|
|
|
|
{
|
|
|
|
|
|
return dict.Count;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (target is IList<object> list)
|
|
|
|
|
|
{
|
|
|
|
|
|
return list.Count;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException($"并非有效集合");
|
|
|
|
|
|
}
|
2024-12-20 23:39:29 +08:00
|
|
|
|
}
|
2025-07-11 23:43:27 +08:00
|
|
|
|
|
2025-07-16 16:16:19 +08:00
|
|
|
|
public static void regType(Type type, string name = "")
|
|
|
|
|
|
{
|
|
|
|
|
|
SereinScript.AddClassType(type, name);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string str(object obj)
|
|
|
|
|
|
{
|
|
|
|
|
|
return obj?.ToString() ?? string.Empty;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static object obj(Type type)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Activator.CreateInstance(type);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-17 15:14:34 +08:00
|
|
|
|
public static object global(string name)
|
|
|
|
|
|
{
|
|
|
|
|
|
return SereinEnv.GetFlowGlobalData(name);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-11 23:43:27 +08:00
|
|
|
|
public static Type type(object type)
|
2024-12-20 23:39:29 +08:00
|
|
|
|
{
|
2025-07-11 23:43:27 +08:00
|
|
|
|
return type.GetType();
|
2024-12-20 23:39:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-11 23:43:27 +08:00
|
|
|
|
public static void log(object value)
|
2024-12-20 23:39:29 +08:00
|
|
|
|
{
|
2025-07-11 23:43:27 +08:00
|
|
|
|
SereinEnv.WriteLine(InfoType.INFO, value?.ToString());
|
2024-12-21 20:47:31 +08:00
|
|
|
|
}
|
2024-12-20 23:39:29 +08:00
|
|
|
|
|
2025-07-11 23:43:27 +08:00
|
|
|
|
public static async Task sleep(object value)
|
2024-12-20 23:39:29 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (value is int @int)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"等待{@int}ms");
|
|
|
|
|
|
await Task.Delay(@int);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (value is TimeSpan timeSpan)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"等待{timeSpan}");
|
|
|
|
|
|
await Task.Delay(timeSpan);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-12-21 20:47:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
2024-12-20 23:39:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|