[WIP] ProcessUtils refactor and trim

Tried to remove the unneeded bits of the ProcessUtils module, and to streamline the process monitoring process to make it sane, and have code only live in one place. I also wanted to make use of the TryExecute_Impersonated if possible. I hope I've done that all today!
This commit is contained in:
Terry MacDonald 2021-11-22 21:53:14 +13:00
parent 0d96a9287d
commit fdb4fce977
7 changed files with 225 additions and 1475 deletions

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Net;
using DisplayMagician.Resources;
using System.Diagnostics;
using DisplayMagician.Processes;
namespace DisplayMagician.GameLibraries
{
@ -101,7 +102,8 @@ namespace DisplayMagician.GameLibraries
{
get
{
int numGameProcesses = 0;
return ProcessUtils.ProcessExited(_epicGameProcessName);
/*int numGameProcesses = 0;
_epicGameProcesses = Process.GetProcessesByName(_epicGameProcessName).ToList();
foreach (Process gameProcess in _epicGameProcesses)
{
@ -135,7 +137,7 @@ namespace DisplayMagician.GameLibraries
if (numGameProcesses > 0)
return true;
else
return false;
return false;*/
}
}

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Net;
using DisplayMagician.Resources;
using System.Diagnostics;
using DisplayMagician.Processes;
namespace DisplayMagician.GameLibraries
{
@ -101,7 +102,8 @@ namespace DisplayMagician.GameLibraries
{
get
{
int numGameProcesses = 0;
return ProcessUtils.ProcessExited(_gogGameProcessName);
/*int numGameProcesses = 0;
_gogGameProcesses = Process.GetProcessesByName(_gogGameProcessName).ToList();
foreach (Process gameProcess in _gogGameProcesses)
{
@ -135,7 +137,7 @@ namespace DisplayMagician.GameLibraries
if (numGameProcesses > 0)
return true;
else
return false;
return false;*/
}
}

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Net;
using DisplayMagician.Resources;
using System.Diagnostics;
using DisplayMagician.Processes;
namespace DisplayMagician.GameLibraries
{
@ -100,7 +101,8 @@ namespace DisplayMagician.GameLibraries
{
get
{
int numGameProcesses = 0;
return ProcessUtils.ProcessExited(_originGameProcessName);
/*int numGameProcesses = 0;
_originGameProcesses = Process.GetProcessesByName(_originGameProcessName).ToList();
foreach (Process gameProcess in _originGameProcesses)
{
@ -134,7 +136,7 @@ namespace DisplayMagician.GameLibraries
if (numGameProcesses > 0)
return true;
else
return false;
return false;*/
}
}

View File

@ -7,6 +7,7 @@ using System.Security;
using DisplayMagician.Resources;
using Microsoft.Win32;
using System.Diagnostics;
using DisplayMagician.Processes;
namespace DisplayMagician.GameLibraries
{
@ -99,8 +100,9 @@ namespace DisplayMagician.GameLibraries
{
get
{
int numGameProcesses = 0;
_steamGameProcesses = Process.GetProcessesByName(_steamGameProcessName).ToList();
//int numGameProcesses = 0;
return ProcessUtils.ProcessExited(_steamGameProcessName);
/*_steamGameProcesses = Process.GetProcessesByName(_steamGameProcessName).ToList();
foreach (Process gameProcess in _steamGameProcesses)
{
try
@ -138,7 +140,7 @@ namespace DisplayMagician.GameLibraries
if (numGameProcesses > 0)
return true;
else
return false;
return false;*/
}
}

View File

@ -5,6 +5,7 @@ using System.Linq;
using System.Net;
using DisplayMagician.Resources;
using System.Diagnostics;
using DisplayMagician.Processes;
namespace DisplayMagician.GameLibraries
{
@ -99,7 +100,8 @@ namespace DisplayMagician.GameLibraries
{
get
{
int numGameProcesses = 0;
return ProcessUtils.ProcessExited(_uplayGameProcessName);
/*int numGameProcesses = 0;
_uplayGameProcesses = Process.GetProcessesByName(_uplayGameProcessName).ToList();
foreach (Process gameProcess in _uplayGameProcesses)
{
@ -133,7 +135,7 @@ namespace DisplayMagician.GameLibraries
if (numGameProcesses > 0)
return true;
else
return false;
return false;*/
}
}

File diff suppressed because it is too large Load Diff

View File

@ -924,7 +924,6 @@ namespace DisplayMagician
{
logger.Warn(ex, $"ShortcutRepository/RunShortcut: Exception setting priority of already running process {processToStart.Executable} to {processToStart.ProcessPriority.ToString("G")}");
}
continue;
}
@ -1138,7 +1137,7 @@ namespace DisplayMagician
// We use the a user supplied executable as the thing we're monitoring instead!
try
{
processesToMonitor.AddRange(Process.GetProcessesByName(shortcutToUse.DifferentExecutableToMonitor));
processesToMonitor.AddRange(Process.GetProcessesByName(ProcessUtils.GetProcessName(shortcutToUse.DifferentExecutableToMonitor)));
logger.Trace($"ShortcutRepository/RunShortcut: {processesToMonitor.Count} '{shortcutToUse.DifferentExecutableToMonitor}' user specified processes to monitor are running");
foundSomethingToMonitor = true;
}
@ -1270,19 +1269,22 @@ namespace DisplayMagician
// And then show this notification
DesktopNotifications.DesktopNotificationManagerCompat.CreateToastNotifier().Show(toast);
// Start the game!
// NOTE: We now have to try and find the processes, as the game library will start to run the game itself, and we have no idea what process it is
// We'll have to look for the game exe later on in this process...
List<Process> gameProcesses;
gameProcesses = gameLibraryToUse.StartGame(gameToRun, shortcutToUse.GameArguments, shortcutToUse.ProcessPriority);
// Delay 500ms
Thread.Sleep(500);
if (gameProcesses.Count == 0)
/*if (gameProcesses.Count == 0)
{
// If there are no children found, then try to find all the running programs with the same names
// (Some games relaunch themselves!)
List<Process> sameNamedProcesses = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(gameToRun.Executable)).ToList();
gameProcesses.AddRange(sameNamedProcesses);
}
}*/
// Wait for GameLibrary to start
@ -1410,7 +1412,7 @@ namespace DisplayMagician
if (shortcutToUse.MonitorDifferentGameExe)
{
// If we are monitoring a different executable rather than the game itself, then lets get that name ready instead
string altGameProcessToMonitor = System.IO.Path.GetFileNameWithoutExtension(shortcutToUse.DifferentGameExeToMonitor);
string altGameProcessToMonitor = ProcessUtils.GetProcessName(shortcutToUse.DifferentGameExeToMonitor);
// Now look for the thing we're supposed to monitor
// and wait until it starts up