2021-11-25 15:36:30 +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.
|
|
|
|
|
2023-07-13 06:20:01 +08:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
2021-11-25 15:36:30 +08:00
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Game.Beatmaps;
|
2023-07-13 06:20:01 +08:00
|
|
|
using osu.Game.Beatmaps.Formats;
|
|
|
|
using osu.Game.IO;
|
|
|
|
using osu.Game.Rulesets.Objects;
|
|
|
|
using osu.Game.Rulesets.Objects.Types;
|
|
|
|
using osu.Game.Skinning;
|
|
|
|
using osuTK;
|
2021-11-25 15:36:30 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
2023-07-12 20:49:49 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Exporter for osu!stable legacy beatmap archives.
|
2023-07-13 06:20:01 +08:00
|
|
|
/// Converts all beatmaps in the set to legacy format and exports it as a legacy package.
|
2023-07-12 20:49:49 +08:00
|
|
|
/// </summary>
|
2022-12-15 20:39:48 +08:00
|
|
|
public class LegacyBeatmapExporter : LegacyArchiveExporter<BeatmapSetInfo>
|
2021-11-25 15:36:30 +08:00
|
|
|
{
|
2023-04-09 21:15:00 +08:00
|
|
|
public LegacyBeatmapExporter(Storage storage)
|
|
|
|
: base(storage)
|
2021-11-25 15:36:30 +08:00
|
|
|
{
|
|
|
|
}
|
2022-11-21 17:58:01 +08:00
|
|
|
|
2023-07-13 06:20:01 +08:00
|
|
|
protected override Stream? GetFileContents(BeatmapSetInfo model, INamedFileUsage file)
|
|
|
|
{
|
2023-08-12 06:50:31 +08:00
|
|
|
var beatmapInfo = model.Beatmaps.SingleOrDefault(o => o.Hash == file.File.Hash);
|
2023-07-13 06:20:01 +08:00
|
|
|
|
2023-08-12 06:50:31 +08:00
|
|
|
if (beatmapInfo == null)
|
2023-07-13 06:20:01 +08:00
|
|
|
return base.GetFileContents(model, file);
|
|
|
|
|
|
|
|
// Read the beatmap contents and skin
|
2023-07-18 18:40:48 +08:00
|
|
|
using var contentStream = base.GetFileContents(model, file);
|
2023-07-13 06:20:01 +08:00
|
|
|
|
|
|
|
if (contentStream == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
using var contentStreamReader = new LineBufferedReader(contentStream);
|
|
|
|
var beatmapContent = new LegacyBeatmapDecoder().Decode(contentStreamReader);
|
|
|
|
|
2023-08-12 06:50:31 +08:00
|
|
|
var workingBeatmap = new FlatWorkingBeatmap(beatmapContent);
|
|
|
|
var playableBeatmap = workingBeatmap.GetPlayableBeatmap(beatmapInfo.Ruleset);
|
|
|
|
|
2023-07-18 18:40:48 +08:00
|
|
|
using var skinStream = base.GetFileContents(model, file);
|
2023-07-18 19:08:05 +08:00
|
|
|
|
|
|
|
if (skinStream == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
using var skinStreamReader = new LineBufferedReader(skinStream);
|
2023-07-13 06:20:01 +08:00
|
|
|
var beatmapSkin = new LegacySkin(new SkinInfo(), null!)
|
|
|
|
{
|
|
|
|
Configuration = new LegacySkinDecoder().Decode(skinStreamReader)
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|
2023-08-12 06:50:31 +08:00
|
|
|
foreach (var controlPoint in playableBeatmap.ControlPointInfo.AllControlPoints)
|
2023-07-13 06:20:01 +08:00
|
|
|
controlPoint.Time = Math.Floor(controlPoint.Time);
|
|
|
|
|
2023-08-12 06:50:31 +08:00
|
|
|
foreach (var hitObject in playableBeatmap.HitObjects)
|
2023-07-13 06:20:01 +08:00
|
|
|
{
|
2023-07-18 18:28:35 +08:00
|
|
|
// Truncate end time before truncating start time because end time is dependent on start time
|
2023-07-18 18:18:43 +08:00
|
|
|
if (hitObject is IHasDuration hasDuration && hitObject is not IHasPath)
|
2023-07-18 18:28:35 +08:00
|
|
|
hasDuration.Duration = Math.Floor(hasDuration.EndTime) - Math.Floor(hitObject.StartTime);
|
|
|
|
|
|
|
|
hitObject.StartTime = Math.Floor(hitObject.StartTime);
|
2023-07-18 18:18:43 +08:00
|
|
|
|
2023-08-16 22:44:08 +08:00
|
|
|
if (hitObject is not IHasPath hasPath) continue;
|
|
|
|
|
2023-08-21 13:27:02 +08:00
|
|
|
// stable's hit object parsing expects the entire slider to use only one type of curve,
|
|
|
|
// and happens to use the last non-empty curve type read for the entire slider.
|
|
|
|
// this clear of the last control point type handles an edge case
|
|
|
|
// wherein the last control point of an otherwise-single-segment slider path has a different type than previous,
|
|
|
|
// which would lead to sliders being mangled when exported back to stable.
|
|
|
|
// normally, that would be handled by the `BezierConverter.ConvertToModernBezier()` call below,
|
|
|
|
// which outputs a slider path containing only Bezier control points,
|
|
|
|
// but a non-inherited last control point is (rightly) not considered to be starting a new segment,
|
|
|
|
// therefore it would fail to clear the `CountSegments() <= 1` check.
|
|
|
|
// by clearing explicitly we both fix the issue and avoid unnecessary conversions to Bezier.
|
2023-08-16 22:44:08 +08:00
|
|
|
if (hasPath.Path.ControlPoints.Count > 1)
|
|
|
|
hasPath.Path.ControlPoints[^1].Type = null;
|
|
|
|
|
|
|
|
if (BezierConverter.CountSegments(hasPath.Path.ControlPoints) <= 1) continue;
|
2023-07-13 06:20:01 +08:00
|
|
|
|
|
|
|
var newControlPoints = BezierConverter.ConvertToModernBezier(hasPath.Path.ControlPoints);
|
|
|
|
|
|
|
|
// Truncate control points to integer positions
|
|
|
|
foreach (var pathControlPoint in newControlPoints)
|
|
|
|
{
|
|
|
|
pathControlPoint.Position = new Vector2(
|
|
|
|
(float)Math.Floor(pathControlPoint.Position.X),
|
|
|
|
(float)Math.Floor(pathControlPoint.Position.Y));
|
|
|
|
}
|
|
|
|
|
|
|
|
hasPath.Path.ControlPoints.Clear();
|
|
|
|
hasPath.Path.ControlPoints.AddRange(newControlPoints);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode to legacy format
|
|
|
|
var stream = new MemoryStream();
|
|
|
|
using (var sw = new StreamWriter(stream, Encoding.UTF8, 1024, true))
|
2023-08-12 06:50:31 +08:00
|
|
|
new LegacyBeatmapEncoder(playableBeatmap, beatmapSkin).Encode(sw);
|
2023-07-13 06:20:01 +08:00
|
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2023-05-07 01:42:28 +08:00
|
|
|
protected override string FileExtension => @".osz";
|
2021-11-25 15:36:30 +08:00
|
|
|
}
|
|
|
|
}
|