1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 11:37:28 +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)); 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. // 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); Assert.That(secondImport, Is.Not.Null);
Debug.Assert(secondImport != null); Debug.Assert(secondImport != null);

View File

@ -237,7 +237,7 @@ namespace osu.Game.Tests.Online
internal class TestBeatmapModelDownloader : BeatmapModelDownloader internal class TestBeatmapModelDownloader : BeatmapModelDownloader
{ {
public TestBeatmapModelDownloader(BeatmapManager importer, IAPIProvider apiProvider) public TestBeatmapModelDownloader(IModelImporter<BeatmapSetInfo> importer, IAPIProvider apiProvider)
: base(importer, 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); var imported = await Import(notification, importTask);
if (!imported.Any()) if (!imported.Any())
return imported; return null;
Debug.Assert(imported.Count() == 1); Debug.Assert(imported.Count() == 1);
imported.First().PerformWrite(updated => var first = imported.First();
first.PerformWrite(updated =>
{ {
var realm = updated.Realm; var realm = updated.Realm;
@ -103,7 +105,7 @@ namespace osu.Game.Beatmaps
realm.Remove(original); realm.Remove(original);
}); });
return imported; return first;
} }
protected override bool ShouldDeleteArchive(string path) => Path.GetExtension(path).ToLowerInvariant() == ".osz"; 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())); 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); beatmapImporter.ImportAsUpdate(notification, importTask, original);
#region Implementation of ICanAcceptFiles #region Implementation of ICanAcceptFiles

View File

@ -1,39 +1,23 @@
// 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.Collections.Generic;
using System.Threading.Tasks;
using osu.Game.Database; using osu.Game.Database;
using osu.Game.Online.API; using osu.Game.Online.API;
using osu.Game.Online.API.Requests; using osu.Game.Online.API.Requests;
using osu.Game.Overlays.Notifications;
namespace osu.Game.Beatmaps namespace osu.Game.Beatmaps
{ {
public class BeatmapModelDownloader : ModelDownloader<BeatmapSetInfo, IBeatmapSetInfo> public class BeatmapModelDownloader : ModelDownloader<BeatmapSetInfo, IBeatmapSetInfo>
{ {
private readonly BeatmapManager beatmapManager;
protected override ArchiveDownloadRequest<IBeatmapSetInfo> CreateDownloadRequest(IBeatmapSetInfo set, bool minimiseDownloadSize) => protected override ArchiveDownloadRequest<IBeatmapSetInfo> CreateDownloadRequest(IBeatmapSetInfo set, bool minimiseDownloadSize) =>
new DownloadBeatmapSetRequest(set, minimiseDownloadSize); new DownloadBeatmapSetRequest(set, minimiseDownloadSize);
public override ArchiveDownloadRequest<IBeatmapSetInfo>? GetExistingDownload(IBeatmapSetInfo model) public override ArchiveDownloadRequest<IBeatmapSetInfo>? GetExistingDownload(IBeatmapSetInfo model)
=> CurrentDownloads.Find(r => r.Model.OnlineID == model.OnlineID); => CurrentDownloads.Find(r => r.Model.OnlineID == model.OnlineID);
public BeatmapModelDownloader(BeatmapManager beatmapImporter, IAPIProvider api) public BeatmapModelDownloader(IModelImporter<BeatmapSetInfo> beatmapImporter, IAPIProvider api)
: base(beatmapImporter, 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> /// <returns>The imported models.</returns>
Task<IEnumerable<Live<TModel>>> Import(ProgressNotification notification, params ImportTask[] tasks); 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> /// <summary>
/// A user displayable name for the model type associated with this manager. /// A user displayable name for the model type associated with this manager.
/// </summary> /// </summary>

View File

@ -44,6 +44,8 @@ namespace osu.Game.Database
public bool Download(T model, bool minimiseDownloadSize = false) => Download(model, minimiseDownloadSize, null); 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) protected bool Download(T model, bool minimiseDownloadSize, TModel? originalModel)
{ {
if (!canDownload(model)) return false; if (!canDownload(model)) return false;
@ -66,7 +68,7 @@ namespace osu.Game.Database
Task.Factory.StartNew(async () => Task.Factory.StartNew(async () =>
{ {
// This gets scheduled back to the update thread, but we want the import to run in the background. // 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. // for now a failed import will be marked as a failed download for simplicity.
if (!imported.Any()) 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); public abstract ArchiveDownloadRequest<T>? GetExistingDownload(T model);
private bool canDownload(T model) => GetExistingDownload(model) == null && api != null; private bool canDownload(T model) => GetExistingDownload(model) == null && api != null;

View File

@ -174,6 +174,8 @@ namespace osu.Game.Database
return imported; return imported;
} }
public virtual Task<Live<TModel>?> ImportAsUpdate(ProgressNotification notification, ImportTask task, TModel original) => throw new NotImplementedException();
/// <summary> /// <summary>
/// Import one <typeparamref name="TModel"/> from the filesystem and delete the file on success. /// 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. /// 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<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) => public Live<ScoreInfo> Import(ScoreInfo item, ArchiveReader archive = null, bool batchImport = false, CancellationToken cancellationToken = default) =>
scoreImporter.ImportModel(item, archive, batchImport, cancellationToken); scoreImporter.ImportModel(item, archive, batchImport, cancellationToken);

View File

@ -90,7 +90,7 @@ namespace osu.Game.Screens.Select.Carousel
Action = () => Action = () =>
{ {
beatmapDownloader.Update(beatmapSetInfo); beatmapDownloader.DownloadAsUpdate(beatmapSetInfo);
attachExistingDownload(); 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<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); public Task<Live<SkinInfo>> Import(ImportTask task, bool batchImport = false, CancellationToken cancellationToken = default) => skinImporter.Import(task, batchImport, cancellationToken);
#endregion #endregion