using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Utils;
using Serein.Script;
using Serein.Script.Node;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace Serein.NodeFlow.Model
{
[NodeProperty(ValuePath = NodeValuePath.Node)]
public partial class SingleScriptNode : NodeModelBase
{
[PropertyInfo(IsNotification = true)]
private string _script;
}
///
/// 流程脚本节点
///
public partial class SingleScriptNode : NodeModelBase
{
///
/// 脚本节点是基础节点
///
public override bool IsBase => true;
private IScriptFlowApi ScriptFlowApi { get; }
private ASTNode mainNode;
private SereinScriptInterpreter ScriptInterpreter;
///
/// 构建流程脚本节点
///
///
public SingleScriptNode(IFlowEnvironment environment):base(environment)
{
//ScriptFlowApi = environment.IOC.Get();
ScriptFlowApi = new ScriptFlowApi(environment, this);
ScriptInterpreter = new SereinScriptInterpreter();
}
static SingleScriptNode()
{
// 挂载静态方法
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();
// 加载基础方法
foreach ((string name, MethodInfo method) item in tempMethods)
{
SereinScriptInterpreter.AddStaticFunction(item.name, item.method);
}
}
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,
};
}
///
/// 导出脚本代码
///
///
///
public override NodeInfo SaveCustomData(NodeInfo nodeInfo)
{
dynamic data = new ExpandoObject();
data.Script = Script ?? "";
nodeInfo.CustomData = data;
return nodeInfo;
}
///
/// 加载自定义数据
///
///
public override void LoadCustomData(NodeInfo nodeInfo)
{
this.Script = nodeInfo.CustomData?.Script ?? "";
// 更新变量名
for (int i = 0; i < Math.Min(this.MethodDetails.ParameterDetailss.Length, nodeInfo.ParameterData.Length); i++)
{
this.MethodDetails.ParameterDetailss[i].Name = nodeInfo.ParameterData[i].ArgName;
}
}
///
/// 重新加载脚本代码
///
public void ReloadScript()
{
try
{
HashSet varNames = new HashSet();
foreach (var pd in MethodDetails.ParameterDetailss)
{
if (varNames.Contains(pd.Name))
{
throw new Exception($"脚本节点重复的变量名称:{pd.Name} - {Guid}");
}
varNames.Add(pd.Name);
}
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);
mainNode = p.Parse(); // 开始解析
}
catch (Exception ex)
{
SereinEnv.WriteLine(InfoType.ERROR, ex.ToString());
}
}
///
/// 执行脚本
///
///
///
public override async Task