1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-13 03:42:57 +08:00

Add ImportAsUpdate method to IModelImporter to avoid otehr changes

This commit is contained in:
Dean Herbert 2022-07-26 15:46:29 +09:00
parent 7d8a78ef01
commit 8370ca9765
11 changed files with 30 additions and 38 deletions

View File

@ -60,7 +60,7 @@ namespace osu.Game.Tests.Database
Assert.That(realm.Realm.All<BeatmapSetInfo>().First(s => !s.DeletePending).Beatmaps, Has.Count.EqualTo(11));
// Second import matches first but contains one extra .osu file.
var secondImport = (await importer.ImportAsUpdate(new ProgressNotification(), new ImportTask(pathOriginal), firstImport.Value)).FirstOrDefault();
var secondImport = await importer.ImportAsUpdate(new ProgressNotification(), new ImportTask(pathOriginal), firstImport.Value);
Assert.That(secondImport, Is.Not.Null);
Debug.Assert(secondImport != null);

View File

@ -237,7 +237,7 @@ namespace osu.Game.Tests.Online
internal class TestBeatmapModelDownloader : BeatmapModelDownloader
{
public TestBeatmapModelDownloader(BeatmapManager importer, IAPIProvider apiProvider)
public TestBeatmapModelDownloader(IModelImporter<BeatmapSetInfo> importer, IAPIProvider apiProvider)
: base(importer, apiProvider)
{
}

View File

@ -41,16 +41,18 @@ namespace osu.Game.Beatmaps
{
}
public async Task<IEnumerable<Live<BeatmapSetInfo>>> ImportAsUpdate(ProgressNotification notification, ImportTask importTask, BeatmapSetInfo original)
public override async Task<Live<BeatmapSetInfo>?> ImportAsUpdate(ProgressNotification notification, ImportTask importTask, BeatmapSetInfo original)
{
var imported = await Import(notification, importTask);
if (!imported.Any())
return imported;
return null;
Debug.Assert(imported.Count() == 1);
imported.First().PerformWrite(updated =>
var first = imported.First();
first.PerformWrite(updated =>
{
var realm = updated.Realm;
@ -103,7 +105,7 @@ namespace osu.Game.Beatmaps
realm.Remove(original);
});
return imported;
return first;
}
protected override bool ShouldDeleteArchive(string path) => Path.GetExtension(path).ToLowerInvariant() == ".osz";

View File

@ -408,7 +408,7 @@ namespace osu.Game.Beatmaps
Realm.Run(r => Undelete(r.All<BeatmapSetInfo>().Where(s => s.DeletePending).ToList()));
}
public Task<IEnumerable<Live<BeatmapSetInfo>>> ImportAsUpdate(ProgressNotification notification, ImportTask importTask, BeatmapSetInfo original) =>
public Task<Live<BeatmapSetInfo>?> ImportAsUpdate(ProgressNotification notification, ImportTask importTask, BeatmapSetInfo original) =>
beatmapImporter.ImportAsUpdate(notification, importTask, original);
#region Implementation of ICanAcceptFiles

View File

@ -1,39 +1,23 @@
// 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.Collections.Generic;
using System.Threading.Tasks;
using osu.Game.Database;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Overlays.Notifications;
namespace osu.Game.Beatmaps
{
public class BeatmapModelDownloader : ModelDownloader<BeatmapSetInfo, IBeatmapSetInfo>
{
private readonly BeatmapManager beatmapManager;
protected override ArchiveDownloadRequest<IBeatmapSetInfo> CreateDownloadRequest(IBeatmapSetInfo set, bool minimiseDownloadSize) =>
new DownloadBeatmapSetRequest(set, minimiseDownloadSize);
public override ArchiveDownloadRequest<IBeatmapSetInfo>? GetExistingDownload(IBeatmapSetInfo model)
=> CurrentDownloads.Find(r => r.Model.OnlineID == model.OnlineID);
public BeatmapModelDownloader(BeatmapManager beatmapImporter, IAPIProvider api)
public BeatmapModelDownloader(IModelImporter<BeatmapSetInfo> beatmapImporter, IAPIProvider api)
: base(beatmapImporter, api)
{
beatmapManager = beatmapImporter;
}
protected override Task<IEnumerable<Live<BeatmapSetInfo>>> Import(ProgressNotification notification, string filename, BeatmapSetInfo? originalModel)
{
if (originalModel != null)
return beatmapManager.ImportAsUpdate(notification, new ImportTask(filename), originalModel);
return base.Import(notification, filename, null);
}
public bool Update(BeatmapSetInfo model) => Download(model, false, model);
}
}

View File

@ -23,6 +23,16 @@ namespace osu.Game.Database
/// <returns>The imported models.</returns>
Task<IEnumerable<Live<TModel>>> Import(ProgressNotification notification, params ImportTask[] tasks);
/// <summary>
/// Process a single import as an update for an existing model.
/// This will still run a full import, but perform any post-processing required to make it feel like an update to the user.
/// </summary>
/// <param name="notification">The notification to update.</param>
/// <param name="task">The import task.</param>
/// <param name="original">The original model which is being updated.</param>
/// <returns>The imported model.</returns>
Task<Live<TModel>?> ImportAsUpdate(ProgressNotification notification, ImportTask task, TModel original);
/// <summary>
/// A user displayable name for the model type associated with this manager.
/// </summary>

View File

@ -44,6 +44,8 @@ namespace osu.Game.Database
public bool Download(T model, bool minimiseDownloadSize = false) => Download(model, minimiseDownloadSize, null);
public void DownloadAsUpdate(TModel originalModel) => Download(originalModel, false, originalModel);
protected bool Download(T model, bool minimiseDownloadSize, TModel? originalModel)
{
if (!canDownload(model)) return false;
@ -66,7 +68,7 @@ namespace osu.Game.Database
Task.Factory.StartNew(async () =>
{
// This gets scheduled back to the update thread, but we want the import to run in the background.
var imported = await Import(notification, filename, originalModel).ConfigureAwait(false);
var imported = await importer.Import(notification, new ImportTask(filename)).ConfigureAwait(false);
// for now a failed import will be marked as a failed download for simplicity.
if (!imported.Any())
@ -105,18 +107,6 @@ namespace osu.Game.Database
}
}
/// <summary>
/// Run the post-download import for the model.
/// </summary>
/// <param name="notification">The notification to update.</param>
/// <param name="filename">The path of the temporary downloaded file.</param>
/// <param name="originalModel">An optional model for update scenarios, to be used as a reference.</param>
/// <returns>The imported model.</returns>
protected virtual Task<IEnumerable<Live<TModel>>> Import(ProgressNotification notification, string filename, TModel? originalModel)
{
return importer.Import(notification, new ImportTask(filename));
}
public abstract ArchiveDownloadRequest<T>? GetExistingDownload(T model);
private bool canDownload(T model) => GetExistingDownload(model) == null && api != null;

View File

@ -174,6 +174,8 @@ namespace osu.Game.Database
return imported;
}
public virtual Task<Live<TModel>?> ImportAsUpdate(ProgressNotification notification, ImportTask task, TModel original) => throw new NotImplementedException();
/// <summary>
/// Import one <typeparamref name="TModel"/> from the filesystem and delete the file on success.
/// Note that this bypasses the UI flow and should only be used for special cases or testing.

View File

@ -268,6 +268,8 @@ namespace osu.Game.Scoring
public Task<IEnumerable<Live<ScoreInfo>>> Import(ProgressNotification notification, params ImportTask[] tasks) => scoreImporter.Import(notification, tasks);
public Task<Live<ScoreInfo>> ImportAsUpdate(ProgressNotification notification, ImportTask task, ScoreInfo original) => scoreImporter.ImportAsUpdate(notification, task, original);
public Live<ScoreInfo> Import(ScoreInfo item, ArchiveReader archive = null, bool batchImport = false, CancellationToken cancellationToken = default) =>
scoreImporter.ImportModel(item, archive, batchImport, cancellationToken);

View File

@ -90,7 +90,7 @@ namespace osu.Game.Screens.Select.Carousel
Action = () =>
{
beatmapDownloader.Update(beatmapSetInfo);
beatmapDownloader.DownloadAsUpdate(beatmapSetInfo);
attachExistingDownload();
};
}

View File

@ -272,6 +272,8 @@ namespace osu.Game.Skinning
public Task<IEnumerable<Live<SkinInfo>>> Import(ProgressNotification notification, params ImportTask[] tasks) => skinImporter.Import(notification, tasks);
public Task<Live<SkinInfo>> ImportAsUpdate(ProgressNotification notification, ImportTask task, SkinInfo original) => skinImporter.ImportAsUpdate(notification, task, original);
public Task<Live<SkinInfo>> Import(ImportTask task, bool batchImport = false, CancellationToken cancellationToken = default) => skinImporter.Import(task, batchImport, cancellationToken);
#endregion