Skip to content

Commit

Permalink
Add a middle step where the restarter is started from a bat file
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-bures committed Jan 9, 2024
1 parent 61cf8da commit 2911f2f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@echo off
echo Starting SpaceWarpRestarter.exe...
cd /d "%~dp0"
start "" "SpaceWarpRestarter.exe" %*
21 changes: 17 additions & 4 deletions src/SpaceWarp.UI/Backend/Processes/ProcessStarter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace SpaceWarp.Backend.Processes;

internal static class ProcessStarter
{
private const uint DetachedProcess = 0x00000008;
private const uint CreateNewConsole = 0x00000010;

[StructLayout(LayoutKind.Sequential)]
private struct ProcessInformation
Expand Down Expand Up @@ -53,6 +53,10 @@ private static extern bool CreateProcess(
out ProcessInformation lpProcessInformation
);

[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle(IntPtr hObject);

public static bool StartDetachedProcess(string applicationName, string commandLine)
{
var si = new StartupInfo();
Expand All @@ -64,13 +68,22 @@ public static bool StartDetachedProcess(string applicationName, string commandLi
IntPtr.Zero,
IntPtr.Zero,
false,
DetachedProcess,
CreateNewConsole,
IntPtr.Zero,
null,
ref si,
out _
out var pi
);

return success;
if (!success)
{
return false;
}

// Close the handles to the process and thread
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

return true;
}
}
22 changes: 8 additions & 14 deletions src/SpaceWarp.UI/UI/ModList/ModListController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,29 +352,22 @@ private void SetupButtons()
_applyChangesButton.style.display = DisplayStyle.None;
_applyChangesButton.RegisterCallback<ClickEvent>(_ =>
{
int currentPid = Process.GetCurrentProcess().Id;
var currentPid = Process.GetCurrentProcess().Id;

// We call on restarter to kill the current process
// and it will call the restarter to restarter.
var restarterPath = Path.Combine(
SpaceWarpPlugin.Instance.SWMetadata.Folder.FullName,
"restarter",
"SpaceWarpRestarter.exe"
"invoke_restarter.bat"
);

// var restarter = new Process();
//
// // We give the restarter the current_pid so that it can kill it.
// restarter.StartInfo = new ProcessStartInfo(restarterPath)
// {
// Arguments = $"{currentPid} \"{string.Join(" ", Environment.GetCommandLineArgs()[1..])}\"",
// UseShellExecute = true
// };
// restarter.Start();
// restarter.Dispose();
var cmd = $"cmd.exe /C \"" +
$"\"{restarterPath}\" " +
$"{currentPid} " +
$"\"{string.Join(" ", Environment.GetCommandLineArgs()[1..])}\"" +
$"\"";


var cmd = $"\"{restarterPath}\" {currentPid} \"{string.Join(" ", Environment.GetCommandLineArgs()[1..])}\"";
Modules.UI.Instance.ModuleLogger.LogDebug($"Starting restarter: {cmd}");
var result = ProcessStarter.StartDetachedProcess(
null,
Expand All @@ -384,6 +377,7 @@ private void SetupButtons()
Modules.UI.Instance.ModuleLogger.LogDebug($"Restarter result: {result}");

// Hang the current process so that the restarter can kill it.
// ReSharper disable once LoopVariableIsNeverChangedInsideLoop
while (result)
{
Thread.Sleep(1000);
Expand Down

0 comments on commit 2911f2f

Please sign in to comment.