Files
ThreadPilot/Models/ProcessPowerPlanAssociation.cs
T
Seyedamin Mousavichashmi c6f74a948f feat: support wildcard executable paths (#63)
Support case-insensitive * and ? matching for persistent process rules and power-plan associations while preserving exact-path behavior.
2026-09-29 22:13:42 +02:00

154 lines
4.8 KiB
C#

namespace ThreadPilot.Models
{
using System;
using System.Collections.Generic;
using System.IO;
using CommunityToolkit.Mvvm.ComponentModel;
using ThreadPilot.Helpers;
using ThreadPilot.Models.Core;
public partial class ProcessPowerPlanAssociation : ObservableObject, IModel
{
[ObservableProperty]
private string id = Guid.NewGuid().ToString();
[ObservableProperty]
private string executableName = string.Empty;
[ObservableProperty]
private string executablePath = string.Empty;
[ObservableProperty]
private string powerPlanGuid = string.Empty;
[ObservableProperty]
private string powerPlanName = string.Empty;
[ObservableProperty]
private string? coreMaskId = null;
[ObservableProperty]
private string? coreMaskName = null;
[ObservableProperty]
private string? processPriority = null;
[ObservableProperty]
private bool isEnabled = true;
[ObservableProperty]
private DateTime createdAt = DateTime.UtcNow;
[ObservableProperty]
private DateTime updatedAt = DateTime.UtcNow;
// IModel implementation - properties are auto-generated by ObservableProperty
[ObservableProperty]
private string description = string.Empty;
[ObservableProperty]
private bool matchByPath = false;
[ObservableProperty]
private int priority = 0;
public ProcessPowerPlanAssociation()
{
}
public ProcessPowerPlanAssociation(string executableName, string powerPlanGuid, string powerPlanName)
{
this.ExecutableName = executableName;
this.PowerPlanGuid = powerPlanGuid;
this.PowerPlanName = powerPlanName;
}
public bool MatchesProcess(ProcessModel process)
{
if (!this.IsEnabled)
{
return false;
}
if (this.MatchByPath && !string.IsNullOrEmpty(this.ExecutablePath))
{
return PathPatternMatcher.IsPathMatch(this.ExecutablePath, process.ExecutablePath);
}
else
{
var processName = NormalizeExecutableName(process.Name);
var associationName = NormalizeExecutableName(this.ExecutableName);
return string.Equals(processName, associationName, StringComparison.OrdinalIgnoreCase);
}
}
public bool MatchesExecutable(string executableName)
{
if (!this.IsEnabled)
{
return false;
}
var associationName = NormalizeExecutableName(this.ExecutableName);
var inputName = NormalizeExecutableName(executableName);
return string.Equals(associationName, inputName, StringComparison.OrdinalIgnoreCase);
}
private static string NormalizeExecutableName(string? executableName)
{
if (string.IsNullOrWhiteSpace(executableName))
{
return string.Empty;
}
return Path.GetFileNameWithoutExtension(executableName.Trim());
}
public ValidationResult Validate()
{
var errors = new List<string>();
if (string.IsNullOrWhiteSpace(this.ExecutableName))
{
errors.Add("Executable name is required");
}
if (string.IsNullOrWhiteSpace(this.PowerPlanGuid))
{
errors.Add("Power plan GUID is required");
}
if (string.IsNullOrWhiteSpace(this.PowerPlanName))
{
errors.Add("Power plan name is required");
}
return errors.Count == 0 ? ValidationResult.Success() : ValidationResult.Failure(errors.ToArray());
}
public IModel Clone()
{
return new ProcessPowerPlanAssociation
{
id = Guid.NewGuid().ToString(), // New ID for clone
ExecutableName = this.ExecutableName,
ExecutablePath = this.ExecutablePath,
PowerPlanGuid = this.PowerPlanGuid,
PowerPlanName = this.PowerPlanName,
CoreMaskId = this.CoreMaskId,
CoreMaskName = this.CoreMaskName,
ProcessPriority = this.ProcessPriority,
IsEnabled = this.IsEnabled,
Description = this.Description,
MatchByPath = this.MatchByPath,
createdAt = DateTime.UtcNow,
updatedAt = DateTime.UtcNow,
};
}
}
}