Skip to content

Commit

Permalink
Disable WIP inject by command line flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mcoot committed Aug 17, 2019
1 parent 426d87c commit b213629
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 18 deletions.
47 changes: 40 additions & 7 deletions InjectorLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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<Process> 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; }
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<Window.Resources>
<ObjectDataProvider x:Key="enumLoginServerMode" MethodName="GetValues" ObjectType="{x:Type System:Enum}">
<ObjectDataProvider.MethodParameters>
Expand Down Expand Up @@ -89,7 +89,7 @@
<StackPanel Margin="0,5,0,0">
<RadioButton x:Name="ProcessDetectionModeProcessNameRadio" Content="By Process Name" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="ProcessDetectionModeRadio" Checked="ProcessDetectionModeProcessNameRadio_Checked" />
<RadioButton x:Name="ProcessDetectionModeProcessIdRadio" Content="By Process ID" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="ProcessDetectionModeRadio" Checked="ProcessDetectionModeProcessIdRadio_Checked" Margin="0,3,0,0" />
<RadioButton x:Name="ProcessDetectionModeCommandLineRadio" Content="By Command Line String" HorizontalAlignment="Left" VerticalAlignment="Top" GroupName="ProcessDetectionModeRadio" Checked="ProcessDetectionModeCommandLineRadio_Checked" Margin="0,3,0,0" />
<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" />
</StackPanel>
</GroupBox>
<GroupBox Header="Extra Command Line Arguments" Height="57" Margin="10,185,10,0" VerticalAlignment="Top">
Expand All @@ -111,8 +111,8 @@
</Grid>
</TabItem>
</TabControl>
<Button x:Name="LauncherButton" Content="Launch" Margin="346,0,10.4,10" Click="LauncherButton_Click" Height="27" VerticalAlignment="Bottom"/>
<ProgressBar x:Name="UpdateProgressBar" HorizontalAlignment="Left" Margin="10,0,0,10" Width="331" Height="27" VerticalAlignment="Bottom"/>
<Button x:Name="LauncherButton" Content="Launch" Margin="346,0,10.4,9.6" Click="LauncherButton_Click" Height="27" VerticalAlignment="Bottom"/>
<ProgressBar x:Name="UpdateProgressBar" HorizontalAlignment="Left" Margin="10,0,0,9.6" Width="331" Height="27" VerticalAlignment="Bottom"/>

</Grid>
</Window>
17 changes: 10 additions & 7 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private void LaunchGame()
// Set up polling for process start if we're doing it by ID
if (config.Injection.ProcessDetectionMode == ProcessDetectionMode.ProcessId)
{
TALauncher.SetTarget(lastLaunchedProcessId);
TALauncher.SetTarget(lastLaunchedProcessId, false);
}
}

Expand Down Expand Up @@ -355,14 +355,16 @@ private void MainAppWindow_Loaded(object sender, RoutedEventArgs e)
MessageBox.Show("Failed to read launcher configuration: " + ex.Message, "Configuration Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
if (((Config)DataContext).Injection.Mode == InjectMode.Automatic)
Config config = (Config)DataContext;

if (config.Injection.Mode == InjectMode.Automatic)
{
InjectionModeAutoRadio.IsChecked = true;
} else
{
InjectionModeManualRadio.IsChecked = true;
}
switch (((Config)DataContext).Injection.ProcessDetectionMode)
switch (config.Injection.ProcessDetectionMode)
{
case ProcessDetectionMode.ProcessName:
ProcessDetectionModeProcessNameRadio.IsChecked = true;
Expand All @@ -381,16 +383,17 @@ private void MainAppWindow_Loaded(object sender, RoutedEventArgs e)
// Download news
try
{
TAModsNews.DownloadNews($"{((Config)DataContext).UpdateUrl}/news.json");
TAModsNews.DownloadNews($"{config.UpdateUrl}/news.json");
} catch (Exception ex)
{
MessageBox.Show("Failed to download server information: " + ex.Message, "News Download Error", MessageBoxButton.OK, MessageBoxImage.Error);
}

// Set up polling for game process if we're doing it by name
if (((Config)DataContext).Injection.ProcessDetectionMode != ProcessDetectionMode.ProcessId)
if (config.Injection.ProcessDetectionMode != ProcessDetectionMode.ProcessId)
{
TALauncher.SetTarget(((Config)DataContext).Injection.RunningProcessName);
MessageBox.Show($"aaa = {config.Injection.ProcessDetectionMode == ProcessDetectionMode.CommandLineString}");
TALauncher.SetTarget(config.Injection.RunningProcessName, config.Injection.ProcessDetectionMode == ProcessDetectionMode.CommandLineString);
}

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

// Prompt to set up Ubermenu if need be
if (((Config)DataContext).PromptForUbermenu && !TAModsUpdater.ConfigUsesUbermenu())
if (config.PromptForUbermenu && !TAModsUpdater.ConfigUsesUbermenu())
{
var doSetUp = MessageBox.Show(
"You have not configured Ubermenu, which allows you to configure TAMods in-game by pressing F1. Do you want to set it up now?",
Expand Down
1 change: 1 addition & 0 deletions TribesLauncherSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Management" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
Expand Down

0 comments on commit b213629

Please sign in to comment.