From 2318d0b1861f4b58e7e4a4a13b18cdd8e98496b9 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Tue, 15 Feb 2022 21:28:14 +0800 Subject: [PATCH] implement time-tags convertor. --- .../Converters/TimeTagsConverterTest.cs | 50 +++++++++++++++++++ .../Converters/TimeTagsConverter.cs | 15 ++++++ 2 files changed, 65 insertions(+) create mode 100644 osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/TimeTagsConverterTest.cs create mode 100644 osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/TimeTagsConverter.cs diff --git a/osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/TimeTagsConverterTest.cs b/osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/TimeTagsConverterTest.cs new file mode 100644 index 000000000..f3a879b25 --- /dev/null +++ b/osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/TimeTagsConverterTest.cs @@ -0,0 +1,50 @@ +// 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; +using osu.Game.Rulesets.Karaoke.Tests.Asserts; + +namespace osu.Game.Rulesets.Karaoke.Tests.IO.Serialization.Converters +{ + [TestFixture] + public class TimeTagsConverterTest : BaseSingleConverterTest + { + protected override JsonConverter[] CreateExtraConverts() + => new JsonConverter[] + { + new TimeTagConverter(), + }; + + [Test] + public void TestSerialize() + { + var timeTags = new[] + { + new TimeTag(new TextIndex(0, TextIndex.IndexState.End), 1000), + new TimeTag(new TextIndex(0), 0) + }; + + const string expected = "[\"[0,start]:0\",\"[0,end]:1000\"]"; + string actual = JsonConvert.SerializeObject(timeTags, CreateSettings()); + Assert.AreEqual(expected, actual); + } + + [Test] + public void TestDeserialize() + { + const string json = "[\"[0,end]:1000\",\"[0,start]:0\"]"; + + var expected = new[] + { + new TimeTag(new TextIndex(0), 0), + new TimeTag(new TextIndex(0, TextIndex.IndexState.End), 1000), + }; + var actual = JsonConvert.DeserializeObject(json, CreateSettings()); + TimeTagAssert.ArePropertyEqual(expected, actual); + } + } +} diff --git a/osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/TimeTagsConverter.cs b/osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/TimeTagsConverter.cs new file mode 100644 index 000000000..35d42db1b --- /dev/null +++ b/osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/TimeTagsConverter.cs @@ -0,0 +1,15 @@ +// Copyright (c) andy840119 . Licensed under the GPL Licence. +// See the LICENCE file in the repository root for full licence text. + +using System.Collections.Generic; +using osu.Game.Rulesets.Karaoke.Objects; +using osu.Game.Rulesets.Karaoke.Utils; + +namespace osu.Game.Rulesets.Karaoke.IO.Serialization.Converters +{ + public class TimeTagsConverter : SortableJsonConvertor + { + protected override IEnumerable GetSortedValue(IEnumerable objects) + => TimeTagsUtils.Sort(objects); + } +}