2021-01-16 20:00:56 +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-11-04 03:30:19 +00:00
|
|
|
using System.Diagnostics;
|
2021-10-27 11:19:34 +00:00
|
|
|
using osu.Framework.Allocation;
|
2021-01-16 20:00:56 +00:00
|
|
|
using osu.Framework.Bindables;
|
2021-10-27 12:26:26 +00:00
|
|
|
using osu.Framework.Graphics;
|
2021-10-27 11:19:34 +00:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-01-17 19:04:12 +00:00
|
|
|
using osu.Framework.Logging;
|
2021-02-05 08:34:05 +00:00
|
|
|
using osu.Framework.Threading;
|
2021-01-16 20:00:56 +00:00
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
|
|
|
|
namespace osu.Game.Online.Rooms
|
|
|
|
{
|
2021-01-19 08:57:40 +00: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>
|
2021-10-27 12:26:26 +00:00
|
|
|
public sealed class OnlinePlayBeatmapAvailabilityTracker : CompositeDrawable
|
2021-01-16 20:00:56 +00:00
|
|
|
{
|
|
|
|
public readonly IBindable<PlaylistItem> SelectedItem = new Bindable<PlaylistItem>();
|
|
|
|
|
2021-10-29 02:57:54 +00: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 12:26:26 +00:00
|
|
|
protected override bool RequiresChildrenUpdate => true;
|
|
|
|
|
2021-10-27 11:19:34 +00:00
|
|
|
[Resolved]
|
|
|
|
private BeatmapManager beatmapManager { get; set; }
|
|
|
|
|
2021-01-16 20:00:56 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The availability state of the currently selected playlist item.
|
|
|
|
/// </summary>
|
|
|
|
public IBindable<BeatmapAvailability> Availability => availability;
|
|
|
|
|
2021-10-27 15:59:48 +00:00
|
|
|
private readonly Bindable<BeatmapAvailability> availability = new Bindable<BeatmapAvailability>(BeatmapAvailability.NotDownloaded());
|
2021-02-05 08:34:05 +00:00
|
|
|
|
|
|
|
private ScheduledDelegate progressUpdate;
|
2021-01-16 20:00:56 +00:00
|
|
|
|
2021-10-27 11:19:34 +00:00
|
|
|
private BeatmapDownloadTracker downloadTracker;
|
|
|
|
|
2021-12-14 10:11:56 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The beatmap matching the required hash (and providing a final <see cref="BeatmapAvailability.LocallyAvailable"/> state).
|
|
|
|
/// </summary>
|
|
|
|
private BeatmapInfo matchingHash;
|
|
|
|
|
2021-01-16 20:00:56 +00:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2021-02-05 08:19:23 +00: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 12:26:26 +00:00
|
|
|
downloadTracker?.RemoveAndDisposeImmediately();
|
2021-01-16 20:00:56 +00:00
|
|
|
|
2021-11-04 03:30:19 +00:00
|
|
|
Debug.Assert(item.NewValue.Beatmap.Value.BeatmapSet != null);
|
|
|
|
|
2021-10-27 12:26:26 +00:00
|
|
|
downloadTracker = new BeatmapDownloadTracker(item.NewValue.Beatmap.Value.BeatmapSet);
|
2021-11-02 19:32:21 +00:00
|
|
|
|
|
|
|
AddInternal(downloadTracker);
|
|
|
|
|
|
|
|
downloadTracker.State.BindValueChanged(_ => updateAvailability(), true);
|
2021-10-27 11:19:34 +00:00
|
|
|
downloadTracker.Progress.BindValueChanged(_ =>
|
|
|
|
{
|
|
|
|
if (downloadTracker.State.Value != DownloadState.Downloading)
|
|
|
|
return;
|
2021-01-19 08:51:31 +00:00
|
|
|
|
2021-10-27 11:19:34 +00:00
|
|
|
// 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)
|
|
|
|
progressUpdate = Scheduler.AddDelayed(updateAvailability, progressUpdate == null ? 0 : 500);
|
2021-11-02 19:32:21 +00:00
|
|
|
}, true);
|
2021-10-27 11:19:34 +00:00
|
|
|
}, true);
|
2021-12-14 10:11:56 +00:00
|
|
|
|
|
|
|
// These events are needed for a fringe case where a modified/altered beatmap is imported with matching OnlineIDs.
|
|
|
|
// During the import process this will cause the existing beatmap set to be silently deleted and replaced with the new one.
|
|
|
|
// This is not exposed to us via `BeatmapDownloadTracker` so we have to take it into our own hands (as we care about the hash matching).
|
|
|
|
beatmapManager.ItemUpdated += itemUpdated;
|
|
|
|
beatmapManager.ItemRemoved += itemRemoved;
|
2021-01-16 20:00:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 10:11:56 +00:00
|
|
|
private void itemUpdated(BeatmapSetInfo item) => Schedule(() =>
|
|
|
|
{
|
|
|
|
if (matchingHash?.BeatmapSet.ID == item.ID || SelectedItem.Value?.Beatmap.Value.BeatmapSet?.OnlineID == item.OnlineID)
|
|
|
|
updateAvailability();
|
|
|
|
});
|
|
|
|
|
|
|
|
private void itemRemoved(BeatmapSetInfo item) => Schedule(() =>
|
|
|
|
{
|
|
|
|
if (matchingHash?.BeatmapSet.ID == item.ID)
|
|
|
|
updateAvailability();
|
|
|
|
});
|
|
|
|
|
2021-01-16 20:00:56 +00:00
|
|
|
private void updateAvailability()
|
|
|
|
{
|
2021-10-27 11:19:34 +00:00
|
|
|
if (downloadTracker == null)
|
|
|
|
return;
|
|
|
|
|
2021-12-14 10:11:56 +00:00
|
|
|
// will be repopulated below if still valid.
|
|
|
|
matchingHash = null;
|
|
|
|
|
2021-10-27 11:19:34 +00:00
|
|
|
switch (downloadTracker.State.Value)
|
2021-01-16 20:00:56 +00:00
|
|
|
{
|
|
|
|
case DownloadState.NotDownloaded:
|
|
|
|
availability.Value = BeatmapAvailability.NotDownloaded();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DownloadState.Downloading:
|
2021-10-27 11:19:34 +00:00
|
|
|
availability.Value = BeatmapAvailability.Downloading((float)downloadTracker.Progress.Value);
|
2021-01-16 20:00:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DownloadState.Importing:
|
|
|
|
availability.Value = BeatmapAvailability.Importing();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DownloadState.LocallyAvailable:
|
2021-12-14 10:11:56 +00:00
|
|
|
matchingHash = findMatchingHash();
|
|
|
|
|
|
|
|
bool hashMatches = matchingHash != null;
|
2021-10-27 11:19:34 +00:00
|
|
|
|
|
|
|
availability.Value = hashMatches ? BeatmapAvailability.LocallyAvailable() : BeatmapAvailability.NotDownloaded();
|
|
|
|
|
|
|
|
// only display a message to the user if a download seems to have just completed.
|
|
|
|
if (!hashMatches && downloadTracker.Progress.Value == 1)
|
|
|
|
Logger.Log("The imported beatmap set does not match the online version.", LoggingTarget.Runtime, LogLevel.Important);
|
|
|
|
|
2021-01-16 20:00:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-10-27 11:19:34 +00:00
|
|
|
throw new ArgumentOutOfRangeException();
|
2021-01-16 20:00:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-27 11:19:34 +00:00
|
|
|
|
2021-12-14 10:11:56 +00:00
|
|
|
private BeatmapInfo findMatchingHash()
|
2021-10-27 11:19:34 +00:00
|
|
|
{
|
|
|
|
int onlineId = SelectedItem.Value.Beatmap.Value.OnlineID;
|
|
|
|
string checksum = SelectedItem.Value.Beatmap.Value.MD5Hash;
|
|
|
|
|
2021-12-14 10:11:56 +00:00
|
|
|
return beatmapManager.QueryBeatmap(b => b.OnlineID == onlineId && b.MD5Hash == checksum && !b.BeatmapSet.DeletePending);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
if (beatmapManager != null)
|
|
|
|
{
|
|
|
|
beatmapManager.ItemUpdated -= itemUpdated;
|
|
|
|
beatmapManager.ItemRemoved -= itemRemoved;
|
|
|
|
}
|
2021-10-27 11:19:34 +00:00
|
|
|
}
|
2021-01-16 20:00:56 +00:00
|
|
|
}
|
|
|
|
}
|