From 306f7e93a0d99f7844c42a6e3c84ee9040f17864 Mon Sep 17 00:00:00 2001 From: Isaac Marovitz <42140194+IsaacMarovitz@users.noreply.github.com> Date: Mon, 5 Jun 2023 00:19:46 +0100 Subject: [PATCH] Dont Error on Invalid Enum Values (#5169) * Dont Error on Invalid Enum * Use TryParse * Log warning --- .../Utilities/TypedStringEnumConverter.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Ryujinx.Common/Utilities/TypedStringEnumConverter.cs b/src/Ryujinx.Common/Utilities/TypedStringEnumConverter.cs index c0127dc4..73765129 100644 --- a/src/Ryujinx.Common/Utilities/TypedStringEnumConverter.cs +++ b/src/Ryujinx.Common/Utilities/TypedStringEnumConverter.cs @@ -1,4 +1,5 @@ #nullable enable +using Ryujinx.Common.Logging; using System; using System.Text.Json; using System.Text.Json.Serialization; @@ -18,12 +19,14 @@ namespace Ryujinx.Common.Utilities public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { var enumValue = reader.GetString(); - if (string.IsNullOrEmpty(enumValue)) + + if (Enum.TryParse(enumValue, out TEnum value)) { - return default; + return value; } - return Enum.Parse(enumValue); + Logger.Warning?.Print(LogClass.Configuration, $"Failed to parse enum value \"{enumValue}\" for {typeof(TEnum)}, using default \"{default(TEnum)}\""); + return default; } public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options) @@ -31,4 +34,4 @@ namespace Ryujinx.Common.Utilities writer.WriteStringValue(value.ToString()); } } -} +} \ No newline at end of file