2021-10-27 10:52:41 +00:00
|
|
|
// 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.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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader(true)]
|
|
|
|
private void load()
|
|
|
|
{
|
2021-10-29 02:56:17 +00:00
|
|
|
if (Manager == null)
|
|
|
|
return;
|
|
|
|
|
2021-10-27 10:52:41 +00:00
|
|
|
// Used to interact with manager classes that don't support interface types. Will eventually be replaced.
|
2021-11-02 21:51:25 +00:00
|
|
|
var scoreInfo = new ScoreInfo
|
|
|
|
{
|
|
|
|
ID = TrackedItem.ID,
|
|
|
|
OnlineScoreID = TrackedItem.OnlineScoreID
|
|
|
|
};
|
2021-10-27 10:52:41 +00:00
|
|
|
|
2021-10-29 02:56:17 +00:00
|
|
|
if (Manager.IsAvailableLocally(scoreInfo))
|
2021-10-27 10:52:41 +00:00
|
|
|
UpdateState(DownloadState.LocallyAvailable);
|
2021-10-29 02:56:17 +00:00
|
|
|
else
|
2021-10-29 02:54:42 +00:00
|
|
|
attachDownload(Manager.GetExistingDownload(scoreInfo));
|
2021-10-27 10:52:41 +00:00
|
|
|
|
2021-11-05 09:05:31 +00:00
|
|
|
Manager.DownloadBegan += downloadBegan;
|
|
|
|
Manager.DownloadFailed += downloadFailed;
|
|
|
|
Manager.ItemUpdated += itemUpdated;
|
|
|
|
Manager.ItemRemoved += itemRemoved;
|
2021-10-27 10:52:41 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 09:05:31 +00:00
|
|
|
private void downloadBegan(ArchiveDownloadRequest<ScoreInfo> request) => Schedule(() =>
|
2021-10-27 10:52:41 +00:00
|
|
|
{
|
2021-11-05 09:05:31 +00:00
|
|
|
if (checkEquality(request.Model, TrackedItem))
|
|
|
|
attachDownload(request);
|
|
|
|
});
|
2021-10-27 10:52:41 +00:00
|
|
|
|
2021-11-05 09:05:31 +00:00
|
|
|
private void downloadFailed(ArchiveDownloadRequest<ScoreInfo> request) => Schedule(() =>
|
2021-10-27 10:52:41 +00:00
|
|
|
{
|
2021-11-05 09:05:31 +00:00
|
|
|
if (checkEquality(request.Model, TrackedItem))
|
|
|
|
attachDownload(null);
|
|
|
|
});
|
2021-10-27 10:52:41 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
|
2021-11-05 09:05:31 +00:00
|
|
|
private void itemUpdated(ScoreInfo item) => Schedule(() =>
|
2021-10-27 10:52:41 +00:00
|
|
|
{
|
2021-11-05 09:05:31 +00:00
|
|
|
if (checkEquality(item, TrackedItem))
|
|
|
|
UpdateState(DownloadState.LocallyAvailable);
|
|
|
|
});
|
2021-10-27 10:52:41 +00:00
|
|
|
|
2021-11-05 09:05:31 +00:00
|
|
|
private void itemRemoved(ScoreInfo item) => Schedule(() =>
|
2021-10-27 10:52:41 +00:00
|
|
|
{
|
2021-11-05 09:05:31 +00:00
|
|
|
if (checkEquality(item, TrackedItem))
|
|
|
|
UpdateState(DownloadState.LocallyAvailable);
|
|
|
|
});
|
2021-10-27 10:52:41 +00:00
|
|
|
|
|
|
|
private bool checkEquality(ScoreInfo x, ScoreInfo y) => x.OnlineScoreID == y.OnlineScoreID;
|
|
|
|
|
|
|
|
#region Disposal
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
attachDownload(null);
|
2021-11-05 09:05:31 +00:00
|
|
|
|
|
|
|
if (Manager != null)
|
|
|
|
{
|
|
|
|
Manager.DownloadBegan -= downloadBegan;
|
|
|
|
Manager.DownloadFailed -= downloadFailed;
|
|
|
|
Manager.ItemUpdated -= itemUpdated;
|
|
|
|
Manager.ItemRemoved -= itemRemoved;
|
|
|
|
}
|
2021-10-27 10:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|