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.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using osu.Framework.Bindables;
|
2021-01-18 03:04:12 +08:00
|
|
|
using osu.Framework.Logging;
|
2021-01-17 04:00:56 +08:00
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
|
|
|
|
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>
|
2021-02-05 11:24:38 +08:00
|
|
|
public class OnlinePlayBeatmapAvailablilityTracker : DownloadTrackingComposite<BeatmapSetInfo, BeatmapManager>
|
2021-01-17 04:00:56 +08:00
|
|
|
{
|
|
|
|
public readonly IBindable<PlaylistItem> SelectedItem = new Bindable<PlaylistItem>();
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The availability state of the currently selected playlist item.
|
|
|
|
/// </summary>
|
|
|
|
public IBindable<BeatmapAvailability> Availability => availability;
|
|
|
|
|
|
|
|
private readonly Bindable<BeatmapAvailability> availability = new Bindable<BeatmapAvailability>();
|
|
|
|
|
2021-02-05 11:24:38 +08:00
|
|
|
public OnlinePlayBeatmapAvailablilityTracker()
|
2021-01-17 04:00:56 +08:00
|
|
|
{
|
|
|
|
State.BindValueChanged(_ => updateAvailability());
|
2021-02-01 16:32:54 +08:00
|
|
|
Progress.BindValueChanged(_ => updateAvailability(), true);
|
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;
|
|
|
|
|
|
|
|
Model.Value = item.NewValue.Beatmap.Value.BeatmapSet;
|
|
|
|
}, true);
|
2021-01-17 04:00:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool VerifyDatabasedModel(BeatmapSetInfo databasedSet)
|
|
|
|
{
|
|
|
|
int? beatmapId = SelectedItem.Value.Beatmap.Value.OnlineBeatmapID;
|
|
|
|
string checksum = SelectedItem.Value.Beatmap.Value.MD5Hash;
|
|
|
|
|
2021-01-19 02:34:24 +08:00
|
|
|
var matchingBeatmap = databasedSet.Beatmaps.FirstOrDefault(b => b.OnlineBeatmapID == beatmapId && b.MD5Hash == checksum);
|
2021-01-19 16:51:31 +08:00
|
|
|
|
|
|
|
if (matchingBeatmap == null)
|
|
|
|
{
|
|
|
|
Logger.Log("The imported beatmap set does not match the online version.", LoggingTarget.Runtime, LogLevel.Important);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2021-01-17 04:00:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool IsModelAvailableLocally()
|
|
|
|
{
|
|
|
|
int? beatmapId = SelectedItem.Value.Beatmap.Value.OnlineBeatmapID;
|
|
|
|
string checksum = SelectedItem.Value.Beatmap.Value.MD5Hash;
|
|
|
|
|
|
|
|
var beatmap = Manager.QueryBeatmap(b => b.OnlineBeatmapID == beatmapId && b.MD5Hash == checksum);
|
|
|
|
return beatmap?.BeatmapSet.DeletePending == false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateAvailability()
|
|
|
|
{
|
|
|
|
switch (State.Value)
|
|
|
|
{
|
|
|
|
case DownloadState.NotDownloaded:
|
|
|
|
availability.Value = BeatmapAvailability.NotDownloaded();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DownloadState.Downloading:
|
2021-01-18 02:16:45 +08:00
|
|
|
availability.Value = BeatmapAvailability.Downloading((float)Progress.Value);
|
2021-01-17 04:00:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DownloadState.Importing:
|
|
|
|
availability.Value = BeatmapAvailability.Importing();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DownloadState.LocallyAvailable:
|
|
|
|
availability.Value = BeatmapAvailability.LocallyAvailable();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(State));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|