1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 15:47:26 +08:00
osu-lazer/osu.Game/Database/DetachedBeatmapStore.cs

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

82 lines
2.9 KiB
C#
Raw Normal View History

2024-08-27 16:37:15 +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;
using System.Threading;
using osu.Framework.Allocation;
2024-08-27 17:13:52 +08:00
using osu.Framework.Bindables;
2024-08-27 16:37:15 +08:00
using osu.Framework.Graphics;
using osu.Game.Beatmaps;
using Realms;
namespace osu.Game.Database
{
public partial class DetachedBeatmapStore : Component
{
private readonly ManualResetEventSlim loaded = new ManualResetEventSlim();
2024-08-27 17:13:52 +08:00
private readonly BindableList<BeatmapSetInfo> detachedBeatmapSets = new BindableList<BeatmapSetInfo>();
2024-08-27 16:37:15 +08:00
2024-08-27 17:13:52 +08:00
private IDisposable? realmSubscription;
2024-08-27 16:37:15 +08:00
[Resolved]
private RealmAccess realm { get; set; } = null!;
public IBindableList<BeatmapSetInfo> GetDetachedBeatmaps(CancellationToken cancellationToken)
2024-08-27 16:37:15 +08:00
{
loaded.Wait(cancellationToken);
2024-08-27 17:13:52 +08:00
return detachedBeatmapSets.GetBoundCopy();
2024-08-27 16:37:15 +08:00
}
[BackgroundDependencyLoader]
private void load()
{
2024-08-27 17:13:52 +08:00
realmSubscription = realm.RegisterForNotifications(getBeatmapSets, beatmapSetsChanged);
2024-08-27 16:37:15 +08:00
}
private void beatmapSetsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet? changes)
{
if (changes == null)
{
2024-08-27 17:13:52 +08:00
if (detachedBeatmapSets.Count > 0 && sender.Count == 0)
2024-08-27 16:37:15 +08:00
{
// Usually we'd reset stuff here, but doing so triggers a silly flow which ends up deadlocking realm.
// Additionally, user should not be at song select when realm is blocking all operations in the first place.
//
// Note that due to the catch-up logic below, once operations are restored we will still be in a roughly
// correct state. The only things that this return will change is the carousel will not empty *during* the blocking
// operation.
return;
}
2024-08-27 17:13:52 +08:00
detachedBeatmapSets.Clear();
detachedBeatmapSets.AddRange(sender.Detach());
2024-08-27 16:37:15 +08:00
loaded.Set();
return;
}
foreach (int i in changes.DeletedIndices.OrderDescending())
2024-08-27 17:13:52 +08:00
detachedBeatmapSets.RemoveAt(i);
2024-08-27 16:37:15 +08:00
foreach (int i in changes.InsertedIndices)
{
2024-08-27 17:13:52 +08:00
detachedBeatmapSets.Insert(i, sender[i].Detach());
2024-08-27 16:37:15 +08:00
}
foreach (int i in changes.NewModifiedIndices)
2024-08-27 17:13:52 +08:00
detachedBeatmapSets.ReplaceRange(i, 1, new[] { sender[i].Detach() });
2024-08-27 16:37:15 +08:00
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
loaded.Set();
2024-08-27 17:13:52 +08:00
realmSubscription?.Dispose();
2024-08-27 16:37:15 +08:00
}
private IQueryable<BeatmapSetInfo> getBeatmapSets(Realm realm) => realm.All<BeatmapSetInfo>().Where(s => !s.DeletePending && !s.Protected);
}
}