Merge pull request #1396 from peppy/key-binding-store-improvements

Improve performance and db safety of KeyBindings
This commit is contained in:
Dean Herbert 2017-10-22 17:39:31 +09:00 committed by GitHub
commit f86a13ef61
2 changed files with 33 additions and 21 deletions

View File

@ -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();
}
}

View File

@ -15,6 +15,8 @@ namespace osu.Game.Input
{
public class KeyBindingStore : DatabaseBackedStore
{
public event Action KeyBindingChanged;
public KeyBindingStore(Func<OsuDbContext> getContext, RulesetStore rulesets, Storage storage = null)
: base(getContext, storage)
{
@ -36,29 +38,32 @@ protected override void Prepare(bool reset = false)
private void insertDefaults(IEnumerable<KeyBinding> 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.Commit();
}
context.SaveChanges();
}
/// <summary>
@ -77,6 +82,8 @@ public void Update(KeyBinding keyBinding)
var context = GetContext();
context.Update(keyBinding);
context.SaveChanges();
KeyBindingChanged?.Invoke();
}
}
}