1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 07:33:20 +08:00

Implement new version of download tracker

This commit is contained in:
Dean Herbert 2021-10-27 19:52:41 +09:00
parent 45db99171e
commit 9015ac6ba8
5 changed files with 386 additions and 3 deletions

View File

@ -15,7 +15,7 @@ using osu.Game.Overlays.Notifications;
namespace osu.Game.Database
{
public abstract class ModelDownloader<TModel> : IModelDownloader<TModel>
where TModel : class, IHasPrimaryKey, ISoftDelete, IEquatable<TModel>
where TModel : class, IHasPrimaryKey, ISoftDelete, IEquatable<TModel>, IHasOnlineID
{
public Action<Notification> PostNotification { protected get; set; }
@ -107,7 +107,7 @@ namespace osu.Game.Database
}
}
public ArchiveDownloadRequest<TModel> GetExistingDownload(TModel model) => currentDownloads.Find(r => r.Model.Equals(model));
public ArchiveDownloadRequest<TModel> GetExistingDownload(TModel model) => currentDownloads.Find(r => r.Model.OnlineID == model.OnlineID);
private bool canDownload(TModel model) => GetExistingDownload(model) == null && api != null;

View File

@ -0,0 +1,172 @@
// 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.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Game.Beatmaps;
using osu.Game.Online.API;
#nullable enable
namespace osu.Game.Online
{
public class BeatmapDownloadTracker : DownloadTracker<IBeatmapSetInfo>
{
[Resolved(CanBeNull = true)]
protected BeatmapManager? Manager { get; private set; }
private ArchiveDownloadRequest<BeatmapSetInfo>? attachedRequest;
public BeatmapDownloadTracker(IBeatmapSetInfo trackedItem)
: base(trackedItem)
{
}
private IBindable<WeakReference<BeatmapSetInfo>>? managerUpdated;
private IBindable<WeakReference<BeatmapSetInfo>>? managerRemoved;
private IBindable<WeakReference<ArchiveDownloadRequest<BeatmapSetInfo>>>? managerDownloadBegan;
private IBindable<WeakReference<ArchiveDownloadRequest<BeatmapSetInfo>>>? managerDownloadFailed;
[BackgroundDependencyLoader(true)]
private void load()
{
// Used to interact with manager classes that don't support interface types. Will eventually be replaced.
var beatmapSetInfo = new BeatmapSetInfo { OnlineBeatmapSetID = TrackedItem.OnlineID };
if ((TrackedItem as BeatmapSetInfo)?.ID > 0 || Manager?.IsAvailableLocally(beatmapSetInfo) == true)
UpdateState(DownloadState.LocallyAvailable);
else if (Manager != null)
attachDownload(Manager.GetExistingDownload(beatmapSetInfo));
if (Manager != null)
{
managerDownloadBegan = Manager.DownloadBegan.GetBoundCopy();
managerDownloadBegan.BindValueChanged(downloadBegan);
managerDownloadFailed = Manager.DownloadFailed.GetBoundCopy();
managerDownloadFailed.BindValueChanged(downloadFailed);
managerUpdated = Manager.ItemUpdated.GetBoundCopy();
managerUpdated.BindValueChanged(itemUpdated);
managerRemoved = Manager.ItemRemoved.GetBoundCopy();
managerRemoved.BindValueChanged(itemRemoved);
}
}
/// <summary>
/// Checks that a database model matches the one expected to be downloaded.
/// </summary>
/// <example>
/// For online play, this could be used to check that the databased model matches the online beatmap.
/// </example>
/// <param name="databasedModel">The model in database.</param>
protected virtual bool VerifyDatabasedModel(BeatmapSetInfo databasedModel) => true; // TODO: do we still need this?
private void downloadBegan(ValueChangedEvent<WeakReference<ArchiveDownloadRequest<BeatmapSetInfo>>> weakRequest)
{
if (weakRequest.NewValue.TryGetTarget(out var request))
{
Schedule(() =>
{
if (checkEquality(request.Model, TrackedItem))
attachDownload(request);
});
}
}
private void downloadFailed(ValueChangedEvent<WeakReference<ArchiveDownloadRequest<BeatmapSetInfo>>> weakRequest)
{
if (weakRequest.NewValue.TryGetTarget(out var request))
{
Schedule(() =>
{
if (checkEquality(request.Model, TrackedItem))
attachDownload(null);
});
}
}
private void attachDownload(ArchiveDownloadRequest<BeatmapSetInfo>? request)
{
if (attachedRequest != null)
{
attachedRequest.Failure -= onRequestFailure;
attachedRequest.DownloadProgressed -= onRequestProgress;
attachedRequest.Success -= onRequestSuccess;
}
attachedRequest = request;
if (attachedRequest != null)
{
if (attachedRequest.Progress == 1)
{
UpdateProgress(1);
UpdateState(DownloadState.Importing);
}
else
{
UpdateProgress(attachedRequest.Progress);
UpdateState(DownloadState.Downloading);
attachedRequest.Failure += onRequestFailure;
attachedRequest.DownloadProgressed += onRequestProgress;
attachedRequest.Success += onRequestSuccess;
}
}
else
{
UpdateState(DownloadState.NotDownloaded);
}
}
private void onRequestSuccess(string _) => Schedule(() => UpdateState(DownloadState.Importing));
private void onRequestProgress(float progress) => Schedule(() => UpdateProgress(progress));
private void onRequestFailure(Exception e) => Schedule(() => attachDownload(null));
private void itemUpdated(ValueChangedEvent<WeakReference<BeatmapSetInfo>> weakItem)
{
if (weakItem.NewValue.TryGetTarget(out var item))
{
Schedule(() =>
{
if (!checkEquality(item, TrackedItem))
return;
if (!VerifyDatabasedModel(item))
{
UpdateState(DownloadState.NotDownloaded);
return;
}
UpdateState(DownloadState.LocallyAvailable);
});
}
}
private void itemRemoved(ValueChangedEvent<WeakReference<BeatmapSetInfo>> weakItem)
{
if (weakItem.NewValue.TryGetTarget(out var item))
{
Schedule(() =>
{
if (checkEquality(item, TrackedItem))
UpdateState(DownloadState.NotDownloaded);
});
}
}
private bool checkEquality(IBeatmapSetInfo x, IBeatmapSetInfo y) => x.OnlineID == y.OnlineID;
#region Disposal
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
attachDownload(null);
}
#endregion
}
}

View File

@ -0,0 +1,39 @@
// 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 osu.Framework.Bindables;
using osu.Framework.Graphics;
#nullable enable
namespace osu.Game.Online
{
public abstract class DownloadTracker<T> : Component
where T : class
{
public readonly T TrackedItem;
/// <summary>
/// Holds the current download state of the download - whether is has already been downloaded, is in progress, or is not downloaded.
/// </summary>
public IBindable<DownloadState> State => state;
private readonly Bindable<DownloadState> state = new Bindable<DownloadState>();
/// <summary>
/// The progress of an active download.
/// </summary>
public IBindableNumber<double> Progress => progress;
private readonly BindableNumber<double> progress = new BindableNumber<double> { MinValue = 0, MaxValue = 1 };
protected DownloadTracker(T trackedItem)
{
TrackedItem = trackedItem;
}
protected void UpdateState(DownloadState newState) => state.Value = newState;
protected void UpdateProgress(double newProgress) => progress.Value = newProgress;
}
}

View File

@ -0,0 +1,172 @@
// 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.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Game.Online.API;
using osu.Game.Scoring;
#nullable enable
namespace osu.Game.Online
{
public class ScoreDownloadTracker : DownloadTracker<ScoreInfo>
{
[Resolved(CanBeNull = true)]
protected ScoreManager? Manager { get; private set; }
private ArchiveDownloadRequest<ScoreInfo>? attachedRequest;
public ScoreDownloadTracker(ScoreInfo trackedItem)
: base(trackedItem)
{
}
private IBindable<WeakReference<ScoreInfo>>? managerUpdated;
private IBindable<WeakReference<ScoreInfo>>? managerRemoved;
private IBindable<WeakReference<ArchiveDownloadRequest<ScoreInfo>>>? managerDownloadBegan;
private IBindable<WeakReference<ArchiveDownloadRequest<ScoreInfo>>>? managerDownloadFailed;
[BackgroundDependencyLoader(true)]
private void load()
{
// Used to interact with manager classes that don't support interface types. Will eventually be replaced.
var beatmapSetInfo = new ScoreInfo { OnlineScoreID = TrackedItem.OnlineScoreID };
if (TrackedItem.ID > 0 || Manager?.IsAvailableLocally(beatmapSetInfo) == true)
UpdateState(DownloadState.LocallyAvailable);
else if (Manager != null)
attachDownload(Manager.GetExistingDownload(beatmapSetInfo));
if (Manager != null)
{
managerDownloadBegan = Manager.DownloadBegan.GetBoundCopy();
managerDownloadBegan.BindValueChanged(downloadBegan);
managerDownloadFailed = Manager.DownloadFailed.GetBoundCopy();
managerDownloadFailed.BindValueChanged(downloadFailed);
managerUpdated = Manager.ItemUpdated.GetBoundCopy();
managerUpdated.BindValueChanged(itemUpdated);
managerRemoved = Manager.ItemRemoved.GetBoundCopy();
managerRemoved.BindValueChanged(itemRemoved);
}
}
/// <summary>
/// Checks that a database model matches the one expected to be downloaded.
/// </summary>
/// <example>
/// For online play, this could be used to check that the databased model matches the online beatmap.
/// </example>
/// <param name="databasedModel">The model in database.</param>
protected virtual bool VerifyDatabasedModel(ScoreInfo databasedModel) => true; // TODO: do we still need this?
private void downloadBegan(ValueChangedEvent<WeakReference<ArchiveDownloadRequest<ScoreInfo>>> weakRequest)
{
if (weakRequest.NewValue.TryGetTarget(out var request))
{
Schedule(() =>
{
if (checkEquality(request.Model, TrackedItem))
attachDownload(request);
});
}
}
private void downloadFailed(ValueChangedEvent<WeakReference<ArchiveDownloadRequest<ScoreInfo>>> weakRequest)
{
if (weakRequest.NewValue.TryGetTarget(out var request))
{
Schedule(() =>
{
if (checkEquality(request.Model, TrackedItem))
attachDownload(null);
});
}
}
private void attachDownload(ArchiveDownloadRequest<ScoreInfo>? request)
{
if (attachedRequest != null)
{
attachedRequest.Failure -= onRequestFailure;
attachedRequest.DownloadProgressed -= onRequestProgress;
attachedRequest.Success -= onRequestSuccess;
}
attachedRequest = request;
if (attachedRequest != null)
{
if (attachedRequest.Progress == 1)
{
UpdateProgress(1);
UpdateState(DownloadState.Importing);
}
else
{
UpdateProgress(attachedRequest.Progress);
UpdateState(DownloadState.Downloading);
attachedRequest.Failure += onRequestFailure;
attachedRequest.DownloadProgressed += onRequestProgress;
attachedRequest.Success += onRequestSuccess;
}
}
else
{
UpdateState(DownloadState.NotDownloaded);
}
}
private void onRequestSuccess(string _) => Schedule(() => UpdateState(DownloadState.Importing));
private void onRequestProgress(float progress) => Schedule(() => UpdateProgress(progress));
private void onRequestFailure(Exception e) => Schedule(() => attachDownload(null));
private void itemUpdated(ValueChangedEvent<WeakReference<ScoreInfo>> weakItem)
{
if (weakItem.NewValue.TryGetTarget(out var item))
{
Schedule(() =>
{
if (!checkEquality(item, TrackedItem))
return;
if (!VerifyDatabasedModel(item))
{
UpdateState(DownloadState.NotDownloaded);
return;
}
UpdateState(DownloadState.LocallyAvailable);
});
}
}
private void itemRemoved(ValueChangedEvent<WeakReference<ScoreInfo>> weakItem)
{
if (weakItem.NewValue.TryGetTarget(out var item))
{
Schedule(() =>
{
if (checkEquality(item, TrackedItem))
UpdateState(DownloadState.NotDownloaded);
});
}
}
private bool checkEquality(ScoreInfo x, ScoreInfo y) => x.OnlineScoreID == y.OnlineScoreID;
#region Disposal
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
attachDownload(null);
}
#endregion
}
}

View File

@ -19,7 +19,7 @@ using osu.Game.Utils;
namespace osu.Game.Scoring
{
public class ScoreInfo : IHasFiles<ScoreFileInfo>, IHasPrimaryKey, ISoftDelete, IEquatable<ScoreInfo>, IDeepCloneable<ScoreInfo>
public class ScoreInfo : IHasFiles<ScoreFileInfo>, IHasPrimaryKey, ISoftDelete, IEquatable<ScoreInfo>, IDeepCloneable<ScoreInfo>, IHasOnlineID
{
public int ID { get; set; }