From fa4680e33b023299c4ae8cf933da4d6bda88e5de Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 12 Dec 2020 11:36:12 +0900 Subject: [PATCH 1/3] Make property has it's name. --- .../IO/Serialization/Converters/RomajiTagConverter.cs | 8 ++++---- .../IO/Serialization/Converters/RubyTagConverter.cs | 8 ++++---- osu.Game.Rulesets.Karaoke/Objects/Lyric.cs | 2 -- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/RomajiTagConverter.cs b/osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/RomajiTagConverter.cs index 5c56a1155..ded067fe5 100644 --- a/osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/RomajiTagConverter.cs +++ b/osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/RomajiTagConverter.cs @@ -19,14 +19,14 @@ public override RomajiTag ReadJson(JsonReader reader, Type objectType, RomajiTag if (value == null || value == "") return new RomajiTag(); - var regux = new Regex("([-0-9]+),([-0-9]+)]:(.*$)"); + var regux = new Regex("(?[-0-9]+),(?[-0-9]+)]:(?.*$)"); var result = regux.Match(value); if (!result.Success) return new RomajiTag(); - var startIndex = int.Parse(result.Groups[1].Value); - var endIndex = int.Parse(result.Groups[2].Value); - var text = result.Groups[3].Value; + var startIndex = int.Parse(result.Groups["start"]?.Value); + var endIndex = int.Parse(result.Groups["end"]?.Value); + var text = result.Groups["romaji"]?.Value; return new RomajiTag { diff --git a/osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/RubyTagConverter.cs b/osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/RubyTagConverter.cs index 22379cff9..a6fa381b6 100644 --- a/osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/RubyTagConverter.cs +++ b/osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/RubyTagConverter.cs @@ -19,14 +19,14 @@ public override RubyTag ReadJson(JsonReader reader, Type objectType, RubyTag exi if (value == null || value == "") return new RubyTag(); - var regux = new Regex("([-0-9]+),([-0-9]+)]:(.*$)"); + var regux = new Regex("(?[-0-9]+),(?[-0-9]+)]:(?.*$)"); var result = regux.Match(value); if (!result.Success) return new RubyTag(); - var startIndex = int.Parse(result.Groups[1].Value); - var endIndex = int.Parse(result.Groups[2].Value); - var text = result.Groups[3].Value; + var startIndex = int.Parse(result.Groups["start"]?.Value); + var endIndex = int.Parse(result.Groups["end"]?.Value); + var text = result.Groups["ruby"]?.Value; return new RubyTag { diff --git a/osu.Game.Rulesets.Karaoke/Objects/Lyric.cs b/osu.Game.Rulesets.Karaoke/Objects/Lyric.cs index 0ccb8f6e9..261a12e85 100644 --- a/osu.Game.Rulesets.Karaoke/Objects/Lyric.cs +++ b/osu.Game.Rulesets.Karaoke/Objects/Lyric.cs @@ -1,14 +1,12 @@ // Copyright (c) andy840119 . Licensed under the GPL Licence. // See the LICENCE file in the repository root for full licence text. -using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using Newtonsoft.Json; using osu.Framework.Bindables; using osu.Framework.Extensions.IEnumerableExtensions; -using osu.Framework.Graphics.Sprites; using osu.Game.Beatmaps; using osu.Game.Beatmaps.ControlPoints; using osu.Game.Rulesets.Judgements; From 46c4a9ec230d053d98463a4a14c1a3845e9e85ee Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 12 Dec 2020 12:59:12 +0900 Subject: [PATCH 2/3] Rewrite time-tag convertor and it's test usnig regular expression. --- .../Converters/TimeTagConverterTest.cs | 46 +++++++++++++++ .../Converters/TimeTagsConverterTest.cs | 45 --------------- .../Converters/TimeTagConverter.cs | 46 +++++++++++++++ .../Converters/TimeTagsConverter.cs | 56 ------------------- 4 files changed, 92 insertions(+), 101 deletions(-) create mode 100644 osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/TimeTagConverterTest.cs delete mode 100644 osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/TimeTagsConverterTest.cs create mode 100644 osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/TimeTagConverter.cs delete mode 100644 osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/TimeTagsConverter.cs diff --git a/osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/TimeTagConverterTest.cs b/osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/TimeTagConverterTest.cs new file mode 100644 index 000000000..911e339c5 --- /dev/null +++ b/osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/TimeTagConverterTest.cs @@ -0,0 +1,46 @@ +// Copyright (c) andy840119 . Licensed under the GPL Licence. +// See the LICENCE file in the repository root for full licence text. + +using Newtonsoft.Json; +using NUnit.Framework; +using osu.Framework.Graphics.Sprites; +using osu.Game.Rulesets.Karaoke.IO.Serialization.Converters; +using osu.Game.Rulesets.Karaoke.Objects; + +namespace osu.Game.Rulesets.Karaoke.Tests.IO.Serialization.Converters +{ + [TestFixture] + public class TimeTagConverterTest : BaseSingleConverterTest + { + [TestCase(1, TimeTagIndex.IndexState.Start, 1000, "[1,start]:1000")] + [TestCase(1, TimeTagIndex.IndexState.End, 1000, "[1,end]:1000")] + [TestCase(-1, TimeTagIndex.IndexState.Start, 1000, "[-1,start]:1000")] // Should not check index is out of range in here. + [TestCase(1, TimeTagIndex.IndexState.Start, -1000, "[1,start]:-1000")] // Should not check if time is negative. + [TestCase(1, TimeTagIndex.IndexState.Start, null, "[1,start]:")] + public void TestSerialize(int index, TimeTagIndex.IndexState state, int? time, string json) + { + var timeTag = new TimeTag(new TimeTagIndex(index, state), time); + + var result = JsonConvert.SerializeObject(timeTag, CreateSettings()); + Assert.AreEqual(result, $"\"{json}\""); + } + + [TestCase("[1,start]:1000", 1, TimeTagIndex.IndexState.Start, 1000)] + [TestCase("[1,end]:1000", 1, TimeTagIndex.IndexState.End, 1000)] + [TestCase("[-1,start]:1000", -1, TimeTagIndex.IndexState.Start, 1000)] // Should not check index is out of range in here. + [TestCase("[1,start]:-1000", 1, TimeTagIndex.IndexState.Start, -1000)] // Should not check if time is negative. + [TestCase("[1,start]:", 1, TimeTagIndex.IndexState.Start, null)] + [TestCase("", TimeTagIndex.IndexState.Start, 0, null)] // Test deal with format is not right below. + [TestCase("[1,???]:", 0, TimeTagIndex.IndexState.Start, null)] + [TestCase("[1,]", 0, TimeTagIndex.IndexState.Start, null)] + [TestCase("[,start]", 0, TimeTagIndex.IndexState.Start, null)] + [TestCase("[]", 0, TimeTagIndex.IndexState.Start, null)] + public void TestDeserialize(string json, int index, TimeTagIndex.IndexState state, int? time) + { + var result = JsonConvert.DeserializeObject($"\"{json}\"", CreateSettings()); + var actual = new TimeTag(new TimeTagIndex(index, state), time); + Assert.AreEqual(result.Index, actual.Index); + Assert.AreEqual(result.Time, actual.Time); + } + } +} diff --git a/osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/TimeTagsConverterTest.cs b/osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/TimeTagsConverterTest.cs deleted file mode 100644 index b0ad17f0b..000000000 --- a/osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/TimeTagsConverterTest.cs +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) andy840119 . Licensed under the GPL Licence. -// See the LICENCE file in the repository root for full licence text. - -using System; -using Newtonsoft.Json; -using NUnit.Framework; -using osu.Framework.Graphics.Sprites; -using osu.Game.Rulesets.Karaoke.IO.Serialization.Converters; -using osu.Game.Rulesets.Karaoke.Objects; -using osu.Game.Rulesets.Karaoke.Utils; - -namespace osu.Game.Rulesets.Karaoke.Tests.IO.Serialization.Converters -{ - [TestFixture] - public class TimeTagsConverterTest : BaseSingleConverterTest - { - [Test] - public void TestSerialize() - { - var rowTimeTag = new[] - { - new TimeTag(new TimeTagIndex(0), 1000d), - new TimeTag(new TimeTagIndex(0, TimeTagIndex.IndexState.End), 1100d), - new TimeTag(new TimeTagIndex(0, TimeTagIndex.IndexState.End), 1200d), - }; - - var result = JsonConvert.SerializeObject(rowTimeTag, CreateSettings()); - - Assert.AreEqual(result, "[\r\n \"0,0,1000\",\r\n \"0,1,1100\",\r\n \"0,1,1200\"\r\n]"); - } - - [Test] - public void TestDeserialize() - { - const string json_string = "[\r\n \"0,0,1000\",\r\n \"0,1,1100\",\r\n \"0,1,1200\"\r\n]"; - var result = JsonConvert.DeserializeObject(json_string, CreateSettings()); - - Assert.IsNotNull(result); - Assert.AreEqual(result.Length, 3); - Assert.AreEqual(result[0].Index.Index, 0); - Assert.AreEqual(result[0].Index.State, TimeTagIndex.IndexState.Start); - Assert.AreEqual(result[0].Time, 1000); - } - } -} diff --git a/osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/TimeTagConverter.cs b/osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/TimeTagConverter.cs new file mode 100644 index 000000000..96c0ef695 --- /dev/null +++ b/osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/TimeTagConverter.cs @@ -0,0 +1,46 @@ +// Copyright (c) andy840119 . Licensed under the GPL Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using System.Text.RegularExpressions; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using osu.Framework.Graphics.Sprites; +using osu.Game.Rulesets.Karaoke.Objects; + +namespace osu.Game.Rulesets.Karaoke.IO.Serialization.Converters +{ + public class TimeTagConverter : JsonConverter + { + public override TimeTag ReadJson(JsonReader reader, Type objectType, TimeTag existingValue, bool hasExistingValue, JsonSerializer serializer) + { + var obj = JToken.Load(reader); + var value = obj.Value(); + + if (value == null || value == "") + return new TimeTag(new TimeTagIndex()); + + var regux = new Regex("(?[-0-9]+),(?start|end)]:(?