Add automatic downloading support when spectating a multiplayer room

This commit is contained in:
Dean Herbert 2024-09-02 16:29:41 +09:00
parent fb66e9bfa2
commit 38a62eed44
No known key found for this signature in database

View File

@ -3,12 +3,19 @@
#nullable disable
using System.Linq;
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Graphics;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Online.Multiplayer;
using osu.Game.Online.Rooms;
using osuTK;
namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match
@ -46,10 +53,11 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match
}
[BackgroundDependencyLoader]
private void load()
private void load(OsuConfigManager config)
{
operationInProgress = ongoingOperationTracker.InProgress.GetBoundCopy();
operationInProgress.BindValueChanged(_ => updateState());
automaticallyDownload = config.GetBindable<bool>(OsuSetting.AutomaticallyDownloadMissingBeatmaps);
}
protected override void OnRoomUpdated()
@ -77,6 +85,70 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match
button.Enabled.Value = Client.Room != null
&& Client.Room.State != MultiplayerRoomState.Closed
&& !operationInProgress.Value;
Scheduler.AddOnce(checkForAutomaticDownload);
}
#region Automatic download handling
[Resolved]
private BeatmapLookupCache beatmapLookupCache { get; set; }
[Resolved]
private BeatmapModelDownloader beatmapDownloader { get; set; } = null!;
[Resolved]
private BeatmapManager beatmaps { get; set; }
private CancellationTokenSource downloadCheckCancellation;
private Bindable<bool> automaticallyDownload;
protected override void PlaylistItemChanged(MultiplayerPlaylistItem item)
{
base.PlaylistItemChanged(item);
Scheduler.AddOnce(checkForAutomaticDownload);
}
private void checkForAutomaticDownload()
{
MultiplayerPlaylistItem item = Client.Room?.Playlist.FirstOrDefault(i => !i.Expired);
downloadCheckCancellation?.Cancel();
if (item == null)
return;
if (!automaticallyDownload.Value)
return;
// While we can support automatic downloads when not spectating, there are some usability concerns.
// - In host rotate mode, this could potentially be unwanted by some users (even though they want automatic downloads everywhere else).
// - When first joining a room, the expectation should be that the user is checking out the room, and they may not immediately want to download the selected beatmap.
//
// Rather than over-complicating this flow, let's only auto-download when spectating for the time being.
// A potential path forward would be to have a local auto-download checkbox above the playlist item list area.
if (Client.LocalUser?.State != MultiplayerUserState.Spectating)
return;
// In a perfect world we'd use BeatmapAvailability, but there's no event-driven flow for when a selection changes.
// ie. if selection changes from "not downloaded" to another "not downloaded" we wouldn't get a value changed raised.
beatmapLookupCache
.GetBeatmapAsync(item.BeatmapID, (downloadCheckCancellation = new CancellationTokenSource()).Token)
.ContinueWith(resolved => Schedule(() =>
{
var beatmapSet = resolved.GetResultSafely()?.BeatmapSet;
if (beatmapSet == null)
return;
if (beatmaps.IsAvailableLocally(new BeatmapSetInfo { OnlineID = beatmapSet.OnlineID }))
return;
beatmapDownloader.Download(beatmapSet);
}));
}
#endregion
}
}