Files
BusyRabbit/Plugins/UnrealSharp/Managed/UnrealSharpPrograms/UnrealSharpBuildTool/BuildToolProcess.cs
wyatt 648386cd73 Lua向C#逻辑迁移 一期 #13
将整个插件代码上传
2025-10-26 21:48:39 +08:00

74 lines
1.9 KiB
C#

using System.Collections;
using System.Diagnostics;
using System.Text;
namespace UnrealSharpBuildTool;
public class BuildToolProcess : Process
{
public BuildToolProcess(string? fileName = null)
{
if (fileName == null)
{
if (string.IsNullOrEmpty(Program.BuildToolOptions.DotNetPath))
{
fileName = "dotnet";
}
else
{
fileName = Program.BuildToolOptions.DotNetPath;
}
}
StartInfo.FileName = fileName;
StartInfo.CreateNoWindow = true;
StartInfo.ErrorDialog = false;
StartInfo.UseShellExecute = false;
StartInfo.RedirectStandardError = true;
StartInfo.RedirectStandardInput = true;
StartInfo.RedirectStandardOutput = true;
EnableRaisingEvents = true;
}
public bool StartBuildToolProcess()
{
StringBuilder output = new StringBuilder();
OutputDataReceived += (sender, e) =>
{
if (e.Data != null)
{
output.AppendLine(e.Data);
}
};
ErrorDataReceived += (sender, e) =>
{
if (e.Data != null)
{
output.AppendLine(e.Data);
}
};
if (!Start())
{
throw new Exception("Failed to start process");
}
BeginErrorReadLine();
BeginOutputReadLine();
WaitForExit();
if (ExitCode != 0)
{
string errorMessage = output.ToString();
if (string.IsNullOrEmpty(errorMessage))
{
errorMessage = "BuildTool process exited with non-zero exit code, but no output was captured.";
}
throw new Exception($"BuildTool process failed with exit code {ExitCode}:\n{errorMessage}");
}
return true;
}
}