add `LegacyExportManager`

This commit is contained in:
cdwcgt 2022-11-17 23:38:24 +09:00
parent e1a21e0cf9
commit 4b29941b47
No known key found for this signature in database
GPG Key ID: 144396D01095C3A2
11 changed files with 209 additions and 143 deletions

View File

@ -121,9 +121,9 @@ public Task TestImportExportedSkinFilename() => runSkinTest(async osu =>
var import1 = await loadSkinIntoOsu(osu, new ImportTask(createOskWithIni("name 1", "author 1"), "custom.osk"));
assertCorrectMetadata(import1, "name 1 [custom]", "author 1", osu);
import1.PerformRead(s =>
import1.PerformRead(async s =>
{
new LegacySkinExporter(osu.Dependencies.Get<Storage>(), osu.Dependencies.Get<INotificationOverlay>()).ExportModelTo(s, exportStream);
await new LegacyExportManager().ExportAsync(s, exportStream);
});
string exportFilename = import1.GetDisplayString();

View File

@ -3,16 +3,16 @@
using osu.Framework.Platform;
using osu.Game.Beatmaps;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
namespace osu.Game.Database
{
public class LegacyBeatmapExporter : LegacyExporter<BeatmapSetInfo>
public class LegacyBeatmapExporter : LegacyModelExporter<BeatmapSetInfo>
{
protected override string FileExtension => ".osz";
public LegacyBeatmapExporter(Storage storage, INotificationOverlay? notificationOverlay)
: base(storage, notificationOverlay)
public LegacyBeatmapExporter(Storage storage, RealmAccess realm, ProgressNotification notification)
: base(storage, realm, notification)
{
}
}

View File

@ -0,0 +1,55 @@
// 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.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Platform;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
using osu.Game.Scoring;
using osu.Game.Skinning;
namespace osu.Game.Database
{
[ExcludeFromDynamicCompile]
public class LegacyExportManager : Component
{
[Resolved]
private RealmAccess realmAccess { get; set; } = null!;
[Resolved]
private Storage exportStorage { get; set; } = null!;
[Resolved]
private INotificationOverlay? notifications { get; set; }
public async Task ExportAsync(IHasGuidPrimaryKey item)
{
var notification = new ProgressNotification
{
State = ProgressNotificationState.Active,
Text = "Exporting...",
CompletionText = "Export completed"
};
notifications?.Post(notification);
switch (item)
{
case SkinInfo:
await new LegacySkinExporter(exportStorage, realmAccess, notification).ExportASync(item);
break;
case ScoreInfo:
await new LegacyScoreExporter(exportStorage, realmAccess, notification).ExportASync(item, false);
break;
case BeatmapSetInfo:
await new LegacyBeatmapExporter(exportStorage, realmAccess, notification).ExportASync(item);
break;
}
}
}
}

View File

@ -1,102 +0,0 @@
// 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.Threading.Tasks;
using osu.Framework.Platform;
using osu.Game.Extensions;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
using SharpCompress.Archives.Zip;
namespace osu.Game.Database
{
/// <summary>
/// A class which handles exporting legacy user data of a single type from osu-stable.
/// </summary>
public abstract class LegacyExporter<TModel>
where TModel : class, IHasNamedFiles
{
/// <summary>
/// The file extension for exports (including the leading '.').
/// </summary>
protected abstract string FileExtension { get; }
protected readonly Storage UserFileStorage;
private readonly Storage exportStorage;
private readonly INotificationOverlay? notificationOverlay;
protected ProgressNotification Notification = null!;
private string filename = null!;
protected LegacyExporter(Storage storage, INotificationOverlay? notificationOverlay)
{
exportStorage = storage.GetStorageForDirectory(@"exports");
UserFileStorage = storage.GetStorageForDirectory(@"files");
this.notificationOverlay = notificationOverlay;
}
/// <summary>
/// Exports an item to a legacy (.zip based) package.
/// </summary>
/// <param name="item">The item to export.</param>
public void Export(TModel item)
{
filename = $"{item.GetDisplayString().GetValidFilename()}{FileExtension}";
Stream stream = exportStorage.CreateFileSafely(filename);
Notification = new ProgressNotification
{
State = ProgressNotificationState.Active,
Text = "Exporting...",
CompletionText = "Export completed"
};
Notification.CompletionClickAction += () => exportStorage.PresentFileExternally(filename);
Notification.CancelRequested += () =>
{
stream.Dispose();
return true;
};
ExportModelTo(item, stream);
notificationOverlay?.Post(Notification);
}
/// <summary>
/// Exports an item to the given output stream.
/// </summary>
/// <param name="model">The item to export.</param>
/// <param name="outputStream">The output stream to export to.</param>
public virtual void ExportModelTo(TModel model, Stream outputStream)
{
using (var archive = ZipArchive.Create())
{
foreach (var file in model.Files)
archive.AddEntry(file.Filename, UserFileStorage.GetStream(file.File.GetStoragePath()));
Task.Factory.StartNew(() =>
{
archive.SaveTo(outputStream);
}, Notification.CancellationToken).ContinueWith(t =>
{
if (t.IsCompletedSuccessfully)
{
outputStream.Dispose();
Notification.State = ProgressNotificationState.Completed;
}
else
{
if (Notification.State == ProgressNotificationState.Cancelled) return;
Notification.State = ProgressNotificationState.Cancelled;
Notification.Text = "Export Failed";
}
});
}
}
}
}

View File

@ -0,0 +1,113 @@
// 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;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Platform;
using osu.Game.Extensions;
using osu.Game.Overlays.Notifications;
using Realms;
using SharpCompress.Archives.Zip;
namespace osu.Game.Database
{
/// <summary>
/// A class which handles exporting legacy user data of a single type from osu-stable.
/// </summary>
public abstract class LegacyModelExporter<TModel>
where TModel : RealmObject
{
/// <summary>
/// The file extension for exports (including the leading '.').
/// </summary>
protected abstract string FileExtension { get; }
protected readonly Storage UserFileStorage;
private readonly Storage exportStorage;
private readonly RealmAccess realmAccess;
private readonly ProgressNotification notification;
protected ProgressNotification Notification = null!;
private string filename = null!;
protected LegacyModelExporter(Storage storage, RealmAccess realm, ProgressNotification notification)
{
exportStorage = storage.GetStorageForDirectory(@"exports");
UserFileStorage = storage.GetStorageForDirectory(@"files");
this.notification = notification;
realmAccess = realm;
}
public async Task ExportASync(IHasGuidPrimaryKey uuid, bool needZipArchive = true)
{
Guid id = uuid.ID;
await Task.Run(() =>
{
realmAccess.Run(r =>
{
if (r.Find<TModel>(id) is IHasNamedFiles model)
{
filename = $"{model.GetDisplayString().GetValidFilename()}{FileExtension}";
}
else
{
return;
}
using (var outputStream = exportStorage.CreateFileSafely(filename))
{
if (needZipArchive)
{
using (var archive = ZipArchive.Create())
{
float i = 0;
foreach (var file in model.Files)
{
if (notification.CancellationToken.IsCancellationRequested) return;
archive.AddEntry(file.Filename, UserFileStorage.GetStream(file.File.GetStoragePath()));
i++;
notification.Progress = i / model.Files.Count();
notification.Text = $"Exporting... ({i}/{model.Files.Count()})";
}
notification.Text = "Saving Zip Archive...";
archive.SaveTo(outputStream);
}
}
else
{
var file = model.Files.SingleOrDefault();
if (file == null)
return;
using (var inputStream = UserFileStorage.GetStream(file.File.GetStoragePath()))
inputStream.CopyTo(outputStream);
}
}
});
}).ContinueWith(t =>
{
if (t.IsFaulted)
{
notification.State = ProgressNotificationState.Cancelled;
return;
}
if (notification.CancellationToken.IsCancellationRequested)
{
return;
}
notification.CompletionText = "Export Complete, Click to open the folder";
notification.CompletionClickAction += () => exportStorage.PresentFileExternally(filename);
notification.State = ProgressNotificationState.Completed;
});
}
}
}

View File

@ -1,36 +1,32 @@
// 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.Linq;
using osu.Framework.Platform;
using osu.Game.Extensions;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
using osu.Game.Scoring;
namespace osu.Game.Database
{
public class LegacyScoreExporter : LegacyExporter<ScoreInfo>
public class LegacyScoreExporter : LegacyModelExporter<ScoreInfo>
{
protected override string FileExtension => ".osr";
public LegacyScoreExporter(Storage storage, INotificationOverlay? notificationOverlay)
: base(storage, notificationOverlay)
public LegacyScoreExporter(Storage storage, RealmAccess realm, ProgressNotification notification)
: base(storage, realm, notification)
{
}
public override void ExportModelTo(ScoreInfo model, Stream outputStream)
{
var file = model.Files.SingleOrDefault();
if (file == null)
return;
using (var inputStream = UserFileStorage.GetStream(file.File.GetStoragePath()))
inputStream.CopyTo(outputStream);
Notification.State = ProgressNotificationState.Completed;
outputStream.Dispose();
}
//public override void ExportModelTo(ScoreInfo model, Stream outputStream)
//{
// var file = model.Files.SingleOrDefault();
// if (file == null)
// return;
//
// using (var inputStream = UserFileStorage.GetStream(file.File.GetStoragePath()))
// inputStream.CopyTo(outputStream);
//
// Notification.State = ProgressNotificationState.Completed;
// outputStream.Dispose();
//}
}
}

View File

@ -2,17 +2,17 @@
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Platform;
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
using osu.Game.Skinning;
namespace osu.Game.Database
{
public class LegacySkinExporter : LegacyExporter<SkinInfo>
public class LegacySkinExporter : LegacyModelExporter<SkinInfo>
{
protected override string FileExtension => ".osk";
public LegacySkinExporter(Storage storage, INotificationOverlay? notificationOverlay)
: base(storage, notificationOverlay)
public LegacySkinExporter(Storage storage, RealmAccess realm, ProgressNotification notification)
: base(storage, realm, notification)
{
}
}

View File

@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
@ -76,7 +77,7 @@ public class LeaderboardScore : OsuClickableContainer, IHasContextMenu, IHasCust
private Storage storage { get; set; }
[Resolved]
private INotificationOverlay notificationOverlay { get; set; }
private LegacyExportManager exporter { get; set; }
public ITooltip<ScoreInfo> GetCustomTooltip() => new LeaderboardScoreTooltip();
public virtual ScoreInfo TooltipContent => Score;
@ -430,7 +431,7 @@ public MenuItem[] ContextMenuItems
if (Score.Files.Count > 0)
{
items.Add(new OsuMenuItem("Export", MenuItemType.Standard, () => new LegacyScoreExporter(storage, notificationOverlay).Export(Score)));
items.Add(new OsuMenuItem("Export", MenuItemType.Standard, () => Task.Run(() => exporter.ExportAsync(Score))));
items.Add(new OsuMenuItem(CommonStrings.ButtonsDelete, MenuItemType.Destructive, () => dialogOverlay?.Push(new LocalScoreDeleteDialog(Score))));
}

View File

@ -126,6 +126,9 @@ public class OsuGame : OsuGameBase, IKeyBindingHandler<GlobalAction>, ILocalUser
[Cached]
private readonly LegacyImportManager legacyImportManager = new LegacyImportManager();
[Cached]
private readonly LegacyExportManager legacyExportManager = new LegacyExportManager();
[Cached]
private readonly ScreenshotManager screenshotManager = new ScreenshotManager();
@ -868,6 +871,7 @@ protected override void LoadComplete()
}), rightFloatingOverlayContent.Add, true);
loadComponentSingleFile(legacyImportManager, Add);
loadComponentSingleFile(legacyExportManager, Add);
loadComponentSingleFile(screenshotManager, Add);

View File

@ -141,16 +141,14 @@ public class ExportSkinButton : SettingsButton
[Resolved]
private Storage storage { get; set; }
[CanBeNull]
private INotificationOverlay notificationOverlay;
[Resolved]
private LegacyExportManager exporter { get; set; }
private Bindable<Skin> currentSkin;
[BackgroundDependencyLoader]
private void load(INotificationOverlay notificationOverlay)
private void load()
{
this.notificationOverlay = notificationOverlay;
Text = SkinSettingsStrings.ExportSkinButton;
Action = export;
}
@ -167,7 +165,7 @@ private void export()
{
try
{
currentSkin.Value.SkinInfo.PerformRead(s => new LegacySkinExporter(storage, notificationOverlay).Export(s));
currentSkin.Value.SkinInfo.PerformRead(s => exporter.ExportAsync(s));
}
catch (Exception e)
{

View File

@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JetBrains.Annotations;
using osu.Framework;
using osu.Framework.Allocation;
@ -93,9 +94,6 @@ protected bool HasUnsavedChanges
[Resolved]
private Storage storage { get; set; }
[Resolved]
private INotificationOverlay notificationOverlay { get; set; }
[Resolved(canBeNull: true)]
private IDialogOverlay dialogOverlay { get; set; }
@ -185,6 +183,9 @@ protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnl
private Bindable<float> editorBackgroundDim;
private Bindable<bool> editorHitMarkers;
[Resolved]
private LegacyExportManager exporter { get; set; }
public Editor(EditorLoader loader = null)
{
this.loader = loader;
@ -941,7 +942,7 @@ private void updateLastSavedHash()
private void exportBeatmap()
{
Save();
new LegacyBeatmapExporter(storage, notificationOverlay).Export(Beatmap.Value.BeatmapSetInfo);
Task.Run(() => exporter.ExportAsync(Beatmap.Value.BeatmapSetInfo));
}
/// <summary>