diff --git a/SabreTools.Filter/FieldManipulator.cs b/SabreTools.Filter/FieldManipulator.cs new file mode 100644 index 00000000..0ed5bd29 --- /dev/null +++ b/SabreTools.Filter/FieldManipulator.cs @@ -0,0 +1,40 @@ +using System; +using System.Linq; +using SabreTools.Models.Internal; + +namespace SabreTools.Filter +{ + public static class FieldManipulator + { + /// + /// Set a field in a given DictionaryBase + /// + public static bool SetField(DictionaryBase? dictionaryBase, string? fieldName, object value) + { + if (dictionaryBase == null || fieldName == null) + return false; + + var constants = TypeHelper.GetConstants(typeof(DictionaryBase)); + if (constants == null || !constants.Any(c => string.Equals(c, fieldName, StringComparison.InvariantCultureIgnoreCase))) + return false; + + dictionaryBase[fieldName] = value; + return true; + } + + /// + /// Remove a field from a given DictionaryBase + /// + public static bool RemoveField(DictionaryBase? dictionaryBase, string? fieldName) + { + if (dictionaryBase == null || fieldName == null) + return false; + + if (!dictionaryBase.ContainsKey(fieldName)) + return false; + + dictionaryBase.Remove(fieldName); + return true; + } + } +} \ No newline at end of file diff --git a/SabreTools.Filter/FilterParser.cs b/SabreTools.Filter/FilterParser.cs index eeaaf700..0f8c2c5f 100644 --- a/SabreTools.Filter/FilterParser.cs +++ b/SabreTools.Filter/FilterParser.cs @@ -1,8 +1,5 @@ using System; using System.Linq; -using System.Reflection; -using System.Xml.Serialization; -using SabreTools.Models; using SabreTools.Models.Internal; namespace SabreTools.Filter @@ -58,7 +55,7 @@ namespace SabreTools.Filter private static (string?, string?) ParseHeaderFilterId(string fieldName) { // Get the set of constants - var constants = GetConstants(typeof(Header)); + var constants = TypeHelper.GetConstants(typeof(Header)); if (constants == null) return (null, null); @@ -77,7 +74,7 @@ namespace SabreTools.Filter private static (string?, string?) ParseMachineFilterId(string fieldName) { // Get the set of constants - var constants = GetConstants(typeof(Machine)); + var constants = TypeHelper.GetConstants(typeof(Machine)); if (constants == null) return (null, null); @@ -96,12 +93,12 @@ namespace SabreTools.Filter private static (string?, string?) ParseDatItemFilterId(string itemName, string fieldName) { // Get the correct item type - var itemType = GetDatItemType(itemName.ToLowerInvariant()); + var itemType = TypeHelper.GetDatItemType(itemName.ToLowerInvariant()); if (itemType == null) return (null, null); // Get the set of constants - var constants = GetConstants(itemType); + var constants = TypeHelper.GetConstants(itemType); if (constants == null) return (null, null); @@ -113,39 +110,5 @@ namespace SabreTools.Filter // Return the sanitized ID return (itemName.ToLowerInvariant(), constantMatch); } - - /// - /// Get constant values for the given type, if possible - /// - 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) - .Where(f => f.CustomAttributes.Any(a => a.AttributeType == typeof(NoFilterAttribute))) - .Select(f => f.GetRawConstantValue() as string) - .Where(v => v != null) - .ToArray()!; - } - - /// - /// Attempt to get the DatItem type from the name - /// - 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()?.ElementName == itemType); - } } } diff --git a/SabreTools.Filter/TypeHelper.cs b/SabreTools.Filter/TypeHelper.cs new file mode 100644 index 00000000..b028e4e6 --- /dev/null +++ b/SabreTools.Filter/TypeHelper.cs @@ -0,0 +1,46 @@ +using System; +using System.Linq; +using System.Reflection; +using System.Xml.Serialization; +using SabreTools.Models; +using SabreTools.Models.Internal; + +namespace SabreTools.Filter +{ + internal static class TypeHelper + { + /// + /// Get constant values for the given type, if possible + /// + public 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) + .Where(f => f.CustomAttributes.Any(a => a.AttributeType == typeof(NoFilterAttribute))) + .Select(f => f.GetRawConstantValue() as string) + .Where(v => v != null) + .ToArray()!; + } + + /// + /// Attempt to get the DatItem type from the name + /// + public 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()?.ElementName == itemType); + } + } +}