mirror of
https://github.com/ppy/osu.git
synced 2024-11-11 10:33:30 +08:00
107 lines
3.5 KiB
C#
107 lines
3.5 KiB
C#
// 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.IO;
|
|
using System.Linq;
|
|
using osu.Framework.Allocation;
|
|
using osu.Framework.Bindables;
|
|
using osu.Framework.Input.Events;
|
|
using osu.Game.Beatmaps;
|
|
using osu.Game.Beatmaps.Drawables.Cards;
|
|
using osu.Game.Configuration;
|
|
using osu.Game.Online.API.Requests.Responses;
|
|
using osu.Game.Overlays.Notifications;
|
|
using osu.Game.Scoring;
|
|
using Realms;
|
|
|
|
namespace osu.Game.Database
|
|
{
|
|
public partial class MissingBeatmapNotification : ProgressNotification
|
|
{
|
|
[Resolved]
|
|
private BeatmapModelDownloader beatmapDownloader { get; set; } = null!;
|
|
|
|
[Resolved]
|
|
private ScoreManager scoreManager { get; set; } = null!;
|
|
|
|
[Resolved]
|
|
private RealmAccess realm { get; set; } = null!;
|
|
|
|
private readonly MemoryStream scoreStream;
|
|
private readonly APIBeatmapSet beatmapSetInfo;
|
|
private readonly string beatmapHash;
|
|
|
|
private Bindable<bool> autodownloadConfig = null!;
|
|
private Bindable<bool> noVideoSetting = null!;
|
|
private BeatmapCardNano card = null!;
|
|
|
|
private IDisposable? realmSubscription;
|
|
|
|
public MissingBeatmapNotification(APIBeatmap beatmap, MemoryStream scoreStream, string beatmapHash)
|
|
{
|
|
beatmapSetInfo = beatmap.BeatmapSet!;
|
|
|
|
this.beatmapHash = beatmapHash;
|
|
this.scoreStream = scoreStream;
|
|
}
|
|
|
|
[BackgroundDependencyLoader]
|
|
private void load(OsuConfigManager config)
|
|
{
|
|
Text = "You do not have the required beatmap for this replay";
|
|
|
|
realmSubscription = realm.RegisterForNotifications(
|
|
realm => realm.All<BeatmapSetInfo>().Where(s => !s.DeletePending), beatmapsChanged);
|
|
|
|
realm.Run(r =>
|
|
{
|
|
if (r.All<BeatmapSetInfo>().Any(s => !s.DeletePending && s.OnlineID == beatmapSetInfo.OnlineID))
|
|
{
|
|
Text = "You have the corresponding beatmapset but no beatmap, you may need to update the beatmapset.";
|
|
}
|
|
});
|
|
|
|
autodownloadConfig = config.GetBindable<bool>(OsuSetting.AutomaticallyDownloadWhenSpectating);
|
|
noVideoSetting = config.GetBindable<bool>(OsuSetting.PreferNoVideo);
|
|
|
|
Content.Add(card = new BeatmapCardNano(beatmapSetInfo));
|
|
}
|
|
|
|
protected override void LoadComplete()
|
|
{
|
|
base.LoadComplete();
|
|
|
|
if (autodownloadConfig.Value)
|
|
beatmapDownloader.Download(beatmapSetInfo, noVideoSetting.Value);
|
|
}
|
|
|
|
protected override void Update()
|
|
{
|
|
base.Update();
|
|
card.Width = Content.DrawWidth;
|
|
}
|
|
|
|
protected override bool OnHover(HoverEvent e) => false;
|
|
protected override void OnHoverLost(HoverLostEvent e) { }
|
|
|
|
private void beatmapsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet? changes)
|
|
{
|
|
if (changes?.InsertedIndices == null) return;
|
|
|
|
if (sender.Any(s => s.Beatmaps.Any(b => b.MD5Hash == beatmapHash)))
|
|
{
|
|
var importTask = new ImportTask(scoreStream, "score.osr");
|
|
scoreManager.Import(this, new[] { importTask });
|
|
realmSubscription?.Dispose();
|
|
}
|
|
}
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
{
|
|
base.Dispose(isDisposing);
|
|
realmSubscription?.Dispose();
|
|
}
|
|
}
|
|
}
|