Files
ThreadPilot/Services/PersistentProcessRuleMatcher.cs
T
Prime Build bffbdeeccc CPU topology v2 phase 8: add persistent rules engine foundation (#14)
Add persistent rules engine foundation

Introduce the foundation for Apply at process start without adding a background loop, Windows Service, installer changes, registry affinity or anti-cheat bypass behavior.

- Add PersistentProcessRule and PersistentRuleApplyResult models
- Add process-name and executable-path rule matching
- Add PersistentRulesEngine using existing safe affinity and priority services
- Add JSON-backed persistent rule store
- Add safe Apply at process start user-facing copy
- Validate incomplete rules so missing affinity/priority payloads fail safely
- Add tests for matching, CpuSelection, legacy affinity, priority, Realtime guardrails, access denied, protected processes, process exit, disabled rules and storage roundtrip

No process monitor hook, UI management, Windows Service, registry/IFEO expansion, version bump or tag changes.
2026-05-20 16:45:43 +02:00

59 lines
1.8 KiB
C#

/*
* ThreadPilot - persistent process rule matcher.
*/
namespace ThreadPilot.Services
{
using System.IO;
using ThreadPilot.Models;
public interface IPersistentProcessRuleMatcher
{
bool IsMatch(PersistentProcessRule rule, ProcessModel process);
}
public sealed class PersistentProcessRuleMatcher : IPersistentProcessRuleMatcher
{
public bool IsMatch(PersistentProcessRule rule, ProcessModel process)
{
ArgumentNullException.ThrowIfNull(rule);
ArgumentNullException.ThrowIfNull(process);
if (!rule.IsEnabled)
{
return false;
}
var rulePath = NormalizePath(rule.ExecutablePath);
if (!string.IsNullOrWhiteSpace(rulePath))
{
var processPath = NormalizePath(process.ExecutablePath);
return !string.IsNullOrWhiteSpace(processPath) &&
string.Equals(rulePath, processPath, StringComparison.OrdinalIgnoreCase);
}
return !string.IsNullOrWhiteSpace(rule.ProcessName) &&
string.Equals(rule.ProcessName.Trim(), process.Name?.Trim(), StringComparison.OrdinalIgnoreCase);
}
private static string? NormalizePath(string? path)
{
if (string.IsNullOrWhiteSpace(path))
{
return null;
}
var trimmed = path.Trim();
try
{
trimmed = Path.GetFullPath(trimmed);
}
catch (Exception ex) when (ex is ArgumentException or NotSupportedException or PathTooLongException)
{
// Keep matching best-effort for inaccessible or malformed process paths.
}
return trimmed.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
}
}
}