-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
59 additions
and
300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 0 additions & 4 deletions
4
plugin_template/BepInEx/plugins/SpaceWarp/restarter/invoke_restarter.bat
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,39 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Management; | ||
using System.Threading; | ||
|
||
namespace SpaceWarpRestarter; | ||
if (args.Length < 1) | ||
{ | ||
Console.WriteLine( | ||
$"Usage: {Path.GetFileNameWithoutExtension(AppContext.BaseDirectory)} <path to KSP2_x64.exe> " + | ||
$"[optional arguments]" | ||
); | ||
Environment.Exit(1); | ||
} | ||
|
||
var processPath = args[0]; | ||
var processName = Path.GetFileNameWithoutExtension(processPath); | ||
var processArgs = args.Length > 1 ? args[1..] : Array.Empty<string>(); | ||
|
||
internal class Restarter | ||
Console.WriteLine($"Waiting for {processName} to exit..."); | ||
|
||
while (true) | ||
{ | ||
public static void Main(string[] args) | ||
if (Process.GetProcessesByName(processName).Length == 0) | ||
{ | ||
// Can only run on Windows | ||
if (Environment.OSVersion.Platform != PlatformID.Win32NT) | ||
{ | ||
Console.WriteLine("This can only run on Windows."); | ||
Environment.Exit(1); | ||
} | ||
|
||
if (args.Length < 1) | ||
try | ||
{ | ||
Console.WriteLine( | ||
$"Usage: {Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().Location)}.exe " + | ||
$"<target process id> " + | ||
$"[optional arguments that will be passed to KSP2_x64.exe]" | ||
); | ||
Environment.Exit(1); | ||
Console.WriteLine($"{processName}.exe is not running. Attempting to start the process..."); | ||
Process.Start(processPath, processArgs); | ||
Console.WriteLine($"{processName}.exe started successfully."); | ||
break; | ||
} | ||
|
||
Console.WriteLine($"Args: {string.Join(" ", args)}"); | ||
|
||
var targetPid = args[0]; | ||
var processArgs = string.Join(" ", args.Length > 1 ? args.Skip(1).ToArray() : Array.Empty<string>()); | ||
|
||
// Get the path to the KSP2_x64.exe | ||
var process = Process.GetProcessById(int.Parse(targetPid)); | ||
var processModule = process.MainModule; | ||
Console.WriteLine($"Found process: {process.ProcessName} ({process.Id}) at {processModule?.FileName}"); | ||
if (processModule != null) | ||
catch (Exception ex) | ||
{ | ||
var processPath = processModule.FileName; | ||
|
||
KillProc(int.Parse(targetPid)); | ||
|
||
Console.WriteLine($"Starting process {processPath} {processArgs}"); | ||
|
||
Thread.Sleep(1000); | ||
|
||
// Launch the process | ||
var ksp2Process = new Process | ||
{ | ||
StartInfo = new ProcessStartInfo | ||
{ | ||
FileName = processPath, | ||
Arguments = processArgs, | ||
UseShellExecute = false, | ||
RedirectStandardOutput = true, | ||
RedirectStandardError = true, | ||
CreateNoWindow = true | ||
} | ||
}; | ||
|
||
// Start the ksp2_process and detach from it | ||
ksp2Process.Start(); | ||
ksp2Process.Dispose(); | ||
} else { | ||
Console.WriteLine("Could not find the process."); | ||
Console.WriteLine($"An error occurred while starting {processName}.exe: {ex.Message}"); | ||
Environment.Exit(1); | ||
} | ||
|
||
Environment.Exit(0); | ||
} | ||
|
||
// Will recursively kill all child processes of the given process and the process itself | ||
private static void KillProc(int pid) | ||
else | ||
{ | ||
var process = Process.GetProcessById(pid); | ||
|
||
var restarterProcess = Process.GetCurrentProcess(); | ||
|
||
// We already guaranteed that this will only run on Windows | ||
#pragma warning disable CA1416 | ||
var searcher = new ManagementObjectSearcher( | ||
$"SELECT * " + | ||
$"FROM Win32_Process " + | ||
$"WHERE ParentProcessId={pid}" | ||
); | ||
|
||
foreach (var child in searcher.Get()) | ||
{ | ||
var processId = int.Parse(child["ProcessId"].ToString()); | ||
if (processId != restarterProcess.Id) | ||
{ | ||
KillProc(processId); | ||
} | ||
} | ||
|
||
Console.WriteLine($"Killing process {process.ProcessName} ({process.Id})"); | ||
process.Kill(); | ||
#pragma warning restore CA1416 | ||
Thread.Sleep(500); | ||
} | ||
} |
Oops, something went wrong.