2023-07-12 09:19:33 +02:00
|
|
|
|
using System.Diagnostics;
|
2023-11-22 09:32:18 -05:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
2023-07-12 09:19:33 +02:00
|
|
|
|
using SPTInstaller.Models;
|
2022-05-13 22:41:15 +01:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
namespace SPTInstaller.Helpers;
|
|
|
|
|
|
|
|
|
|
public enum PatcherExitCode
|
2022-05-13 22:41:15 +01:00
|
|
|
|
{
|
2023-07-12 09:19:33 +02:00
|
|
|
|
ProgramClosed = 0,
|
|
|
|
|
Success = 10,
|
|
|
|
|
EftExeNotFound = 11,
|
|
|
|
|
NoPatchFolder = 12,
|
|
|
|
|
MissingFile = 13,
|
|
|
|
|
MissingDir = 14,
|
|
|
|
|
PatchFailed = 15
|
|
|
|
|
}
|
2022-05-13 22:41:15 +01:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
public static class ProcessHelper
|
|
|
|
|
{
|
|
|
|
|
public static Result PatchClientFiles(FileInfo executable, DirectoryInfo workingDir)
|
2022-07-09 00:33:55 -04:00
|
|
|
|
{
|
2023-07-12 09:19:33 +02:00
|
|
|
|
if (!executable.Exists || !workingDir.Exists)
|
2022-05-13 22:41:15 +01:00
|
|
|
|
{
|
2023-07-12 09:19:33 +02:00
|
|
|
|
return Result.FromError(
|
|
|
|
|
$"Could not find executable ({executable.Name}) or working directory ({workingDir.Name})");
|
|
|
|
|
}
|
2022-07-09 13:08:41 -04:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
var process = new Process();
|
|
|
|
|
process.StartInfo.FileName = executable.FullName;
|
|
|
|
|
process.StartInfo.WorkingDirectory = workingDir.FullName;
|
|
|
|
|
process.EnableRaisingEvents = true;
|
|
|
|
|
process.StartInfo.Arguments = "autoclose";
|
|
|
|
|
process.Start();
|
2022-05-19 14:41:44 +01:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
process.WaitForExit();
|
2022-05-19 14:41:44 +01:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
switch ((PatcherExitCode)process.ExitCode)
|
|
|
|
|
{
|
|
|
|
|
case PatcherExitCode.Success:
|
|
|
|
|
return Result.FromSuccess("Patcher Finished Successfully, extracting Aki");
|
2022-05-19 14:41:44 +01:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
case PatcherExitCode.ProgramClosed:
|
|
|
|
|
return Result.FromError("Patcher was closed before completing!");
|
2022-05-19 14:41:44 +01:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
case PatcherExitCode.EftExeNotFound:
|
|
|
|
|
return Result.FromError("EscapeFromTarkov.exe is missing from the install Path");
|
2022-05-19 14:41:44 +01:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
case PatcherExitCode.NoPatchFolder:
|
|
|
|
|
return Result.FromError("Patchers Folder called 'Aki_Patches' is missing");
|
2022-05-19 14:41:44 +01:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
case PatcherExitCode.MissingFile:
|
|
|
|
|
return Result.FromError("EFT files was missing a Vital file to continue");
|
2022-05-19 14:41:44 +01:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
case PatcherExitCode.PatchFailed:
|
|
|
|
|
return Result.FromError("A patch failed to apply");
|
2022-05-19 14:41:44 +01:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
default:
|
|
|
|
|
return Result.FromError("an unknown error occurred in the patcher");
|
2022-05-13 22:41:15 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-22 09:32:18 -05:00
|
|
|
|
|
|
|
|
|
public static ReadProcessResult RunAndReadProcessOutputs(string fileName, string args, int timeout = 5000)
|
|
|
|
|
{
|
|
|
|
|
using var proc = new Process();
|
|
|
|
|
|
|
|
|
|
proc.StartInfo = new ProcessStartInfo
|
|
|
|
|
{
|
|
|
|
|
FileName = fileName,
|
|
|
|
|
Arguments = args,
|
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
|
RedirectStandardError = true,
|
|
|
|
|
CreateNoWindow = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var outputBuilder = new StringBuilder();
|
|
|
|
|
var errorBuilder = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
using AutoResetEvent outputWaitHandle = new AutoResetEvent(false);
|
|
|
|
|
using AutoResetEvent errorWaitHandle = new AutoResetEvent(false);
|
|
|
|
|
|
|
|
|
|
proc.OutputDataReceived += (s, e) =>
|
|
|
|
|
{
|
|
|
|
|
if (e.Data == null)
|
|
|
|
|
{
|
|
|
|
|
outputWaitHandle.Set();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
outputBuilder.AppendLine(e.Data);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
proc.ErrorDataReceived += (s, e) =>
|
|
|
|
|
{
|
|
|
|
|
if (e.Data == null)
|
|
|
|
|
{
|
|
|
|
|
errorWaitHandle.Set();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
errorBuilder.AppendLine(e.Data);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
proc.Start();
|
|
|
|
|
|
|
|
|
|
proc.BeginOutputReadLine();
|
|
|
|
|
proc.BeginErrorReadLine();
|
|
|
|
|
|
|
|
|
|
if (!proc.WaitForExit(timeout) || !outputWaitHandle.WaitOne(timeout) || !errorWaitHandle.WaitOne(timeout))
|
|
|
|
|
{
|
|
|
|
|
return ReadProcessResult.FromError("Process timed out");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ReadProcessResult.FromSuccess(outputBuilder.ToString(), errorBuilder.ToString());
|
|
|
|
|
}
|
2023-07-12 09:19:33 +02:00
|
|
|
|
}
|