using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Artemis.Core.Services;
namespace Artemis.Core.Modules;
///
/// Evaluates to true or false by checking if the specified process is running
///
public class ProcessActivationRequirement : IModuleActivationRequirement
{
///
/// Creates a new instance of the class
///
/// The name of the process that must run
/// The location of where the process must be running from (optional)
public ProcessActivationRequirement(string? processName, string? location = null)
{
if (string.IsNullOrWhiteSpace(processName) && string.IsNullOrWhiteSpace(location))
throw new ArgumentNullException($"Atleast one {nameof(processName)} and {nameof(location)} must not be null");
ProcessName = processName;
Location = location;
}
///
/// The name of the process that must run
///
public string? ProcessName { get; set; }
///
/// The location of where the process must be running from
///
public string? Location { get; set; }
///
public bool Evaluate()
{
if (!ProcessMonitor.IsStarted || (ProcessName == null && Location == null))
return false;
IEnumerable 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();
}
///
public string GetUserFriendlyDescription()
{
string description = $"Requirement met when \"{ProcessName}.exe\" is running";
if (Location != null)
description += $" from \"{Location}\"";
return description;
}
}