2024-12-20 23:39:29 +08:00
|
|
|
|
using Serein.Library;
|
|
|
|
|
|
using Serein.Library.Api;
|
|
|
|
|
|
using Serein.Library.Utils;
|
|
|
|
|
|
using Serein.Script;
|
|
|
|
|
|
using Serein.Script.Node;
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-12-20 23:39:29 +08:00
|
|
|
|
private IScriptFlowApi ScriptFlowApi { get; }
|
|
|
|
|
|
|
|
|
|
|
|
private ASTNode mainNode;
|
2024-12-21 20:47:31 +08:00
|
|
|
|
private SereinScriptInterpreter ScriptInterpreter;
|
2024-12-20 23:39:29 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 构建流程脚本节点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="environment"></param>
|
|
|
|
|
|
public SingleScriptNode(IFlowEnvironment environment):base(environment)
|
|
|
|
|
|
{
|
|
|
|
|
|
//ScriptFlowApi = environment.IOC.Get<ScriptFlowApi>();
|
|
|
|
|
|
ScriptFlowApi = new ScriptFlowApi(environment, this);
|
2024-12-21 20:47:31 +08:00
|
|
|
|
ScriptInterpreter = new SereinScriptInterpreter();
|
|
|
|
|
|
}
|
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
|
|
|
|
// 挂载静态方法
|
|
|
|
|
|
var tempMethods = typeof(BaseFunc).GetMethods().Where(method =>
|
|
|
|
|
|
!(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)
|
|
|
|
|
|
{
|
2024-12-21 20:47:31 +08:00
|
|
|
|
SereinScriptInterpreter.AddStaticFunction(item.name, item.method);
|
2024-12-20 23:39:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-14 21:38:07 +08:00
|
|
|
|
|
2024-12-21 20:47:31 +08:00
|
|
|
|
public override void OnCreating()
|
|
|
|
|
|
{
|
|
|
|
|
|
MethodInfo? method = this.GetType().GetMethod(nameof(GetFlowApi));
|
|
|
|
|
|
if (method != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
ScriptInterpreter.AddFunction(nameof(GetFlowApi), method, () => this); // 挂载获取流程接口
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var md = MethodDetails;
|
|
|
|
|
|
var pd = md.ParameterDetailss ??= new ParameterDetails[1];
|
|
|
|
|
|
md.ParamsArgIndex = 0;
|
|
|
|
|
|
pd[0] = new ParameterDetails
|
|
|
|
|
|
{
|
|
|
|
|
|
Index = 0,
|
|
|
|
|
|
Name = "object",
|
|
|
|
|
|
IsExplicitData = true,
|
|
|
|
|
|
DataValue = string.Empty,
|
|
|
|
|
|
DataType = typeof(object),
|
|
|
|
|
|
ExplicitType = typeof(object),
|
|
|
|
|
|
ArgDataSourceNodeGuid = string.Empty,
|
|
|
|
|
|
ArgDataSourceType = ConnectionArgSourceType.GetPreviousNodeData,
|
|
|
|
|
|
NodeModel = this,
|
|
|
|
|
|
ExplicitTypeName = "Value",
|
|
|
|
|
|
Items = null,
|
|
|
|
|
|
IsParams = true,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-12-21 20:47:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 重新加载脚本代码
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void 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-03-15 14:02:12 +08:00
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
foreach (var pd in MethodDetails.ParameterDetailss)
|
|
|
|
|
|
{
|
|
|
|
|
|
sb.AppendLine($"let {pd.Name};"); // 提前声明这些变量
|
|
|
|
|
|
}
|
|
|
|
|
|
sb.Append(Script);
|
|
|
|
|
|
var p = new SereinScriptParser(sb.ToString());
|
|
|
|
|
|
//var p = new SereinScriptParser(Script);
|
2025-03-14 21:38:07 +08:00
|
|
|
|
mainNode = p.Parse(); // 开始解析
|
2024-12-20 23:39:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
SereinEnv.WriteLine(InfoType.ERROR, ex.ToString());
|
2025-03-14 21:38:07 +08:00
|
|
|
|
|
2024-12-20 23:39:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 执行脚本
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="context"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public override async Task<object?> ExecutingAsync(IDynamicContext context)
|
|
|
|
|
|
{
|
2024-12-26 16:42:05 +08:00
|
|
|
|
var @params = await GetParametersAsync(context);
|
2025-03-15 14:02:12 +08:00
|
|
|
|
|
2025-03-14 21:38:07 +08:00
|
|
|
|
|
|
|
|
|
|
//context.AddOrUpdate($"{context.Guid}_{this.Guid}_Params", @params[0]); // 后面再改
|
|
|
|
|
|
ReloadScript();// 每次都重新解析
|
|
|
|
|
|
|
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++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var argName = MethodDetails.ParameterDetailss[i].Name;
|
|
|
|
|
|
var argData = agrDatas[i];
|
|
|
|
|
|
scriptContext.SetVarValue(argName, argData);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-03-15 14:02:12 +08:00
|
|
|
|
|
2025-03-14 21:38:07 +08:00
|
|
|
|
|
2025-03-15 14:02:12 +08:00
|
|
|
|
FlowRunCompleteHandler onFlowStop = (e) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
scriptContext.OnExit();
|
|
|
|
|
|
};
|
2025-03-14 21:38:07 +08:00
|
|
|
|
|
2025-03-15 14:02:12 +08:00
|
|
|
|
var envEvent = (IFlowEnvironmentEvent)context.Env;
|
|
|
|
|
|
envEvent.OnFlowRunComplete += onFlowStop; // 防止运行后台流程
|
2024-12-21 20:47:31 +08:00
|
|
|
|
var result = await ScriptInterpreter.InterpretAsync(scriptContext, mainNode); // 从入口节点执行
|
2025-03-15 14:02:12 +08:00
|
|
|
|
envEvent.OnFlowRunComplete -= onFlowStop;
|
2024-12-26 22:24:44 +08:00
|
|
|
|
//SereinEnv.WriteLine(InfoType.INFO, "FlowContext Guid : " + context.Guid);
|
2024-12-20 23:39:29 +08:00
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-21 20:47:31 +08:00
|
|
|
|
|
|
|
|
|
|
#region 挂载的方法
|
|
|
|
|
|
|
|
|
|
|
|
public IScriptFlowApi GetFlowApi()
|
|
|
|
|
|
{
|
|
|
|
|
|
return ScriptFlowApi;
|
2024-12-20 23:39:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static class BaseFunc
|
|
|
|
|
|
{
|
2024-12-21 20:47:31 +08:00
|
|
|
|
public static DateTime GetNow() => DateTime.Now;
|
|
|
|
|
|
|
2024-12-20 23:39:29 +08:00
|
|
|
|
public static Type TypeOf(object type)
|
|
|
|
|
|
{
|
|
|
|
|
|
return type.GetType();
|
|
|
|
|
|
}
|
2024-12-21 20:47:31 +08:00
|
|
|
|
|
2024-12-20 23:39:29 +08:00
|
|
|
|
|
|
|
|
|
|
public static void Print(object value)
|
|
|
|
|
|
{
|
|
|
|
|
|
SereinEnv.WriteLine(InfoType.INFO, value?.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#region 数据转换
|
|
|
|
|
|
public static int ToInt(object value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return int.Parse(value.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
public static double ToDouble(object value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return double.Parse(value.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
public static bool ToBool(object value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return bool.Parse(value.ToString());
|
2024-12-21 20:47:31 +08:00
|
|
|
|
}
|
2024-12-20 23:39:29 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
public static async Task Delay(object value)
|
|
|
|
|
|
{
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|