- Add the isolated Windows launcher and checkpoint worker. - Restore native bell and training interactions and enter the custom start page directly. - Update run configuration, encounters, assists and the in-game GM menu.
81 lines
3.0 KiB
C#
81 lines
3.0 KiB
C#
using System.ComponentModel;
|
|
using System.Diagnostics;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
|
|
namespace PBZRogue.Runtime;
|
|
|
|
internal static class DetachedProcess
|
|
{
|
|
// Standard Process.Start can inherit other open handles even when its standard
|
|
// streams are redirected. Both long-lived children must be detached from the
|
|
// short-lived launcher's handles, including captured CLI output.
|
|
public static Process Start(ProcessStartInfo start)
|
|
{
|
|
var command = new StringBuilder();
|
|
AppendArgument(command, start.FileName);
|
|
foreach (var argument in start.ArgumentList)
|
|
{
|
|
command.Append(' ');
|
|
AppendArgument(command, argument);
|
|
}
|
|
var startup = new StartupInfo { Size = (uint)Marshal.SizeOf<StartupInfo>() };
|
|
if (!CreateProcessW(start.FileName, command, IntPtr.Zero, IntPtr.Zero, false,
|
|
0x08000000, IntPtr.Zero, start.WorkingDirectory, ref startup, out var created))
|
|
throw new Win32Exception(Marshal.GetLastWin32Error(), "Cannot start the isolated game process.");
|
|
try
|
|
{
|
|
var process = Process.GetProcessById((int)created.ProcessId);
|
|
try { _ = process.Handle; return process; }
|
|
catch { process.Dispose(); throw; }
|
|
}
|
|
finally
|
|
{
|
|
CloseHandle(created.Thread);
|
|
CloseHandle(created.Process);
|
|
}
|
|
}
|
|
|
|
private static void AppendArgument(StringBuilder command, string argument)
|
|
{
|
|
command.Append('"');
|
|
int slashes = 0;
|
|
foreach (char character in argument)
|
|
{
|
|
if (character == '\\') { slashes++; continue; }
|
|
command.Append('\\', character == '"' ? slashes * 2 + 1 : slashes);
|
|
command.Append(character);
|
|
slashes = 0;
|
|
}
|
|
command.Append('\\', slashes * 2);
|
|
command.Append('"');
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
private struct StartupInfo
|
|
{
|
|
public uint Size;
|
|
public IntPtr Reserved, Desktop, Title;
|
|
public uint X, Y, Width, Height, CharacterWidth, CharacterHeight, FillAttribute, Flags;
|
|
public ushort ShowWindow, ReservedSize;
|
|
public IntPtr ReservedBytes, StandardInput, StandardOutput, StandardError;
|
|
}
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
private struct ProcessInformation
|
|
{
|
|
public IntPtr Process, Thread;
|
|
public uint ProcessId, ThreadId;
|
|
}
|
|
|
|
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
private static extern bool CreateProcessW(string application, StringBuilder command,
|
|
IntPtr processAttributes, IntPtr threadAttributes, [MarshalAs(UnmanagedType.Bool)] bool inheritHandles,
|
|
uint flags, IntPtr environment, string directory, ref StartupInfo startup, out ProcessInformation process);
|
|
|
|
[DllImport("kernel32.dll", ExactSpelling = true)]
|
|
[return: MarshalAs(UnmanagedType.Bool)]
|
|
private static extern bool CloseHandle(IntPtr handle);
|
|
}
|