重写了 启动器示例工程 的代码

This commit is contained in:
fengjiayi
2024-12-12 20:47:10 +08:00
parent e7ab65cead
commit 3d0ed00fba
2 changed files with 78 additions and 41 deletions

55
FlowStartTool/FlowEnv.cs Normal file
View File

@@ -0,0 +1,55 @@
using Serein.Library;
using Serein.Library.Api;
using Serein.Library.Utils;
using Serein.NodeFlow.Env;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Serein.FlowStartTool
{
internal class FlowEnv
{
public IFlowEnvironment? Env;
public bool IsRuning;
public async Task StartFlow(SereinProjectData flowProjectData, string fileDataPath)
{
IsRuning = true;
SynchronizationContext? uiContext = SynchronizationContext.Current; // 在UI线程上获取UI线程上下文信息
var uIContextOperation = new UIContextOperation(uiContext); // 封装一个调用UI线程的工具类
//if (OperatingSystem.IsLinux())
//{
//}
// if (uIContextOperation is null)
//{
// throw new Exception("无法封装 UIContextOperation ");
//}
//else
//{
// env = new FlowEnvironmentDecorator(uIContextOperation);
// this.window = window;
//}
Env = new FlowEnvironmentDecorator(uIContextOperation);
Env.LoadProject(new FlowEnvInfo { Project = flowProjectData }, fileDataPath); // 加载项目
// 获取环境输出
Env.OnEnvOut += (infoType, value) =>
{
Console.WriteLine($"{DateTime.UtcNow} [{infoType}] : {value}{Environment.NewLine}");
};
await Env.StartRemoteServerAsync(7525); // 启动 web socket 监听远程请求
//await Env.StartAsync();
IsRuning = false;
}
}
}

View File

@@ -11,20 +11,26 @@ namespace Serein.FlowStartTool
{
public class Program
{
/// <summary>
/// 运行环境
/// </summary>
private static readonly FlowEnv flowEnv = new FlowEnv();
public static void Main(string[] args)
{
#region
#if debug
args = [@"F:\临时\project\linux\project.dnf"];
#endif
Console.WriteLine("Hello :) ");
Console.WriteLine($"args : {string.Join(" , ", args)}");
string filePath;
string fileDataPath;
SereinProjectData? flowProjectData;
string exeAssemblyDictPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string? assembly = Assembly.GetExecutingAssembly()?.Location;
string exeAssemblyDictPath = Path.GetDirectoryName(assembly)!;
if (args.Length == 1)
{
@@ -35,6 +41,7 @@ namespace Serein.FlowStartTool
{
Console.WriteLine("loading project file data...");
filePath = Process.GetCurrentProcess().ProcessName + ".dnf";
fileDataPath = exeAssemblyDictPath;
}
@@ -45,9 +52,11 @@ namespace Serein.FlowStartTool
Console.WriteLine($"Current Name : {filePath}");
Console.WriteLine($"Dict Path : {fileDataPath}");
#endregion
#region
try
{
// 读取文件内容
string content = File.ReadAllText(filePath); // 读取整个文件内容
flowProjectData = JsonConvert.DeserializeObject<SereinProjectData>(content);
if (flowProjectData is null || string.IsNullOrEmpty(fileDataPath))
@@ -60,47 +69,20 @@ namespace Serein.FlowStartTool
Console.WriteLine($"读取文件时发生错误:{ex.Message}");
return;
}
#endregion
IsRuning = true;
_ = Task.Run(async () => await StartFlow(flowProjectData, fileDataPath));
while (IsRuning)
#region
_ = Task.Run(async () => await flowEnv.StartFlow(flowProjectData, fileDataPath));
while (flowEnv.IsRuning)
{
Console.ReadKey();
}
#endregion
}
public static IFlowEnvironment? Env;
public static bool IsRuning;
public static async Task StartFlow(SereinProjectData flowProjectData, string fileDataPath)
{
SynchronizationContext? uiContext = SynchronizationContext.Current; // 在UI线程上获取UI线程上下文信息
var uIContextOperation = new UIContextOperation(uiContext); // 封装一个调用UI线程的工具类
//if (OperatingSystem.IsLinux())
//{
//}
// if (uIContextOperation is null)
//{
// throw new Exception("无法封装 UIContextOperation ");
//}
//else
//{
// env = new FlowEnvironmentDecorator(uIContextOperation);
// this.window = window;
//}
Env = new FlowEnvironmentDecorator(uIContextOperation);
Env.LoadProject(new FlowEnvInfo { Project = flowProjectData }, fileDataPath); // 加载项目
await Env.StartRemoteServerAsync(7525); // 启动 web socket 监听远程请求
//await Env.StartAsync();
IsRuning = false;
}
}
}