mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Create FilterRunner class
This commit is contained in:
68
SabreTools.Filter/FilterRunner.cs
Normal file
68
SabreTools.Filter/FilterRunner.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using SabreTools.Models.Internal;
|
||||
|
||||
namespace SabreTools.Filter
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a set of filters that can be run against an object
|
||||
/// </summary>
|
||||
public class FilterRunner
|
||||
{
|
||||
/// <summary>
|
||||
/// Set of filters to be run against an object
|
||||
/// </summary>
|
||||
public FilterObject[] Filters { get; init; }
|
||||
|
||||
public FilterRunner(FilterObject[]? filters)
|
||||
{
|
||||
if (filters == null)
|
||||
throw new ArgumentNullException(nameof(filters));
|
||||
|
||||
this.Filters = filters;
|
||||
}
|
||||
|
||||
public FilterRunner(string[]? filterStrings)
|
||||
{
|
||||
if (filterStrings == null)
|
||||
throw new ArgumentNullException(nameof(filterStrings));
|
||||
|
||||
var filters = new List<FilterObject>();
|
||||
foreach (string filterString in filterStrings)
|
||||
{
|
||||
filters.Add(new FilterObject(filterString));
|
||||
}
|
||||
|
||||
this.Filters = filters.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Run filtering on a DictionaryBase item
|
||||
/// </summary>
|
||||
public bool Run(DictionaryBase dictionaryBase)
|
||||
{
|
||||
string? itemName = dictionaryBase switch
|
||||
{
|
||||
Header => MetadataFile.HeaderKey,
|
||||
Machine => MetadataFile.MachineKey,
|
||||
DatItem => dictionaryBase.ReadString(DatItem.TypeKey),
|
||||
_ => null,
|
||||
};
|
||||
|
||||
// Loop through and run each filter in order
|
||||
foreach (var filter in this.Filters)
|
||||
{
|
||||
// TODO: Make this step more accurate
|
||||
// If the filter isn't for this object type, skip
|
||||
if (filter.Key[0] != itemName)
|
||||
continue;
|
||||
|
||||
// If we don't get a match, it's a failure
|
||||
if (!filter.Matches(dictionaryBase))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ namespace SabreTools.Models.Internal
|
||||
/// <summary>
|
||||
/// Determine what type of file an item is
|
||||
/// </summary>
|
||||
/// <remarks>TODO: Create attribute for internal names</remarks>
|
||||
public enum ItemType
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user