Files
SabreTools/SabreTools.Filter/FilterRunner.cs

71 lines
2.0 KiB
C#
Raw Normal View History

2023-08-11 16:31:53 -04:00
using System;
using System.Collections.Generic;
2023-09-04 23:51:37 -04:00
using SabreTools.Models.Metadata;
2023-08-11 16:31:53 -04:00
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>
2024-02-28 20:14:45 -05:00
#if NETFRAMEWORK || NETCOREAPP3_1
public FilterObject[] Filters { get; private set; }
#else
2023-08-11 16:31:53 -04:00
public FilterObject[] Filters { get; init; }
2024-02-28 20:14:45 -05:00
#endif
2023-08-11 16:31:53 -04:00
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();
}
2024-02-28 20:14:45 -05:00
2023-08-11 16:31:53 -04:00
/// <summary>
/// Run filtering on a DictionaryBase item
/// </summary>
public bool Run(DictionaryBase dictionaryBase)
{
string? itemName = dictionaryBase switch
{
Header => MetadataFile.HeaderKey,
Machine => MetadataFile.MachineKey,
2023-08-11 22:14:28 -04:00
DatItem => TypeHelper.GetXmlRootAttributeElementName(dictionaryBase.GetType()),
2023-08-11 16:31:53 -04:00
_ => null,
};
// Loop through and run each filter in order
foreach (var filter in this.Filters)
{
// 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;
}
}
}