diff --git a/src/MaaWpfGui/Configuration/Converter/TolerantEnumConverter.cs b/src/MaaWpfGui/Configuration/Converter/TolerantEnumConverter.cs index baf7f0ed6c..0088eb45f6 100644 --- a/src/MaaWpfGui/Configuration/Converter/TolerantEnumConverter.cs +++ b/src/MaaWpfGui/Configuration/Converter/TolerantEnumConverter.cs @@ -14,6 +14,7 @@ #nullable enable using System; +using System.Collections.Generic; using System.Text.Json; using System.Text.Json.Serialization; using Serilog; @@ -48,6 +49,7 @@ internal sealed class TolerantEnumConverter : JsonConverter { private static readonly bool _isFlagsEnum = typeof(TEnum).IsDefined(typeof(FlagsAttribute), inherit: false); private static readonly TypeCode _underlyingTypeCode = Type.GetTypeCode(Enum.GetUnderlyingType(typeof(TEnum))); + private static readonly HashSet _definedValues = GetDefinedValues(); private static readonly ulong _validFlagsMask = GetValidFlagsMask(); private readonly ILogger _logger = Log.ForContext>(); @@ -67,7 +69,7 @@ internal sealed class TolerantEnumConverter : JsonConverter if (!IsValidValue(converted)) { _logger.Warning( - "Numeric value {Value} is not a defined member of {EnumType}, using default value {Default}", + "Numeric value {Value} is not a supported member of {EnumType}, using default value {Default}", longVal, typeof(TEnum).Name, default(TEnum)); return default; } @@ -127,7 +129,8 @@ internal sealed class TolerantEnumConverter : JsonConverter private static bool IsValidValue(TEnum value) { - if (Enum.IsDefined(value)) + var rawValue = ToUInt64(value); + if (_definedValues.Contains(rawValue)) { return true; } @@ -137,10 +140,20 @@ internal sealed class TolerantEnumConverter : JsonConverter return false; } - var rawValue = ToUInt64(value); return rawValue != 0 && (rawValue & ~_validFlagsMask) == 0; } + private static HashSet GetDefinedValues() + { + HashSet values = []; + foreach (var value in Enum.GetValues()) + { + values.Add(ToUInt64(value)); + } + + return values; + } + private static ulong GetValidFlagsMask() { ulong mask = 0;