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)]:(?