2024-11-08 17:30:51 +08:00
|
|
|
|
using Serein.Library.Api;
|
|
|
|
|
|
using System;
|
2024-12-09 22:57:06 +08:00
|
|
|
|
using System.Collections.Concurrent;
|
2024-11-08 17:30:51 +08:00
|
|
|
|
using System.Collections.Generic;
|
2024-12-12 20:31:50 +08:00
|
|
|
|
using System.Data;
|
2025-06-22 21:53:37 +08:00
|
|
|
|
using System.Diagnostics;
|
2024-11-08 17:30:51 +08:00
|
|
|
|
using System.Diagnostics.Contracts;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2024-12-12 20:31:50 +08:00
|
|
|
|
using System.Xml.Linq;
|
2024-11-08 17:30:51 +08:00
|
|
|
|
|
2025-03-18 11:52:54 +08:00
|
|
|
|
namespace Serein.Library
|
2024-11-08 17:30:51 +08:00
|
|
|
|
{
|
2025-07-27 23:34:01 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 全局运行环境
|
|
|
|
|
|
/// </summary>
|
2024-11-08 17:30:51 +08:00
|
|
|
|
public static class SereinEnv
|
|
|
|
|
|
{
|
|
|
|
|
|
private static IFlowEnvironment environment;
|
2024-12-09 22:57:06 +08:00
|
|
|
|
|
2024-12-12 20:31:50 +08:00
|
|
|
|
#region 全局数据(暂时使用静态全局变量)
|
2024-12-09 22:57:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 记录全局数据
|
|
|
|
|
|
/// </summary>
|
2024-12-12 20:31:50 +08:00
|
|
|
|
private static ConcurrentDictionary<string, object> EnvGlobalData { get; } = new ConcurrentDictionary<string, object>();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加或更新全局数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="name"></param>
|
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static void AddOrUpdateFlowGlobalData(string name, object data)
|
|
|
|
|
|
{
|
|
|
|
|
|
SereinEnv.EnvGlobalData.AddOrUpdate(name, data, (k, o) => data);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-23 16:20:41 +08:00
|
|
|
|
/*/// <summary>
|
2024-12-12 20:31:50 +08:00
|
|
|
|
/// 更改某个数据的名称
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="oldName">旧名称</param>
|
|
|
|
|
|
/// <param name="newName">新名称</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static bool ChangeNameFlowGlobalData(string oldName, string newName)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(oldName) || string.IsNullOrEmpty(newName))
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 确保存在,然后尝试移除
|
|
|
|
|
|
if (SereinEnv.EnvGlobalData.ContainsKey(oldName)
|
|
|
|
|
|
&& SereinEnv.EnvGlobalData.TryRemove(oldName, out var data))
|
|
|
|
|
|
{
|
|
|
|
|
|
SereinEnv.EnvGlobalData.AddOrUpdate(newName, data, (k, o) => data);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-07-23 16:20:41 +08:00
|
|
|
|
}*/
|
2024-12-12 20:31:50 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取全局数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="name"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static object GetFlowGlobalData(string name)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!string.IsNullOrEmpty(name) && SereinEnv.EnvGlobalData.TryGetValue(name, out var data))
|
|
|
|
|
|
{
|
|
|
|
|
|
return data;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-09 22:57:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 清空全局数据
|
|
|
|
|
|
/// </summary>
|
2024-12-12 20:31:50 +08:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static void ClearFlowGlobalData()
|
2024-12-09 22:57:06 +08:00
|
|
|
|
{
|
|
|
|
|
|
foreach (var nodeObj in EnvGlobalData.Values)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (nodeObj != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (typeof(IDisposable).IsAssignableFrom(nodeObj?.GetType()) && nodeObj is IDisposable disposable)
|
|
|
|
|
|
{
|
|
|
|
|
|
disposable?.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
EnvGlobalData.Clear();
|
2024-12-12 20:31:50 +08:00
|
|
|
|
return;
|
2024-12-09 22:57:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-12 20:31:50 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-12-09 22:57:06 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置运行流程
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="environment"></param>
|
2024-11-08 17:30:51 +08:00
|
|
|
|
public static void SetEnv(IFlowEnvironment environment)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (environment != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
SereinEnv.environment = environment;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-09 22:57:06 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 输出内容
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="type">类型</param>
|
|
|
|
|
|
/// <param name="message">内容</param>
|
|
|
|
|
|
/// <param name="class">级别</param>
|
|
|
|
|
|
public static void WriteLine(InfoType type, string message, InfoClass @class = InfoClass.General)
|
2024-11-08 17:30:51 +08:00
|
|
|
|
{
|
2025-06-22 21:53:37 +08:00
|
|
|
|
Debug.WriteLine($"{type} : {message}");
|
2025-09-04 10:39:57 +08:00
|
|
|
|
Console.WriteLine($"{type} : {message}");
|
2025-06-22 21:53:37 +08:00
|
|
|
|
SereinEnv.environment?.WriteLine(type,message,@class);
|
2024-11-08 17:30:51 +08:00
|
|
|
|
}
|
2024-12-26 00:26:50 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 输出异常信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="ex"></param>
|
|
|
|
|
|
/// <param name="class"></param>
|
|
|
|
|
|
public static void WriteLine(Exception ex, InfoClass @class = InfoClass.General)
|
|
|
|
|
|
{
|
2025-07-31 23:59:31 +08:00
|
|
|
|
if(@class == InfoClass.Debug)
|
2025-07-17 22:46:40 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
SereinEnv.environment.WriteLine(InfoType.ERROR, ex.ToString(), @class);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
SereinEnv.environment.WriteLine(InfoType.ERROR, ex.Message, @class);
|
|
|
|
|
|
}
|
2024-12-26 00:26:50 +08:00
|
|
|
|
}
|
2024-12-09 22:57:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-07-09 21:49:26 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 尝试在UI线程上触发事件
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="action"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async static Task TriggerEvent(Action action)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (environment is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
action?.Invoke();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var uco = environment.UIContextOperation;
|
|
|
|
|
|
if (uco is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
action?.Invoke();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
await uco.InvokeAsync(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
action?.Invoke();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-12-09 22:57:06 +08:00
|
|
|
|
|
2024-11-08 17:30:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|