2019-12-10 19:44:45 +08:00
// 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.
2020-04-21 13:47:12 +08:00
using System ;
2020-04-21 13:49:31 +08:00
using System.Collections ;
2019-12-13 18:00:28 +08:00
using System.Collections.Generic ;
2019-12-10 19:44:45 +08:00
using System.IO ;
using System.Linq ;
using NUnit.Framework ;
2020-04-21 13:47:12 +08:00
using osu.Framework.Audio.Track ;
using osu.Framework.Graphics.Textures ;
2019-12-10 19:44:45 +08:00
using osu.Game.Beatmaps ;
2020-04-21 13:49:31 +08:00
using osu.Game.Beatmaps.ControlPoints ;
2019-12-10 19:44:45 +08:00
using osu.Game.Beatmaps.Formats ;
using osu.Game.IO ;
2019-12-13 18:00:28 +08:00
using osu.Game.IO.Serialization ;
2020-04-21 13:47:12 +08:00
using osu.Game.Rulesets.Catch ;
using osu.Game.Rulesets.Mania ;
using osu.Game.Rulesets.Osu ;
using osu.Game.Rulesets.Taiko ;
2019-12-10 19:44:45 +08:00
using osu.Game.Tests.Resources ;
namespace osu.Game.Tests.Beatmaps.Formats
{
[TestFixture]
public class LegacyBeatmapEncoderTest
{
2019-12-13 18:00:28 +08:00
private static IEnumerable < string > allBeatmaps = > TestResources . GetStore ( ) . GetAvailableResources ( ) . Where ( res = > res . EndsWith ( ".osu" ) ) ;
2019-12-10 19:44:45 +08:00
2019-12-13 18:00:28 +08:00
[TestCaseSource(nameof(allBeatmaps))]
2020-04-21 13:48:32 +08:00
public void TestBeatmap ( string name )
2019-12-10 19:44:45 +08:00
{
2020-04-21 13:48:32 +08:00
var decoded = decode ( name , out var encoded ) ;
2019-12-10 19:44:45 +08:00
2020-04-21 13:49:31 +08:00
sort ( decoded ) ;
sort ( encoded ) ;
2019-12-13 18:00:28 +08:00
Assert . That ( encoded . Serialize ( ) , Is . EqualTo ( decoded . Serialize ( ) ) ) ;
2019-12-10 19:44:45 +08:00
}
2020-04-21 13:49:31 +08:00
private void sort ( IBeatmap beatmap )
{
// Sort control points to ensure a sane ordering, as they may be parsed in different orders. This works because each group contains only uniquely-typed control points.
foreach ( var g in beatmap . ControlPointInfo . Groups )
{
ArrayList . Adapter ( ( IList ) g . ControlPoints ) . Sort (
Comparer < ControlPoint > . Create ( ( c1 , c2 ) = > string . Compare ( c1 . GetType ( ) . ToString ( ) , c2 . GetType ( ) . ToString ( ) , StringComparison . Ordinal ) ) ) ;
}
}
2020-04-21 13:47:12 +08:00
private IBeatmap decode ( string filename , out IBeatmap encoded )
2019-12-10 19:44:45 +08:00
{
2020-04-21 13:47:12 +08:00
using ( var stream = TestResources . GetStore ( ) . GetStream ( filename ) )
2019-12-10 19:44:45 +08:00
using ( var sr = new LineBufferedReader ( stream ) )
{
2020-04-21 13:47:12 +08:00
var legacyDecoded = convert ( new LegacyBeatmapDecoder { ApplyOffsets = false } . Decode ( sr ) ) ;
2019-12-10 19:44:45 +08:00
using ( var ms = new MemoryStream ( ) )
using ( var sw = new StreamWriter ( ms ) )
2020-04-21 13:47:12 +08:00
using ( var sr2 = new LineBufferedReader ( ms , true ) )
2019-12-10 19:44:45 +08:00
{
new LegacyBeatmapEncoder ( legacyDecoded ) . Encode ( sw ) ;
2020-04-21 13:47:12 +08:00
sw . Flush ( ) ;
2019-12-10 19:44:45 +08:00
ms . Position = 0 ;
2020-04-21 13:47:12 +08:00
encoded = convert ( new LegacyBeatmapDecoder { ApplyOffsets = false } . Decode ( sr2 ) ) ;
2019-12-10 19:44:45 +08:00
return legacyDecoded ;
}
}
}
2020-04-21 13:47:12 +08:00
private IBeatmap convert ( IBeatmap beatmap )
{
switch ( beatmap . BeatmapInfo . RulesetID )
{
case 0 :
beatmap . BeatmapInfo . Ruleset = new OsuRuleset ( ) . RulesetInfo ;
break ;
case 1 :
beatmap . BeatmapInfo . Ruleset = new TaikoRuleset ( ) . RulesetInfo ;
break ;
case 2 :
beatmap . BeatmapInfo . Ruleset = new CatchRuleset ( ) . RulesetInfo ;
break ;
case 3 :
beatmap . BeatmapInfo . Ruleset = new ManiaRuleset ( ) . RulesetInfo ;
break ;
}
return new TestWorkingBeatmap ( beatmap ) . GetPlayableBeatmap ( beatmap . BeatmapInfo . Ruleset ) ;
}
private class TestWorkingBeatmap : WorkingBeatmap
{
private readonly IBeatmap beatmap ;
public TestWorkingBeatmap ( IBeatmap beatmap )
: base ( beatmap . BeatmapInfo , null )
{
this . beatmap = beatmap ;
}
protected override IBeatmap GetBeatmap ( ) = > beatmap ;
protected override Texture GetBackground ( ) = > throw new NotImplementedException ( ) ;
protected override Track GetTrack ( ) = > throw new NotImplementedException ( ) ;
}
2019-12-10 19:44:45 +08:00
}
}