From b2136298e0c29ac6146adfaf56427147ecec2eab Mon Sep 17 00:00:00 2001 From: JosephSpearritt Date: Sat, 17 Aug 2019 17:12:41 +1000 Subject: [PATCH] Disable WIP inject by command line flag --- InjectorLauncher.cs | 47 ++++++++++++++++++++++++++++++++------ MainWindow.xaml | 8 +++---- MainWindow.xaml.cs | 17 ++++++++------ TribesLauncherSharp.csproj | 1 + 4 files changed, 55 insertions(+), 18 deletions(-) diff --git a/InjectorLauncher.cs b/InjectorLauncher.cs index 4744e9c..52e2275 100644 --- a/InjectorLauncher.cs +++ b/InjectorLauncher.cs @@ -111,20 +111,23 @@ public OnProcessStatusEventArgs(string processName, int processId) private class ProcessTarget { private bool TargetById { get; } + private bool MatchHostXArg { get; } private string TargetName { get; } private int TargetId { get; } - public ProcessTarget(int processId) + public ProcessTarget(int processId, bool matchHostXArg) { TargetById = true; + MatchHostXArg = matchHostXArg; TargetId = processId; TargetName = null; } - public ProcessTarget(string processName) + public ProcessTarget(string processName, bool matchHostXArg) { TargetById = false; + MatchHostXArg = matchHostXArg; TargetId = 0; TargetName = processName; } @@ -144,9 +147,39 @@ public Process FindTargetProcess() } else { var procs = Process.GetProcessesByName(TargetName); - return procs.Length > 0 ? procs[0] : null; + if (procs.Length == 0) return null; + + if (MatchHostXArg) + { + // Find an arg matching the string "hostx" + // i.e. a client, not a server + return GetProcessWithMatchingCommandLine(procs, TargetName, "-hostx="); + } else + { + return procs[0]; + } } } + + private Process GetProcessWithMatchingCommandLine(IEnumerable processes, string procName, string needle) + { + string wmiQuery = $"select ProcessId, CommandLine from Win32_Process where Name='{procName}'"; + System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(wmiQuery); + System.Management.ManagementObjectCollection ret = searcher.Get(); + foreach (System.Management.ManagementObject obj in ret) + { + if (obj is null || obj["CommandLine"] is null) continue; + string args = obj["CommandLine"].ToString(); + if (args.IndexOf(needle) != -1) + { + // Find the actual process corresponding... + int procId = (int)obj["ProcessId"]; + return processes.Where((p) => p.Id == procId).DefaultIfEmpty(null).First(); + } + } + + return null; + } } private ProcessTarget Target { get; set; } @@ -197,14 +230,14 @@ private void PollingTimer_Tick(object sender, ElapsedEventArgs e) } } - public void SetTarget(string processName) + public void SetTarget(string processName, bool matchHostXArg) { - Target = new ProcessTarget(processName); + Target = new ProcessTarget(processName, matchHostXArg); } - public void SetTarget(int processId) + public void SetTarget(int processId, bool matchHostXArg) { - Target = new ProcessTarget(processId); + Target = new ProcessTarget(processId, matchHostXArg); } public void UnsetTarget() diff --git a/MainWindow.xaml b/MainWindow.xaml index 4fd446a..968d7f5 100644 --- a/MainWindow.xaml +++ b/MainWindow.xaml @@ -6,7 +6,7 @@ xmlns:System="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:TribesLauncherSharp" mc:Ignorable="d" - Title="TribesLauncher" Height="352.533" Width="464.666" ResizeMode="CanMinimize" Loaded="MainAppWindow_Loaded" Closing="MainAppWindow_Closing" Icon="Resources/icon.ico"> + Title="TribesLauncher" Height="358.933" Width="464.666" ResizeMode="CanMinimize" Loaded="MainAppWindow_Loaded" Closing="MainAppWindow_Closing" Icon="Resources/icon.ico"> @@ -89,7 +89,7 @@ - + @@ -111,8 +111,8 @@ -