Files
SabreTools/SabreTools.Filter/FilterParser.cs

152 lines
5.5 KiB
C#
Raw Normal View History

2023-08-11 11:15:34 -04:00
using System;
using System.Linq;
using System.Reflection;
using System.Xml.Serialization;
2023-08-11 11:29:25 -04:00
using SabreTools.Models;
2023-08-11 11:15:34 -04:00
using SabreTools.Models.Internal;
namespace SabreTools.Filter
{
public static class FilterParser
{
/// <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
2023-08-11 12:47:59 -04:00
if (string.IsNullOrWhiteSpace(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
if (string.IsNullOrWhiteSpace(itemName) || string.IsNullOrWhiteSpace(fieldName))
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
2023-08-11 12:47:59 -04:00
"header" => ParseHeaderFilterId(fieldName),
2023-08-11 11:15:34 -04:00
// Machine
2023-08-11 12:47:59 -04:00
"game" => ParseMachineFilterId(fieldName),
"machine" => ParseMachineFilterId(fieldName),
"resource" => ParseMachineFilterId(fieldName),
"set" => ParseMachineFilterId(fieldName),
2023-08-11 11:15:34 -04:00
// DatItem
2023-08-11 12:47:59 -04: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
var constants = GetConstants(typeof(Header));
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
var constants = 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>
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 12:47:59 -04:00
var itemType = GetDatItemType(itemName.ToLowerInvariant());
if (itemType == null)
return (null, null);
// Get the set of constants
var constants = 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
/// <summary>
/// Get constant values for the given type, if possible
/// </summary>
private static string[]? GetConstants(Type? type)
{
if (type == null)
return null;
var fields = type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);
if (fields == null)
return null;
return fields
.Where(f => f.IsLiteral && !f.IsInitOnly)
2023-08-11 11:29:25 -04:00
.Where(f => f.CustomAttributes.Any(a => a.AttributeType == typeof(NoFilterAttribute)))
2023-08-11 11:15:34 -04:00
.Select(f => f.GetRawConstantValue() as string)
.Where(v => v != null)
.ToArray()!;
}
/// <summary>
/// Attempt to get the DatItem type from the name
/// </summary>
private static Type? GetDatItemType(string? itemType)
{
if (string.IsNullOrWhiteSpace(itemType))
return null;
return AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(t => t.IsAssignableFrom(typeof(DatItem)) && t.IsClass)
.FirstOrDefault(t => t.GetCustomAttribute<XmlRootAttribute>()?.ElementName == itemType);
}
2023-08-11 11:15:34 -04:00
}
}