1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 20:47:28 +08:00

legacy export broken

This commit is contained in:
OliBomby 2023-07-11 02:30:16 +02:00
parent f8289927d2
commit 06e5ef88c0
6 changed files with 87 additions and 0 deletions

View File

@ -18,12 +18,15 @@ using osu.Framework.Platform;
using osu.Game.Beatmaps.Formats;
using osu.Game.Database;
using osu.Game.Extensions;
using osu.Game.IO;
using osu.Game.IO.Archives;
using osu.Game.Models;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Overlays.Notifications;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Types;
using osu.Game.Skinning;
using osu.Game.Utils;
@ -400,6 +403,56 @@ namespace osu.Game.Beatmaps
public Task Export(BeatmapSetInfo beatmap) => beatmapExporter.ExportAsync(beatmap.ToLive(Realm));
/// <summary>
/// Creates a copy of the <see cref="BeatmapSetInfo"/> and converts all beatmaps to legacy format, then exports it as a legacy package.
/// </summary>
/// <param name="beatmap"></param>
/// <returns></returns>
public Task ExportLegacy(BeatmapSetInfo beatmap)
{
var copy = beatmap.Clone(); // does the detach from realm
// convert all beatmaps to legacy format
foreach (var beatmapInfo in copy.Beatmaps)
{
// Convert beatmap
var file = beatmapInfo.File;
if (file == null)
continue;
using var oldStream = new LineBufferedReader(ReadFile(file));
var beatmapContent = new LegacyBeatmapDecoder().Decode(oldStream);
foreach (var controlPoint in beatmapContent.ControlPointInfo.AllControlPoints)
controlPoint.Time = Math.Floor(controlPoint.Time);
foreach (var hitObject in beatmapContent.HitObjects)
{
hitObject.StartTime = Math.Floor(hitObject.StartTime);
if (hitObject is IHasPath hasPath && BezierConverter.CountSegments(hasPath.Path.ControlPoints) > 1)
{
var newControlPoints = BezierConverter.ConvertToModernBezier(hasPath.Path.ControlPoints);
hasPath.Path.ControlPoints.Clear();
hasPath.Path.ControlPoints.AddRange(newControlPoints);
}
}
using var newStream = new MemoryStream();
using var sw = new StreamWriter(newStream, Encoding.UTF8, 1024, true);
new LegacyBeatmapEncoder(beatmapContent, null).Encode(sw);
newStream.Seek(0, SeekOrigin.Begin);
beatmapInfo.MD5Hash = newStream.ComputeMD5Hash();
beatmapInfo.Hash = newStream.ComputeSHA2Hash();
AddFile(copy, newStream, file.Filename);
}
return beatmapExporter.ExportAsync(new RealmLiveUnmanaged<BeatmapSetInfo>(copy));
}
private void updateHashAndMarkDirty(BeatmapSetInfo setInfo)
{
setInfo.Hash = beatmapImporter.ComputeHash(setInfo);

View File

@ -90,6 +90,19 @@ namespace osu.Game.Beatmaps
return ID == other.ID;
}
public BeatmapSetInfo Clone()
{
var clone = (BeatmapSetInfo)this.Detach().MemberwiseClone();
for (int i = 0; i < clone.Beatmaps.Count; i++)
clone.Beatmaps[i] = clone.Beatmaps[i].Clone();
for (int i = 0; i < clone.Files.Count; i++)
clone.Files[i] = new RealmNamedFileUsage(clone.Files[i].File, clone.Files[i].Filename);
return clone;
}
public override string ToString() => Metadata.GetDisplayString();
public bool Equals(IBeatmapSetInfo? other) => other is BeatmapSetInfo b && Equals(b);

View File

@ -33,6 +33,8 @@ namespace osu.Game.Database
Realm = realm;
}
public Stream ReadFile(RealmNamedFileUsage file) => realmFileStore.Storage.GetStream(file.File.GetStoragePath());
public void DeleteFile(TModel item, RealmNamedFileUsage file) =>
performFileOperation(item, managed => DeleteFile(managed, managed.Files.First(f => f.Filename == file.Filename), managed.Realm));

View File

@ -39,6 +39,11 @@ namespace osu.Game.Localisation
/// </summary>
public static LocalisableString ExportPackage => new TranslatableString(getKey(@"export_package"), @"Export package");
/// <summary>
/// "Export legacy package"
/// </summary>
public static LocalisableString ExportLegacyPackage => new TranslatableString(getKey(@"export_legacy_package"), @"Export legacy package");
/// <summary>
/// "Create new difficulty"
/// </summary>

View File

@ -39,6 +39,13 @@ namespace osu.Game.Rulesets.Objects
new[] { new Vector2d(1, 0), new Vector2d(1, 1.2447058f), new Vector2d(-0.8526471f, 2.118367f), new Vector2d(-2.6211002f, 7.854936e-06f), new Vector2d(-0.8526448f, -2.118357f), new Vector2d(1, -1.2447058f), new Vector2d(1, 0) })
};
/// <summary>
/// Counts the number of segments in a slider path.
/// </summary>
/// <param name="controlPoints">The control points of the path.</param>
/// <returns>The number of segments in a slider path.</returns>
public static int CountSegments(IList<PathControlPoint> controlPoints) => controlPoints.Where((t, i) => t.Type != null && i < controlPoints.Count - 1).Count();
/// <summary>
/// Converts a slider path to bezier control point positions compatible with the legacy osu! client.
/// </summary>

View File

@ -966,6 +966,7 @@ namespace osu.Game.Screens.Edit
{
new EditorMenuItem(WebCommonStrings.ButtonsSave, MenuItemType.Standard, () => Save()),
new EditorMenuItem(EditorStrings.ExportPackage, MenuItemType.Standard, exportBeatmap) { Action = { Disabled = !RuntimeInfo.IsDesktop } },
new EditorMenuItem(EditorStrings.ExportLegacyPackage, MenuItemType.Standard, exportLegacyBeatmap) { Action = { Disabled = !RuntimeInfo.IsDesktop } },
new EditorMenuItemSpacer(),
createDifficultyCreationMenu(),
createDifficultySwitchMenu(),
@ -981,6 +982,12 @@ namespace osu.Game.Screens.Edit
beatmapManager.Export(Beatmap.Value.BeatmapSetInfo);
}
private void exportLegacyBeatmap()
{
Save();
beatmapManager.ExportLegacy(Beatmap.Value.BeatmapSetInfo);
}
/// <summary>
/// Beatmaps of the currently edited set, grouped by ruleset and ordered by difficulty.
/// </summary>