diff --git a/BizHawk.Common/BizHawk.Common.csproj b/BizHawk.Common/BizHawk.Common.csproj index 375d6039a1..757e287f0e 100644 --- a/BizHawk.Common/BizHawk.Common.csproj +++ b/BizHawk.Common/BizHawk.Common.csproj @@ -52,6 +52,7 @@ + @@ -81,6 +82,7 @@ + diff --git a/BizHawk.Common/DescribableEnumConverter.cs b/BizHawk.Common/DescribableEnumConverter.cs new file mode 100644 index 0000000000..3547d04995 --- /dev/null +++ b/BizHawk.Common/DescribableEnumConverter.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.Globalization; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace BizHawk.Common +{ + public class DescribableEnumConverter : EnumConverter + { + private Type enumType; + + public DescribableEnumConverter(Type type) : base(type) + { + enumType = type; + } + + public override bool CanConvertTo(ITypeDescriptorContext context, Type destType) + { + return destType == typeof(string); + } + + public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, + object value, Type destType) + { + var fi = enumType.GetField(Enum.GetName(enumType, value)); + var attr = (DisplayAttribute)fi.GetCustomAttribute(typeof(DisplayAttribute)); + if (attr != null) + return attr.Name; + else + return value.ToString(); + } + + public override bool CanConvertFrom(ITypeDescriptorContext context, Type srcType) + { + return srcType == typeof(string); + } + + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, + object value) + { + foreach (var fi in enumType.GetFields(BindingFlags.Public | BindingFlags.Static)) + { + var attr = (DisplayAttribute)fi.GetCustomAttribute(typeof(DisplayAttribute)); + if (attr != null && attr.Name.Equals(value)) + return Enum.Parse(enumType, fi.Name); + } + return Enum.Parse(enumType, (string)value); + } + + public override bool GetStandardValuesSupported(ITypeDescriptorContext context) + { + return true; + } + + public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) + { + return true; + } + + public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) + { + var ret = new List(); + foreach (var fi in enumType.GetFields(BindingFlags.Public | BindingFlags.Static)) + { + ret.Add(fi.GetValue(null)); + } + return new StandardValuesCollection(ret); + } + } +} diff --git a/BizHawk.Emulation.Cores/Consoles/Sega/Saturn/Saturnus.cs b/BizHawk.Emulation.Cores/Consoles/Sega/Saturn/Saturnus.cs index 96f1446950..d24af1f3eb 100644 --- a/BizHawk.Emulation.Cores/Consoles/Sega/Saturn/Saturnus.cs +++ b/BizHawk.Emulation.Cores/Consoles/Sega/Saturn/Saturnus.cs @@ -345,6 +345,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Sega.Saturn [DisplayName("Default Region")] [Description("Used when Region Autodetect is disabled or fails")] [DefaultValue(RegionType.Japan)] + [TypeConverter(typeof(DescribableEnumConverter))] public RegionType Region { get; set; } public enum LanguageType