2021-11-25 07:36:30 +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.
|
|
|
|
|
2023-07-12 22:20:01 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
2021-11-25 07:36:30 +00:00
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Game.Beatmaps;
|
2023-07-12 22:20:01 +00: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 07:36:30 +00:00
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
2023-07-12 12:49:49 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Exporter for osu!stable legacy beatmap archives.
|
2023-07-12 22:20:01 +00:00
|
|
|
/// Converts all beatmaps in the set to legacy format and exports it as a legacy package.
|
2023-07-12 12:49:49 +00:00
|
|
|
/// </summary>
|
2022-12-15 12:39:48 +00:00
|
|
|
public class LegacyBeatmapExporter : LegacyArchiveExporter<BeatmapSetInfo>
|
2021-11-25 07:36:30 +00:00
|
|
|
{
|
2023-04-09 13:15:00 +00:00
|
|
|
public LegacyBeatmapExporter(Storage storage)
|
|
|
|
: base(storage)
|
2021-11-25 07:36:30 +00:00
|
|
|
{
|
|
|
|
}
|
2022-11-21 09:58:01 +00:00
|
|
|
|
2023-07-12 22:20:01 +00:00
|
|
|
protected override Stream? GetFileContents(BeatmapSetInfo model, INamedFileUsage file)
|
|
|
|
{
|
|
|
|
bool isBeatmap = model.Beatmaps.Any(o => o.Hash == file.File.Hash);
|
|
|
|
|
|
|
|
if (!isBeatmap)
|
|
|
|
return base.GetFileContents(model, file);
|
|
|
|
|
|
|
|
// Read the beatmap contents and skin
|
2023-07-18 10:40:48 +00:00
|
|
|
using var contentStream = base.GetFileContents(model, file);
|
2023-07-12 22:20:01 +00:00
|
|
|
|
|
|
|
if (contentStream == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
using var contentStreamReader = new LineBufferedReader(contentStream);
|
|
|
|
var beatmapContent = new LegacyBeatmapDecoder().Decode(contentStreamReader);
|
|
|
|
|
2023-07-18 10:40:48 +00:00
|
|
|
using var skinStream = base.GetFileContents(model, file);
|
2023-07-12 22:20:01 +00:00
|
|
|
using var skinStreamReader = new LineBufferedReader(contentStream);
|
|
|
|
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
|
|
|
|
foreach (var controlPoint in beatmapContent.ControlPointInfo.AllControlPoints)
|
|
|
|
controlPoint.Time = Math.Floor(controlPoint.Time);
|
|
|
|
|
|
|
|
foreach (var hitObject in beatmapContent.HitObjects)
|
|
|
|
{
|
2023-07-18 10:28:35 +00:00
|
|
|
// Truncate end time before truncating start time because end time is dependent on start time
|
2023-07-18 10:18:43 +00:00
|
|
|
if (hitObject is IHasDuration hasDuration && hitObject is not IHasPath)
|
2023-07-18 10:28:35 +00:00
|
|
|
hasDuration.Duration = Math.Floor(hasDuration.EndTime) - Math.Floor(hitObject.StartTime);
|
|
|
|
|
|
|
|
hitObject.StartTime = Math.Floor(hitObject.StartTime);
|
2023-07-18 10:18:43 +00:00
|
|
|
|
2023-07-12 22:20:01 +00:00
|
|
|
if (hitObject is not IHasPath hasPath || BezierConverter.CountSegments(hasPath.Path.ControlPoints) <= 1) continue;
|
|
|
|
|
|
|
|
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))
|
|
|
|
new LegacyBeatmapEncoder(beatmapContent, beatmapSkin).Encode(sw);
|
|
|
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2023-05-06 17:42:28 +00:00
|
|
|
protected override string FileExtension => @".osz";
|
2021-11-25 07:36:30 +00:00
|
|
|
}
|
|
|
|
}
|