From fc4a6cb125c50668765d8b43f5bcf88cb242f28c Mon Sep 17 00:00:00 2001 From: cdwcgt Date: Sat, 19 Nov 2022 01:02:35 +0900 Subject: [PATCH] working with test --- osu.Game.Tests/Skins/IO/ImportSkinTest.cs | 31 +++--- osu.Game/Database/LegacyBeatmapExporter.cs | 5 +- osu.Game/Database/LegacyExportManager.cs | 2 +- osu.Game/Database/LegacyModelExporter.cs | 106 +++++++++++---------- osu.Game/Database/LegacyScoreExporter.cs | 52 +++++++--- osu.Game/Database/LegacySkinExporter.cs | 5 +- 6 files changed, 113 insertions(+), 88 deletions(-) diff --git a/osu.Game.Tests/Skins/IO/ImportSkinTest.cs b/osu.Game.Tests/Skins/IO/ImportSkinTest.cs index ef68b06476..0c3c459e87 100644 --- a/osu.Game.Tests/Skins/IO/ImportSkinTest.cs +++ b/osu.Game.Tests/Skins/IO/ImportSkinTest.cs @@ -10,12 +10,11 @@ using System.Runtime.CompilerServices; using System.Threading.Tasks; using NUnit.Framework; using osu.Framework.Allocation; -using osu.Framework.Extensions; using osu.Framework.Platform; using osu.Game.Database; using osu.Game.Extensions; using osu.Game.IO; -using osu.Game.Overlays; +using osu.Game.Overlays.Notifications; using osu.Game.Skinning; using SharpCompress.Archives.Zip; @@ -121,9 +120,9 @@ namespace osu.Game.Tests.Skins.IO var import1 = await loadSkinIntoOsu(osu, new ImportTask(createOskWithIni("name 1", "author 1"), "custom.osk")); assertCorrectMetadata(import1, "name 1 [custom]", "author 1", osu); - import1.PerformRead(async s => + await import1.PerformRead(async s => { - await new LegacyExportManager().ExportAsync(s, exportStream); + await new LegacySkinExporter(osu.Dependencies.Get(), osu.Dependencies.Get(), new ProgressNotification(), exportStream).ExportASync(s); }); string exportFilename = import1.GetDisplayString(); @@ -190,7 +189,7 @@ namespace osu.Game.Tests.Skins.IO }); [Test] - public Task TestExportThenImportDefaultSkin() => runSkinTest(osu => + public Task TestExportThenImportDefaultSkin() => runSkinTest(async osu => { var skinManager = osu.Dependencies.Get(); @@ -200,30 +199,28 @@ namespace osu.Game.Tests.Skins.IO Guid originalSkinId = skinManager.CurrentSkinInfo.Value.ID; - skinManager.CurrentSkinInfo.Value.PerformRead(s => + await skinManager.CurrentSkinInfo.Value.PerformRead(async s => { Assert.IsFalse(s.Protected); Assert.AreEqual(typeof(ArgonSkin), s.CreateInstance(skinManager).GetType()); - new LegacySkinExporter(osu.Dependencies.Get(), osu.Dependencies.Get()).ExportModelTo(s, exportStream); + await new LegacySkinExporter(osu.Dependencies.Get(), osu.Dependencies.Get(), new ProgressNotification(), exportStream).ExportASync(s); Assert.Greater(exportStream.Length, 0); }); - var imported = skinManager.Import(new ImportTask(exportStream, "exported.osk")); + var imported = await skinManager.Import(new ImportTask(exportStream, "exported.osk")); - imported.GetResultSafely().PerformRead(s => + imported.PerformRead(s => { Assert.IsFalse(s.Protected); Assert.AreNotEqual(originalSkinId, s.ID); Assert.AreEqual(typeof(ArgonSkin), s.CreateInstance(skinManager).GetType()); }); - - return Task.CompletedTask; }); [Test] - public Task TestExportThenImportClassicSkin() => runSkinTest(osu => + public Task TestExportThenImportClassicSkin() => runSkinTest(async osu => { var skinManager = osu.Dependencies.Get(); @@ -235,26 +232,24 @@ namespace osu.Game.Tests.Skins.IO Guid originalSkinId = skinManager.CurrentSkinInfo.Value.ID; - skinManager.CurrentSkinInfo.Value.PerformRead(s => + await skinManager.CurrentSkinInfo.Value.PerformRead(async s => { Assert.IsFalse(s.Protected); Assert.AreEqual(typeof(DefaultLegacySkin), s.CreateInstance(skinManager).GetType()); - new LegacySkinExporter(osu.Dependencies.Get(), osu.Dependencies.Get()).ExportModelTo(s, exportStream); + await new LegacySkinExporter(osu.Dependencies.Get(), osu.Dependencies.Get(), new ProgressNotification(), exportStream).ExportASync(s); Assert.Greater(exportStream.Length, 0); }); - var imported = skinManager.Import(new ImportTask(exportStream, "exported.osk")); + var imported = await skinManager.Import(new ImportTask(exportStream, "exported.osk")); - imported.GetResultSafely().PerformRead(s => + imported.PerformRead(s => { Assert.IsFalse(s.Protected); Assert.AreNotEqual(originalSkinId, s.ID); Assert.AreEqual(typeof(DefaultLegacySkin), s.CreateInstance(skinManager).GetType()); }); - - return Task.CompletedTask; }); #endregion diff --git a/osu.Game/Database/LegacyBeatmapExporter.cs b/osu.Game/Database/LegacyBeatmapExporter.cs index 140ce43fbd..5505d141ee 100644 --- a/osu.Game/Database/LegacyBeatmapExporter.cs +++ b/osu.Game/Database/LegacyBeatmapExporter.cs @@ -1,6 +1,7 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System.IO; using osu.Framework.Platform; using osu.Game.Beatmaps; using osu.Game.Overlays.Notifications; @@ -11,8 +12,8 @@ namespace osu.Game.Database { protected override string FileExtension => ".osz"; - public LegacyBeatmapExporter(Storage storage, RealmAccess realm, ProgressNotification notification) - : base(storage, realm, notification) + public LegacyBeatmapExporter(Storage storage, RealmAccess realm, ProgressNotification notification, Stream? stream = null) + : base(storage, realm, notification, stream) { } } diff --git a/osu.Game/Database/LegacyExportManager.cs b/osu.Game/Database/LegacyExportManager.cs index 18cd93ea35..08594ab020 100644 --- a/osu.Game/Database/LegacyExportManager.cs +++ b/osu.Game/Database/LegacyExportManager.cs @@ -43,7 +43,7 @@ namespace osu.Game.Database break; case ScoreInfo: - await new LegacyScoreExporter(exportStorage, realmAccess, notification).ExportASync(item, false); + await new LegacyScoreExporter(exportStorage, realmAccess, notification).ExportASync(item); break; case BeatmapSetInfo: diff --git a/osu.Game/Database/LegacyModelExporter.cs b/osu.Game/Database/LegacyModelExporter.cs index d181226803..a4b0f7ba9d 100644 --- a/osu.Game/Database/LegacyModelExporter.cs +++ b/osu.Game/Database/LegacyModelExporter.cs @@ -2,6 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using System; +using System.IO; using System.Linq; using System.Threading.Tasks; using osu.Framework.Platform; @@ -25,89 +26,92 @@ namespace osu.Game.Database protected readonly Storage UserFileStorage; - private readonly Storage exportStorage; + protected readonly Storage ExportStorage; - private readonly RealmAccess realmAccess; + protected readonly RealmAccess RealmAccess; - private readonly ProgressNotification notification; + protected readonly ProgressNotification Notification; - protected ProgressNotification Notification = null!; + protected string Filename = null!; - private string filename = null!; + protected Stream? OutputStream; - protected LegacyModelExporter(Storage storage, RealmAccess realm, ProgressNotification notification) + protected bool ShouldDisposeStream; + + protected LegacyModelExporter(Storage storage, RealmAccess realm, ProgressNotification notification, Stream? stream = null) { - exportStorage = storage.GetStorageForDirectory(@"exports"); + ExportStorage = storage.GetStorageForDirectory(@"exports"); UserFileStorage = storage.GetStorageForDirectory(@"files"); - this.notification = notification; - realmAccess = realm; + Notification = notification; + RealmAccess = realm; + OutputStream = stream; + ShouldDisposeStream = false; } - public async Task ExportASync(IHasGuidPrimaryKey uuid, bool needZipArchive = true) + public virtual async Task ExportASync(IHasGuidPrimaryKey uuid) { Guid id = uuid.ID; await Task.Run(() => { - realmAccess.Run(r => + RealmAccess.Run(r => { if (r.Find(id) is IHasNamedFiles model) { - filename = $"{model.GetDisplayString().GetValidFilename()}{FileExtension}"; + Filename = $"{model.GetDisplayString().GetValidFilename()}{FileExtension}"; } else { return; } - using (var outputStream = exportStorage.CreateFileSafely(filename)) + if (OutputStream == null) { - if (needZipArchive) + OutputStream = ExportStorage.CreateFileSafely(Filename); + ShouldDisposeStream = true; + } + + using (var archive = ZipArchive.Create()) + { + float i = 0; + + foreach (var file in model.Files) { - using (var archive = ZipArchive.Create()) - { - float i = 0; + if (Notification.CancellationToken.IsCancellationRequested) return; - 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); - } + archive.AddEntry(file.Filename, UserFileStorage.GetStream(file.File.GetStoragePath())); + i++; + Notification.Progress = i / model.Files.Count(); + Notification.Text = $"Exporting... ({i}/{model.Files.Count()})"; } - else - { - var file = model.Files.SingleOrDefault(); - if (file == null) - return; - using (var inputStream = UserFileStorage.GetStream(file.File.GetStoragePath())) - inputStream.CopyTo(outputStream); - } + Notification.Text = "Saving Zip Archive..."; + archive.SaveTo(OutputStream); } }); - }).ContinueWith(t => + }).ContinueWith(OnComplete); + } + + protected void OnComplete(Task t) + { + if (ShouldDisposeStream) { - if (t.IsFaulted) - { - notification.State = ProgressNotificationState.Cancelled; - return; - } + OutputStream?.Dispose(); + } - if (notification.CancellationToken.IsCancellationRequested) - { - return; - } + if (t.IsFaulted) + { + Notification.State = ProgressNotificationState.Cancelled; + return; + } - notification.CompletionText = "Export Complete, Click to open the folder"; - notification.CompletionClickAction += () => exportStorage.PresentFileExternally(filename); - notification.State = ProgressNotificationState.Completed; - }); + if (Notification.CancellationToken.IsCancellationRequested) + { + return; + } + + Notification.CompletionText = "Export Complete, Click to open the folder"; + Notification.CompletionClickAction += () => ExportStorage.PresentFileExternally(Filename); + Notification.State = ProgressNotificationState.Completed; } } } diff --git a/osu.Game/Database/LegacyScoreExporter.cs b/osu.Game/Database/LegacyScoreExporter.cs index ffbec0530b..3004c02978 100644 --- a/osu.Game/Database/LegacyScoreExporter.cs +++ b/osu.Game/Database/LegacyScoreExporter.cs @@ -1,7 +1,11 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System.IO; +using System.Linq; +using System.Threading.Tasks; using osu.Framework.Platform; +using osu.Game.Extensions; using osu.Game.Overlays.Notifications; using osu.Game.Scoring; @@ -11,22 +15,42 @@ namespace osu.Game.Database { protected override string FileExtension => ".osr"; - public LegacyScoreExporter(Storage storage, RealmAccess realm, ProgressNotification notification) - : base(storage, realm, notification) + public LegacyScoreExporter(Storage storage, RealmAccess realm, ProgressNotification notification, Stream? stream = null) + : base(storage, realm, notification, stream) { } - //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 async Task ExportASync(IHasGuidPrimaryKey uuid) + { + await Task.Run(() => + { + RealmAccess.Run(r => + { + if (r.Find(uuid.ID) is IHasNamedFiles model) + { + Filename = $"{model.GetDisplayString().GetValidFilename()}{FileExtension}"; + } + else + { + return; + } + + var file = model.Files.SingleOrDefault(); + if (file == null) + return; + + if (Notification.CancellationToken.IsCancellationRequested) return; + + if (OutputStream == null) + { + OutputStream = ExportStorage.CreateFileSafely(Filename); + ShouldDisposeStream = true; + } + + using (var inputStream = UserFileStorage.GetStream(file.File.GetStoragePath())) + inputStream.CopyTo(OutputStream); + }); + }).ContinueWith(OnComplete); + } } } diff --git a/osu.Game/Database/LegacySkinExporter.cs b/osu.Game/Database/LegacySkinExporter.cs index a35636c248..4168763324 100644 --- a/osu.Game/Database/LegacySkinExporter.cs +++ b/osu.Game/Database/LegacySkinExporter.cs @@ -1,6 +1,7 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System.IO; using osu.Framework.Platform; using osu.Game.Overlays.Notifications; using osu.Game.Skinning; @@ -11,8 +12,8 @@ namespace osu.Game.Database { protected override string FileExtension => ".osk"; - public LegacySkinExporter(Storage storage, RealmAccess realm, ProgressNotification notification) - : base(storage, realm, notification) + public LegacySkinExporter(Storage storage, RealmAccess realm, ProgressNotification notification, Stream? stream = null) + : base(storage, realm, notification, stream) { } }