osu/osu.Game/Beatmaps/Drawables/BeatmapSetDownloader.cs

85 lines
2.6 KiB
C#
Raw Normal View History

2018-06-04 09:08:39 +00: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;
using osu.Framework.Graphics;
2018-06-04 09:08:39 +00:00
namespace osu.Game.Beatmaps.Drawables
2018-06-04 09:08:39 +00:00
{
2018-06-08 11:53:58 +00:00
/// <summary>
/// A component to allow downloading of a beatmap set. Automatically handles state syncing between other instances.
/// </summary>
public class BeatmapSetDownloader : Component
2018-06-04 09:08:39 +00:00
{
private readonly BeatmapSetInfo set;
private readonly bool noVideo;
private BeatmapManager beatmaps;
2018-06-08 11:53:58 +00:00
/// <summary>
/// Whether the associated beatmap set has been downloading (by this instance or any other instance).
/// </summary>
public readonly BindableBool Downloaded = new BindableBool();
2018-06-04 09:08:39 +00:00
public BeatmapSetDownloader(BeatmapSetInfo set, bool noVideo = false)
2018-06-04 09:08:39 +00:00
{
this.set = set;
this.noVideo = noVideo;
}
[BackgroundDependencyLoader]
2018-06-04 10:10:33 +00:00
private void load(BeatmapManager beatmaps)
2018-06-04 09:08:39 +00:00
{
this.beatmaps = beatmaps;
2018-06-04 09:08:39 +00:00
beatmaps.ItemAdded += setAdded;
beatmaps.ItemRemoved += setRemoved;
2018-06-04 09:08:39 +00:00
// initial value
if (set.OnlineBeatmapSetID != null)
2018-06-08 11:54:09 +00:00
Downloaded.Value = beatmaps.QueryBeatmapSets(s => s.OnlineBeatmapSetID == set.OnlineBeatmapSetID && !s.DeletePending).Any();
2018-06-04 09:08:39 +00:00
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (beatmaps != null)
{
beatmaps.ItemAdded -= setAdded;
beatmaps.ItemRemoved -= setRemoved;
}
}
2018-06-08 11:53:58 +00:00
/// <summary>
/// Begin downloading the associated beatmap set.
/// </summary>
/// <returns>True if downloading began. False if an existing download is active or completed.</returns>
public bool Download()
{
if (Downloaded.Value)
return false;
if (beatmaps.GetExistingDownload(set) != null)
return false;
beatmaps.Download(set, noVideo);
return true;
}
private void setAdded(BeatmapSetInfo s)
{
if (s.OnlineBeatmapSetID == set.OnlineBeatmapSetID)
Downloaded.Value = true;
}
private void setRemoved(BeatmapSetInfo s)
{
if (s.OnlineBeatmapSetID == set.OnlineBeatmapSetID)
Downloaded.Value = false;
}
2018-06-04 09:08:39 +00:00
}
}