Add provisions for generic item filters

This commit is contained in:
Matt Nadareski
2024-03-05 09:39:12 -05:00
parent 8875948946
commit 6b7597c052
5 changed files with 37 additions and 5 deletions

View File

@@ -45,6 +45,8 @@ namespace SabreTools.Filter
"set" => ParseMachineFilterId(fieldName!),
// DatItem
"datitem" => ParseDatItemFilterId(fieldName!),
"item" => ParseDatItemFilterId(fieldName!),
_ => ParseDatItemFilterId(itemName, fieldName!),
};
}
@@ -87,6 +89,23 @@ namespace SabreTools.Filter
return (MetadataFile.MachineKey, constantMatch);
}
/// <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>

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SabreTools.Models.Metadata;
namespace SabreTools.Filter
@@ -57,7 +58,7 @@ namespace SabreTools.Filter
foreach (var filter in this.Filters)
{
// If the filter isn't for this object type, skip
if (filter.Key[0] != itemName)
if (filter.Key[0] != itemName || (filter.Key[0] == "item" && TypeHelper.GetDatItemTypeNames().Contains(itemName)))
continue;
// If we don't get a match, it's a failure

View File

@@ -37,6 +37,18 @@ namespace SabreTools.Filter
#endif
}
/// <summary>
/// Attempt to get all DatItem types
/// </summary>
public static string?[] GetDatItemTypeNames()
{
return AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(t => typeof(DatItem).IsAssignableFrom(t) && t.IsClass)
.Select(GetXmlRootAttributeElementName)
.ToArray();
}
/// <summary>
/// Attempt to get the DatItem type from the name
/// </summary>