-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstallerRunner.cs
More file actions
87 lines (76 loc) · 2.6 KB
/
Copy pathInstallerRunner.cs
File metadata and controls
87 lines (76 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System.Diagnostics;
namespace CMAPI.Installer;
internal sealed class InstallerRunner
{
private readonly InstallerPackage package;
public InstallerRunner(InstallerPackage package)
{
this.package = package;
}
public async Task<int> RunAsync(
MaintenanceAction action,
string gamePath,
Action<string> writeLine,
CancellationToken cancellationToken)
{
string powerShell = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.System),
"WindowsPowerShell",
"v1.0",
"powershell.exe"
);
if (!File.Exists(powerShell))
powerShell = "powershell.exe";
ProcessStartInfo startInfo = new()
{
FileName = powerShell,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
WorkingDirectory = package.RootPath
};
startInfo.ArgumentList.Add("-NoLogo");
startInfo.ArgumentList.Add("-NoProfile");
startInfo.ArgumentList.Add("-NonInteractive");
startInfo.ArgumentList.Add("-ExecutionPolicy");
startInfo.ArgumentList.Add("Bypass");
startInfo.ArgumentList.Add("-File");
startInfo.ArgumentList.Add(package.ScriptPath);
startInfo.ArgumentList.Add("-Action");
startInfo.ArgumentList.Add(action.ToString());
startInfo.ArgumentList.Add("-GamePath");
startInfo.ArgumentList.Add(gamePath);
if (action != MaintenanceAction.Uninstall)
{
startInfo.ArgumentList.Add("-PackagePath");
startInfo.ArgumentList.Add(package.RootPath);
}
using Process process = new() { StartInfo = startInfo };
process.OutputDataReceived += (_, eventArgs) =>
{
if (eventArgs.Data != null)
writeLine(eventArgs.Data);
};
process.ErrorDataReceived += (_, eventArgs) =>
{
if (eventArgs.Data != null)
writeLine("ERROR: " + eventArgs.Data);
};
if (!process.Start())
throw new InvalidOperationException("Windows PowerShell could not be started.");
process.BeginOutputReadLine();
process.BeginErrorReadLine();
try
{
await process.WaitForExitAsync(cancellationToken);
}
catch (OperationCanceledException)
{
if (!process.HasExited)
process.Kill(entireProcessTree: true);
throw;
}
return process.ExitCode;
}
}