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;
|
2021-12-17 09:53:46 +00:00
|
|
|
using System.Linq;
|
2021-10-27 10:52:41 +00:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Game.Beatmaps;
|
2021-12-17 09:53:46 +00:00
|
|
|
using osu.Game.Database;
|
2021-10-27 10:52:41 +00:00
|
|
|
using osu.Game.Online.API;
|
|
|
|
|
|
|
|
namespace osu.Game.Online
|
|
|
|
{
|
|
|
|
public partial class BeatmapDownloadTracker : DownloadTracker<IBeatmapSetInfo>
|
|
|
|
{
|
2021-11-25 08:23:46 +00:00
|
|
|
[Resolved(CanBeNull = true)]
|
|
|
|
protected BeatmapModelDownloader? Downloader { get; private set; }
|
|
|
|
|
2021-11-05 08:37:05 +00:00
|
|
|
private ArchiveDownloadRequest<IBeatmapSetInfo>? attachedRequest;
|
2021-10-27 10:52:41 +00:00
|
|
|
|
2021-12-17 09:53:46 +00:00
|
|
|
private IDisposable? realmSubscription;
|
|
|
|
|
|
|
|
[Resolved]
|
2022-01-24 10:59:58 +00:00
|
|
|
private RealmAccess realm { get; set; } = null!;
|
2021-12-17 09:53:46 +00:00
|
|
|
|
2021-10-27 10:52:41 +00:00
|
|
|
public BeatmapDownloadTracker(IBeatmapSetInfo trackedItem)
|
|
|
|
: base(trackedItem)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-12-17 09:53:46 +00:00
|
|
|
protected override void LoadComplete()
|
2021-10-27 10:52:41 +00:00
|
|
|
{
|
2021-12-17 09:53:46 +00:00
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
if (Downloader == null)
|
2021-10-29 02:54:19 +00:00
|
|
|
return;
|
|
|
|
|
2021-12-17 09:53:46 +00:00
|
|
|
Downloader.DownloadBegan += downloadBegan;
|
|
|
|
Downloader.DownloadFailed += downloadFailed;
|
|
|
|
|
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-12 08:50:31 +00:00
|
|
|
var beatmapSetInfo = new BeatmapSetInfo { OnlineID = TrackedItem.OnlineID };
|
2021-10-27 10:52:41 +00:00
|
|
|
|
2024-09-26 07:48:42 +00:00
|
|
|
// Required local for iOS. Will cause runtime crash if inlined.
|
2024-09-25 07:45:37 +00:00
|
|
|
int onlineId = TrackedItem.OnlineID;
|
|
|
|
|
|
|
|
realmSubscription = realm.RegisterForNotifications(r => r.All<BeatmapSetInfo>().Where(s => s.OnlineID == onlineId && !s.DeletePending), (items, _) =>
|
2021-12-17 09:53:46 +00:00
|
|
|
{
|
|
|
|
if (items.Any())
|
|
|
|
Schedule(() => UpdateState(DownloadState.LocallyAvailable));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Schedule(() =>
|
|
|
|
{
|
|
|
|
UpdateState(DownloadState.NotDownloaded);
|
|
|
|
attachDownload(Downloader.GetExistingDownload(beatmapSetInfo));
|
|
|
|
});
|
|
|
|
}
|
2022-01-23 10:50:29 +00:00
|
|
|
});
|
2021-10-27 10:52:41 +00:00
|
|
|
}
|
|
|
|
|
2021-11-06 04:57:34 +00:00
|
|
|
private void downloadBegan(ArchiveDownloadRequest<IBeatmapSetInfo> 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-06 04:57:34 +00:00
|
|
|
private void downloadFailed(ArchiveDownloadRequest<IBeatmapSetInfo> 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
|
|
|
|
2021-11-05 08:37:05 +00:00
|
|
|
private void attachDownload(ArchiveDownloadRequest<IBeatmapSetInfo>? request)
|
2021-10-27 10:52:41 +00: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));
|
|
|
|
|
|
|
|
private bool checkEquality(IBeatmapSetInfo x, IBeatmapSetInfo y) => x.OnlineID == y.OnlineID;
|
|
|
|
|
|
|
|
#region Disposal
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
attachDownload(null);
|
2021-11-05 09:05:31 +00:00
|
|
|
|
2021-12-17 09:53:46 +00:00
|
|
|
realmSubscription?.Dispose();
|
|
|
|
|
2021-11-25 08:23:46 +00:00
|
|
|
if (Downloader != null)
|
|
|
|
{
|
|
|
|
Downloader.DownloadBegan -= downloadBegan;
|
|
|
|
Downloader.DownloadFailed -= downloadFailed;
|
|
|
|
}
|
2021-10-27 10:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|