diff --git a/osu.Game.Tests/Skins/IO/ImportSkinTest.cs b/osu.Game.Tests/Skins/IO/ImportSkinTest.cs index 726ff74d7f..8043c01d2d 100644 --- a/osu.Game.Tests/Skins/IO/ImportSkinTest.cs +++ b/osu.Game.Tests/Skins/IO/ImportSkinTest.cs @@ -119,10 +119,7 @@ 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); - await import1.PerformRead(async s => - { - await new LegacySkinExporter(osu.Dependencies.Get(), osu.Dependencies.Get()).ExportToStreamAsync(s, exportStream); - }); + await new LegacySkinExporter(osu.Dependencies.Get()).ExportToStreamAsync(import1, exportStream); string exportFilename = import1.GetDisplayString(); @@ -203,7 +200,7 @@ namespace osu.Game.Tests.Skins.IO Assert.IsFalse(s.Protected); Assert.AreEqual(typeof(ArgonSkin), s.CreateInstance(skinManager).GetType()); - await new LegacySkinExporter(osu.Dependencies.Get(), osu.Dependencies.Get()).ExportToStreamAsync(s, exportStream); + await new LegacySkinExporter(osu.Dependencies.Get()).ExportToStreamAsync(skinManager.CurrentSkinInfo.Value, exportStream); Assert.Greater(exportStream.Length, 0); }); @@ -236,7 +233,7 @@ namespace osu.Game.Tests.Skins.IO Assert.IsFalse(s.Protected); Assert.AreEqual(typeof(DefaultLegacySkin), s.CreateInstance(skinManager).GetType()); - await new LegacySkinExporter(osu.Dependencies.Get(), osu.Dependencies.Get()).ExportToStreamAsync(s, exportStream); + await new LegacySkinExporter(osu.Dependencies.Get()).ExportToStreamAsync(skinManager.CurrentSkinInfo.Value, exportStream); Assert.Greater(exportStream.Length, 0); }); diff --git a/osu.Game/Beatmaps/BeatmapManager.cs b/osu.Game/Beatmaps/BeatmapManager.cs index d3bf8081b1..877a0c7667 100644 --- a/osu.Game/Beatmaps/BeatmapManager.cs +++ b/osu.Game/Beatmaps/BeatmapManager.cs @@ -79,7 +79,7 @@ namespace osu.Game.Beatmaps workingBeatmapCache = CreateWorkingBeatmapCache(audioManager, gameResources, userResources, defaultBeatmap, host); - beatmapExporter = new LegacyBeatmapExporter(storage, realm) + beatmapExporter = new LegacyBeatmapExporter(storage) { PostNotification = obj => PostNotification?.Invoke(obj) }; @@ -400,7 +400,7 @@ namespace osu.Game.Beatmaps public Task?> ImportAsUpdate(ProgressNotification notification, ImportTask importTask, BeatmapSetInfo original) => beatmapImporter.ImportAsUpdate(notification, importTask, original); - public Task Export(BeatmapSetInfo beatmap) => beatmapExporter.ExportAsync(beatmap); + public Task Export(BeatmapSetInfo beatmap) => beatmapExporter.ExportAsync(beatmap, Realm); private void updateHashAndMarkDirty(BeatmapSetInfo setInfo) { diff --git a/osu.Game/Database/LegacyArchiveExporter.cs b/osu.Game/Database/LegacyArchiveExporter.cs index 35f09189c9..297139a20e 100644 --- a/osu.Game/Database/LegacyArchiveExporter.cs +++ b/osu.Game/Database/LegacyArchiveExporter.cs @@ -20,8 +20,8 @@ namespace osu.Game.Database public abstract class LegacyArchiveExporter : LegacyModelExporter where TModel : RealmObject, IHasNamedFiles, IHasGuidPrimaryKey { - protected LegacyArchiveExporter(Storage storage, RealmAccess realm) - : base(storage, realm) + protected LegacyArchiveExporter(Storage storage) + : base(storage) { } diff --git a/osu.Game/Database/LegacyBeatmapExporter.cs b/osu.Game/Database/LegacyBeatmapExporter.cs index 107b91a234..e89d3da8c7 100644 --- a/osu.Game/Database/LegacyBeatmapExporter.cs +++ b/osu.Game/Database/LegacyBeatmapExporter.cs @@ -8,8 +8,8 @@ namespace osu.Game.Database { public class LegacyBeatmapExporter : LegacyArchiveExporter { - public LegacyBeatmapExporter(Storage storage, RealmAccess realm) - : base(storage, realm) + public LegacyBeatmapExporter(Storage storage) + : base(storage) { } diff --git a/osu.Game/Database/LegacyModelExporter.cs b/osu.Game/Database/LegacyModelExporter.cs index 0f09c2c19b..58606cb5fa 100644 --- a/osu.Game/Database/LegacyModelExporter.cs +++ b/osu.Game/Database/LegacyModelExporter.cs @@ -31,24 +31,35 @@ namespace osu.Game.Database private readonly Storage exportStorage; protected virtual string GetFilename(TModel item) => item.GetDisplayString(); - private readonly RealmAccess realmAccess; - public Action? PostNotification { get; set; } // Store the model being exporting. - private static readonly List exporting_models = new List(); + private static readonly List> exporting_models = new List>(); /// /// Construct exporter. /// Create a new exporter for each export, otherwise it will cause confusing notifications. /// /// Storage for storing exported files. Basically it is used to provide export stream - /// The RealmAccess used to provide the exported file. - protected LegacyModelExporter(Storage storage, RealmAccess realm) + protected LegacyModelExporter(Storage storage) { exportStorage = storage.GetStorageForDirectory(@"exports"); UserFileStorage = storage.GetStorageForDirectory(@"files"); - realmAccess = realm; + } + + /// + /// Export the model to default folder. + /// + /// The model should export. + /// Realm that convert model to Live. + /// + /// The Cancellation token that can cancel the exporting. + /// If specified CancellationToken, then use it. Otherwise use PostNotification's CancellationToken. + /// + /// + public Task ExportAsync(TModel model, RealmAccess realm, CancellationToken cancellationToken = default) + { + return ExportAsync(model.ToLive(realm), cancellationToken); } /// @@ -60,7 +71,7 @@ namespace osu.Game.Database /// If specified CancellationToken, then use it. Otherwise use PostNotification's CancellationToken. /// /// - public async Task ExportAsync(TModel model, CancellationToken cancellationToken = default) + public async Task ExportAsync(Live model, CancellationToken cancellationToken = default) { // check if the model is being exporting already if (!exporting_models.Contains(model)) @@ -73,7 +84,8 @@ namespace osu.Game.Database return false; } - string itemFilename = GetFilename(model).GetValidFilename(); + string itemFilename = model.PerformRead(s => GetFilename(s).GetValidFilename()); + IEnumerable existingExports = exportStorage .GetFiles(string.Empty, $"{itemFilename}*{FileExtension}") @@ -128,15 +140,13 @@ namespace osu.Game.Database /// The notification will displayed to the user /// The Cancellation token that can cancel the exporting. /// Whether the export was successful - public Task ExportToStreamAsync(TModel model, Stream stream, ProgressNotification? notification = null, CancellationToken cancellationToken = default) + public Task ExportToStreamAsync(Live model, Stream stream, ProgressNotification? notification = null, CancellationToken cancellationToken = default) { - Guid id = model.ID; return Task.Run(() => { - realmAccess.Run(r => + model.PerformRead(s => { - TModel refetchModel = r.Find(id); - ExportToStream(refetchModel, stream, notification, cancellationToken); + ExportToStream(s, stream, notification, cancellationToken); }); }, cancellationToken).ContinueWith(t => { diff --git a/osu.Game/Database/LegacyScoreExporter.cs b/osu.Game/Database/LegacyScoreExporter.cs index ef55ffdd96..bd7d273a64 100644 --- a/osu.Game/Database/LegacyScoreExporter.cs +++ b/osu.Game/Database/LegacyScoreExporter.cs @@ -13,8 +13,7 @@ namespace osu.Game.Database { public class LegacyScoreExporter : LegacyModelExporter { - public LegacyScoreExporter(Storage storage, RealmAccess realm) - : base(storage, realm) + public LegacyScoreExporter(Storage storage) : base(storage) { } @@ -28,7 +27,7 @@ namespace osu.Game.Database protected override string FileExtension => ".osr"; - protected override void ExportToStream(ScoreInfo model, Stream stream, ProgressNotification notification, CancellationToken cancellationToken = default) + protected override void ExportToStream(ScoreInfo model, Stream stream, ProgressNotification? notification, CancellationToken cancellationToken = default) { var file = model.Files.SingleOrDefault(); if (file == null) diff --git a/osu.Game/Database/LegacySkinExporter.cs b/osu.Game/Database/LegacySkinExporter.cs index d3e6a2f0f4..2a531203bc 100644 --- a/osu.Game/Database/LegacySkinExporter.cs +++ b/osu.Game/Database/LegacySkinExporter.cs @@ -8,8 +8,7 @@ namespace osu.Game.Database { public class LegacySkinExporter : LegacyArchiveExporter { - public LegacySkinExporter(Storage storage, RealmAccess realm) - : base(storage, realm) + public LegacySkinExporter(Storage storage) : base(storage) { } diff --git a/osu.Game/Overlays/Settings/Sections/SkinSection.cs b/osu.Game/Overlays/Settings/Sections/SkinSection.cs index df5d7e0c27..5382eac675 100644 --- a/osu.Game/Overlays/Settings/Sections/SkinSection.cs +++ b/osu.Game/Overlays/Settings/Sections/SkinSection.cs @@ -159,7 +159,7 @@ namespace osu.Game.Overlays.Settings.Sections { try { - skins.CurrentSkinInfo.Value.PerformRead(s => skins.ExportSkin(s)); + skins.ExportCurrentSkin(); } catch (Exception e) { diff --git a/osu.Game/Scoring/ScoreManager.cs b/osu.Game/Scoring/ScoreManager.cs index 3201fec696..5e432b6a96 100644 --- a/osu.Game/Scoring/ScoreManager.cs +++ b/osu.Game/Scoring/ScoreManager.cs @@ -50,7 +50,7 @@ namespace osu.Game.Scoring PostNotification = obj => PostNotification?.Invoke(obj) }; - scoreExporter = new LegacyScoreExporter(storage, realm) + scoreExporter = new LegacyScoreExporter(storage) { PostNotification = obj => PostNotification?.Invoke(obj) }; @@ -193,7 +193,7 @@ namespace osu.Game.Scoring public Task>> Import(ProgressNotification notification, ImportTask[] tasks, ImportParameters parameters = default) => scoreImporter.Import(notification, tasks); - public Task Export(ScoreInfo score) => scoreExporter.ExportAsync(score); + public Task Export(ScoreInfo score) => scoreExporter.ExportAsync(score, Realm); public Task> ImportAsUpdate(ProgressNotification notification, ImportTask task, ScoreInfo original) => scoreImporter.ImportAsUpdate(notification, task, original); diff --git a/osu.Game/Skinning/SkinManager.cs b/osu.Game/Skinning/SkinManager.cs index 1a76ec8623..45536e04eb 100644 --- a/osu.Game/Skinning/SkinManager.cs +++ b/osu.Game/Skinning/SkinManager.cs @@ -123,7 +123,7 @@ namespace osu.Game.Skinning SourceChanged?.Invoke(); }; - skinExporter = new LegacySkinExporter(storage, realm) + skinExporter = new LegacySkinExporter(storage) { PostNotification = obj => PostNotification?.Invoke(obj) }; @@ -305,7 +305,9 @@ namespace osu.Game.Skinning public Task> Import(ImportTask task, ImportParameters parameters = default, CancellationToken cancellationToken = default) => skinImporter.Import(task, parameters, cancellationToken); - public Task ExportSkin(SkinInfo skin) => skinExporter.ExportAsync(skin); + public Task ExportCurrentSkin() => ExportSkin(CurrentSkinInfo.Value); + + public Task ExportSkin(Live skin) => skinExporter.ExportAsync(skin); #endregion