mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Use reflection for DatItem filter checking
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Xml.Serialization;
|
||||||
using SabreTools.Models;
|
using SabreTools.Models;
|
||||||
using SabreTools.Models.Internal;
|
using SabreTools.Models.Internal;
|
||||||
|
|
||||||
@@ -11,7 +12,6 @@ namespace SabreTools.Filter
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Parse a filter ID string into the item name and field name, if possible
|
/// Parse a filter ID string into the item name and field name, if possible
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>TODO: Have validation of fields done automatically</remarks>
|
|
||||||
public static (string?, string?) ParseFilterId(string filterId)
|
public static (string?, string?) ParseFilterId(string filterId)
|
||||||
{
|
{
|
||||||
// If we don't have a filter ID, we can't do anything
|
// If we don't have a filter ID, we can't do anything
|
||||||
@@ -36,8 +36,7 @@ namespace SabreTools.Filter
|
|||||||
"set" => ParseMachineFilterId(splitFilter),
|
"set" => ParseMachineFilterId(splitFilter),
|
||||||
|
|
||||||
// DatItem
|
// DatItem
|
||||||
// TODO: Implement parsers for all item types
|
_ => ParseDatItemFilterId(splitFilter),
|
||||||
_ => (null, null),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,7 +65,7 @@ namespace SabreTools.Filter
|
|||||||
private static (string?, string?) ParseMachineFilterId(string[] filterId)
|
private static (string?, string?) ParseMachineFilterId(string[] filterId)
|
||||||
{
|
{
|
||||||
// Get the set of constants
|
// Get the set of constants
|
||||||
var constants = GetConstants(typeof(Header));
|
var constants = GetConstants(typeof(Machine));
|
||||||
if (constants == null)
|
if (constants == null)
|
||||||
return (null, null);
|
return (null, null);
|
||||||
|
|
||||||
@@ -79,6 +78,30 @@ namespace SabreTools.Filter
|
|||||||
return (MetadataFile.MachineKey, constantMatch);
|
return (MetadataFile.MachineKey, constantMatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parse and validate item fields
|
||||||
|
/// </summary>
|
||||||
|
private static (string?, string?) ParseDatItemFilterId(string[] filterId)
|
||||||
|
{
|
||||||
|
// Get the correct item type
|
||||||
|
var itemType = GetDatItemType(filterId[0].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
|
||||||
|
string? constantMatch = constants.FirstOrDefault(c => string.Equals(c, filterId[1], StringComparison.OrdinalIgnoreCase));
|
||||||
|
if (constantMatch == null)
|
||||||
|
return (null, null);
|
||||||
|
|
||||||
|
// Return the sanitized ID
|
||||||
|
return (filterId[0].ToLowerInvariant(), constantMatch);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get constant values for the given type, if possible
|
/// Get constant values for the given type, if possible
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -98,5 +121,19 @@ namespace SabreTools.Filter
|
|||||||
.Where(v => v != null)
|
.Where(v => v != null)
|
||||||
.ToArray()!;
|
.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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user