2021-01-17 04:00:56 +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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2021-01-17 04:00:56 +08:00
|
|
|
using System;
|
2021-11-04 11:30:19 +08:00
|
|
|
using System.Diagnostics;
|
2021-12-17 18:21:41 +08:00
|
|
|
using System.Linq;
|
2022-02-15 22:30:39 +08:00
|
|
|
using System.Threading.Tasks;
|
2021-10-27 19:19:34 +08:00
|
|
|
using osu.Framework.Allocation;
|
2021-01-17 04:00:56 +08:00
|
|
|
using osu.Framework.Bindables;
|
2022-02-15 22:30:39 +08:00
|
|
|
using osu.Framework.Extensions;
|
2021-10-27 20:26:26 +08:00
|
|
|
using osu.Framework.Graphics;
|
2021-10-27 19:19:34 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-01-18 03:04:12 +08:00
|
|
|
using osu.Framework.Logging;
|
2021-02-05 16:34:05 +08:00
|
|
|
using osu.Framework.Threading;
|
2021-01-17 04:00:56 +08:00
|
|
|
using osu.Game.Beatmaps;
|
2021-12-17 18:21:41 +08:00
|
|
|
using osu.Game.Database;
|
2022-02-15 22:30:39 +08:00
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2022-01-10 17:26:49 +08:00
|
|
|
using Realms;
|
2021-01-17 04:00:56 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Online.Rooms
|
|
|
|
{
|
2021-01-19 16:57:40 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Represent a checksum-verifying beatmap availability tracker usable for online play screens.
|
|
|
|
///
|
|
|
|
/// This differs from a regular download tracking composite as this accounts for the
|
|
|
|
/// databased beatmap set's checksum, to disallow from playing with an altered version of the beatmap.
|
|
|
|
/// </summary>
|
2022-04-13 16:02:33 +08:00
|
|
|
public class OnlinePlayBeatmapAvailabilityTracker : CompositeDrawable
|
2021-01-17 04:00:56 +08:00
|
|
|
{
|
|
|
|
public readonly IBindable<PlaylistItem> SelectedItem = new Bindable<PlaylistItem>();
|
|
|
|
|
2021-10-29 10:57:54 +08:00
|
|
|
// Required to allow child components to update. Can potentially be replaced with a `CompositeComponent` class if or when we make one.
|
2021-10-27 20:26:26 +08:00
|
|
|
protected override bool RequiresChildrenUpdate => true;
|
|
|
|
|
2021-12-17 18:21:41 +08:00
|
|
|
[Resolved]
|
2022-01-24 18:59:58 +08:00
|
|
|
private RealmAccess realm { get; set; } = null!;
|
2021-12-17 18:21:41 +08:00
|
|
|
|
2022-02-15 22:30:39 +08:00
|
|
|
[Resolved]
|
|
|
|
private BeatmapLookupCache beatmapLookupCache { get; set; } = null!;
|
|
|
|
|
2021-01-17 04:00:56 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The availability state of the currently selected playlist item.
|
|
|
|
/// </summary>
|
2022-04-13 16:02:33 +08:00
|
|
|
public virtual IBindable<BeatmapAvailability> Availability => availability;
|
2021-01-17 04:00:56 +08:00
|
|
|
|
2021-10-27 23:59:48 +08:00
|
|
|
private readonly Bindable<BeatmapAvailability> availability = new Bindable<BeatmapAvailability>(BeatmapAvailability.NotDownloaded());
|
2021-02-05 16:34:05 +08:00
|
|
|
|
|
|
|
private ScheduledDelegate progressUpdate;
|
2021-10-27 19:19:34 +08:00
|
|
|
private BeatmapDownloadTracker downloadTracker;
|
2021-12-17 18:21:41 +08:00
|
|
|
private IDisposable realmSubscription;
|
2022-02-16 15:03:08 +08:00
|
|
|
private APIBeatmap selectedBeatmap;
|
2021-12-17 18:21:41 +08:00
|
|
|
|
2021-01-17 04:00:56 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2021-02-05 16:19:23 +08:00
|
|
|
SelectedItem.BindValueChanged(item =>
|
|
|
|
{
|
|
|
|
// the underlying playlist is regularly cleared for maintenance purposes (things which probably need to be fixed eventually).
|
|
|
|
// to avoid exposing a state change when there may actually be none, ignore all nulls for now.
|
|
|
|
if (item.NewValue == null)
|
|
|
|
return;
|
|
|
|
|
2021-10-27 20:26:26 +08:00
|
|
|
downloadTracker?.RemoveAndDisposeImmediately();
|
2022-02-16 15:03:08 +08:00
|
|
|
selectedBeatmap = null;
|
2021-01-17 04:00:56 +08:00
|
|
|
|
2022-02-15 22:33:26 +08:00
|
|
|
beatmapLookupCache.GetBeatmapAsync(item.NewValue.Beatmap.OnlineID).ContinueWith(task => Schedule(() =>
|
2022-02-15 22:30:39 +08:00
|
|
|
{
|
|
|
|
var beatmap = task.GetResultSafely();
|
2021-11-04 11:30:19 +08:00
|
|
|
|
2022-02-15 22:33:26 +08:00
|
|
|
if (SelectedItem.Value?.Beatmap.OnlineID == beatmap.OnlineID)
|
2022-02-16 15:03:08 +08:00
|
|
|
{
|
|
|
|
selectedBeatmap = beatmap;
|
|
|
|
beginTracking();
|
|
|
|
}
|
2022-02-15 22:30:39 +08:00
|
|
|
}), TaskContinuationOptions.OnlyOnRanToCompletion);
|
|
|
|
}, true);
|
|
|
|
}
|
2021-11-03 03:32:21 +08:00
|
|
|
|
2022-02-16 15:03:08 +08:00
|
|
|
private void beginTracking()
|
2022-02-15 22:30:39 +08:00
|
|
|
{
|
2022-02-16 15:03:08 +08:00
|
|
|
Debug.Assert(selectedBeatmap.BeatmapSet != null);
|
2021-11-03 03:32:21 +08:00
|
|
|
|
2022-02-16 15:03:08 +08:00
|
|
|
downloadTracker = new BeatmapDownloadTracker(selectedBeatmap.BeatmapSet);
|
2022-01-10 17:26:49 +08:00
|
|
|
|
2022-02-15 22:30:39 +08:00
|
|
|
AddInternal(downloadTracker);
|
|
|
|
|
2022-02-16 15:03:08 +08:00
|
|
|
downloadTracker.State.BindValueChanged(_ => Scheduler.AddOnce(updateAvailability), true);
|
2022-02-15 22:30:39 +08:00
|
|
|
downloadTracker.Progress.BindValueChanged(_ =>
|
|
|
|
{
|
|
|
|
if (downloadTracker.State.Value != DownloadState.Downloading)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// incoming progress changes are going to be at a very high rate.
|
|
|
|
// we don't want to flood the network with this, so rate limit how often we send progress updates.
|
|
|
|
if (progressUpdate?.Completed != false)
|
2022-02-16 15:03:08 +08:00
|
|
|
progressUpdate = Scheduler.AddDelayed(updateAvailability, progressUpdate == null ? 0 : 500);
|
2021-12-17 18:21:41 +08:00
|
|
|
}, true);
|
2022-02-15 22:30:39 +08:00
|
|
|
|
|
|
|
// handles changes to hash that didn't occur from the import process (ie. a user editing the beatmap in the editor, somehow).
|
|
|
|
realmSubscription?.Dispose();
|
2022-02-16 15:03:08 +08:00
|
|
|
realmSubscription = realm.RegisterForNotifications(r => filteredBeatmaps(), (items, changes, ___) =>
|
2022-02-15 22:30:39 +08:00
|
|
|
{
|
|
|
|
if (changes == null)
|
|
|
|
return;
|
|
|
|
|
2022-02-16 15:03:08 +08:00
|
|
|
Scheduler.AddOnce(updateAvailability);
|
2022-02-15 22:30:39 +08:00
|
|
|
});
|
2021-01-17 04:00:56 +08:00
|
|
|
}
|
|
|
|
|
2022-02-16 15:03:08 +08:00
|
|
|
private void updateAvailability()
|
2021-01-17 04:00:56 +08:00
|
|
|
{
|
2022-02-16 15:03:08 +08:00
|
|
|
if (downloadTracker == null || selectedBeatmap == null)
|
2021-10-27 19:19:34 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
switch (downloadTracker.State.Value)
|
2021-01-17 04:00:56 +08:00
|
|
|
{
|
|
|
|
case DownloadState.NotDownloaded:
|
|
|
|
availability.Value = BeatmapAvailability.NotDownloaded();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DownloadState.Downloading:
|
2021-10-27 19:19:34 +08:00
|
|
|
availability.Value = BeatmapAvailability.Downloading((float)downloadTracker.Progress.Value);
|
2021-01-17 04:00:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DownloadState.Importing:
|
|
|
|
availability.Value = BeatmapAvailability.Importing();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DownloadState.LocallyAvailable:
|
2022-02-16 15:03:08 +08:00
|
|
|
bool available = filteredBeatmaps().Any();
|
2021-10-27 19:19:34 +08:00
|
|
|
|
2022-02-15 22:11:33 +08:00
|
|
|
availability.Value = available ? BeatmapAvailability.LocallyAvailable() : BeatmapAvailability.NotDownloaded();
|
2021-10-27 19:19:34 +08:00
|
|
|
|
|
|
|
// only display a message to the user if a download seems to have just completed.
|
2022-02-15 22:11:33 +08:00
|
|
|
if (!available && downloadTracker.Progress.Value == 1)
|
2021-10-27 19:19:34 +08:00
|
|
|
Logger.Log("The imported beatmap set does not match the online version.", LoggingTarget.Runtime, LogLevel.Important);
|
|
|
|
|
2021-01-17 04:00:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-10-27 19:19:34 +08:00
|
|
|
throw new ArgumentOutOfRangeException();
|
2021-01-17 04:00:56 +08:00
|
|
|
}
|
|
|
|
}
|
2021-10-27 19:19:34 +08:00
|
|
|
|
2022-02-16 15:03:08 +08:00
|
|
|
private IQueryable<BeatmapInfo> filteredBeatmaps()
|
2021-10-27 19:19:34 +08:00
|
|
|
{
|
2022-02-16 15:03:08 +08:00
|
|
|
int onlineId = selectedBeatmap.OnlineID;
|
|
|
|
string checksum = selectedBeatmap.MD5Hash;
|
2022-02-15 22:50:07 +08:00
|
|
|
|
|
|
|
return realm.Realm
|
|
|
|
.All<BeatmapInfo>()
|
|
|
|
.Filter("OnlineID == $0 && MD5Hash == $1 && BeatmapSet.DeletePending == false", onlineId, checksum);
|
2021-12-14 18:11:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
2021-12-17 18:21:41 +08:00
|
|
|
realmSubscription?.Dispose();
|
2021-10-27 19:19:34 +08:00
|
|
|
}
|
2021-01-17 04:00:56 +08:00
|
|
|
}
|
|
|
|
}
|