diff --git a/SabreTools.Core/MappingAttribute.cs b/SabreTools.Core/MappingAttribute.cs new file mode 100644 index 00000000..25442f39 --- /dev/null +++ b/SabreTools.Core/MappingAttribute.cs @@ -0,0 +1,24 @@ +using System; + +namespace SabreTools.Core +{ + /// + /// Maps a set of strings to an enum value + /// + [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)] + public class MappingAttribute : Attribute + { + /// + /// Set of mapping strings + /// + public string[] Mappings { get; private set; } + + /// + /// Constructor + /// + public MappingAttribute(params string[] mappings) + { + this.Mappings = mappings; + } + } +} \ No newline at end of file diff --git a/SabreTools.Core/Tools/AttributeHelper.cs b/SabreTools.Core/Tools/AttributeHelper.cs new file mode 100644 index 00000000..8c31f5eb --- /dev/null +++ b/SabreTools.Core/Tools/AttributeHelper.cs @@ -0,0 +1,48 @@ +using System; +using System.Linq; + +namespace SabreTools.Core.Tools +{ + public static class AttributeHelper + { + /// + /// Get the MappingAttribute from a supported value + /// + /// Value to use + /// MappingAttribute attached to the value + public static MappingAttribute GetAttribute(T value) + { + // Null value in, null value out + if (value == null) + return null; + + // Current enumeration type + var enumType = typeof(T); + if (Nullable.GetUnderlyingType(enumType) != null) + enumType = Nullable.GetUnderlyingType(enumType); + + // If the value returns a null on ToString, just return null + string valueStr = value.ToString(); + if (string.IsNullOrWhiteSpace(valueStr)) + return null; + + // Get the member info array + var memberInfos = enumType?.GetMember(valueStr); + if (memberInfos == null) + return null; + + // Get the enum value info from the array, if possible + var enumValueMemberInfo = memberInfos.FirstOrDefault(m => m.DeclaringType == enumType); + if (enumValueMemberInfo == null) + return null; + + // Try to get the relevant attribute + var attributes = enumValueMemberInfo.GetCustomAttributes(typeof(MappingAttribute), true); + if (attributes == null) + return null; + + // Return the first attribute, if possible + return (MappingAttribute)attributes.FirstOrDefault(); + } + } +} \ No newline at end of file diff --git a/SabreTools.Core/Tools/Converters.cs b/SabreTools.Core/Tools/Converters.cs index a37e3f3c..778f41c2 100644 --- a/SabreTools.Core/Tools/Converters.cs +++ b/SabreTools.Core/Tools/Converters.cs @@ -1,4 +1,6 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Linq; using System.Text.RegularExpressions; namespace SabreTools.Core.Tools @@ -1030,13 +1032,13 @@ namespace SabreTools.Core.Tools "sampleof" => MachineField.SampleOf, "sample_of" => MachineField.SampleOf, - + "type" => MachineField.Type, #endregion #region AttractMode - + "players" => MachineField.Players, "rotation" => MachineField.Rotation, @@ -1127,13 +1129,13 @@ namespace SabreTools.Core.Tools "system" => MachineField.System, "msxsystem" => MachineField.System, "msx_system" => MachineField.System, - + "country" => MachineField.Country, #endregion #region SoftwareList - + "supported" => MachineField.Supported, #endregion @@ -1340,6 +1342,44 @@ namespace SabreTools.Core.Tools _ => null, }; } + + /// + /// Get a set of mappings from strings to enum values + /// + /// Enum type to generate from + /// Dictionary of string to enum values + private static Dictionary GenerateToEnum(Type type) + { + try + { + // Get all of the values for the enum type + var values = Enum.GetValues(type); + + // Build the output dictionary + Dictionary mappings = new(); + foreach (T value in values) + { + // Try to get the mapping attribute + MappingAttribute attr = AttributeHelper.GetAttribute(value); + if (attr?.Mappings == null || !attr.Mappings.Any()) + continue; + + // Loop through the mappings and add each + foreach (string mapString in attr.Mappings) + { + mappings[mapString] = value; + } + } + + // Return the output dictionary + return mappings; + } + catch + { + // This should not happen, only if the type was not an enum + return new Dictionary(); + } + } #endregion @@ -1780,6 +1820,41 @@ namespace SabreTools.Core.Tools }; } + /// + /// Get a set of mappings from enum values to string + /// + /// Enum type to generate from + /// Dictionary of enum to string values + private static Dictionary GenerateToString(Type type) + { + try + { + // Get all of the values for the enum type + var values = Enum.GetValues(type); + + // Build the output dictionary + Dictionary mappings = new(); + foreach (T value in values) + { + // Try to get the mapping attribute + MappingAttribute attr = AttributeHelper.GetAttribute(value); + if (attr?.Mappings == null || !attr.Mappings.Any()) + continue; + + // Always use the first value in the list + mappings[value] = attr.Mappings[0]; + } + + // Return the output dictionary + return mappings; + } + catch + { + // This should not happen, only if the type was not an enum + return new Dictionary(); + } + } + #endregion } }