2018-06-04 17:08:39 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Configuration;
|
2018-06-05 07:12:43 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
2018-07-03 21:43:42 +08:00
|
|
|
|
using osu.Game.Online.API.Requests;
|
2018-06-04 17:08:39 +08:00
|
|
|
|
|
2018-06-05 07:30:54 +08:00
|
|
|
|
namespace osu.Game.Beatmaps.Drawables
|
2018-06-04 17:08:39 +08:00
|
|
|
|
{
|
2018-06-08 19:53:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A component to allow downloading of a beatmap set. Automatically handles state syncing between other instances.
|
|
|
|
|
/// </summary>
|
2018-06-06 05:09:26 +08:00
|
|
|
|
public class BeatmapSetDownloader : Component
|
2018-06-04 17:08:39 +08:00
|
|
|
|
{
|
|
|
|
|
private readonly BeatmapSetInfo set;
|
|
|
|
|
private readonly bool noVideo;
|
|
|
|
|
|
2018-06-04 17:34:48 +08:00
|
|
|
|
private BeatmapManager beatmaps;
|
|
|
|
|
|
2018-06-08 19:53:58 +08:00
|
|
|
|
/// <summary>
|
2018-07-03 21:43:42 +08:00
|
|
|
|
/// Holds the current download state of the beatmap, whether is has already been downloaded, is in progress, or is not downloaded.
|
2018-06-08 19:53:58 +08:00
|
|
|
|
/// </summary>
|
2018-07-03 21:43:42 +08:00
|
|
|
|
public readonly Bindable<DownloadStatus> DownloadState = new Bindable<DownloadStatus>();
|
2018-06-04 17:08:39 +08:00
|
|
|
|
|
2018-06-05 07:12:43 +08:00
|
|
|
|
public BeatmapSetDownloader(BeatmapSetInfo set, bool noVideo = false)
|
2018-06-04 17:08:39 +08:00
|
|
|
|
{
|
|
|
|
|
this.set = set;
|
|
|
|
|
this.noVideo = noVideo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2018-06-04 18:10:33 +08:00
|
|
|
|
private void load(BeatmapManager beatmaps)
|
2018-06-04 17:08:39 +08:00
|
|
|
|
{
|
2018-06-04 17:34:48 +08:00
|
|
|
|
this.beatmaps = beatmaps;
|
2018-06-04 17:08:39 +08:00
|
|
|
|
|
2018-06-04 17:34:48 +08:00
|
|
|
|
beatmaps.ItemAdded += setAdded;
|
|
|
|
|
beatmaps.ItemRemoved += setRemoved;
|
2018-07-03 21:43:42 +08:00
|
|
|
|
beatmaps.BeatmapDownloadBegan += downloadBegan;
|
|
|
|
|
beatmaps.BeatmapDownloadFailed += downloadFailed;
|
2018-06-04 17:08:39 +08:00
|
|
|
|
|
2018-06-04 17:47:39 +08:00
|
|
|
|
// initial value
|
2018-07-03 21:43:42 +08:00
|
|
|
|
if (set.OnlineBeatmapSetID != null && beatmaps.QueryBeatmapSets(s => s.OnlineBeatmapSetID == set.OnlineBeatmapSetID && !s.DeletePending).Any())
|
|
|
|
|
DownloadState.Value = DownloadStatus.Downloaded;
|
|
|
|
|
else if (beatmaps.GetExistingDownload(set) != null)
|
|
|
|
|
DownloadState.Value = DownloadStatus.Downloading;
|
|
|
|
|
else
|
|
|
|
|
DownloadState.Value = DownloadStatus.NotDownloaded;
|
2018-06-04 17:08:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-04 17:34:48 +08:00
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
|
|
if (beatmaps != null)
|
|
|
|
|
{
|
|
|
|
|
beatmaps.ItemAdded -= setAdded;
|
|
|
|
|
beatmaps.ItemRemoved -= setRemoved;
|
2018-07-03 21:43:42 +08:00
|
|
|
|
beatmaps.BeatmapDownloadBegan -= downloadBegan;
|
|
|
|
|
beatmaps.BeatmapDownloadFailed -= downloadFailed;
|
2018-06-04 17:34:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-08 19:53:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Begin downloading the associated beatmap set.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>True if downloading began. False if an existing download is active or completed.</returns>
|
2018-07-03 21:43:42 +08:00
|
|
|
|
public void Download()
|
2018-06-04 17:47:39 +08:00
|
|
|
|
{
|
2018-07-03 21:43:42 +08:00
|
|
|
|
if (DownloadState.Value > DownloadStatus.NotDownloaded)
|
|
|
|
|
return;
|
2018-06-05 07:12:43 +08:00
|
|
|
|
|
2018-10-22 11:17:23 +08:00
|
|
|
|
if (beatmaps.Download(set, noVideo))
|
|
|
|
|
{
|
2018-10-18 20:51:05 +08:00
|
|
|
|
// Only change state if download can happen
|
|
|
|
|
DownloadState.Value = DownloadStatus.Downloading;
|
|
|
|
|
}
|
2018-06-04 17:47:39 +08:00
|
|
|
|
}
|
2018-06-04 17:34:48 +08:00
|
|
|
|
|
|
|
|
|
private void setAdded(BeatmapSetInfo s)
|
|
|
|
|
{
|
|
|
|
|
if (s.OnlineBeatmapSetID == set.OnlineBeatmapSetID)
|
2018-07-03 21:43:42 +08:00
|
|
|
|
DownloadState.Value = DownloadStatus.Downloaded;
|
2018-06-04 17:34:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setRemoved(BeatmapSetInfo s)
|
|
|
|
|
{
|
|
|
|
|
if (s.OnlineBeatmapSetID == set.OnlineBeatmapSetID)
|
2018-07-03 21:43:42 +08:00
|
|
|
|
DownloadState.Value = DownloadStatus.NotDownloaded;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void downloadBegan(DownloadBeatmapSetRequest d)
|
|
|
|
|
{
|
|
|
|
|
if (d.BeatmapSet.OnlineBeatmapSetID == set.OnlineBeatmapSetID)
|
|
|
|
|
DownloadState.Value = DownloadStatus.Downloading;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void downloadFailed(DownloadBeatmapSetRequest d)
|
|
|
|
|
{
|
|
|
|
|
if (d.BeatmapSet.OnlineBeatmapSetID == set.OnlineBeatmapSetID)
|
|
|
|
|
DownloadState.Value = DownloadStatus.NotDownloaded;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum DownloadStatus
|
|
|
|
|
{
|
|
|
|
|
NotDownloaded,
|
|
|
|
|
Downloading,
|
|
|
|
|
Downloaded,
|
2018-06-04 17:34:48 +08:00
|
|
|
|
}
|
2018-06-04 17:08:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|