2020-08-27 15:35:42 +00: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.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2020-08-27 15:35:42 +00:00
|
|
|
using System.Collections.Generic;
|
2021-12-04 15:47:53 +00:00
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
2020-08-27 15:35:42 +00:00
|
|
|
using System.Linq;
|
|
|
|
using NUnit.Framework;
|
2020-08-27 15:57:55 +00:00
|
|
|
using osu.Framework.Utils;
|
2020-08-27 15:35:42 +00:00
|
|
|
using osu.Game.Beatmaps;
|
2022-03-24 07:34:21 +00:00
|
|
|
using osu.Game.Beatmaps.Formats;
|
2021-12-04 15:47:53 +00:00
|
|
|
using osu.Game.Replays;
|
2020-08-27 15:35:42 +00:00
|
|
|
using osu.Game.Rulesets;
|
|
|
|
using osu.Game.Rulesets.Catch;
|
|
|
|
using osu.Game.Rulesets.Mania;
|
2022-01-19 05:25:40 +00:00
|
|
|
using osu.Game.Rulesets.Mania.Mods;
|
2020-08-27 15:35:42 +00:00
|
|
|
using osu.Game.Rulesets.Osu;
|
2021-12-04 15:47:53 +00:00
|
|
|
using osu.Game.Rulesets.Osu.Replays;
|
|
|
|
using osu.Game.Rulesets.Osu.UI;
|
|
|
|
using osu.Game.Rulesets.Replays;
|
2020-08-27 15:35:42 +00:00
|
|
|
using osu.Game.Rulesets.Scoring;
|
|
|
|
using osu.Game.Rulesets.Taiko;
|
2020-08-27 15:57:55 +00:00
|
|
|
using osu.Game.Scoring;
|
2020-08-27 15:35:42 +00:00
|
|
|
using osu.Game.Scoring.Legacy;
|
|
|
|
using osu.Game.Tests.Resources;
|
|
|
|
|
|
|
|
namespace osu.Game.Tests.Beatmaps.Formats
|
|
|
|
{
|
|
|
|
[TestFixture]
|
|
|
|
public class LegacyScoreDecoderTest
|
|
|
|
{
|
2021-12-04 15:47:53 +00:00
|
|
|
private CultureInfo originalCulture;
|
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void SetUp()
|
|
|
|
{
|
|
|
|
originalCulture = CultureInfo.CurrentCulture;
|
|
|
|
}
|
|
|
|
|
2020-08-27 15:35:42 +00:00
|
|
|
[Test]
|
|
|
|
public void TestDecodeManiaReplay()
|
|
|
|
{
|
|
|
|
var decoder = new TestLegacyScoreDecoder();
|
|
|
|
|
|
|
|
using (var resourceStream = TestResources.OpenResource("Replays/mania-replay.osr"))
|
|
|
|
{
|
|
|
|
var score = decoder.Parse(resourceStream);
|
|
|
|
|
2021-11-24 06:25:49 +00:00
|
|
|
Assert.AreEqual(3, score.ScoreInfo.Ruleset.OnlineID);
|
2020-08-27 15:35:42 +00:00
|
|
|
|
|
|
|
Assert.AreEqual(2, score.ScoreInfo.Statistics[HitResult.Great]);
|
|
|
|
Assert.AreEqual(1, score.ScoreInfo.Statistics[HitResult.Good]);
|
|
|
|
|
|
|
|
Assert.AreEqual(829_931, score.ScoreInfo.TotalScore);
|
|
|
|
Assert.AreEqual(3, score.ScoreInfo.MaxCombo);
|
2022-01-19 05:25:40 +00:00
|
|
|
|
|
|
|
Assert.IsTrue(score.ScoreInfo.Mods.Any(m => m is ManiaModClassic));
|
|
|
|
Assert.IsTrue(score.ScoreInfo.APIMods.Any(m => m.Acronym == "CL"));
|
|
|
|
Assert.IsTrue(score.ScoreInfo.ModsJson.Contains("CL"));
|
|
|
|
|
2020-08-27 15:57:55 +00:00
|
|
|
Assert.IsTrue(Precision.AlmostEquals(0.8889, score.ScoreInfo.Accuracy, 0.0001));
|
|
|
|
Assert.AreEqual(ScoreRank.B, score.ScoreInfo.Rank);
|
2020-08-27 15:35:42 +00:00
|
|
|
|
|
|
|
Assert.That(score.Replay.Frames, Is.Not.Empty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 07:39:56 +00:00
|
|
|
[TestCase(3, true)]
|
|
|
|
[TestCase(6, false)]
|
|
|
|
[TestCase(LegacyBeatmapDecoder.LATEST_VERSION, false)]
|
|
|
|
public void TestLegacyBeatmapReplayOffsetsDecode(int beatmapVersion, bool offsetApplied)
|
|
|
|
{
|
|
|
|
const double first_frame_time = 48;
|
|
|
|
const double second_frame_time = 65;
|
|
|
|
|
|
|
|
var decoder = new TestLegacyScoreDecoder(beatmapVersion);
|
|
|
|
|
|
|
|
using (var resourceStream = TestResources.OpenResource("Replays/mania-replay.osr"))
|
|
|
|
{
|
|
|
|
var score = decoder.Parse(resourceStream);
|
|
|
|
|
2022-03-24 07:43:41 +00:00
|
|
|
Assert.That(score.Replay.Frames[0].Time, Is.EqualTo(first_frame_time + (offsetApplied ? LegacyBeatmapDecoder.EARLY_VERSION_TIMING_OFFSET : 0)));
|
|
|
|
Assert.That(score.Replay.Frames[1].Time, Is.EqualTo(second_frame_time + (offsetApplied ? LegacyBeatmapDecoder.EARLY_VERSION_TIMING_OFFSET : 0)));
|
2022-03-24 07:39:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 07:34:21 +00:00
|
|
|
[TestCase(3)]
|
|
|
|
[TestCase(6)]
|
|
|
|
[TestCase(LegacyBeatmapDecoder.LATEST_VERSION)]
|
2022-03-24 07:39:56 +00:00
|
|
|
public void TestLegacyBeatmapReplayOffsetsEncodeDecode(int beatmapVersion)
|
2022-03-24 07:34:21 +00:00
|
|
|
{
|
|
|
|
const double first_frame_time = 2000;
|
|
|
|
const double second_frame_time = 3000;
|
|
|
|
|
|
|
|
var ruleset = new OsuRuleset().RulesetInfo;
|
|
|
|
var scoreInfo = TestResources.CreateTestScoreInfo(ruleset);
|
|
|
|
var beatmap = new TestBeatmap(ruleset)
|
|
|
|
{
|
|
|
|
BeatmapInfo =
|
|
|
|
{
|
|
|
|
BeatmapVersion = beatmapVersion
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var score = new Score
|
|
|
|
{
|
|
|
|
ScoreInfo = scoreInfo,
|
|
|
|
Replay = new Replay
|
|
|
|
{
|
|
|
|
Frames = new List<ReplayFrame>
|
|
|
|
{
|
|
|
|
new OsuReplayFrame(first_frame_time, OsuPlayfield.BASE_SIZE / 2, OsuAction.LeftButton),
|
|
|
|
new OsuReplayFrame(second_frame_time, OsuPlayfield.BASE_SIZE / 2, OsuAction.LeftButton)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-24 08:00:49 +00:00
|
|
|
var decodedAfterEncode = encodeThenDecode(beatmapVersion, score, beatmap);
|
2022-03-24 07:34:21 +00:00
|
|
|
|
|
|
|
Assert.That(decodedAfterEncode.Replay.Frames[0].Time, Is.EqualTo(first_frame_time));
|
|
|
|
Assert.That(decodedAfterEncode.Replay.Frames[1].Time, Is.EqualTo(second_frame_time));
|
|
|
|
}
|
|
|
|
|
2021-12-04 15:47:53 +00:00
|
|
|
[Test]
|
|
|
|
public void TestCultureInvariance()
|
|
|
|
{
|
|
|
|
var ruleset = new OsuRuleset().RulesetInfo;
|
2021-12-13 07:34:48 +00:00
|
|
|
var scoreInfo = TestResources.CreateTestScoreInfo(ruleset);
|
2021-12-04 15:47:53 +00:00
|
|
|
var beatmap = new TestBeatmap(ruleset);
|
|
|
|
var score = new Score
|
|
|
|
{
|
|
|
|
ScoreInfo = scoreInfo,
|
|
|
|
Replay = new Replay
|
|
|
|
{
|
|
|
|
Frames = new List<ReplayFrame>
|
|
|
|
{
|
|
|
|
new OsuReplayFrame(2000, OsuPlayfield.BASE_SIZE / 2, OsuAction.LeftButton)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// the "se" culture is used here, as it encodes the negative number sign as U+2212 MINUS SIGN,
|
|
|
|
// rather than the classic ASCII U+002D HYPHEN-MINUS.
|
|
|
|
CultureInfo.CurrentCulture = new CultureInfo("se");
|
|
|
|
|
2022-03-24 08:00:49 +00:00
|
|
|
var decodedAfterEncode = encodeThenDecode(LegacyBeatmapDecoder.LATEST_VERSION, score, beatmap);
|
2021-12-04 15:47:53 +00:00
|
|
|
|
|
|
|
Assert.Multiple(() =>
|
|
|
|
{
|
|
|
|
Assert.That(decodedAfterEncode, Is.Not.Null);
|
|
|
|
|
|
|
|
Assert.That(decodedAfterEncode.ScoreInfo.User.Username, Is.EqualTo(scoreInfo.User.Username));
|
|
|
|
Assert.That(decodedAfterEncode.ScoreInfo.Ruleset, Is.EqualTo(scoreInfo.Ruleset));
|
|
|
|
Assert.That(decodedAfterEncode.ScoreInfo.TotalScore, Is.EqualTo(scoreInfo.TotalScore));
|
|
|
|
Assert.That(decodedAfterEncode.ScoreInfo.MaxCombo, Is.EqualTo(scoreInfo.MaxCombo));
|
|
|
|
Assert.That(decodedAfterEncode.ScoreInfo.Date, Is.EqualTo(scoreInfo.Date));
|
|
|
|
|
|
|
|
Assert.That(decodedAfterEncode.Replay.Frames.Count, Is.EqualTo(1));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-24 08:00:49 +00:00
|
|
|
private static Score encodeThenDecode(int beatmapVersion, Score score, TestBeatmap beatmap)
|
|
|
|
{
|
|
|
|
var encodeStream = new MemoryStream();
|
|
|
|
|
|
|
|
var encoder = new LegacyScoreEncoder(score, beatmap);
|
|
|
|
encoder.Encode(encodeStream);
|
|
|
|
|
|
|
|
var decodeStream = new MemoryStream(encodeStream.GetBuffer());
|
|
|
|
|
|
|
|
var decoder = new TestLegacyScoreDecoder(beatmapVersion);
|
|
|
|
var decodedAfterEncode = decoder.Parse(decodeStream);
|
|
|
|
return decodedAfterEncode;
|
|
|
|
}
|
|
|
|
|
2021-12-04 15:47:53 +00:00
|
|
|
[TearDown]
|
|
|
|
public void TearDown()
|
|
|
|
{
|
|
|
|
CultureInfo.CurrentCulture = originalCulture;
|
|
|
|
}
|
|
|
|
|
2020-08-27 15:35:42 +00:00
|
|
|
private class TestLegacyScoreDecoder : LegacyScoreDecoder
|
|
|
|
{
|
2022-03-24 07:34:21 +00:00
|
|
|
private readonly int beatmapVersion;
|
|
|
|
|
2020-08-27 15:35:42 +00:00
|
|
|
private static readonly Dictionary<int, Ruleset> rulesets = new Ruleset[]
|
|
|
|
{
|
|
|
|
new OsuRuleset(),
|
|
|
|
new TaikoRuleset(),
|
|
|
|
new CatchRuleset(),
|
|
|
|
new ManiaRuleset()
|
|
|
|
}.ToDictionary(ruleset => ((ILegacyRuleset)ruleset).LegacyID);
|
|
|
|
|
2022-03-24 07:34:21 +00:00
|
|
|
public TestLegacyScoreDecoder(int beatmapVersion = LegacyBeatmapDecoder.LATEST_VERSION)
|
|
|
|
{
|
|
|
|
this.beatmapVersion = beatmapVersion;
|
|
|
|
}
|
|
|
|
|
2020-08-27 15:35:42 +00:00
|
|
|
protected override Ruleset GetRuleset(int rulesetId) => rulesets[rulesetId];
|
|
|
|
|
|
|
|
protected override WorkingBeatmap GetBeatmap(string md5Hash) => new TestWorkingBeatmap(new Beatmap
|
|
|
|
{
|
|
|
|
BeatmapInfo = new BeatmapInfo
|
|
|
|
{
|
|
|
|
MD5Hash = md5Hash,
|
|
|
|
Ruleset = new OsuRuleset().RulesetInfo,
|
2022-03-24 07:34:21 +00:00
|
|
|
Difficulty = new BeatmapDifficulty(),
|
|
|
|
BeatmapVersion = beatmapVersion,
|
2020-08-27 15:35:42 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|