1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-17 06:02:55 +08:00
osu-lazer/osu.Game/Database/MissingBeatmapNotification.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

104 lines
3.6 KiB
C#
Raw Normal View History

2023-09-04 15:17:21 +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;
2023-09-04 15:17:21 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables.Cards;
2023-09-04 15:17:21 +08:00
using osu.Game.Configuration;
using osu.Game.IO.Archives;
using osu.Game.Localisation;
2023-09-04 15:17:21 +08:00
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Overlays.Notifications;
using osu.Game.Scoring;
using Realms;
2023-09-04 15:17:21 +08:00
namespace osu.Game.Database
{
public partial class MissingBeatmapNotification : SimpleNotification
2023-09-04 15:17:21 +08:00
{
[Resolved]
private BeatmapModelDownloader beatmapDownloader { get; set; } = null!;
[Resolved]
private ScoreManager scoreManager { get; set; } = null!;
[Resolved]
private RealmAccess realm { get; set; } = null!;
2023-09-04 15:17:21 +08:00
private readonly ArchiveReader scoreArchive;
2023-09-04 15:17:21 +08:00
private readonly APIBeatmapSet beatmapSetInfo;
private readonly string beatmapHash;
2023-09-04 15:17:21 +08:00
private Bindable<bool> autoDownloadConfig = null!;
2023-09-04 16:14:04 +08:00
private Bindable<bool> noVideoSetting = null!;
private BeatmapCardNano card = null!;
2023-09-04 15:17:21 +08:00
private IDisposable? realmSubscription;
public MissingBeatmapNotification(APIBeatmap beatmap, ArchiveReader scoreArchive, string beatmapHash)
2023-09-04 15:17:21 +08:00
{
beatmapSetInfo = beatmap.BeatmapSet!;
this.beatmapHash = beatmapHash;
this.scoreArchive = scoreArchive;
2023-09-04 15:17:21 +08:00
}
[BackgroundDependencyLoader]
private void load(OsuConfigManager config)
2023-09-04 15:17:21 +08:00
{
realmSubscription = realm.RegisterForNotifications(
realm => realm.All<BeatmapSetInfo>().Where(s => !s.DeletePending), beatmapsChanged);
autoDownloadConfig = config.GetBindable<bool>(OsuSetting.AutomaticallyDownloadMissingBeatmaps);
2023-09-04 16:14:04 +08:00
noVideoSetting = config.GetBindable<bool>(OsuSetting.PreferNoVideo);
2023-09-04 15:17:21 +08:00
Content.Add(card = new BeatmapCardNano(beatmapSetInfo));
2023-09-04 16:14:04 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
2023-09-04 15:17:21 +08:00
if (autoDownloadConfig.Value)
{
Text = NotificationsStrings.DownloadingBeatmapForReplay;
2023-09-04 16:14:04 +08:00
beatmapDownloader.Download(beatmapSetInfo, noVideoSetting.Value);
}
else
{
bool missingSetMatchesExistingOnlineId = realm.Run(r => r.All<BeatmapSetInfo>().Any(s => !s.DeletePending && s.OnlineID == beatmapSetInfo.OnlineID));
Text = missingSetMatchesExistingOnlineId ? NotificationsStrings.MismatchingBeatmapForReplay : NotificationsStrings.MissingBeatmapForReplay;
}
2023-09-04 15:17:21 +08:00
}
protected override void Update()
{
base.Update();
card.Width = Content.DrawWidth;
}
private void beatmapsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet? changes)
2023-09-04 15:17:21 +08:00
{
if (changes?.InsertedIndices == null) return;
2023-09-04 15:17:21 +08:00
if (sender.Any(s => s.Beatmaps.Any(b => b.MD5Hash == beatmapHash)))
{
string name = scoreArchive.Filenames.First(f => f.EndsWith(".osr", StringComparison.OrdinalIgnoreCase));
var importTask = new ImportTask(scoreArchive.GetStream(name), name);
scoreManager.Import(new[] { importTask });
realmSubscription?.Dispose();
Close(false);
}
2023-09-04 15:17:21 +08:00
}
2023-09-04 23:21:08 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
realmSubscription?.Dispose();
}
2023-09-04 15:17:21 +08:00
}
}