mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Create Filtering object, add helpers
This commit is contained in:
@@ -12,38 +12,50 @@ namespace SabreTools.Filter
|
||||
/// <summary>
|
||||
/// Parse a filter ID string into the item name and field name, if possible
|
||||
/// </summary>
|
||||
public static (string?, string?) ParseFilterId(string filterId)
|
||||
public static (string?, string?) ParseFilterId(string itemFieldString)
|
||||
{
|
||||
// If we don't have a filter ID, we can't do anything
|
||||
if (string.IsNullOrWhiteSpace(filterId))
|
||||
if (string.IsNullOrWhiteSpace(itemFieldString))
|
||||
return (null, null);
|
||||
|
||||
// If we only have one part, we can't do anything
|
||||
string[] splitFilter = filterId.Split('.');
|
||||
string[] splitFilter = itemFieldString.Split('.');
|
||||
if (splitFilter.Length != 2)
|
||||
return (null, null);
|
||||
|
||||
return ParseFilterId(splitFilter[0], splitFilter[1]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse a filter ID string into the item name and field name, if possible
|
||||
/// </summary>
|
||||
public static (string?, string?) ParseFilterId(string itemName, string? fieldName)
|
||||
{
|
||||
// If we don't have a filter ID, we can't do anything
|
||||
if (string.IsNullOrWhiteSpace(itemName) || string.IsNullOrWhiteSpace(fieldName))
|
||||
return (null, null);
|
||||
|
||||
// Return santized values based on the split ID
|
||||
return splitFilter[0].ToLowerInvariant() switch
|
||||
return itemName.ToLowerInvariant() switch
|
||||
{
|
||||
// Header
|
||||
"header" => ParseHeaderFilterId(splitFilter),
|
||||
"header" => ParseHeaderFilterId(fieldName),
|
||||
|
||||
// Machine
|
||||
"game" => ParseMachineFilterId(splitFilter),
|
||||
"machine" => ParseMachineFilterId(splitFilter),
|
||||
"resource" => ParseMachineFilterId(splitFilter),
|
||||
"set" => ParseMachineFilterId(splitFilter),
|
||||
"game" => ParseMachineFilterId(fieldName),
|
||||
"machine" => ParseMachineFilterId(fieldName),
|
||||
"resource" => ParseMachineFilterId(fieldName),
|
||||
"set" => ParseMachineFilterId(fieldName),
|
||||
|
||||
// DatItem
|
||||
_ => ParseDatItemFilterId(splitFilter),
|
||||
_ => ParseDatItemFilterId(itemName, fieldName),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse and validate header fields
|
||||
/// </summary>
|
||||
private static (string?, string?) ParseHeaderFilterId(string[] filterId)
|
||||
private static (string?, string?) ParseHeaderFilterId(string fieldName)
|
||||
{
|
||||
// Get the set of constants
|
||||
var constants = GetConstants(typeof(Header));
|
||||
@@ -51,7 +63,7 @@ namespace SabreTools.Filter
|
||||
return (null, null);
|
||||
|
||||
// Get if there's a match to the constant
|
||||
string? constantMatch = constants.FirstOrDefault(c => string.Equals(c, filterId[1], StringComparison.OrdinalIgnoreCase));
|
||||
string? constantMatch = constants.FirstOrDefault(c => string.Equals(c, fieldName, StringComparison.OrdinalIgnoreCase));
|
||||
if (constantMatch == null)
|
||||
return (null, null);
|
||||
|
||||
@@ -62,7 +74,7 @@ namespace SabreTools.Filter
|
||||
/// <summary>
|
||||
/// Parse and validate machine/game fields
|
||||
/// </summary>
|
||||
private static (string?, string?) ParseMachineFilterId(string[] filterId)
|
||||
private static (string?, string?) ParseMachineFilterId(string fieldName)
|
||||
{
|
||||
// Get the set of constants
|
||||
var constants = GetConstants(typeof(Machine));
|
||||
@@ -70,7 +82,7 @@ namespace SabreTools.Filter
|
||||
return (null, null);
|
||||
|
||||
// Get if there's a match to the constant
|
||||
string? constantMatch = constants.FirstOrDefault(c => string.Equals(c, filterId[1], StringComparison.OrdinalIgnoreCase));
|
||||
string? constantMatch = constants.FirstOrDefault(c => string.Equals(c, fieldName, StringComparison.OrdinalIgnoreCase));
|
||||
if (constantMatch == null)
|
||||
return (null, null);
|
||||
|
||||
@@ -81,10 +93,10 @@ namespace SabreTools.Filter
|
||||
/// <summary>
|
||||
/// Parse and validate item fields
|
||||
/// </summary>
|
||||
private static (string?, string?) ParseDatItemFilterId(string[] filterId)
|
||||
private static (string?, string?) ParseDatItemFilterId(string itemName, string fieldName)
|
||||
{
|
||||
// Get the correct item type
|
||||
var itemType = GetDatItemType(filterId[0].ToLowerInvariant());
|
||||
var itemType = GetDatItemType(itemName.ToLowerInvariant());
|
||||
if (itemType == null)
|
||||
return (null, null);
|
||||
|
||||
@@ -94,12 +106,12 @@ namespace SabreTools.Filter
|
||||
return (null, null);
|
||||
|
||||
// Get if there's a match to the constant
|
||||
string? constantMatch = constants.FirstOrDefault(c => string.Equals(c, filterId[1], StringComparison.OrdinalIgnoreCase));
|
||||
string? constantMatch = constants.FirstOrDefault(c => string.Equals(c, fieldName, StringComparison.OrdinalIgnoreCase));
|
||||
if (constantMatch == null)
|
||||
return (null, null);
|
||||
|
||||
// Return the sanitized ID
|
||||
return (filterId[0].ToLowerInvariant(), constantMatch);
|
||||
return (itemName.ToLowerInvariant(), constantMatch);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user