重写了Script的解释器代码,使其更加直观。重写了流程控制的部分代码,分离运行环境IOC与流程IOC。

This commit is contained in:
fengjiayi
2025-07-18 22:45:06 +08:00
parent 88de5a21f5
commit fc05cd662b
38 changed files with 567 additions and 1418 deletions

View File

@@ -17,10 +17,11 @@ namespace Serein.Library.Api
string Guid {get; }
/// <summary>
/// 运行环境包含IOC容器。
/// 运行环境
/// </summary>
IFlowEnvironment Env { get; }
/// <summary>
/// 是否正在运行
/// </summary>

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Serein.Library.Api
@@ -8,6 +9,13 @@ namespace Serein.Library.Api
/// </summary>
public interface IFlowControl
{
/// <summary>
/// <para>单例模式IOC容器内部维护了一个实例字典默认使用类型的FullName作为Key如果以“接口-实现类”的方式注册那么将使用接口类型的FullName作为Key。</para>
/// <para>当某个类型注册绑定成功后,将不会因为其它地方尝试注册相同类型的行为导致类型被重新创建。</para>
/// </summary>
ISereinIOC IOC { get; }
/// <summary>
/// <para>需要你提供一个由你实现的ISereinIOC接口实现类</para>
/// <para>当你将流程运行环境集成在你的项目时,并希望流程运行时使用你提供的对象,而非自动创建</para>
@@ -15,7 +23,8 @@ namespace Serein.Library.Api
/// <para>注意,是流程运行时,而非运行环境</para>
/// </summary>
/// <param name="ioc"></param>
void UseExternalIOC(ISereinIOC ioc);
/// <param name="setDefultMemberOnReset">用于每次启动时重置IOC后默认注册某些类型</param>
void UseExternalIOC(ISereinIOC ioc, Action<ISereinIOC> setDefultMemberOnReset = null);
/// <summary>
/// 开始运行流程

View File

@@ -759,8 +759,7 @@ namespace Serein.Library.Api
/// <summary>
/// <para>单例模式IOC容器内部维护了一个实例字典默认使用类型的FullName作为Key如果以“接口-实现类”的方式注册那么将使用接口类型的FullName作为Key。</para>
/// <para>当某个类型注册绑定成功后,将不会因为其它地方尝试注册相同类型的行为导致类型被重新创建。</para>
/// <para>运行环境使用的IOC默认情况下无需对其进行调用</para>
/// </summary>
ISereinIOC IOC { get; }

View File

@@ -15,7 +15,8 @@ namespace Serein.Library
/// <summary>
/// 动态流程上下文
/// </summary>
/// <param name="flowEnvironment"></param>
/// <param name="flowEnvironment">脚本运行时的IOC</param>
/// <param name="ioc">脚本运行时使用的IOC容器</param>
public DynamicContext(IFlowEnvironment flowEnvironment)
{
Env = flowEnvironment;

View File

@@ -372,6 +372,8 @@ namespace Serein.Library
private readonly IFlowEnvironment flowEnvironment;
public static Serein.Library.Utils.ObjectPool<IDynamicContext> FlowContextPool { get; set; }
public ISereinIOC IOC => throw new NotImplementedException();
public LightweightFlowControl(IFlowCallTree flowCallTree, IFlowEnvironment flowEnvironment)
{
this.flowCallTree = flowCallTree;
@@ -478,7 +480,12 @@ namespace Serein.Library
public void UseExternalIOC(ISereinIOC ioc)
{
throw new NotImplementedException();
}
}
public void UseExternalIOC(ISereinIOC ioc, Action<ISereinIOC> setDefultMemberOnReset = null)
{
throw new NotImplementedException();
}
#endregion
}

View File

@@ -3,6 +3,7 @@ using Serein.Library.Api;
using Serein.Library.Utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
@@ -77,7 +78,8 @@ namespace Serein.Library
partial void OnIsInterruptChanged(bool oldValue, bool newValue)
{
if (newValue && _getInterruptTask is null)
Debug.WriteLine($" {nameof(NodeDebugSetting)}.{nameof(NodeDebugSetting.OnIsInterruptChanged)} 暂未实现,需要重新设计中断逻辑");
/*if (newValue && _getInterruptTask is null)
{
// 设置获取中断的委托
_getInterruptTask = () => NodeModel.Env.IOC.Get<FlowInterruptTool>().WaitTriggerAsync(NodeModel.Guid);
@@ -97,7 +99,7 @@ namespace Serein.Library
_getInterruptTask = null;
}
}
}*/
}

View File

@@ -335,7 +335,7 @@ namespace Serein.Library
#region -
/*#region “枚举-类型”转换器
if (ExplicitType is not null && ExplicitType.IsEnum && DataType != ExplicitType)
{
var resultEnum = Enum.Parse(ExplicitType, DataValue);
@@ -347,7 +347,7 @@ namespace Serein.Library
return value;
}
}
#endregion
#endregion*/
// 需要获取预入参数据
object inputParameter;

View File

@@ -128,6 +128,7 @@ namespace Serein.Library.Utils
total += ms;
Console.WriteLine($"运行1次耗时 :{total} 毫秒:");
Debug.WriteLine($"运行1次耗时 :{total} 毫秒:");
return result;
}
}

View File

@@ -17,15 +17,26 @@ namespace Serein.Library.Utils
public class EmitMethodInfo
{
/// <summary>
/// 方法声明类型
/// </summary>
public Type DeclaringType { get; set; }
/// <summary>
/// 方法类型
/// </summary>
public EmitMethodType EmitMethodType { get; set; }
/// <summary>
/// 是异步方法
/// </summary>
public bool IsTask { get; set; }
public bool IsAsync { get; set; }
/// <summary>
/// 是静态的
/// </summary>
public bool IsStatic { get; set; }
}
public enum EmitMethodType
@@ -41,16 +52,7 @@ namespace Serein.Library.Utils
/// <summary>
/// 有返回值的异步方法
/// </summary>
HasResultTask,
/// <summary>
/// 普通的方法。如果方法返回void时将会返回null。
/// </summary>
StaticFunc,
/// <summary>
/// 无返回值的异步方法
/// </summary>
StaticTask,
TaskHasResult,
}
public static bool IsGenericTask(Type returnType, out Type taskResult)
@@ -179,26 +181,31 @@ namespace Serein.Library.Utils
}
// 处理返回值如果没有返回值则返回null
il.Emit(OpCodes.Ret); // 返回
EmitMethodType emitMethodType;
if (IsTask)
{
if (IsTaskGenerics)
{
emitMethodType = EmitMethodType.TaskHasResult;
@delegate = dynamicMethod.CreateDelegate(typeof(Func<object, object[], Task<object>>));
}
else
{
emitMethodType = EmitMethodType.Task;
@delegate = dynamicMethod.CreateDelegate(typeof(Func<object, object[], Task>));
}
}
else
{
emitMethodType = EmitMethodType.Func;
@delegate = dynamicMethod.CreateDelegate(typeof(Func<object, object[], object>));
}
return new EmitMethodInfo
{
EmitMethodType = emitMethodType,
DeclaringType = methodInfo.DeclaringType,
IsTask = IsTask,
IsAsync = IsTask,
IsStatic = isStatic
};
}

View File

@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Serein.Library.Utils
{
/// <summary>
/// 对于 linq 的异步扩展方法
/// </summary>
public static class LinqAsyncHelper
{
public static async Task<IEnumerable<TResult>> SelectAsync<TSource, TResult>(this IEnumerable<TSource> source,
Func<TSource, Task<TResult>> method)
{
return await Task.WhenAll(source.Select(async s => await method(s)));
}
public static async Task<IEnumerable<TResult>> SelectAsync<TSource, TResult>(this IEnumerable<TSource> source,
Func<TSource, Task<TResult>> method,
int concurrency = int.MaxValue)
{
var semaphore = new SemaphoreSlim(concurrency);
try
{
return await Task.WhenAll(source.Select(async s =>
{
try
{
await semaphore.WaitAsync();
return await method(s);
}
finally
{
semaphore.Release();
}
}));
}
finally
{
semaphore.Dispose();
}
}
}
}

View File

@@ -12,7 +12,7 @@ namespace Serein.Library.Utils
{
/// <summary>
/// 一个轻量级的IOC容器
/// 一个轻量级的单例IOC容器
/// </summary>
public class SereinIOC : ISereinIOC
{
@@ -45,7 +45,7 @@ namespace Serein.Library.Utils
public event IOCMembersChangedHandler OnIOCMembersChanged;
/// <summary>
/// 一个轻量级的IOC容器
/// 一个轻量级的D单例IOC容器
/// </summary>
public SereinIOC()
{