1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-12 13:28:33 +00:00

Merge branch 'master' into development

This commit is contained in:
Robert 2023-09-20 22:54:27 +02:00
commit 3394786d31
2 changed files with 34 additions and 13 deletions

View File

@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Artemis.Core.Services;
namespace Artemis.Core.Modules;
@ -38,16 +35,7 @@ public class ProcessActivationRequirement : IModuleActivationRequirement
/// <inheritdoc />
public bool Evaluate()
{
if (!ProcessMonitor.IsStarted || (ProcessName == null && Location == null))
return false;
IEnumerable<ProcessInfo> processes = ProcessMonitor.Processes;
if (ProcessName != null)
processes = processes.Where(p => string.Equals(p.ProcessName, ProcessName, StringComparison.InvariantCultureIgnoreCase));
if (Location != null)
processes = processes.Where(p => string.Equals(Path.GetDirectoryName(p.Executable), Location, StringComparison.InvariantCultureIgnoreCase));
return processes.Any();
return ProcessMonitor.IsProcessRunning(ProcessName, Location);
}
/// <inheritdoc />

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Threading;
@ -99,6 +100,23 @@ public static partial class ProcessMonitor
FreeBuffer();
}
}
/// <summary>
/// Returns whether the specified process is running
/// </summary>
/// <param name="processName">The name of the process to check</param>
/// <param name="processLocation">The location of where the process must be running from (optional)</param>
/// <returns></returns>
public static bool IsProcessRunning(string? processName = null, string? processLocation = null)
{
if (!IsStarted || (processName == null && processLocation == null))
return false;
lock (LOCK)
{
return _processes.Values.Any(x => IsProcessRunning(x, processName, processLocation));
}
}
// ReSharper disable once SuggestBaseTypeForParameter
private static void HandleStoppedProcesses(HashSet<int> currentProcessIds)
@ -112,6 +130,21 @@ public static partial class ProcessMonitor
OnProcessStopped(info);
}
}
private static bool IsProcessRunning(ProcessInfo info, string? processName, string? processLocation)
{
if (processName != null && processLocation != null)
return string.Equals(info.ProcessName, processName, StringComparison.InvariantCultureIgnoreCase) &&
string.Equals(Path.GetDirectoryName(info.Executable), processLocation, StringComparison.InvariantCultureIgnoreCase);
if (processName != null)
return string.Equals(info.ProcessName, processName, StringComparison.InvariantCultureIgnoreCase);
if (processLocation != null)
return string.Equals(Path.GetDirectoryName(info.Executable), processLocation, StringComparison.InvariantCultureIgnoreCase);
return false;
}
private static void OnProcessStarted(ProcessInfo processInfo)
{