Add field manipulator class

This commit is contained in:
Matt Nadareski
2023-08-11 13:46:27 -04:00
parent 25ba9db624
commit 33342c6929
3 changed files with 90 additions and 41 deletions

View File

@@ -0,0 +1,40 @@
using System;
using System.Linq;
using SabreTools.Models.Internal;
namespace SabreTools.Filter
{
public static class FieldManipulator
{
/// <summary>
/// Set a field in a given DictionaryBase
/// </summary>
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;
}
/// <summary>
/// Remove a field from a given DictionaryBase
/// </summary>
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;
}
}
}

View File

@@ -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);
}
/// <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)
.Where(f => f.CustomAttributes.Any(a => a.AttributeType == typeof(NoFilterAttribute)))
.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);
}
}
}

View File

@@ -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
{
/// <summary>
/// Get constant values for the given type, if possible
/// </summary>
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()!;
}
/// <summary>
/// Attempt to get the DatItem type from the name
/// </summary>
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<XmlRootAttribute>()?.ElementName == itemType);
}
}
}