working with test

This commit is contained in:
cdwcgt 2022-11-19 01:02:35 +09:00
parent 4b29941b47
commit fc4a6cb125
No known key found for this signature in database
GPG Key ID: 144396D01095C3A2
6 changed files with 113 additions and 88 deletions

View File

@ -10,12 +10,11 @@ using System.Runtime.CompilerServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Extensions;
using osu.Framework.Platform; using osu.Framework.Platform;
using osu.Game.Database; using osu.Game.Database;
using osu.Game.Extensions; using osu.Game.Extensions;
using osu.Game.IO; using osu.Game.IO;
using osu.Game.Overlays; using osu.Game.Overlays.Notifications;
using osu.Game.Skinning; using osu.Game.Skinning;
using SharpCompress.Archives.Zip; 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")); var import1 = await loadSkinIntoOsu(osu, new ImportTask(createOskWithIni("name 1", "author 1"), "custom.osk"));
assertCorrectMetadata(import1, "name 1 [custom]", "author 1", osu); 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<Storage>(), osu.Dependencies.Get<RealmAccess>(), new ProgressNotification(), exportStream).ExportASync(s);
}); });
string exportFilename = import1.GetDisplayString(); string exportFilename = import1.GetDisplayString();
@ -190,7 +189,7 @@ namespace osu.Game.Tests.Skins.IO
}); });
[Test] [Test]
public Task TestExportThenImportDefaultSkin() => runSkinTest(osu => public Task TestExportThenImportDefaultSkin() => runSkinTest(async osu =>
{ {
var skinManager = osu.Dependencies.Get<SkinManager>(); var skinManager = osu.Dependencies.Get<SkinManager>();
@ -200,30 +199,28 @@ namespace osu.Game.Tests.Skins.IO
Guid originalSkinId = skinManager.CurrentSkinInfo.Value.ID; Guid originalSkinId = skinManager.CurrentSkinInfo.Value.ID;
skinManager.CurrentSkinInfo.Value.PerformRead(s => await skinManager.CurrentSkinInfo.Value.PerformRead(async s =>
{ {
Assert.IsFalse(s.Protected); Assert.IsFalse(s.Protected);
Assert.AreEqual(typeof(ArgonSkin), s.CreateInstance(skinManager).GetType()); Assert.AreEqual(typeof(ArgonSkin), s.CreateInstance(skinManager).GetType());
new LegacySkinExporter(osu.Dependencies.Get<Storage>(), osu.Dependencies.Get<INotificationOverlay>()).ExportModelTo(s, exportStream); await new LegacySkinExporter(osu.Dependencies.Get<Storage>(), osu.Dependencies.Get<RealmAccess>(), new ProgressNotification(), exportStream).ExportASync(s);
Assert.Greater(exportStream.Length, 0); 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.IsFalse(s.Protected);
Assert.AreNotEqual(originalSkinId, s.ID); Assert.AreNotEqual(originalSkinId, s.ID);
Assert.AreEqual(typeof(ArgonSkin), s.CreateInstance(skinManager).GetType()); Assert.AreEqual(typeof(ArgonSkin), s.CreateInstance(skinManager).GetType());
}); });
return Task.CompletedTask;
}); });
[Test] [Test]
public Task TestExportThenImportClassicSkin() => runSkinTest(osu => public Task TestExportThenImportClassicSkin() => runSkinTest(async osu =>
{ {
var skinManager = osu.Dependencies.Get<SkinManager>(); var skinManager = osu.Dependencies.Get<SkinManager>();
@ -235,26 +232,24 @@ namespace osu.Game.Tests.Skins.IO
Guid originalSkinId = skinManager.CurrentSkinInfo.Value.ID; Guid originalSkinId = skinManager.CurrentSkinInfo.Value.ID;
skinManager.CurrentSkinInfo.Value.PerformRead(s => await skinManager.CurrentSkinInfo.Value.PerformRead(async s =>
{ {
Assert.IsFalse(s.Protected); Assert.IsFalse(s.Protected);
Assert.AreEqual(typeof(DefaultLegacySkin), s.CreateInstance(skinManager).GetType()); Assert.AreEqual(typeof(DefaultLegacySkin), s.CreateInstance(skinManager).GetType());
new LegacySkinExporter(osu.Dependencies.Get<Storage>(), osu.Dependencies.Get<INotificationOverlay>()).ExportModelTo(s, exportStream); await new LegacySkinExporter(osu.Dependencies.Get<Storage>(), osu.Dependencies.Get<RealmAccess>(), new ProgressNotification(), exportStream).ExportASync(s);
Assert.Greater(exportStream.Length, 0); 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.IsFalse(s.Protected);
Assert.AreNotEqual(originalSkinId, s.ID); Assert.AreNotEqual(originalSkinId, s.ID);
Assert.AreEqual(typeof(DefaultLegacySkin), s.CreateInstance(skinManager).GetType()); Assert.AreEqual(typeof(DefaultLegacySkin), s.CreateInstance(skinManager).GetType());
}); });
return Task.CompletedTask;
}); });
#endregion #endregion

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System.IO;
using osu.Framework.Platform; using osu.Framework.Platform;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Overlays.Notifications; using osu.Game.Overlays.Notifications;
@ -11,8 +12,8 @@ namespace osu.Game.Database
{ {
protected override string FileExtension => ".osz"; protected override string FileExtension => ".osz";
public LegacyBeatmapExporter(Storage storage, RealmAccess realm, ProgressNotification notification) public LegacyBeatmapExporter(Storage storage, RealmAccess realm, ProgressNotification notification, Stream? stream = null)
: base(storage, realm, notification) : base(storage, realm, notification, stream)
{ {
} }
} }

View File

@ -43,7 +43,7 @@ namespace osu.Game.Database
break; break;
case ScoreInfo: case ScoreInfo:
await new LegacyScoreExporter(exportStorage, realmAccess, notification).ExportASync(item, false); await new LegacyScoreExporter(exportStorage, realmAccess, notification).ExportASync(item);
break; break;
case BeatmapSetInfo: case BeatmapSetInfo:

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using osu.Framework.Platform; using osu.Framework.Platform;
@ -25,89 +26,92 @@ namespace osu.Game.Database
protected readonly Storage UserFileStorage; 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"); UserFileStorage = storage.GetStorageForDirectory(@"files");
this.notification = notification; Notification = notification;
realmAccess = realm; 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; Guid id = uuid.ID;
await Task.Run(() => await Task.Run(() =>
{ {
realmAccess.Run(r => RealmAccess.Run(r =>
{ {
if (r.Find<TModel>(id) is IHasNamedFiles model) if (r.Find<TModel>(id) is IHasNamedFiles model)
{ {
filename = $"{model.GetDisplayString().GetValidFilename()}{FileExtension}"; Filename = $"{model.GetDisplayString().GetValidFilename()}{FileExtension}";
} }
else else
{ {
return; 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()) if (Notification.CancellationToken.IsCancellationRequested) return;
{
float i = 0;
foreach (var file in model.Files) archive.AddEntry(file.Filename, UserFileStorage.GetStream(file.File.GetStoragePath()));
{ i++;
if (notification.CancellationToken.IsCancellationRequested) return; Notification.Progress = i / model.Files.Count();
archive.AddEntry(file.Filename, UserFileStorage.GetStream(file.File.GetStoragePath())); Notification.Text = $"Exporting... ({i}/{model.Files.Count()})";
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())) Notification.Text = "Saving Zip Archive...";
inputStream.CopyTo(outputStream); archive.SaveTo(OutputStream);
}
} }
}); });
}).ContinueWith(t => }).ContinueWith(OnComplete);
}
protected void OnComplete(Task t)
{
if (ShouldDisposeStream)
{ {
if (t.IsFaulted) OutputStream?.Dispose();
{ }
notification.State = ProgressNotificationState.Cancelled;
return;
}
if (notification.CancellationToken.IsCancellationRequested) if (t.IsFaulted)
{ {
return; Notification.State = ProgressNotificationState.Cancelled;
} return;
}
notification.CompletionText = "Export Complete, Click to open the folder"; if (Notification.CancellationToken.IsCancellationRequested)
notification.CompletionClickAction += () => exportStorage.PresentFileExternally(filename); {
notification.State = ProgressNotificationState.Completed; return;
}); }
Notification.CompletionText = "Export Complete, Click to open the folder";
Notification.CompletionClickAction += () => ExportStorage.PresentFileExternally(Filename);
Notification.State = ProgressNotificationState.Completed;
} }
} }
} }

View File

@ -1,7 +1,11 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // 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.Framework.Platform;
using osu.Game.Extensions;
using osu.Game.Overlays.Notifications; using osu.Game.Overlays.Notifications;
using osu.Game.Scoring; using osu.Game.Scoring;
@ -11,22 +15,42 @@ namespace osu.Game.Database
{ {
protected override string FileExtension => ".osr"; protected override string FileExtension => ".osr";
public LegacyScoreExporter(Storage storage, RealmAccess realm, ProgressNotification notification) public LegacyScoreExporter(Storage storage, RealmAccess realm, ProgressNotification notification, Stream? stream = null)
: base(storage, realm, notification) : base(storage, realm, notification, stream)
{ {
} }
//public override void ExportModelTo(ScoreInfo model, Stream outputStream) public override async Task ExportASync(IHasGuidPrimaryKey uuid)
//{ {
// var file = model.Files.SingleOrDefault(); await Task.Run(() =>
// if (file == null) {
// return; RealmAccess.Run(r =>
// {
// using (var inputStream = UserFileStorage.GetStream(file.File.GetStoragePath())) if (r.Find<ScoreInfo>(uuid.ID) is IHasNamedFiles model)
// inputStream.CopyTo(outputStream); {
// Filename = $"{model.GetDisplayString().GetValidFilename()}{FileExtension}";
// Notification.State = ProgressNotificationState.Completed; }
// outputStream.Dispose(); 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);
}
} }
} }

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System.IO;
using osu.Framework.Platform; using osu.Framework.Platform;
using osu.Game.Overlays.Notifications; using osu.Game.Overlays.Notifications;
using osu.Game.Skinning; using osu.Game.Skinning;
@ -11,8 +12,8 @@ namespace osu.Game.Database
{ {
protected override string FileExtension => ".osk"; protected override string FileExtension => ".osk";
public LegacySkinExporter(Storage storage, RealmAccess realm, ProgressNotification notification) public LegacySkinExporter(Storage storage, RealmAccess realm, ProgressNotification notification, Stream? stream = null)
: base(storage, realm, notification) : base(storage, realm, notification, stream)
{ {
} }
} }