-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #305 from andy840119/io/fix-regex-issue
Using better regular expression to parse ruby, romaji and time-tag
- Loading branch information
Showing
7 changed files
with
100 additions
and
111 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/TimeTagConverterTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) andy840119 <[email protected]>. 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<TimeTagConverter> | ||
{ | ||
[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("", 0, TimeTagIndex.IndexState.Start, 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<TimeTag>($"\"{json}\"", CreateSettings()); | ||
var actual = new TimeTag(new TimeTagIndex(index, state), time); | ||
Assert.AreEqual(result.Index, actual.Index); | ||
Assert.AreEqual(result.Time, actual.Time); | ||
} | ||
} | ||
} |
45 changes: 0 additions & 45 deletions
45
osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/TimeTagsConverterTest.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/TimeTagConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) andy840119 <[email protected]>. 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<TimeTag> | ||
{ | ||
public override TimeTag ReadJson(JsonReader reader, Type objectType, TimeTag existingValue, bool hasExistingValue, JsonSerializer serializer) | ||
{ | ||
var obj = JToken.Load(reader); | ||
var value = obj.Value<string>(); | ||
|
||
if (value == null || value == "") | ||
return new TimeTag(new TimeTagIndex()); | ||
|
||
var regux = new Regex("(?<index>[-0-9]+),(?<state>start|end)]:(?<time>[-0-9]+|s*|)"); | ||
var result = regux.Match(value); | ||
if (!result.Success) | ||
return new TimeTag(new TimeTagIndex()); | ||
|
||
var index = int.Parse(result.Groups["index"]?.Value); | ||
var state = result.Groups["state"]?.Value == "start" ? TimeTagIndex.IndexState.Start : TimeTagIndex.IndexState.End; | ||
var timeStr = result.Groups["time"]?.Value; | ||
var time = timeStr == "" ? default(int?) : int.Parse(timeStr); | ||
|
||
return new TimeTag(new TimeTagIndex(index, state), time); | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, TimeTag value, JsonSerializer serializer) | ||
{ | ||
var tag = value.Index; | ||
var state = tag.State == TimeTagIndex.IndexState.Start ? "start" : "end"; | ||
var time = value.Time; | ||
|
||
var str = $"[{tag.Index},{state}]:{time}"; | ||
writer.WriteValue(str); | ||
} | ||
} | ||
} |
56 changes: 0 additions & 56 deletions
56
osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/TimeTagsConverter.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
// Copyright (c) andy840119 <[email protected]>. 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; | ||
|