mirror of
https://github.com/osukey/osukey.git
synced 2025-08-03 22:56:36 +09:00
Merge pull request #14900 from peppy/model-downloader-split
Split `ArchiveModelManager` into two pieces
This commit is contained in:
@ -30,7 +30,7 @@ namespace osu.Game.Database
|
||||
/// </summary>
|
||||
/// <typeparam name="TModel">The model type.</typeparam>
|
||||
/// <typeparam name="TFileModel">The associated file join type.</typeparam>
|
||||
public abstract class ArchiveModelManager<TModel, TFileModel> : ICanAcceptFiles, IModelManager<TModel>, IModelFileManager<TModel, TFileModel>
|
||||
public abstract class ArchiveModelManager<TModel, TFileModel> : ICanAcceptFiles, IModelManager<TModel>, IModelFileManager<TModel, TFileModel>, IPresentImports<TModel>
|
||||
where TModel : class, IHasFiles<TFileModel>, IHasPrimaryKey, ISoftDelete
|
||||
where TFileModel : class, INamedFileInfo, new()
|
||||
{
|
||||
@ -57,9 +57,6 @@ namespace osu.Game.Database
|
||||
/// </summary>
|
||||
private static readonly ThreadedTaskScheduler import_scheduler_low_priority = new ThreadedTaskScheduler(import_queue_request_concurrency, nameof(ArchiveModelManager<TModel, TFileModel>));
|
||||
|
||||
/// <summary>
|
||||
/// Set an endpoint for notifications to be posted to.
|
||||
/// </summary>
|
||||
public Action<Notification> PostNotification { protected get; set; }
|
||||
|
||||
/// <summary>
|
||||
@ -252,10 +249,7 @@ namespace osu.Game.Database
|
||||
return import;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fired when the user requests to view the resulting import.
|
||||
/// </summary>
|
||||
public Action<IEnumerable<TModel>> PresentImport;
|
||||
public Action<IEnumerable<TModel>> PresentImport { protected get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Silently import an item from an <see cref="ArchiveReader"/>.
|
||||
@ -802,6 +796,17 @@ namespace osu.Game.Database
|
||||
/// <returns>An existing model which matches the criteria to skip importing, else null.</returns>
|
||||
protected TModel CheckForExisting(TModel model) => model.Hash == null ? null : ModelStore.ConsumableItems.FirstOrDefault(b => b.Hash == model.Hash);
|
||||
|
||||
public bool IsAvailableLocally(TModel model) => CheckLocalAvailability(model, ModelStore.ConsumableItems.Where(m => !m.DeletePending));
|
||||
|
||||
/// <summary>
|
||||
/// Performs implementation specific comparisons to determine whether a given model is present in the local store.
|
||||
/// </summary>
|
||||
/// <param name="model">The <typeparamref name="TModel"/> whose existence needs to be checked.</param>
|
||||
/// <param name="items">The usable items present in the store.</param>
|
||||
/// <returns>Whether the <typeparamref name="TModel"/> exists.</returns>
|
||||
protected virtual bool CheckLocalAvailability(TModel model, IQueryable<TModel> items)
|
||||
=> model.ID > 0 && items.Any(i => i.ID == model.ID && i.Files.Any());
|
||||
|
||||
/// <summary>
|
||||
/// Whether import can be skipped after finding an existing import early in the process.
|
||||
/// Only valid when <see cref="ComputeHash"/> is not overridden.
|
||||
@ -838,7 +843,7 @@ namespace osu.Game.Database
|
||||
|
||||
private DbSet<TModel> queryModel() => ContextFactory.Get().Set<TModel>();
|
||||
|
||||
protected virtual string HumanisedModelName => $"{typeof(TModel).Name.Replace(@"Info", "").ToLower()}";
|
||||
public virtual string HumanisedModelName => $"{typeof(TModel).Name.Replace(@"Info", "").ToLower()}";
|
||||
|
||||
#region Event handling / delaying
|
||||
|
||||
|
@ -11,7 +11,7 @@ namespace osu.Game.Database
|
||||
/// Represents a <see cref="IModelManager{TModel}"/> that can download new models from an external source.
|
||||
/// </summary>
|
||||
/// <typeparam name="TModel">The model type.</typeparam>
|
||||
public interface IModelDownloader<TModel> : IModelManager<TModel>
|
||||
public interface IModelDownloader<TModel> : IPostNotifications
|
||||
where TModel : class
|
||||
{
|
||||
/// <summary>
|
||||
@ -26,13 +26,6 @@ namespace osu.Game.Database
|
||||
/// </summary>
|
||||
IBindable<WeakReference<ArchiveDownloadRequest<TModel>>> DownloadFailed { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a given <typeparamref name="TModel"/> is already available in the local store.
|
||||
/// </summary>
|
||||
/// <param name="model">The <typeparamref name="TModel"/> whose existence needs to be checked.</param>
|
||||
/// <returns>Whether the <typeparamref name="TModel"/> exists.</returns>
|
||||
bool IsAvailableLocally(TModel model);
|
||||
|
||||
/// <summary>
|
||||
/// Begin a download for the requested <typeparamref name="TModel"/>.
|
||||
/// </summary>
|
||||
|
@ -17,7 +17,7 @@ namespace osu.Game.Database
|
||||
/// Represents a model manager that publishes events when <typeparamref name="TModel"/>s are added or removed.
|
||||
/// </summary>
|
||||
/// <typeparam name="TModel">The model type.</typeparam>
|
||||
public interface IModelManager<TModel>
|
||||
public interface IModelManager<TModel> : IPostNotifications
|
||||
where TModel : class
|
||||
{
|
||||
/// <summary>
|
||||
@ -123,5 +123,17 @@ namespace osu.Game.Database
|
||||
/// <param name="lowPriority">Whether this is a low priority import.</param>
|
||||
/// <param name="cancellationToken">An optional cancellation token.</param>
|
||||
Task<TModel> Import(TModel item, ArchiveReader archive = null, bool lowPriority = false, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a given <typeparamref name="TModel"/> is already available in the local store.
|
||||
/// </summary>
|
||||
/// <param name="model">The <typeparamref name="TModel"/> whose existence needs to be checked.</param>
|
||||
/// <returns>Whether the <typeparamref name="TModel"/> exists.</returns>
|
||||
bool IsAvailableLocally(TModel model);
|
||||
|
||||
/// <summary>
|
||||
/// A user displayable name for the model type associated with this manager.
|
||||
/// </summary>
|
||||
string HumanisedModelName => $"{typeof(TModel).Name.Replace(@"Info", "").ToLower()}";
|
||||
}
|
||||
}
|
||||
|
16
osu.Game/Database/IPostNotifications.cs
Normal file
16
osu.Game/Database/IPostNotifications.cs
Normal file
@ -0,0 +1,16 @@
|
||||
// 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;
|
||||
using osu.Game.Overlays.Notifications;
|
||||
|
||||
namespace osu.Game.Database
|
||||
{
|
||||
public interface IPostNotifications
|
||||
{
|
||||
/// <summary>
|
||||
/// And action which will be fired when a notification should be presented to the user.
|
||||
/// </summary>
|
||||
public Action<Notification> PostNotification { set; }
|
||||
}
|
||||
}
|
17
osu.Game/Database/IPresentImports.cs
Normal file
17
osu.Game/Database/IPresentImports.cs
Normal file
@ -0,0 +1,17 @@
|
||||
// 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;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace osu.Game.Database
|
||||
{
|
||||
public interface IPresentImports<TModel>
|
||||
where TModel : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Fired when the user requests to view the resulting import.
|
||||
/// </summary>
|
||||
public Action<IEnumerable<TModel>> PresentImport { set; }
|
||||
}
|
||||
}
|
@ -1,29 +1,24 @@
|
||||
// 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 Humanizer;
|
||||
using osu.Framework.Logging;
|
||||
using osu.Framework.Platform;
|
||||
using osu.Game.Online.API;
|
||||
using osu.Game.Overlays.Notifications;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Humanizer;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Logging;
|
||||
using osu.Framework.Platform;
|
||||
using osu.Game.Online.API;
|
||||
using osu.Game.Overlays.Notifications;
|
||||
|
||||
namespace osu.Game.Database
|
||||
{
|
||||
/// <summary>
|
||||
/// An <see cref="ArchiveModelManager{TModel, TFileModel}"/> that has the ability to download models using an <see cref="IAPIProvider"/> and
|
||||
/// import them into the store.
|
||||
/// </summary>
|
||||
/// <typeparam name="TModel">The model type.</typeparam>
|
||||
/// <typeparam name="TFileModel">The associated file join type.</typeparam>
|
||||
public abstract class DownloadableArchiveModelManager<TModel, TFileModel> : ArchiveModelManager<TModel, TFileModel>, IModelDownloader<TModel>
|
||||
where TModel : class, IHasFiles<TFileModel>, IHasPrimaryKey, ISoftDelete, IEquatable<TModel>
|
||||
where TFileModel : class, INamedFileInfo, new()
|
||||
public abstract class ModelDownloader<TModel> : IModelDownloader<TModel>
|
||||
where TModel : class, IHasPrimaryKey, ISoftDelete, IEquatable<TModel>
|
||||
{
|
||||
public Action<Notification> PostNotification { protected get; set; }
|
||||
|
||||
public IBindable<WeakReference<ArchiveDownloadRequest<TModel>>> DownloadBegan => downloadBegan;
|
||||
|
||||
private readonly Bindable<WeakReference<ArchiveDownloadRequest<TModel>>> downloadBegan = new Bindable<WeakReference<ArchiveDownloadRequest<TModel>>>();
|
||||
@ -32,18 +27,15 @@ namespace osu.Game.Database
|
||||
|
||||
private readonly Bindable<WeakReference<ArchiveDownloadRequest<TModel>>> downloadFailed = new Bindable<WeakReference<ArchiveDownloadRequest<TModel>>>();
|
||||
|
||||
private readonly IModelManager<TModel> modelManager;
|
||||
private readonly IAPIProvider api;
|
||||
|
||||
private readonly List<ArchiveDownloadRequest<TModel>> currentDownloads = new List<ArchiveDownloadRequest<TModel>>();
|
||||
|
||||
private readonly MutableDatabaseBackedStoreWithFileIncludes<TModel, TFileModel> modelStore;
|
||||
|
||||
protected DownloadableArchiveModelManager(Storage storage, IDatabaseContextFactory contextFactory, IAPIProvider api, MutableDatabaseBackedStoreWithFileIncludes<TModel, TFileModel> modelStore,
|
||||
IIpcHost importHost = null)
|
||||
: base(storage, contextFactory, modelStore, importHost)
|
||||
protected ModelDownloader(IModelManager<TModel> modelManager, IAPIProvider api, IIpcHost importHost = null)
|
||||
{
|
||||
this.modelManager = modelManager;
|
||||
this.api = api;
|
||||
this.modelStore = modelStore;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -76,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, new ImportTask(filename)).ConfigureAwait(false);
|
||||
var imported = await modelManager.Import(notification, new ImportTask(filename)).ConfigureAwait(false);
|
||||
|
||||
// for now a failed import will be marked as a failed download for simplicity.
|
||||
if (!imported.Any())
|
||||
@ -111,21 +103,10 @@ namespace osu.Game.Database
|
||||
notification.State = ProgressNotificationState.Cancelled;
|
||||
|
||||
if (!(error is OperationCanceledException))
|
||||
Logger.Error(error, $"{HumanisedModelName.Titleize()} download failed!");
|
||||
Logger.Error(error, $"{modelManager.HumanisedModelName.Titleize()} download failed!");
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsAvailableLocally(TModel model) => CheckLocalAvailability(model, modelStore.ConsumableItems.Where(m => !m.DeletePending));
|
||||
|
||||
/// <summary>
|
||||
/// Performs implementation specific comparisons to determine whether a given model is present in the local store.
|
||||
/// </summary>
|
||||
/// <param name="model">The <typeparamref name="TModel"/> whose existence needs to be checked.</param>
|
||||
/// <param name="items">The usable items present in the store.</param>
|
||||
/// <returns>Whether the <typeparamref name="TModel"/> exists.</returns>
|
||||
protected virtual bool CheckLocalAvailability(TModel model, IQueryable<TModel> items)
|
||||
=> model.ID > 0 && items.Any(i => i.ID == model.ID && i.Files.Any());
|
||||
|
||||
public ArchiveDownloadRequest<TModel> GetExistingDownload(TModel model) => currentDownloads.Find(r => r.Model.Equals(model));
|
||||
|
||||
private bool canDownload(TModel model) => GetExistingDownload(model) == null && api != null;
|
Reference in New Issue
Block a user