From 8d992d08de5c807b76f0aed37a60910498369ece Mon Sep 17 00:00:00 2001 From: cozyGalvinism Date: Fri, 27 Jan 2023 12:01:45 +0100 Subject: [PATCH 1/2] Implement a Run function for running programs on the game runner --- src/XIVLauncher.Common.Unix/UnixGameRunner.cs | 33 +++++++++++++++++++ .../WindowsGameRunner.cs | 28 ++++++++++++++++ .../PlatformAbstractions/IGameRunner.cs | 2 ++ 3 files changed, 63 insertions(+) diff --git a/src/XIVLauncher.Common.Unix/UnixGameRunner.cs b/src/XIVLauncher.Common.Unix/UnixGameRunner.cs index e612377de..f1b45cf2f 100644 --- a/src/XIVLauncher.Common.Unix/UnixGameRunner.cs +++ b/src/XIVLauncher.Common.Unix/UnixGameRunner.cs @@ -31,4 +31,37 @@ public UnixGameRunner(CompatibilityTools compatibility, DalamudLauncher dalamudL return compatibility.RunInPrefix($"\"{path}\" {arguments}", workingDirectory, environment, writeLog: true); } } + + public Process? Run(string path, string workingDirectory, string arguments, IDictionary environment, bool withCompatibility) + { + if (withCompatibility) + { + return compatibility.RunInPrefix($"\"{path}\" {arguments}", workingDirectory, environment, writeLog: true); + } + + var psi = new ProcessStartInfo(path, arguments) + { + WorkingDirectory = workingDirectory + }; + + foreach (var envVar in environment) + { + if (psi.Environment.ContainsKey(envVar.Key)) + { + psi.Environment[envVar.Key] = envVar.Value; + } + else + { + psi.Environment.Add(envVar.Key, envVar.Value); + } + } + + var p = new Process() + { + StartInfo = psi + }; + p.Start(); + + return p; + } } \ No newline at end of file diff --git a/src/XIVLauncher.Common.Windows/WindowsGameRunner.cs b/src/XIVLauncher.Common.Windows/WindowsGameRunner.cs index 229fcc943..e01764d0e 100644 --- a/src/XIVLauncher.Common.Windows/WindowsGameRunner.cs +++ b/src/XIVLauncher.Common.Windows/WindowsGameRunner.cs @@ -44,4 +44,32 @@ public Process Start(string path, string workingDirectory, string arguments, IDi return NativeAclFix.LaunchGame(workingDirectory, path, arguments, environment, dpiAwareness, process => { }); } } + + public Process? Run(string path, string workingDirectory, string arguments, IDictionary environment, bool withCompatibility) + { + var psi = new ProcessStartInfo(path, arguments) + { + WorkingDirectory = workingDirectory + }; + + foreach (var envVar in environment) + { + if (psi.Environment.ContainsKey(envVar.Key)) + { + psi.Environment[envVar.Key] = envVar.Value; + } + else + { + psi.Environment.Add(envVar.Key, envVar.Value); + } + } + + var p = new Process() + { + StartInfo = psi + }; + p.Start(); + + return p; + } } \ No newline at end of file diff --git a/src/XIVLauncher.Common/PlatformAbstractions/IGameRunner.cs b/src/XIVLauncher.Common/PlatformAbstractions/IGameRunner.cs index d0a19656b..18449f2cb 100644 --- a/src/XIVLauncher.Common/PlatformAbstractions/IGameRunner.cs +++ b/src/XIVLauncher.Common/PlatformAbstractions/IGameRunner.cs @@ -6,4 +6,6 @@ namespace XIVLauncher.Common.PlatformAbstractions; public interface IGameRunner { Process? Start(string path, string workingDirectory, string arguments, IDictionary environment, DpiAwareness dpiAwareness); + + Process? Run(string path, string workingDirectory, string arguments, IDictionary environment, bool withCompatibility); } \ No newline at end of file From c74266baac9ca776e87a9c6d642bd1cce1f130cd Mon Sep 17 00:00:00 2001 From: cozyGalvinism Date: Wed, 8 Feb 2023 13:30:29 +0100 Subject: [PATCH 2/2] Fix potential issue of XLCore not being able to read info from stdout anymore So instead, the process output is now sent to the log --- src/XIVLauncher.Common.Unix/UnixGameRunner.cs | 18 ++++++++++++++++-- .../WindowsGameRunner.cs | 16 +++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/XIVLauncher.Common.Unix/UnixGameRunner.cs b/src/XIVLauncher.Common.Unix/UnixGameRunner.cs index f1b45cf2f..34241f5b4 100644 --- a/src/XIVLauncher.Common.Unix/UnixGameRunner.cs +++ b/src/XIVLauncher.Common.Unix/UnixGameRunner.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; +using Serilog; using XIVLauncher.Common.Dalamud; using XIVLauncher.Common.PlatformAbstractions; using XIVLauncher.Common.Unix.Compatibility; @@ -32,6 +33,11 @@ public UnixGameRunner(CompatibilityTools compatibility, DalamudLauncher dalamudL } } + private void ProcessOutputHandler(object sender, DataReceivedEventArgs args) + { + Log.Information("Process output: {0}", args.Data); + } + public Process? Run(string path, string workingDirectory, string arguments, IDictionary environment, bool withCompatibility) { if (withCompatibility) @@ -41,7 +47,10 @@ public UnixGameRunner(CompatibilityTools compatibility, DalamudLauncher dalamudL var psi = new ProcessStartInfo(path, arguments) { - WorkingDirectory = workingDirectory + WorkingDirectory = workingDirectory, + RedirectStandardError = true, + RedirectStandardOutput = true, + UseShellExecute = false }; foreach (var envVar in environment) @@ -58,9 +67,14 @@ public UnixGameRunner(CompatibilityTools compatibility, DalamudLauncher dalamudL var p = new Process() { - StartInfo = psi + StartInfo = psi, }; + + p.OutputDataReceived += ProcessOutputHandler; + p.Start(); + p.BeginOutputReadLine(); + p.BeginErrorReadLine(); return p; } diff --git a/src/XIVLauncher.Common.Windows/WindowsGameRunner.cs b/src/XIVLauncher.Common.Windows/WindowsGameRunner.cs index e01764d0e..1535820e9 100644 --- a/src/XIVLauncher.Common.Windows/WindowsGameRunner.cs +++ b/src/XIVLauncher.Common.Windows/WindowsGameRunner.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; +using Serilog; using XIVLauncher.Common.Dalamud; using XIVLauncher.Common.Game; using XIVLauncher.Common.PlatformAbstractions; @@ -45,11 +46,19 @@ public Process Start(string path, string workingDirectory, string arguments, IDi } } + private void ProcessOutputHandler(object sender, DataReceivedEventArgs args) + { + Log.Information("Process output: {0}", args.Data); + } + public Process? Run(string path, string workingDirectory, string arguments, IDictionary environment, bool withCompatibility) { var psi = new ProcessStartInfo(path, arguments) { - WorkingDirectory = workingDirectory + WorkingDirectory = workingDirectory, + RedirectStandardError = true, + RedirectStandardOutput = true, + UseShellExecute = false }; foreach (var envVar in environment) @@ -68,7 +77,12 @@ public Process Start(string path, string workingDirectory, string arguments, IDi { StartInfo = psi }; + + p.OutputDataReceived += ProcessOutputHandler; + p.Start(); + p.BeginOutputReadLine(); + p.BeginErrorReadLine(); return p; }