1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 14:37:30 +08:00
osu-lazer/osu.Game/Database/ModelDownloader.cs

131 lines
5.2 KiB
C#
Raw Normal View History

// 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Humanizer;
2020-05-19 15:44:22 +08:00
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
{
public abstract class ModelDownloader<TModel> : IModelDownloader<TModel>
where TModel : class, IHasPrimaryKey, ISoftDelete, IEquatable<TModel>
{
public Action<Notification> PostNotification { protected get; set; }
2020-05-19 15:44:22 +08:00
public IBindable<WeakReference<ArchiveDownloadRequest<TModel>>> DownloadBegan => downloadBegan;
2020-05-19 15:44:22 +08:00
private readonly Bindable<WeakReference<ArchiveDownloadRequest<TModel>>> downloadBegan = new Bindable<WeakReference<ArchiveDownloadRequest<TModel>>>();
public IBindable<WeakReference<ArchiveDownloadRequest<TModel>>> DownloadFailed => downloadFailed;
private readonly Bindable<WeakReference<ArchiveDownloadRequest<TModel>>> downloadFailed = new Bindable<WeakReference<ArchiveDownloadRequest<TModel>>>();
private readonly IModelManager<TModel> modelManager;
private readonly IAPIProvider api;
2019-06-12 12:30:23 +08:00
private readonly List<ArchiveDownloadRequest<TModel>> currentDownloads = new List<ArchiveDownloadRequest<TModel>>();
protected ModelDownloader(IModelManager<TModel> modelManager, IAPIProvider api, IIpcHost importHost = null)
{
this.modelManager = modelManager;
this.api = api;
}
/// <summary>
2019-11-17 20:48:23 +08:00
/// Creates the download request for this <typeparamref name="TModel"/>.
/// </summary>
2019-11-17 20:48:23 +08:00
/// <param name="model">The <typeparamref name="TModel"/> to be downloaded.</param>
2019-06-26 19:07:01 +08:00
/// <param name="minimiseDownloadSize">Whether this download should be optimised for slow connections. Generally means extras are not included in the download bundle.</param>
/// <returns>The request object.</returns>
2019-06-19 00:41:19 +08:00
protected abstract ArchiveDownloadRequest<TModel> CreateDownloadRequest(TModel model, bool minimiseDownloadSize);
2019-06-19 00:41:19 +08:00
public bool Download(TModel model, bool minimiseDownloadSize = false)
{
if (!canDownload(model)) return false;
2019-06-19 00:41:19 +08:00
var request = CreateDownloadRequest(model, minimiseDownloadSize);
DownloadNotification notification = new DownloadNotification
{
2019-06-13 00:26:36 +08:00
Text = $"Downloading {request.Model}",
};
request.DownloadProgressed += progress =>
{
notification.State = ProgressNotificationState.Active;
notification.Progress = progress;
};
request.Success += filename =>
{
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 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())
2020-05-19 15:44:22 +08:00
downloadFailed.Value = new WeakReference<ArchiveDownloadRequest<TModel>>(request);
currentDownloads.Remove(request);
}, TaskCreationOptions.LongRunning);
};
request.Failure += triggerFailure;
notification.CancelRequested += () =>
{
request.Cancel();
return true;
};
currentDownloads.Add(request);
PostNotification?.Invoke(notification);
api.PerformAsync(request);
2020-05-19 15:44:22 +08:00
downloadBegan.Value = new WeakReference<ArchiveDownloadRequest<TModel>>(request);
return true;
void triggerFailure(Exception error)
{
currentDownloads.Remove(request);
2020-05-19 15:44:22 +08:00
downloadFailed.Value = new WeakReference<ArchiveDownloadRequest<TModel>>(request);
notification.State = ProgressNotificationState.Cancelled;
if (!(error is OperationCanceledException))
Logger.Error(error, $"{modelManager.HumanisedModelName.Titleize()} download failed!");
}
}
public ArchiveDownloadRequest<TModel> GetExistingDownload(TModel model) => currentDownloads.Find(r => r.Model.Equals(model));
private bool canDownload(TModel model) => GetExistingDownload(model) == null && api != null;
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;
}
}
}
}