mirror of
https://github.com/ppy/osu
synced 2025-02-17 10:57:03 +00:00
Merge pull request #1396 from peppy/key-binding-store-improvements
Improve performance and db safety of KeyBindings
This commit is contained in:
commit
f86a13ef61
@ -6,6 +6,7 @@ using System.Collections.Generic;
|
|||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Input.Bindings;
|
using osu.Framework.Input.Bindings;
|
||||||
using osu.Game.Rulesets;
|
using osu.Game.Rulesets;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace osu.Game.Input.Bindings
|
namespace osu.Game.Input.Bindings
|
||||||
{
|
{
|
||||||
@ -44,11 +45,15 @@ namespace osu.Game.Input.Bindings
|
|||||||
private void load(KeyBindingStore keyBindings)
|
private void load(KeyBindingStore keyBindings)
|
||||||
{
|
{
|
||||||
store = 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,8 @@ namespace osu.Game.Input
|
|||||||
{
|
{
|
||||||
public class KeyBindingStore : DatabaseBackedStore
|
public class KeyBindingStore : DatabaseBackedStore
|
||||||
{
|
{
|
||||||
|
public event Action KeyBindingChanged;
|
||||||
|
|
||||||
public KeyBindingStore(Func<OsuDbContext> getContext, RulesetStore rulesets, Storage storage = null)
|
public KeyBindingStore(Func<OsuDbContext> getContext, RulesetStore rulesets, Storage storage = null)
|
||||||
: base(getContext, storage)
|
: base(getContext, storage)
|
||||||
{
|
{
|
||||||
@ -36,29 +38,32 @@ namespace osu.Game.Input
|
|||||||
|
|
||||||
private void insertDefaults(IEnumerable<KeyBinding> defaults, int? rulesetId = null, int? variant = null)
|
private void insertDefaults(IEnumerable<KeyBinding> defaults, int? rulesetId = null, int? variant = null)
|
||||||
{
|
{
|
||||||
var context = GetContext();
|
using (var context = GetContext())
|
||||||
|
using (var transaction = context.Database.BeginTransaction())
|
||||||
// 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);
|
// compare counts in database vs defaults
|
||||||
int aimCount = group.Count();
|
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)
|
if (aimCount <= count)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
foreach (var insertable in group.Skip(count).Take(aimCount - count))
|
foreach (var insertable in group.Skip(count).Take(aimCount - count))
|
||||||
// insert any defaults which are missing.
|
// insert any defaults which are missing.
|
||||||
context.DatabasedKeyBinding.Add(new DatabasedKeyBinding
|
context.DatabasedKeyBinding.Add(new DatabasedKeyBinding
|
||||||
{
|
{
|
||||||
KeyCombination = insertable.KeyCombination,
|
KeyCombination = insertable.KeyCombination,
|
||||||
Action = insertable.Action,
|
Action = insertable.Action,
|
||||||
RulesetID = rulesetId,
|
RulesetID = rulesetId,
|
||||||
Variant = variant
|
Variant = variant
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
context.SaveChanges();
|
||||||
|
transaction.Commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
context.SaveChanges();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -77,6 +82,8 @@ namespace osu.Game.Input
|
|||||||
var context = GetContext();
|
var context = GetContext();
|
||||||
context.Update(keyBinding);
|
context.Update(keyBinding);
|
||||||
context.SaveChanges();
|
context.SaveChanges();
|
||||||
|
|
||||||
|
KeyBindingChanged?.Invoke();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user