2019-06-12 20:06:43 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
2019-06-11 22:19:10 +08:00
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using osu.Framework.Logging;
|
2019-06-11 20:59:33 +08:00
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Overlays.Notifications;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2019-06-11 23:23:44 +08:00
|
|
|
using System.Linq;
|
2019-06-11 20:59:33 +08:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
2019-06-11 22:19:10 +08:00
|
|
|
/// <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>
|
2019-06-12 12:28:44 +08:00
|
|
|
public abstract class DownloadableArchiveModelManager<TModel, TFileModel> : ArchiveModelManager<TModel, TFileModel>, IModelDownloader<TModel>
|
2019-06-11 20:59:33 +08:00
|
|
|
where TModel : class, IHasFiles<TFileModel>, IHasPrimaryKey, ISoftDelete
|
|
|
|
where TFileModel : INamedFileInfo, new()
|
|
|
|
{
|
2019-06-12 12:30:23 +08:00
|
|
|
public event Action<ArchiveDownloadRequest<TModel>> DownloadBegan;
|
2019-06-11 20:59:33 +08:00
|
|
|
|
2019-06-12 12:30:23 +08:00
|
|
|
public event Action<ArchiveDownloadRequest<TModel>> DownloadFailed;
|
2019-06-11 20:59:33 +08:00
|
|
|
|
|
|
|
private readonly IAPIProvider api;
|
|
|
|
|
2019-06-12 12:30:23 +08:00
|
|
|
private readonly List<ArchiveDownloadRequest<TModel>> currentDownloads = new List<ArchiveDownloadRequest<TModel>>();
|
2019-06-11 20:59:33 +08:00
|
|
|
|
2019-06-11 23:23:44 +08:00
|
|
|
private readonly MutableDatabaseBackedStoreWithFileIncludes<TModel, TFileModel> modelStore;
|
|
|
|
|
2019-06-12 12:28:44 +08:00
|
|
|
protected DownloadableArchiveModelManager(Storage storage, IDatabaseContextFactory contextFactory, IAPIProvider api, MutableDatabaseBackedStoreWithFileIncludes<TModel, TFileModel> modelStore, IIpcHost importHost = null)
|
2019-06-11 22:51:06 +08:00
|
|
|
: base(storage, contextFactory, modelStore, importHost)
|
2019-06-11 20:59:33 +08:00
|
|
|
{
|
|
|
|
this.api = api;
|
2019-06-11 23:23:44 +08:00
|
|
|
this.modelStore = modelStore;
|
2019-06-11 20:59:33 +08:00
|
|
|
}
|
|
|
|
|
2019-06-11 22:44:36 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Creates the download request for this <see cref="TModel"/>.
|
|
|
|
/// The <paramref name="options"/> parameters will be provided when the download was initiated with extra options meant
|
|
|
|
/// to be used in the creation of the request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="model">The <see cref="TModel"/> to be downloaded.</param>
|
|
|
|
/// <param name="options">Extra parameters for request creation, null if none were passed.</param>
|
|
|
|
/// <returns>The request object.</returns>
|
2019-06-12 12:30:23 +08:00
|
|
|
protected abstract ArchiveDownloadRequest<TModel> CreateDownloadRequest(TModel model, object[] options);
|
2019-06-11 20:59:33 +08:00
|
|
|
|
|
|
|
public bool Download(TModel model)
|
|
|
|
{
|
2019-06-11 22:06:08 +08:00
|
|
|
if (!canDownload(model)) return false;
|
2019-06-11 20:59:33 +08:00
|
|
|
|
2019-06-11 22:44:36 +08:00
|
|
|
var request = CreateDownloadRequest(model, null);
|
|
|
|
|
|
|
|
performDownloadWithRequest(request);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Download(TModel model, params object[] extra)
|
|
|
|
{
|
|
|
|
if (!canDownload(model)) return false;
|
|
|
|
|
|
|
|
var request = CreateDownloadRequest(model, extra);
|
2019-06-11 22:06:08 +08:00
|
|
|
|
|
|
|
performDownloadWithRequest(request);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-11 22:19:10 +08:00
|
|
|
/// <summary>
|
2019-06-12 02:32:53 +08:00
|
|
|
/// Checks whether a given <see cref="TModel"/> is available in the local store already.
|
2019-06-11 22:19:10 +08:00
|
|
|
/// </summary>
|
2019-06-12 02:32:53 +08:00
|
|
|
/// <param name="model">The <see cref="TModel"/> whose existence needs to be checked.</param>
|
|
|
|
/// <returns>Whether the <see cref="TModel"/> exists locally.</returns>
|
|
|
|
public virtual bool IsAvailableLocally(TModel model) => modelStore.ConsumableItems.Any(m => m.Equals(model) && !m.DeletePending);
|
|
|
|
|
2019-06-12 12:30:23 +08:00
|
|
|
public ArchiveDownloadRequest<TModel> GetExistingDownload(TModel model) => currentDownloads.Find(r => r.Info.Equals(model));
|
2019-06-11 22:06:08 +08:00
|
|
|
|
|
|
|
private bool canDownload(TModel model) => GetExistingDownload(model) == null && api != null;
|
2019-06-11 20:59:33 +08:00
|
|
|
|
2019-06-12 12:30:23 +08:00
|
|
|
private void performDownloadWithRequest(ArchiveDownloadRequest<TModel> request)
|
2019-06-11 22:06:08 +08:00
|
|
|
{
|
2019-06-11 20:59:33 +08:00
|
|
|
DownloadNotification notification = new DownloadNotification
|
|
|
|
{
|
2019-06-11 22:06:08 +08:00
|
|
|
Text = $"Downloading {request.Info}",
|
2019-06-11 20:59:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
request.DownloadProgressed += progress =>
|
|
|
|
{
|
|
|
|
notification.State = ProgressNotificationState.Active;
|
|
|
|
notification.Progress = progress;
|
|
|
|
};
|
|
|
|
|
|
|
|
request.Success += filename =>
|
|
|
|
{
|
2019-06-12 20:06:43 +08:00
|
|
|
Task.Factory.StartNew(async () =>
|
2019-06-11 20:59:33 +08:00
|
|
|
{
|
2019-06-12 20:06:43 +08:00
|
|
|
// This gets scheduled back to the update thread, but we want the import to run in the background.
|
|
|
|
await Import(notification, filename);
|
2019-06-11 20:59:33 +08:00
|
|
|
currentDownloads.Remove(request);
|
|
|
|
}, TaskCreationOptions.LongRunning);
|
|
|
|
};
|
|
|
|
|
|
|
|
request.Failure += error =>
|
|
|
|
{
|
|
|
|
DownloadFailed?.Invoke(request);
|
|
|
|
|
|
|
|
if (error is OperationCanceledException) return;
|
|
|
|
|
|
|
|
notification.State = ProgressNotificationState.Cancelled;
|
2019-06-11 22:19:10 +08:00
|
|
|
// TODO: maybe implement a Name for every model that we can use in this message?
|
2019-06-11 20:59:33 +08:00
|
|
|
Logger.Error(error, "Download failed!");
|
|
|
|
currentDownloads.Remove(request);
|
|
|
|
};
|
|
|
|
|
|
|
|
notification.CancelRequested += () =>
|
|
|
|
{
|
|
|
|
request.Cancel();
|
|
|
|
currentDownloads.Remove(request);
|
|
|
|
notification.State = ProgressNotificationState.Cancelled;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
currentDownloads.Add(request);
|
|
|
|
PostNotification?.Invoke(notification);
|
|
|
|
|
|
|
|
Task.Factory.StartNew(() =>
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
request.Perform(api);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}, TaskCreationOptions.LongRunning);
|
|
|
|
|
|
|
|
DownloadBegan?.Invoke(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
private class DownloadNotification : ProgressNotification
|
|
|
|
{
|
|
|
|
public override bool IsImportant => false;
|
|
|
|
|
|
|
|
protected override Notification CreateCompletionNotification() => new SilencedProgressCompletionNotification
|
|
|
|
{
|
|
|
|
Activated = CompletionClickAction,
|
|
|
|
Text = CompletionText
|
|
|
|
};
|
|
|
|
|
|
|
|
private class SilencedProgressCompletionNotification : ProgressCompletionNotification
|
|
|
|
{
|
|
|
|
public override bool IsImportant => false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|