From d286b43c81162cc7d7d73b20b7e7172a16656c60 Mon Sep 17 00:00:00 2001 From: Meivyn Date: Sat, 3 Apr 2021 03:32:14 -0400 Subject: [PATCH] Attach console to an explicitly defined PID --- IPA.Injector/Injector.cs | 19 +++++++++++++++++-- IPA.Loader/Logging/ConsoleWindow.cs | 8 ++++---- docs/articles/command-line.md | 10 ++++++++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/IPA.Injector/Injector.cs b/IPA.Injector/Injector.cs index 1ea7e058..e2ec5c44 100644 --- a/IPA.Injector/Injector.cs +++ b/IPA.Injector/Injector.cs @@ -43,8 +43,23 @@ namespace IPA.Injector try { - if (Environment.GetCommandLineArgs().Contains("--verbose")) - WinConsole.Initialize(); + var cmd = Environment.GetCommandLineArgs(); + + if (cmd.Contains("--verbose")) + { + var arg = string.Empty; + + for (var i = 0; i < cmd.Length; i++) + { + if (cmd[i] == "-pid") + { + arg = cmd[i + 1]; + break; + } + } + + WinConsole.Initialize(uint.TryParse(arg, out uint pid) ? pid : WinConsole.AttachParent); + } SetupLibraryLoading(); diff --git a/IPA.Loader/Logging/ConsoleWindow.cs b/IPA.Loader/Logging/ConsoleWindow.cs index 89f07fb2..178aa44a 100644 --- a/IPA.Loader/Logging/ConsoleWindow.cs +++ b/IPA.Loader/Logging/ConsoleWindow.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.ComponentModel; using System.IO; using System.Runtime.InteropServices; @@ -22,11 +22,11 @@ namespace IPA.Logging internal static bool IsInitialized; - public static void Initialize(bool alwaysCreateNewConsole = false) + public static void Initialize(uint pid, bool alwaysCreateNewConsole = false) { bool consoleAttached = true; if (alwaysCreateNewConsole - || (AttachConsole(AttachParent) == 0 + || (AttachConsole(pid) == 0 && Marshal.GetLastWin32Error() != ErrorAccessDenied)) { consoleAttached = AllocConsole() != 0; @@ -155,7 +155,7 @@ namespace IPA.Logging private const uint FileAttributeNormal = 0x80; private const uint ErrorAccessDenied = 5; - private const uint AttachParent = 0xFFFFFFFF; + public const uint AttachParent = 0xFFFFFFFF; #endregion } diff --git a/docs/articles/command-line.md b/docs/articles/command-line.md index ea3e846c..2e3b6822 100644 --- a/docs/articles/command-line.md +++ b/docs/articles/command-line.md @@ -26,6 +26,16 @@ Here's a quick list of what they are and what they do. > Makes a console appear with log information at startup. > + > Optionally, to start Beat Saber with an external console, an explicit `-pid` can be specified. This allows the game to be launched without being interrupted by Steam's "Allow game launch?" popup when directly launching `Beat Saber.exe` with arguments. + > - Using PowerShell: + > ```powershell + > .\steam -applaunch 620980 --verbose -pid $PID + > ``` + > - Using Command Prompt: + > ```powershell + > powershell -c ".\steam -applaunch 620980 --verbose -pid (Get-CimInstance Win32_Process -Filter ProcessId=$PID).ParentProcessId" + > ``` + > - `--debug`