From a021aaf54673acf78c5272d077e757b1ded87bc8 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Mon, 21 Dec 2020 16:42:21 +0900 Subject: [PATCH 1/2] Fix room category being serialised as ints --- osu.Game/Online/Multiplayer/Room.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/osu.Game/Online/Multiplayer/Room.cs b/osu.Game/Online/Multiplayer/Room.cs index 9a21543b2e..53ae142ad4 100644 --- a/osu.Game/Online/Multiplayer/Room.cs +++ b/osu.Game/Online/Multiplayer/Room.cs @@ -35,9 +35,22 @@ namespace osu.Game.Online.Multiplayer public readonly Bindable ChannelId = new Bindable(); [Cached] - [JsonProperty("category")] + [JsonIgnore] public readonly Bindable Category = new Bindable(); + // Todo: osu-framework bug (https://github.com/ppy/osu-framework/issues/4106) + [JsonProperty("category")] + private string categoryString + { + get => Category.Value.ToString().ToLower(); + set + { + if (!Enum.TryParse(value, true, out var enumValue)) + enumValue = RoomCategory.Normal; + Category.Value = enumValue; + } + } + [Cached] [JsonIgnore] public readonly Bindable Duration = new Bindable(TimeSpan.FromMinutes(30)); From e23d81bfc64594c80d21ea1043ec085a47161f25 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Mon, 21 Dec 2020 16:56:45 +0900 Subject: [PATCH 2/2] Use enum property --- .../Converters/SnakeCaseStringEnumConverter.cs | 16 ++++++++++++++++ osu.Game/Online/Multiplayer/Room.cs | 13 +++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 osu.Game/IO/Serialization/Converters/SnakeCaseStringEnumConverter.cs diff --git a/osu.Game/IO/Serialization/Converters/SnakeCaseStringEnumConverter.cs b/osu.Game/IO/Serialization/Converters/SnakeCaseStringEnumConverter.cs new file mode 100644 index 0000000000..1d82a5bc87 --- /dev/null +++ b/osu.Game/IO/Serialization/Converters/SnakeCaseStringEnumConverter.cs @@ -0,0 +1,16 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using Newtonsoft.Json.Converters; +using Newtonsoft.Json.Serialization; + +namespace osu.Game.IO.Serialization.Converters +{ + public class SnakeCaseStringEnumConverter : StringEnumConverter + { + public SnakeCaseStringEnumConverter() + { + NamingStrategy = new SnakeCaseNamingStrategy(); + } + } +} diff --git a/osu.Game/Online/Multiplayer/Room.cs b/osu.Game/Online/Multiplayer/Room.cs index 53ae142ad4..66e3e8975a 100644 --- a/osu.Game/Online/Multiplayer/Room.cs +++ b/osu.Game/Online/Multiplayer/Room.cs @@ -6,6 +6,7 @@ using System.Linq; using Newtonsoft.Json; using osu.Framework.Allocation; using osu.Framework.Bindables; +using osu.Game.IO.Serialization.Converters; using osu.Game.Online.Multiplayer.GameTypes; using osu.Game.Online.Multiplayer.RoomStatuses; using osu.Game.Users; @@ -40,15 +41,11 @@ namespace osu.Game.Online.Multiplayer // Todo: osu-framework bug (https://github.com/ppy/osu-framework/issues/4106) [JsonProperty("category")] - private string categoryString + [JsonConverter(typeof(SnakeCaseStringEnumConverter))] + private RoomCategory category { - get => Category.Value.ToString().ToLower(); - set - { - if (!Enum.TryParse(value, true, out var enumValue)) - enumValue = RoomCategory.Normal; - Category.Value = enumValue; - } + get => Category.Value; + set => Category.Value = value; } [Cached]