1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 20:38:33 +08:00
osu-lazer/osu.Game.Tests/Beatmaps/Formats/OsuJsonDecoderTest.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

235 lines
9.9 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
using System.IO;
using System.Linq;
using DeepEqual.Syntax;
using NUnit.Framework;
using osu.Game.Audio;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Formats;
using osu.Game.IO;
using osu.Game.IO.Serialization;
using osu.Game.Rulesets.Objects.Types;
using osu.Game.Rulesets.Osu;
using osu.Game.Rulesets.Osu.Beatmaps;
2019-09-06 14:24:00 +08:00
using osu.Game.Rulesets.Scoring;
using osu.Game.Tests.Resources;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Tests.Beatmaps.Formats
{
[TestFixture]
public class OsuJsonDecoderTest
{
private const string normal = "Soleily - Renatus (Gamu) [Insane].osu";
private const string marathon = "Within Temptation - The Unforgiving (Armin) [Marathon].osu";
private const string with_sb = "Kozato snow - Rengetsu Ouka (_Kiva) [Yuki YukI].osu";
2018-04-13 17:19:50 +08:00
[Test]
public void TestDecodeMetadata()
{
var beatmap = decodeAsJson(normal);
var meta = beatmap.BeatmapInfo.Metadata;
2022-01-12 21:34:07 +08:00
Assert.AreEqual(241526, beatmap.BeatmapInfo.BeatmapSet?.OnlineID);
Assert.AreEqual("Soleily", meta.Artist);
Assert.AreEqual("Soleily", meta.ArtistUnicode);
Assert.AreEqual("03. Renatus - Soleily 192kbps.mp3", meta.AudioFile);
Assert.AreEqual("Gamu", meta.Author.Username);
Assert.AreEqual("machinetop_background.jpg", meta.BackgroundFile);
Assert.AreEqual(164471, meta.PreviewTime);
Assert.AreEqual(string.Empty, meta.Source);
Assert.AreEqual("MBC7 Unisphere 地球ヤバイEP Chikyu Yabai", meta.Tags);
Assert.AreEqual("Renatus", meta.Title);
Assert.AreEqual("Renatus", meta.TitleUnicode);
}
2018-04-13 17:19:50 +08:00
[Test]
public void TestDecodeGeneral()
{
var beatmap = decodeAsJson(normal);
var beatmapInfo = beatmap.BeatmapInfo;
Assert.AreEqual(0, beatmapInfo.AudioLeadIn);
Assert.AreEqual(0.7f, beatmapInfo.StackLeniency);
Assert.AreEqual(false, beatmapInfo.SpecialStyle);
Assert.IsTrue(beatmapInfo.Ruleset.OnlineID == 0);
Assert.AreEqual(false, beatmapInfo.LetterboxInBreaks);
Assert.AreEqual(false, beatmapInfo.WidescreenStoryboard);
Assert.AreEqual(CountdownType.None, beatmapInfo.Countdown);
Assert.AreEqual(0, beatmapInfo.CountdownOffset);
}
2018-04-13 17:19:50 +08:00
[Test]
public void TestDecodeEditor()
{
var beatmap = decodeAsJson(normal);
var beatmapInfo = beatmap.BeatmapInfo;
2018-04-13 17:19:50 +08:00
int[] expectedBookmarks =
{
11505, 22054, 32604, 43153, 53703, 64252, 74802, 85351,
95901, 106450, 116999, 119637, 130186, 140735, 151285,
161834, 164471, 175020, 185570, 196119, 206669, 209306
};
Assert.AreEqual(expectedBookmarks.Length, beatmapInfo.Bookmarks.Length);
for (int i = 0; i < expectedBookmarks.Length; i++)
Assert.AreEqual(expectedBookmarks[i], beatmapInfo.Bookmarks[i]);
Assert.AreEqual(1.8, beatmapInfo.DistanceSpacing);
Assert.AreEqual(4, beatmapInfo.BeatDivisor);
Assert.AreEqual(4, beatmapInfo.GridSize);
Assert.AreEqual(2, beatmapInfo.TimelineZoom);
}
2018-04-13 17:19:50 +08:00
[Test]
public void TestDecodeDifficulty()
{
var beatmap = decodeAsJson(normal);
var difficulty = beatmap.Difficulty;
Assert.AreEqual(6.5f, difficulty.DrainRate);
Assert.AreEqual(4, difficulty.CircleSize);
Assert.AreEqual(8, difficulty.OverallDifficulty);
Assert.AreEqual(9, difficulty.ApproachRate);
2018-03-05 15:14:36 +08:00
Assert.AreEqual(1.8, difficulty.SliderMultiplier);
Assert.AreEqual(2, difficulty.SliderTickRate);
}
2018-04-13 17:19:50 +08:00
[Test]
public void TestDecodePostConverted()
{
var converted = new OsuBeatmapConverter(decodeAsJson(normal), new OsuRuleset()).Convert();
var processor = new OsuBeatmapProcessor(converted);
processor.PreProcess();
foreach (var o in converted.HitObjects)
o.ApplyDefaults(converted.ControlPointInfo, converted.Difficulty);
processor.PostProcess();
var beatmap = converted.Serialize().Deserialize<Beatmap>();
var curveData = beatmap.HitObjects[0] as IHasPathWithRepeats;
var positionData = beatmap.HitObjects[0] as IHasPosition;
Assert.IsNotNull(positionData);
Assert.IsNotNull(curveData);
Assert.AreEqual(90, curveData.Path.Distance);
Assert.AreEqual(new Vector2(192, 168), positionData.Position);
Assert.AreEqual(956, beatmap.HitObjects[0].StartTime);
Assert.IsTrue(beatmap.HitObjects[0].Samples.Any(s => s.Name == HitSampleInfo.HIT_NORMAL));
positionData = beatmap.HitObjects[1] as IHasPosition;
Assert.IsNotNull(positionData);
Assert.AreEqual(new Vector2(304, 56), positionData.Position);
Assert.AreEqual(1285, beatmap.HitObjects[1].StartTime);
Assert.IsTrue(beatmap.HitObjects[1].Samples.Any(s => s.Name == HitSampleInfo.HIT_CLAP));
}
[Test]
public void TestDecodeHitObjects()
{
var beatmap = decodeAsJson(normal);
2018-04-13 17:19:50 +08:00
var curveData = beatmap.HitObjects[0] as IHasPathWithRepeats;
var positionData = beatmap.HitObjects[0] as IHasPosition;
2018-04-13 17:19:50 +08:00
Assert.IsNotNull(positionData);
Assert.IsNotNull(curveData);
Assert.AreEqual(90, curveData.Path.Distance);
Assert.AreEqual(new Vector2(192, 168), positionData.Position);
Assert.AreEqual(956, beatmap.HitObjects[0].StartTime);
2019-06-30 20:58:30 +08:00
Assert.IsTrue(beatmap.HitObjects[0].Samples.Any(s => s.Name == HitSampleInfo.HIT_NORMAL));
2018-04-13 17:19:50 +08:00
positionData = beatmap.HitObjects[1] as IHasPosition;
2018-04-13 17:19:50 +08:00
Assert.IsNotNull(positionData);
Assert.AreEqual(new Vector2(304, 56), positionData.Position);
Assert.AreEqual(1285, beatmap.HitObjects[1].StartTime);
2019-06-30 20:58:30 +08:00
Assert.IsTrue(beatmap.HitObjects[1].Samples.Any(s => s.Name == HitSampleInfo.HIT_CLAP));
}
2018-04-13 17:19:50 +08:00
[TestCase(normal)]
[TestCase(marathon)]
2019-03-08 09:42:25 +08:00
[Ignore("temporarily disabled pending DeepEqual fix (https://github.com/jamesfoster/DeepEqual/pull/35)")]
// Currently fails:
// [TestCase(with_sb)]
public void TestParity(string beatmap)
{
2018-04-20 13:41:57 +08:00
var legacy = decode(beatmap, out Beatmap json);
2018-07-02 12:24:09 +08:00
json.WithDeepEqual(legacy)
.IgnoreProperty(r => r.DeclaringType == typeof(HitWindows)
// Todo: CustomSampleBank shouldn't exist going forward, we need a conversion mechanism
|| r.Name == nameof(LegacyDecoder<Beatmap>.LegacySampleControlPoint.CustomSampleBank))
.Assert();
}
2018-04-13 17:19:50 +08:00
[Test]
public void TestGetJsonDecoder()
{
Decoder<Beatmap> decoder;
using (var stream = TestResources.OpenResource(normal))
using (var sr = new LineBufferedReader(stream))
{
var legacyDecoded = new LegacyBeatmapDecoder { ApplyOffsets = false }.Decode(sr);
2020-04-03 10:02:57 +08:00
using (var memStream = new MemoryStream())
using (var memWriter = new StreamWriter(memStream))
using (var memReader = new LineBufferedReader(memStream))
{
2020-04-03 10:02:57 +08:00
memWriter.Write(legacyDecoded.Serialize());
memWriter.Flush();
2020-04-03 10:02:57 +08:00
memStream.Position = 0;
decoder = Decoder.GetDecoder<Beatmap>(memReader);
}
}
Assert.IsInstanceOf(typeof(JsonBeatmapDecoder), decoder);
}
/// <summary>
2017-12-21 18:40:41 +08:00
/// Reads a .osu file first with a <see cref="LegacyBeatmapDecoder"/>, serializes the resulting <see cref="Beatmap"/> to JSON
2017-12-21 13:19:25 +08:00
/// and then deserializes the result back into a <see cref="Beatmap"/> through an <see cref="JsonBeatmapDecoder"/>.
/// </summary>
/// <param name="filename">The .osu file to decode.</param>
2017-12-21 18:40:41 +08:00
/// <returns>The <see cref="Beatmap"/> after being decoded by an <see cref="LegacyBeatmapDecoder"/>.</returns>
2018-04-20 13:41:57 +08:00
private Beatmap decodeAsJson(string filename)
{
decode(filename, out Beatmap jsonDecoded);
return jsonDecoded;
}
2018-04-13 17:19:50 +08:00
/// <summary>
2017-12-21 18:40:41 +08:00
/// Reads a .osu file first with a <see cref="LegacyBeatmapDecoder"/>, serializes the resulting <see cref="Beatmap"/> to JSON
2017-12-21 13:19:25 +08:00
/// and then deserializes the result back into a <see cref="Beatmap"/> through an <see cref="JsonBeatmapDecoder"/>.
/// </summary>
/// <param name="filename">The .osu file to decode.</param>
2018-04-20 13:41:57 +08:00
/// <param name="jsonDecoded">The <see cref="Beatmap"/> after being decoded by an <see cref="JsonBeatmapDecoder"/>.</param>
2017-12-21 18:40:41 +08:00
/// <returns>The <see cref="Beatmap"/> after being decoded by an <see cref="LegacyBeatmapDecoder"/>.</returns>
2018-04-20 13:41:57 +08:00
private Beatmap decode(string filename, out Beatmap jsonDecoded)
{
2019-01-28 17:19:57 +08:00
using (var stream = TestResources.OpenResource(filename))
using (var sr = new LineBufferedReader(stream))
{
2018-03-09 20:23:03 +08:00
var legacyDecoded = new LegacyBeatmapDecoder { ApplyOffsets = false }.Decode(sr);
2019-04-01 11:44:46 +08:00
using (var ms = new MemoryStream())
using (var sw = new StreamWriter(ms))
using (var sr2 = new LineBufferedReader(ms))
{
sw.Write(legacyDecoded.Serialize());
sw.Flush();
2018-04-13 17:19:50 +08:00
ms.Position = 0;
2018-04-20 13:41:57 +08:00
jsonDecoded = new JsonBeatmapDecoder().Decode(sr2);
return legacyDecoded;
}
}
}
}
}