2021-10-27 18:52:41 +08: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;
|
2021-12-17 17:58:21 +08:00
|
|
|
using System.Linq;
|
2021-10-27 18:52:41 +08:00
|
|
|
using osu.Framework.Allocation;
|
2021-12-17 17:58:21 +08:00
|
|
|
using osu.Game.Database;
|
2021-12-10 17:32:06 +08:00
|
|
|
using osu.Game.Extensions;
|
2021-10-27 18:52:41 +08:00
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Scoring;
|
|
|
|
|
|
|
|
namespace osu.Game.Online
|
|
|
|
{
|
|
|
|
public class ScoreDownloadTracker : DownloadTracker<ScoreInfo>
|
|
|
|
{
|
2021-11-25 16:21:05 +08:00
|
|
|
[Resolved(CanBeNull = true)]
|
|
|
|
protected ScoreModelDownloader? Downloader { get; private set; }
|
|
|
|
|
2021-11-05 16:37:05 +08:00
|
|
|
private ArchiveDownloadRequest<IScoreInfo>? attachedRequest;
|
2021-10-27 18:52:41 +08:00
|
|
|
|
2021-12-17 17:58:21 +08:00
|
|
|
private IDisposable? realmSubscription;
|
|
|
|
|
|
|
|
[Resolved]
|
2022-01-24 18:59:58 +08:00
|
|
|
private RealmAccess realm { get; set; } = null!;
|
2021-12-17 17:58:21 +08:00
|
|
|
|
2021-10-27 18:52:41 +08:00
|
|
|
public ScoreDownloadTracker(ScoreInfo trackedItem)
|
|
|
|
: base(trackedItem)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-12-17 17:58:21 +08:00
|
|
|
protected override void LoadComplete()
|
2021-10-27 18:52:41 +08:00
|
|
|
{
|
2021-12-17 17:58:21 +08:00
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
if (Downloader == null)
|
2021-10-29 10:56:17 +08:00
|
|
|
return;
|
|
|
|
|
2021-10-27 18:52:41 +08:00
|
|
|
// Used to interact with manager classes that don't support interface types. Will eventually be replaced.
|
2021-11-03 05:51:25 +08:00
|
|
|
var scoreInfo = new ScoreInfo
|
|
|
|
{
|
|
|
|
ID = TrackedItem.ID,
|
2021-12-10 14:37:12 +08:00
|
|
|
OnlineID = TrackedItem.OnlineID
|
2021-11-03 05:51:25 +08:00
|
|
|
};
|
2021-10-27 18:52:41 +08:00
|
|
|
|
2021-11-25 16:21:05 +08:00
|
|
|
Downloader.DownloadBegan += downloadBegan;
|
|
|
|
Downloader.DownloadFailed += downloadFailed;
|
2021-12-17 17:58:21 +08:00
|
|
|
|
2022-04-12 17:01:13 +08:00
|
|
|
realmSubscription = realm.RegisterForNotifications(r => r.All<ScoreInfo>().Where(s =>
|
|
|
|
((s.OnlineID > 0 && s.OnlineID == TrackedItem.OnlineID)
|
|
|
|
|| (!string.IsNullOrEmpty(s.Hash) && s.Hash == TrackedItem.Hash))
|
|
|
|
&& !s.DeletePending), (items, changes, ___) =>
|
2021-12-17 17:58:21 +08:00
|
|
|
{
|
|
|
|
if (items.Any())
|
|
|
|
Schedule(() => UpdateState(DownloadState.LocallyAvailable));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Schedule(() =>
|
|
|
|
{
|
|
|
|
UpdateState(DownloadState.NotDownloaded);
|
|
|
|
attachDownload(Downloader.GetExistingDownload(scoreInfo));
|
|
|
|
});
|
|
|
|
}
|
2022-01-23 18:50:29 +08:00
|
|
|
});
|
2021-10-27 18:52:41 +08:00
|
|
|
}
|
|
|
|
|
2021-11-06 12:57:34 +08:00
|
|
|
private void downloadBegan(ArchiveDownloadRequest<IScoreInfo> request) => Schedule(() =>
|
2021-10-27 18:52:41 +08:00
|
|
|
{
|
2021-11-05 17:05:31 +08:00
|
|
|
if (checkEquality(request.Model, TrackedItem))
|
|
|
|
attachDownload(request);
|
|
|
|
});
|
2021-10-27 18:52:41 +08:00
|
|
|
|
2021-11-06 12:57:34 +08:00
|
|
|
private void downloadFailed(ArchiveDownloadRequest<IScoreInfo> request) => Schedule(() =>
|
2021-10-27 18:52:41 +08:00
|
|
|
{
|
2021-11-05 17:05:31 +08:00
|
|
|
if (checkEquality(request.Model, TrackedItem))
|
|
|
|
attachDownload(null);
|
|
|
|
});
|
2021-10-27 18:52:41 +08:00
|
|
|
|
2021-11-05 16:37:05 +08:00
|
|
|
private void attachDownload(ArchiveDownloadRequest<IScoreInfo>? request)
|
2021-10-27 18:52:41 +08:00
|
|
|
{
|
|
|
|
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-12-10 17:32:06 +08:00
|
|
|
private bool checkEquality(IScoreInfo x, IScoreInfo y) => x.MatchesOnlineID(y);
|
2021-10-27 18:52:41 +08:00
|
|
|
|
|
|
|
#region Disposal
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
attachDownload(null);
|
2021-11-05 17:05:31 +08:00
|
|
|
|
2021-12-17 17:58:21 +08:00
|
|
|
realmSubscription?.Dispose();
|
|
|
|
|
2021-11-25 16:21:05 +08:00
|
|
|
if (Downloader != null)
|
|
|
|
{
|
|
|
|
Downloader.DownloadBegan -= downloadBegan;
|
|
|
|
Downloader.DownloadFailed -= downloadFailed;
|
|
|
|
}
|
2021-10-27 18:52:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|