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 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<Storage>(), osu.Dependencies.Get<RealmAccess>(), 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<SkinManager>();
@ -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<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);
});
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<SkinManager>();
@ -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<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);
});
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

View File

@ -1,6 +1,7 @@
// 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 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)
{
}
}

View File

@ -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:

View File

@ -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<TModel>(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;
}
}
}

View File

@ -1,7 +1,11 @@
// 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 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<ScoreInfo>(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);
}
}
}

View File

@ -1,6 +1,7 @@
// 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 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)
{
}
}