From 4f392a867a0fe4af30fa4fdf78e3f5faedac9971 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 20 Oct 2017 16:53:51 +0900 Subject: [PATCH 1/2] Use a transaction for key binding population --- osu.Game/Input/KeyBindingStore.cs | 40 ++++++++++++++++--------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/osu.Game/Input/KeyBindingStore.cs b/osu.Game/Input/KeyBindingStore.cs index 54cf48bc2a..bebbb471ac 100644 --- a/osu.Game/Input/KeyBindingStore.cs +++ b/osu.Game/Input/KeyBindingStore.cs @@ -36,29 +36,31 @@ protected override void Prepare(bool reset = false) private void insertDefaults(IEnumerable defaults, int? rulesetId = null, int? variant = null) { - var context = GetContext(); - - // compare counts in database vs defaults - foreach (var group in defaults.GroupBy(k => k.Action)) + using (var context = GetContext()) + using (var transaction = context.Database.BeginTransaction()) { - int count = query(context, rulesetId, variant).Count(k => (int)k.Action == (int)group.Key); - int aimCount = group.Count(); + // compare counts in database vs defaults + foreach (var group in defaults.GroupBy(k => k.Action)) + { + int count = query(context, rulesetId, variant).Count(k => (int)k.Action == (int)group.Key); + int aimCount = group.Count(); - if (aimCount <= count) - continue; + if (aimCount <= count) + continue; - foreach (var insertable in group.Skip(count).Take(aimCount - count)) - // insert any defaults which are missing. - context.DatabasedKeyBinding.Add(new DatabasedKeyBinding - { - KeyCombination = insertable.KeyCombination, - Action = insertable.Action, - RulesetID = rulesetId, - Variant = variant - }); + foreach (var insertable in group.Skip(count).Take(aimCount - count)) + // insert any defaults which are missing. + context.DatabasedKeyBinding.Add(new DatabasedKeyBinding + { + KeyCombination = insertable.KeyCombination, + Action = insertable.Action, + RulesetID = rulesetId, + Variant = variant + }); + } + + context.SaveChanges(transaction); } - - context.SaveChanges(); } /// From 1a7e23b5c1d422941a749902e42c25b837978480 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Sat, 21 Oct 2017 23:39:31 +0900 Subject: [PATCH 2/2] Don't query database on keypress --- .../Input/Bindings/DatabasedKeyBindingInputManager.cs | 9 +++++++-- osu.Game/Input/KeyBindingStore.cs | 7 ++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/osu.Game/Input/Bindings/DatabasedKeyBindingInputManager.cs b/osu.Game/Input/Bindings/DatabasedKeyBindingInputManager.cs index 8a3c65a35e..6dedf7385b 100644 --- a/osu.Game/Input/Bindings/DatabasedKeyBindingInputManager.cs +++ b/osu.Game/Input/Bindings/DatabasedKeyBindingInputManager.cs @@ -6,6 +6,7 @@ using osu.Framework.Allocation; using osu.Framework.Input.Bindings; using osu.Game.Rulesets; +using System.Linq; namespace osu.Game.Input.Bindings { @@ -44,11 +45,15 @@ protected DatabasedKeyBindingInputManager(RulesetInfo ruleset = null, int? varia private void load(KeyBindingStore keyBindings) { store = keyBindings; + store.KeyBindingChanged += ReloadMappings; } - protected override void ReloadMappings() + protected override void Dispose(bool isDisposing) { - KeyBindings = store.Query(ruleset?.ID, variant); + base.Dispose(isDisposing); + store.KeyBindingChanged -= ReloadMappings; } + + protected override void ReloadMappings() => KeyBindings = store.Query(ruleset?.ID, variant).ToList(); } } diff --git a/osu.Game/Input/KeyBindingStore.cs b/osu.Game/Input/KeyBindingStore.cs index bebbb471ac..07c0319f2f 100644 --- a/osu.Game/Input/KeyBindingStore.cs +++ b/osu.Game/Input/KeyBindingStore.cs @@ -15,6 +15,8 @@ namespace osu.Game.Input { public class KeyBindingStore : DatabaseBackedStore { + public event Action KeyBindingChanged; + public KeyBindingStore(Func getContext, RulesetStore rulesets, Storage storage = null) : base(getContext, storage) { @@ -59,7 +61,8 @@ private void insertDefaults(IEnumerable defaults, int? rulesetId = n }); } - context.SaveChanges(transaction); + context.SaveChanges(); + transaction.Commit(); } } @@ -79,6 +82,8 @@ public void Update(KeyBinding keyBinding) var context = GetContext(); context.Update(keyBinding); context.SaveChanges(); + + KeyBindingChanged?.Invoke(); } } }