Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix timing point truncation causing missnaps on compatibility-exported lazer beatmaps #30607

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions osu.Game.Tests/Beatmaps/IO/LegacyBeatmapExporterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.IO;
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.Tests.Resources;
using osu.Game.Tests.Visual;
using MemoryStream = System.IO.MemoryStream;

namespace osu.Game.Tests.Beatmaps.IO
{
[HeadlessTest]
public partial class LegacyBeatmapExporterTest : OsuTestScene
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering if this test would be better suited to the LegacyBeatmapEncoderTest class?

Copy link
Collaborator

@bdach bdach Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd actually prefer them to be here. Encoder shouldn't be caring about backcompat logic, encoder should be encoding, i.e. dumb formatting logic. This same location is where I'd also put legacy export stability tests that you asked for in relation to BSS and which we don't currently have.

{
[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"));
AddAssert("timing point has decimal offset", () => Math.Abs(beatmap.Beatmap.ControlPointInfo.TimingPoints[0].Time - 284.725) < 0.001);
AddAssert("kiai has decimal offset", () => Math.Abs(beatmap.Beatmap.ControlPointInfo.EffectPoints[0].Time - 28520.019) < 0.001);
AddAssert("hit object has decimal offset", () => Math.Abs(beatmap.Beatmap.HitObjects[0].StartTime - 28520.019) < 0.001);

// 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));
AddAssert("timing point has truncated offset", () => Math.Abs(beatmap.Beatmap.ControlPointInfo.TimingPoints[0].Time - 284) < 0.001);
AddAssert("kiai is snapped", () => Math.Abs(beatmap.Beatmap.ControlPointInfo.EffectPoints[0].Time - 28519) < 0.001);
AddAssert("hit object is snapped", () => Math.Abs(beatmap.Beatmap.HitObjects[0].StartTime - 28519) < 0.001);
}

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]));
}
}
}
Binary file not shown.
20 changes: 19 additions & 1 deletion osu.Game/Database/LegacyBeatmapExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,25 @@ public LegacyBeatmapExporter(Storage storage)
};

// Convert beatmap elements to be compatible with legacy format
// So we truncate time and position values to integers, and convert paths with multiple segments to bezier curves
// So we truncate time and position values to integers, and convert paths with multiple segments to Bézier curves

// We must first truncate all timing points and move all objects in the timing section with it to ensure everything stays snapped
for (int i = 0; i < playableBeatmap.ControlPointInfo.TimingPoints.Count; i++)
{
var timingPoint = playableBeatmap.ControlPointInfo.TimingPoints[i];
double offset = Math.Floor(timingPoint.Time) - timingPoint.Time;
double nextTimingPointTime = i + 1 < playableBeatmap.ControlPointInfo.TimingPoints.Count
? playableBeatmap.ControlPointInfo.TimingPoints[i + 1].Time
: double.PositiveInfinity;

// Offset all control points in the timing section (including the current one)
foreach (var controlPoint in playableBeatmap.ControlPointInfo.AllControlPoints.Where(o => o.Time >= timingPoint.Time && o.Time < nextTimingPointTime))
controlPoint.Time += offset;

// Offset all hit objects in the timing section
foreach (var hitObject in playableBeatmap.HitObjects.Where(o => o.StartTime >= timingPoint.Time && o.StartTime < nextTimingPointTime))
hitObject.StartTime += offset;
}

foreach (var controlPoint in playableBeatmap.ControlPointInfo.AllControlPoints)
controlPoint.Time = Math.Floor(controlPoint.Time);
Expand Down
Loading