1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 23:47:25 +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.

115 lines
4.0 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;
2024-08-28 16:46:36 +08:00
using System.Threading.Tasks;
2024-08-27 16:37:15 +08:00
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]
2024-08-28 16:46:36 +08:00
private void load(CancellationToken cancellationToken)
2024-08-27 16:37:15 +08:00
{
2024-08-28 16:46:36 +08:00
realmSubscription = realm.RegisterForNotifications(r => r.All<BeatmapSetInfo>().Where(s => !s.DeletePending && !s.Protected), beatmapSetsChanged);
loaded.Wait(cancellationToken);
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-28 16:46:36 +08:00
// Detaching beatmaps takes some time, so let's make sure it doesn't run on the update thread.
var frozenSets = sender.Freeze();
Task.Factory.StartNew(() =>
{
realm.Run(_ =>
{
var detached = frozenSets.Detach();
detachedBeatmapSets.Clear();
detachedBeatmapSets.AddRange(detached);
loaded.Set();
});
}, TaskCreationOptions.LongRunning);
2024-08-27 16:37:15 +08:00
return;
}
foreach (int i in changes.DeletedIndices.OrderDescending())
2024-08-28 16:46:36 +08:00
removeAt(i);
2024-08-27 16:37:15 +08:00
foreach (int i in changes.InsertedIndices)
2024-08-28 16:46:36 +08:00
insert(sender[i].Detach(), i);
2024-08-27 16:37:15 +08:00
foreach (int i in changes.NewModifiedIndices)
2024-08-28 16:46:36 +08:00
replaceRange(sender[i].Detach(), i);
}
private void replaceRange(BeatmapSetInfo set, int i)
{
if (loaded.IsSet)
detachedBeatmapSets.ReplaceRange(i, 1, new[] { set });
else
Schedule(() => { detachedBeatmapSets.ReplaceRange(i, 1, new[] { set }); });
}
private void insert(BeatmapSetInfo set, int i)
{
if (loaded.IsSet)
detachedBeatmapSets.Insert(i, set);
else
Schedule(() => { detachedBeatmapSets.Insert(i, set); });
}
private void removeAt(int i)
{
if (loaded.IsSet)
detachedBeatmapSets.RemoveAt(i);
else
Schedule(() => { detachedBeatmapSets.RemoveAt(i); });
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
}
}
}