mirror of
https://github.com/osukey/osukey.git
synced 2025-08-04 15:16:38 +09:00
Update all read queries to use direct realm subscriptions/queries
This commit is contained in:
@ -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
|
||||
{
|
||||
@ -22,6 +23,9 @@ namespace osu.Game.Input.Bindings
|
||||
|
||||
private readonly int? variant;
|
||||
|
||||
private IDisposable realmSubscription;
|
||||
private IQueryable<RealmKeyBinding> realmKeyBindings;
|
||||
|
||||
[Resolved]
|
||||
private RealmKeyBindingStore store { get; set; }
|
||||
|
||||
@ -49,35 +53,42 @@ namespace osu.Game.Input.Bindings
|
||||
|
||||
protected override void LoadComplete()
|
||||
{
|
||||
var realm = realmFactory.Get();
|
||||
|
||||
if (ruleset == null || ruleset.ID.HasValue)
|
||||
{
|
||||
var rulesetId = ruleset?.ID;
|
||||
|
||||
realmKeyBindings = realm.All<RealmKeyBinding>()
|
||||
.Where(b => b.RulesetID == rulesetId && b.Variant == variant);
|
||||
|
||||
realmSubscription = realmKeyBindings
|
||||
.SubscribeForNotifications((sender, changes, error) =>
|
||||
{
|
||||
// first subscription ignored as we are handling this in LoadComplete.
|
||||
if (changes == null)
|
||||
return;
|
||||
|
||||
ReloadMappings();
|
||||
});
|
||||
}
|
||||
|
||||
base.LoadComplete();
|
||||
store.KeyBindingChanged += ReloadMappings;
|
||||
}
|
||||
|
||||
protected override void ReloadMappings()
|
||||
{
|
||||
if (realmKeyBindings != null)
|
||||
KeyBindings = realmKeyBindings.Detach();
|
||||
else
|
||||
base.ReloadMappings();
|
||||
}
|
||||
|
||||
protected override void Dispose(bool isDisposing)
|
||||
{
|
||||
base.Dispose(isDisposing);
|
||||
|
||||
if (store != null)
|
||||
store.KeyBindingChanged -= ReloadMappings;
|
||||
}
|
||||
|
||||
protected override void ReloadMappings()
|
||||
{
|
||||
if (ruleset != null && !ruleset.ID.HasValue)
|
||||
// if the provided ruleset is not stored to the database, we have no way to retrieve custom bindings.
|
||||
// fallback to defaults instead.
|
||||
KeyBindings = DefaultKeyBindings;
|
||||
else
|
||||
{
|
||||
var rulesetId = ruleset?.ID;
|
||||
|
||||
// #1
|
||||
KeyBindings = store.Query(rulesetId, variant).Detach();
|
||||
|
||||
// #2 (Clearly shows lifetime of realm context access)
|
||||
using (var realm = realmFactory.Get())
|
||||
KeyBindings = realm.All<RealmKeyBinding>().Where(b => b.RulesetID == rulesetId && b.Variant == variant).Detach();
|
||||
}
|
||||
realmSubscription?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -21,8 +21,8 @@ namespace osu.Game.Input
|
||||
/// </summary>
|
||||
public event Action? KeyBindingChanged;
|
||||
|
||||
public RealmKeyBindingStore(RealmContextFactory contextFactory, Storage? storage = null)
|
||||
: base(contextFactory, storage)
|
||||
public RealmKeyBindingStore(RealmContextFactory realmFactory, Storage? storage = null)
|
||||
: base(realmFactory, storage)
|
||||
{
|
||||
}
|
||||
|
||||
@ -57,35 +57,13 @@ namespace osu.Game.Input
|
||||
{
|
||||
var instance = ruleset.CreateInstance();
|
||||
|
||||
using (ContextFactory.GetForWrite())
|
||||
using (RealmFactory.GetForWrite())
|
||||
{
|
||||
foreach (var variant in instance.AvailableVariants)
|
||||
insertDefaults(instance.GetDefaultKeyBindings(variant), ruleset.ID, variant);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve all key bindings for the provided specification.
|
||||
/// </summary>
|
||||
/// <param name="rulesetId">An optional ruleset ID. If null, global bindings are returned.</param>
|
||||
/// <param name="variant">An optional ruleset variant. If null, the no-variant bindings are returned.</param>
|
||||
/// <returns>A list of all key bindings found for the query, detached from the database.</returns>
|
||||
public List<RealmKeyBinding> Query(int? rulesetId = null, int? variant = null) => query(rulesetId, variant).ToList();
|
||||
|
||||
/// <summary>
|
||||
/// Retrieve all key bindings for the provided action type.
|
||||
/// </summary>
|
||||
/// <param name="action">The action to lookup.</param>
|
||||
/// <typeparam name="T">The enum type of the action.</typeparam>
|
||||
/// <returns>A list of all key bindings found for the query, detached from the database.</returns>
|
||||
public List<RealmKeyBinding> Query<T>(T action)
|
||||
where T : Enum
|
||||
{
|
||||
int lookup = (int)(object)action;
|
||||
|
||||
return query(null, null).Where(rkb => rkb.Action == lookup).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Update the database mapping for the provided key binding.
|
||||
/// </summary>
|
||||
@ -96,7 +74,7 @@ namespace osu.Game.Input
|
||||
// the incoming instance could already be a live access object.
|
||||
Live<RealmKeyBinding>? realmBinding = keyBinding as Live<RealmKeyBinding>;
|
||||
|
||||
using (var realm = ContextFactory.GetForWrite())
|
||||
using (var realm = RealmFactory.GetForWrite())
|
||||
{
|
||||
if (realmBinding == null)
|
||||
{
|
||||
@ -105,7 +83,7 @@ namespace osu.Game.Input
|
||||
// if neither of the above cases succeeded, retrieve a realm object for further processing.
|
||||
rkb = realm.Context.Find<RealmKeyBinding>(keyBinding.ID);
|
||||
|
||||
realmBinding = new Live<RealmKeyBinding>(rkb, ContextFactory);
|
||||
realmBinding = new Live<RealmKeyBinding>(rkb, RealmFactory);
|
||||
}
|
||||
|
||||
realmBinding.PerformUpdate(modification);
|
||||
@ -116,7 +94,7 @@ namespace osu.Game.Input
|
||||
|
||||
private void insertDefaults(IEnumerable<IKeyBinding> defaults, int? rulesetId = null, int? variant = null)
|
||||
{
|
||||
using (var usage = ContextFactory.GetForWrite())
|
||||
using (var usage = RealmFactory.GetForWrite())
|
||||
{
|
||||
// compare counts in database vs defaults
|
||||
foreach (var group in defaults.GroupBy(k => k.Action))
|
||||
@ -149,6 +127,6 @@ namespace osu.Game.Input
|
||||
/// <param name="rulesetId">An optional ruleset ID. If null, global bindings are returned.</param>
|
||||
/// <param name="variant">An optional ruleset variant. If null, the no-variant bindings are returned.</param>
|
||||
private IQueryable<RealmKeyBinding> query(int? rulesetId = null, int? variant = null) =>
|
||||
ContextFactory.Get().All<RealmKeyBinding>().Where(b => b.RulesetID == rulesetId && b.Variant == variant);
|
||||
RealmFactory.Get().All<RealmKeyBinding>().Where(b => b.RulesetID == rulesetId && b.Variant == variant);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user