diff --git a/osu.Game/Input/Bindings/DatabasedKeyBindingInputManager.cs b/osu.Game/Input/Bindings/DatabasedKeyBindingInputManager.cs index a02dd00a77..e565c13b31 100644 --- a/osu.Game/Input/Bindings/DatabasedKeyBindingInputManager.cs +++ b/osu.Game/Input/Bindings/DatabasedKeyBindingInputManager.cs @@ -44,7 +44,7 @@ namespace osu.Game.Input.Bindings protected override void ReloadMappings() { - KeyBindings = store.GetProcessedList(DefaultMappings, ruleset?.ID, variant); + KeyBindings = store.Query(ruleset?.ID, variant); } } } \ No newline at end of file diff --git a/osu.Game/Input/KeyBindingStore.cs b/osu.Game/Input/KeyBindingStore.cs index 0cd556289f..057fa4d4e7 100644 --- a/osu.Game/Input/KeyBindingStore.cs +++ b/osu.Game/Input/KeyBindingStore.cs @@ -22,14 +22,11 @@ namespace osu.Game.Input { var ruleset = info.CreateInstance(); foreach (var variant in ruleset.AvailableVariants) - GetProcessedList(ruleset.GetDefaultKeyBindings(), info.ID, variant); + insertDefaults(ruleset.GetDefaultKeyBindings(), info.ID, variant); } } - public void Register(KeyBindingInputManager manager) - { - GetProcessedList(manager.DefaultMappings); - } + public void Register(KeyBindingInputManager manager) => insertDefaults(manager.DefaultMappings); protected override int StoreVersion => 3; @@ -61,29 +58,37 @@ namespace osu.Game.Input Connection.CreateTable(); } + private void insertDefaults(IEnumerable defaults, int? rulesetId = null, int? variant = null) + { + var query = Query(rulesetId, variant); + + // compare counts in database vs defaults + foreach (var group in defaults.GroupBy(k => k.Action)) + { + int count; + while (group.Count() > (count = query.Count(k => (int)k.Action == (int)group.Key))) + { + var insertable = group.Skip(count).First(); + + // insert any defaults which are missing. + Connection.Insert(new DatabasedKeyBinding + { + KeyCombination = insertable.KeyCombination, + Action = (int)insertable.Action, + RulesetID = rulesetId, + Variant = variant + }); + } + } + } + protected override Type[] ValidTypes => new[] { typeof(DatabasedKeyBinding) }; - public IEnumerable GetProcessedList(IEnumerable defaults, int? rulesetId = null, int? variant = null) - { - var databaseEntries = Query(b => b.RulesetID == rulesetId && b.Variant == variant); - - if (!databaseEntries.Any()) - { - // if there are no entries for this category in the database, we should populate our defaults. - Connection.InsertAll(defaults.Select(k => new DatabasedKeyBinding - { - KeyCombination = k.KeyCombination, - Action = (int)k.Action, - RulesetID = rulesetId, - Variant = variant - })); - } - - return databaseEntries; - } + public IEnumerable Query(int? rulesetId = null, int? variant = null) => + Query(b => b.RulesetID == rulesetId && b.Variant == variant); public void Update(KeyBinding keyBinding) { diff --git a/osu.Game/OsuGameBase.cs b/osu.Game/OsuGameBase.cs index 54b091d7d9..571c75022e 100644 --- a/osu.Game/OsuGameBase.cs +++ b/osu.Game/OsuGameBase.cs @@ -202,6 +202,7 @@ namespace osu.Game } }); + KeyBindingStore.Register(globalBinding); dependencies.Cache(globalBinding); // TODO: This is temporary until we reimplement the local FPS display.