Files
SabreTools/SabreTools.Filter/FilterParser.cs

134 lines
4.9 KiB
C#
Raw Normal View History

2023-08-11 11:15:34 -04:00
using System;
using System.Linq;
2023-09-04 23:51:37 -04:00
using SabreTools.Models.Metadata;
2023-08-11 11:15:34 -04:00
namespace SabreTools.Filter
{
2023-08-11 13:34:28 -04:00
internal static class FilterParser
2023-08-11 11:15:34 -04:00
{
/// <summary>
/// Parse a filter ID string into the item name and field name, if possible
/// </summary>
2023-08-11 12:47:59 -04:00
public static (string?, string?) ParseFilterId(string itemFieldString)
2023-08-11 11:15:34 -04:00
{
// If we don't have a filter ID, we can't do anything
2024-02-28 20:14:45 -05:00
if (string.IsNullOrEmpty(itemFieldString))
2023-08-11 11:15:34 -04:00
return (null, null);
// If we only have one part, we can't do anything
2023-08-11 12:47:59 -04:00
string[] splitFilter = itemFieldString.Split('.');
2023-08-11 11:15:34 -04:00
if (splitFilter.Length != 2)
return (null, null);
2023-08-11 12:47:59 -04:00
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
2024-02-28 20:14:45 -05:00
if (string.IsNullOrEmpty(itemName) || string.IsNullOrEmpty(fieldName))
2023-08-11 12:47:59 -04:00
return (null, null);
2023-08-11 11:15:34 -04:00
// Return santized values based on the split ID
2023-08-11 12:47:59 -04:00
return itemName.ToLowerInvariant() switch
2023-08-11 11:15:34 -04:00
{
// Header
2024-02-28 20:14:45 -05:00
"header" => ParseHeaderFilterId(fieldName!),
2023-08-11 11:15:34 -04:00
// Machine
2024-02-28 20:14:45 -05:00
"game" => ParseMachineFilterId(fieldName!),
"machine" => ParseMachineFilterId(fieldName!),
"resource" => ParseMachineFilterId(fieldName!),
"set" => ParseMachineFilterId(fieldName!),
2023-08-11 11:15:34 -04:00
// DatItem
"datitem" => ParseDatItemFilterId(fieldName!),
"item" => ParseDatItemFilterId(fieldName!),
2024-02-28 20:14:45 -05:00
_ => ParseDatItemFilterId(itemName, fieldName!),
2023-08-11 11:15:34 -04:00
};
}
/// <summary>
/// Parse and validate header fields
/// </summary>
2023-08-11 12:47:59 -04:00
private static (string?, string?) ParseHeaderFilterId(string fieldName)
2023-08-11 11:15:34 -04:00
{
// Get the set of constants
2023-08-11 13:46:27 -04:00
var constants = TypeHelper.GetConstants(typeof(Header));
2023-08-11 11:15:34 -04:00
if (constants == null)
return (null, null);
// Get if there's a match to the constant
2023-08-11 12:47:59 -04:00
string? constantMatch = constants.FirstOrDefault(c => string.Equals(c, fieldName, StringComparison.OrdinalIgnoreCase));
2023-08-11 11:15:34 -04:00
if (constantMatch == null)
return (null, null);
2023-08-11 11:29:25 -04:00
// Return the sanitized ID
return (MetadataFile.HeaderKey, constantMatch);
2023-08-11 11:15:34 -04:00
}
/// <summary>
/// Parse and validate machine/game fields
/// </summary>
2023-08-11 12:47:59 -04:00
private static (string?, string?) ParseMachineFilterId(string fieldName)
2023-08-11 11:15:34 -04:00
{
// Get the set of constants
2023-08-11 13:46:27 -04:00
var constants = TypeHelper.GetConstants(typeof(Machine));
2023-08-11 11:15:34 -04:00
if (constants == null)
return (null, null);
// Get if there's a match to the constant
2023-08-11 12:47:59 -04:00
string? constantMatch = constants.FirstOrDefault(c => string.Equals(c, fieldName, StringComparison.OrdinalIgnoreCase));
2023-08-11 11:15:34 -04:00
if (constantMatch == null)
return (null, null);
2023-08-11 11:29:25 -04:00
// Return the sanitized ID
return (MetadataFile.MachineKey, constantMatch);
2023-08-11 11:15:34 -04:00
}
/// <summary>
/// Parse and validate item fields
/// </summary>
private static (string?, string?) ParseDatItemFilterId(string fieldName)
{
// Get all item types
var itemTypes = TypeHelper.GetDatItemTypeNames();
// If we get any matches
string? match = itemTypes.FirstOrDefault(t => t != null && ParseDatItemFilterId(t, fieldName) != (null, null));
if (match != null)
return ("item", ParseDatItemFilterId(match, fieldName).Item2);
// Nothing was found
return (null, null);
}
/// <summary>
/// Parse and validate item fields
/// </summary>
2023-08-11 12:47:59 -04:00
private static (string?, string?) ParseDatItemFilterId(string itemName, string fieldName)
{
// Get the correct item type
2023-08-11 13:46:27 -04:00
var itemType = TypeHelper.GetDatItemType(itemName.ToLowerInvariant());
if (itemType == null)
return (null, null);
// Get the set of constants
2023-08-11 13:46:27 -04:00
var constants = TypeHelper.GetConstants(itemType);
if (constants == null)
return (null, null);
// Get if there's a match to the constant
2023-08-11 12:47:59 -04:00
string? constantMatch = constants.FirstOrDefault(c => string.Equals(c, fieldName, StringComparison.OrdinalIgnoreCase));
if (constantMatch == null)
return (null, null);
// Return the sanitized ID
2023-08-11 12:47:59 -04:00
return (itemName.ToLowerInvariant(), constantMatch);
}
2023-08-11 11:15:34 -04:00
}
}