Skip to content

Commit b213629

Browse files
committed
Disable WIP inject by command line flag
1 parent 426d87c commit b213629

4 files changed

Lines changed: 55 additions & 18 deletions

File tree

InjectorLauncher.cs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,23 @@ public OnProcessStatusEventArgs(string processName, int processId)
111111
private class ProcessTarget
112112
{
113113
private bool TargetById { get; }
114+
private bool MatchHostXArg { get; }
114115
private string TargetName { get; }
115116
private int TargetId { get; }
116117

117118

118-
public ProcessTarget(int processId)
119+
public ProcessTarget(int processId, bool matchHostXArg)
119120
{
120121
TargetById = true;
122+
MatchHostXArg = matchHostXArg;
121123
TargetId = processId;
122124
TargetName = null;
123125
}
124126

125-
public ProcessTarget(string processName)
127+
public ProcessTarget(string processName, bool matchHostXArg)
126128
{
127129
TargetById = false;
130+
MatchHostXArg = matchHostXArg;
128131
TargetId = 0;
129132
TargetName = processName;
130133
}
@@ -144,9 +147,39 @@ public Process FindTargetProcess()
144147
} else
145148
{
146149
var procs = Process.GetProcessesByName(TargetName);
147-
return procs.Length > 0 ? procs[0] : null;
150+
if (procs.Length == 0) return null;
151+
152+
if (MatchHostXArg)
153+
{
154+
// Find an arg matching the string "hostx"
155+
// i.e. a client, not a server
156+
return GetProcessWithMatchingCommandLine(procs, TargetName, "-hostx=");
157+
} else
158+
{
159+
return procs[0];
160+
}
148161
}
149162
}
163+
164+
private Process GetProcessWithMatchingCommandLine(IEnumerable<Process> processes, string procName, string needle)
165+
{
166+
string wmiQuery = $"select ProcessId, CommandLine from Win32_Process where Name='{procName}'";
167+
System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(wmiQuery);
168+
System.Management.ManagementObjectCollection ret = searcher.Get();
169+
foreach (System.Management.ManagementObject obj in ret)
170+
{
171+
if (obj is null || obj["CommandLine"] is null) continue;
172+
string args = obj["CommandLine"].ToString();
173+
if (args.IndexOf(needle) != -1)
174+
{
175+
// Find the actual process corresponding...
176+
int procId = (int)obj["ProcessId"];
177+
return processes.Where((p) => p.Id == procId).DefaultIfEmpty(null).First();
178+
}
179+
}
180+
181+
return null;
182+
}
150183
}
151184

152185
private ProcessTarget Target { get; set; }
@@ -197,14 +230,14 @@ private void PollingTimer_Tick(object sender, ElapsedEventArgs e)
197230
}
198231
}
199232

200-
public void SetTarget(string processName)
233+
public void SetTarget(string processName, bool matchHostXArg)
201234
{
202-
Target = new ProcessTarget(processName);
235+
Target = new ProcessTarget(processName, matchHostXArg);
203236
}
204237

205-
public void SetTarget(int processId)
238+
public void SetTarget(int processId, bool matchHostXArg)
206239
{
207-
Target = new ProcessTarget(processId);
240+
Target = new ProcessTarget(processId, matchHostXArg);
208241
}
209242

210243
public void UnsetTarget()

MainWindow.xaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
xmlns:System="clr-namespace:System;assembly=mscorlib"
77
xmlns:local="clr-namespace:TribesLauncherSharp"
88
mc:Ignorable="d"
9-
Title="TribesLauncher" Height="352.533" Width="464.666" ResizeMode="CanMinimize" Loaded="MainAppWindow_Loaded" Closing="MainAppWindow_Closing" Icon="Resources/icon.ico">
9+
Title="TribesLauncher" Height="358.933" Width="464.666" ResizeMode="CanMinimize" Loaded="MainAppWindow_Loaded" Closing="MainAppWindow_Closing" Icon="Resources/icon.ico">
1010
<Window.Resources>
1111
<ObjectDataProvider x:Key="enumLoginServerMode" MethodName="GetValues" ObjectType="{x:Type System:Enum}">
1212
<ObjectDataProvider.MethodParameters>
@@ -89,7 +89,7 @@
8989
<StackPanel Margin="0,5,0,0">
9090
<RadioButton x:Name="ProcessDetectionModeProcessNameRadio" Content="By Process Name" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="ProcessDetectionModeRadio" Checked="ProcessDetectionModeProcessNameRadio_Checked" />
9191
<RadioButton x:Name="ProcessDetectionModeProcessIdRadio" Content="By Process ID" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="ProcessDetectionModeRadio" Checked="ProcessDetectionModeProcessIdRadio_Checked" Margin="0,3,0,0" />
92-
<RadioButton x:Name="ProcessDetectionModeCommandLineRadio" Content="By Command Line String" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="ProcessDetectionModeRadio" Checked="ProcessDetectionModeCommandLineRadio_Checked" Margin="0,3,0,0" />
92+
<RadioButton x:Name="ProcessDetectionModeCommandLineRadio" Content="By Command Line String" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="ProcessDetectionModeRadio" Checked="ProcessDetectionModeCommandLineRadio_Checked" Margin="0,3,0,0" Visibility="Hidden" />
9393
</StackPanel>
9494
</GroupBox>
9595
<GroupBox Header="Extra Command Line Arguments" Height="57" Margin="10,185,10,0" VerticalAlignment="Top">
@@ -111,8 +111,8 @@
111111
</Grid>
112112
</TabItem>
113113
</TabControl>
114-
<Button x:Name="LauncherButton" Content="Launch" Margin="346,0,10.4,10" Click="LauncherButton_Click" Height="27" VerticalAlignment="Bottom"/>
115-
<ProgressBar x:Name="UpdateProgressBar" HorizontalAlignment="Left" Margin="10,0,0,10" Width="331" Height="27" VerticalAlignment="Bottom"/>
114+
<Button x:Name="LauncherButton" Content="Launch" Margin="346,0,10.4,9.6" Click="LauncherButton_Click" Height="27" VerticalAlignment="Bottom"/>
115+
<ProgressBar x:Name="UpdateProgressBar" HorizontalAlignment="Left" Margin="10,0,0,9.6" Width="331" Height="27" VerticalAlignment="Bottom"/>
116116

117117
</Grid>
118118
</Window>

MainWindow.xaml.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private void LaunchGame()
156156
// Set up polling for process start if we're doing it by ID
157157
if (config.Injection.ProcessDetectionMode == ProcessDetectionMode.ProcessId)
158158
{
159-
TALauncher.SetTarget(lastLaunchedProcessId);
159+
TALauncher.SetTarget(lastLaunchedProcessId, false);
160160
}
161161
}
162162

@@ -355,14 +355,16 @@ private void MainAppWindow_Loaded(object sender, RoutedEventArgs e)
355355
MessageBox.Show("Failed to read launcher configuration: " + ex.Message, "Configuration Error", MessageBoxButton.OK, MessageBoxImage.Error);
356356
}
357357
}
358-
if (((Config)DataContext).Injection.Mode == InjectMode.Automatic)
358+
Config config = (Config)DataContext;
359+
360+
if (config.Injection.Mode == InjectMode.Automatic)
359361
{
360362
InjectionModeAutoRadio.IsChecked = true;
361363
} else
362364
{
363365
InjectionModeManualRadio.IsChecked = true;
364366
}
365-
switch (((Config)DataContext).Injection.ProcessDetectionMode)
367+
switch (config.Injection.ProcessDetectionMode)
366368
{
367369
case ProcessDetectionMode.ProcessName:
368370
ProcessDetectionModeProcessNameRadio.IsChecked = true;
@@ -381,16 +383,17 @@ private void MainAppWindow_Loaded(object sender, RoutedEventArgs e)
381383
// Download news
382384
try
383385
{
384-
TAModsNews.DownloadNews($"{((Config)DataContext).UpdateUrl}/news.json");
386+
TAModsNews.DownloadNews($"{config.UpdateUrl}/news.json");
385387
} catch (Exception ex)
386388
{
387389
MessageBox.Show("Failed to download server information: " + ex.Message, "News Download Error", MessageBoxButton.OK, MessageBoxImage.Error);
388390
}
389391

390392
// Set up polling for game process if we're doing it by name
391-
if (((Config)DataContext).Injection.ProcessDetectionMode != ProcessDetectionMode.ProcessId)
393+
if (config.Injection.ProcessDetectionMode != ProcessDetectionMode.ProcessId)
392394
{
393-
TALauncher.SetTarget(((Config)DataContext).Injection.RunningProcessName);
395+
MessageBox.Show($"aaa = {config.Injection.ProcessDetectionMode == ProcessDetectionMode.CommandLineString}");
396+
TALauncher.SetTarget(config.Injection.RunningProcessName, config.Injection.ProcessDetectionMode == ProcessDetectionMode.CommandLineString);
394397
}
395398

396399
// Prompt to update if need be
@@ -420,7 +423,7 @@ private void MainAppWindow_Loaded(object sender, RoutedEventArgs e)
420423
}
421424

422425
// Prompt to set up Ubermenu if need be
423-
if (((Config)DataContext).PromptForUbermenu && !TAModsUpdater.ConfigUsesUbermenu())
426+
if (config.PromptForUbermenu && !TAModsUpdater.ConfigUsesUbermenu())
424427
{
425428
var doSetUp = MessageBox.Show(
426429
"You have not configured Ubermenu, which allows you to configure TAMods in-game by pressing F1. Do you want to set it up now?",

TribesLauncherSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<Reference Include="System" />
6464
<Reference Include="System.Data" />
6565
<Reference Include="System.Drawing" />
66+
<Reference Include="System.Management" />
6667
<Reference Include="System.Xml" />
6768
<Reference Include="Microsoft.CSharp" />
6869
<Reference Include="System.Core" />

0 commit comments

Comments
 (0)