1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 08:52:55 +08:00

Fix DatabasedKeyBindingContainer re-querying realm on receiving notification

This commit is contained in:
Dean Herbert 2022-01-25 16:44:44 +09:00
parent 0d575006fb
commit 1f9cf00db8

View File

@ -8,6 +8,7 @@ using osu.Framework.Allocation;
using osu.Framework.Input.Bindings;
using osu.Game.Database;
using osu.Game.Rulesets;
using Realms;
namespace osu.Game.Input.Bindings
{
@ -46,41 +47,36 @@ namespace osu.Game.Input.Bindings
throw new InvalidOperationException($"{nameof(variant)} can not be null when a non-null {nameof(ruleset)} is provided.");
}
private IQueryable<RealmKeyBinding> queryRealmKeyBindings()
{
string rulesetName = ruleset?.ShortName;
return realm.Realm.All<RealmKeyBinding>()
.Where(b => b.RulesetName == rulesetName && b.Variant == variant);
}
protected override void LoadComplete()
{
realmSubscription = realm.RegisterForNotifications(r => queryRealmKeyBindings(), (sender, changes, error) =>
realmSubscription = realm.RegisterForNotifications(queryRealmKeyBindings, (sender, changes, error) =>
{
// The first fire of this is a bit redundant as this is being called in base.LoadComplete,
// but this is safest in case the subscription is restored after a context recycle.
ReloadMappings();
reloadMappings(sender.AsQueryable());
});
base.LoadComplete();
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
protected override void ReloadMappings() => reloadMappings(queryRealmKeyBindings(realm.Realm));
realmSubscription?.Dispose();
private IQueryable<RealmKeyBinding> queryRealmKeyBindings(Realm realm)
{
string rulesetName = ruleset?.ShortName;
return realm.All<RealmKeyBinding>()
.Where(b => b.RulesetName == rulesetName && b.Variant == variant);
}
protected override void ReloadMappings()
private void reloadMappings(IQueryable<RealmKeyBinding> realmKeyBindings)
{
var defaults = DefaultKeyBindings.ToList();
List<RealmKeyBinding> newBindings = queryRealmKeyBindings().Detach()
// this ordering is important to ensure that we read entries from the database in the order
// enforced by DefaultKeyBindings. allow for song select to handle actions that may otherwise
// have been eaten by the music controller due to query order.
.OrderBy(b => defaults.FindIndex(d => (int)d.Action == b.ActionInt)).ToList();
List<RealmKeyBinding> newBindings = realmKeyBindings.Detach()
// this ordering is important to ensure that we read entries from the database in the order
// enforced by DefaultKeyBindings. allow for song select to handle actions that may otherwise
// have been eaten by the music controller due to query order.
.OrderBy(b => defaults.FindIndex(d => (int)d.Action == b.ActionInt)).ToList();
// In the case no bindings were found in the database, presume this usage is for a non-databased ruleset.
// This actually should never be required and can be removed if it is ever deemed to cause a problem.
@ -91,5 +87,12 @@ namespace osu.Game.Input.Bindings
else
KeyBindings = newBindings;
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
realmSubscription?.Dispose();
}
}
}