1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-20 21:43:21 +08:00
osu-lazer/osu.Game.Tests/Beatmaps/IO/LegacyBeatmapExporterTest.cs

107 lines
4.7 KiB
C#
Raw Normal View History

2024-11-13 21:09:19 +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.
using System.IO;
using System.Text;
2024-11-13 21:09:19 +08:00
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Extensions;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.IO.Archives;
2024-11-13 21:09:19 +08:00
using osu.Game.Tests.Resources;
using osu.Game.Tests.Visual;
using MemoryStream = System.IO.MemoryStream;
namespace osu.Game.Tests.Beatmaps.IO
{
[HeadlessTest]
public partial class LegacyBeatmapExporterTest : OsuTestScene
{
[Resolved]
private BeatmapManager beatmaps { get; set; } = null!;
[Test]
public void TestObjectsSnappedAfterTruncatingExport()
{
IWorkingBeatmap beatmap = null!;
MemoryStream outStream = null!;
// Ensure importer encoding is correct
AddStep("import beatmap", () => beatmap = importBeatmapFromArchives(@"decimal-timing-beatmap.olz"));
2024-11-14 21:24:11 +08:00
AddAssert("timing point has decimal offset", () => beatmap.Beatmap.ControlPointInfo.TimingPoints[0].Time, () => Is.EqualTo(284.725).Within(0.001));
AddAssert("kiai has decimal offset", () => beatmap.Beatmap.ControlPointInfo.EffectPoints[0].Time, () => Is.EqualTo(28520.019).Within(0.001));
AddAssert("hit object has decimal offset", () => beatmap.Beatmap.HitObjects[0].StartTime, () => Is.EqualTo(28520.019).Within(0.001));
2024-11-13 21:09:19 +08:00
// Ensure exporter legacy conversion is correct
AddStep("export", () =>
{
outStream = new MemoryStream();
new LegacyBeatmapExporter(LocalStorage)
.ExportToStream((BeatmapSetInfo)beatmap.BeatmapInfo.BeatmapSet!, outStream, null);
});
AddStep("import beatmap again", () => beatmap = importBeatmapFromStream(outStream));
2024-11-14 21:24:11 +08:00
AddAssert("timing point has truncated offset", () => beatmap.Beatmap.ControlPointInfo.TimingPoints[0].Time, () => Is.EqualTo(284).Within(0.001));
AddAssert("kiai is snapped", () => beatmap.Beatmap.ControlPointInfo.EffectPoints[0].Time, () => Is.EqualTo(28519).Within(0.001));
AddAssert("hit object is snapped", () => beatmap.Beatmap.HitObjects[0].StartTime, () => Is.EqualTo(28519).Within(0.001));
2024-11-13 21:09:19 +08:00
}
[Test]
public void TestExportStability()
{
IWorkingBeatmap beatmap = null!;
MemoryStream firstExport = null!;
MemoryStream secondExport = null!;
// Ensure importer encoding is correct
AddStep("import beatmap", () => beatmap = importBeatmapFromArchives(@"legacy-export-stability-test.olz"));
AddStep("export once", () =>
{
firstExport = new MemoryStream();
new LegacyBeatmapExporter(LocalStorage)
.ExportToStream((BeatmapSetInfo)beatmap.BeatmapInfo.BeatmapSet!, firstExport, null);
});
AddStep("import beatmap again", () => beatmap = importBeatmapFromStream(firstExport));
AddStep("export again", () =>
{
secondExport = new MemoryStream();
new LegacyBeatmapExporter(LocalStorage)
.ExportToStream((BeatmapSetInfo)beatmap.BeatmapInfo.BeatmapSet!, secondExport, null);
});
const string osu_filename = @"legacy export - stability test (spaceman_atlas) [].osu";
AddAssert("exports are identical",
() => getStringContentsOf(osu_filename, firstExport.GetBuffer()),
() => Is.EqualTo(getStringContentsOf(osu_filename, secondExport.GetBuffer())));
string getStringContentsOf(string filename, byte[] archiveBytes)
{
using var memoryStream = new MemoryStream(archiveBytes);
using var archiveReader = new ZipArchiveReader(memoryStream);
byte[] fileContent = archiveReader.GetStream(filename).ReadAllBytesToArray();
return Encoding.UTF8.GetString(fileContent);
}
}
2024-11-13 21:09:19 +08:00
private IWorkingBeatmap importBeatmapFromStream(Stream stream)
{
var imported = beatmaps.Import(new ImportTask(stream, "filename.osz")).GetResultSafely();
return imported.AsNonNull().PerformRead(s => beatmaps.GetWorkingBeatmap(s.Beatmaps[0]));
}
private IWorkingBeatmap importBeatmapFromArchives(string filename)
{
var imported = beatmaps.Import(new ImportTask(TestResources.OpenResource($@"Archives/{filename}"), filename)).GetResultSafely();
return imported.AsNonNull().PerformRead(s => beatmaps.GetWorkingBeatmap(s.Beatmaps[0]));
}
}
}