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

View File

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

View File

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

View File

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

View File

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

File diff suppressed because it is too large Load Diff

View File

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