diff --git a/osu.Game/Input/Bindings/DatabasedKeyBinding.cs b/osu.Game/Input/Bindings/DatabasedKeyBinding.cs index 3870cc5b04..73896b5ffe 100644 --- a/osu.Game/Input/Bindings/DatabasedKeyBinding.cs +++ b/osu.Game/Input/Bindings/DatabasedKeyBinding.cs @@ -11,6 +11,9 @@ namespace osu.Game.Input.Bindings [Table("KeyBinding")] public class DatabasedKeyBinding : KeyBinding { + [PrimaryKey, AutoIncrement] + public int ID { get; set; } + [ForeignKey(typeof(RulesetInfo))] public int? RulesetID { get; set; } @@ -28,7 +31,7 @@ namespace osu.Game.Input.Bindings public new int Action { get { return (int)base.Action; } - private set { base.Action = value; } + set { base.Action = value; } } } } \ No newline at end of file diff --git a/osu.Game/Input/Bindings/DatabasedKeyBindingInputManager.cs b/osu.Game/Input/Bindings/DatabasedKeyBindingInputManager.cs index 52e5af152c..a02dd00a77 100644 --- a/osu.Game/Input/Bindings/DatabasedKeyBindingInputManager.cs +++ b/osu.Game/Input/Bindings/DatabasedKeyBindingInputManager.cs @@ -44,8 +44,7 @@ namespace osu.Game.Input.Bindings protected override void ReloadMappings() { - KeyBindings.Clear(); - KeyBindings.AddRange(store.GetProcessedList(DefaultMappings, ruleset?.ID, variant)); + KeyBindings = store.GetProcessedList(DefaultMappings, ruleset?.ID, variant); } } } \ No newline at end of file diff --git a/osu.Game/Input/KeyBindingStore.cs b/osu.Game/Input/KeyBindingStore.cs index e26942a06c..0cd556289f 100644 --- a/osu.Game/Input/KeyBindingStore.cs +++ b/osu.Game/Input/KeyBindingStore.cs @@ -3,22 +3,35 @@ using System; using System.Collections.Generic; +using System.Linq; using osu.Framework.Input.Bindings; using osu.Framework.Platform; using osu.Game.Database; using osu.Game.Input.Bindings; +using osu.Game.Rulesets; using SQLite.Net; namespace osu.Game.Input { public class KeyBindingStore : DatabaseBackedStore { - public KeyBindingStore(SQLiteConnection connection, Storage storage = null) + public KeyBindingStore(SQLiteConnection connection, RulesetStore rulesets, Storage storage = null) : base(connection, storage) { + foreach (var info in rulesets.Query()) + { + var ruleset = info.CreateInstance(); + foreach (var variant in ruleset.AvailableVariants) + GetProcessedList(ruleset.GetDefaultKeyBindings(), info.ID, variant); + } } - protected override int StoreVersion => 2; + public void Register(KeyBindingInputManager manager) + { + GetProcessedList(manager.DefaultMappings); + } + + protected override int StoreVersion => 3; protected override void PerformMigration(int currentVersion, int targetVersion) { @@ -29,6 +42,8 @@ namespace osu.Game.Input switch (currentVersion) { case 1: + case 2: + case 3: // cannot migrate; breaking underlying changes. Reset(); break; @@ -38,6 +53,11 @@ namespace osu.Game.Input protected override void Prepare(bool reset = false) { + if (reset) + { + Connection.DropTable(); + } + Connection.CreateTable(); } @@ -46,16 +66,28 @@ namespace osu.Game.Input typeof(DatabasedKeyBinding) }; - public List GetProcessedList(IEnumerable defaults, int? rulesetId, int? variant) + public IEnumerable GetProcessedList(IEnumerable defaults, int? rulesetId = null, int? variant = null) { - //todo: cache and share reference - List bindings = new List(defaults); + var databaseEntries = Query(b => b.RulesetID == rulesetId && b.Variant == variant); - // load from database if present. - foreach (var b in Query(b => b.RulesetID == rulesetId && b.Variant == variant)) - bindings.Add(b); + 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 bindings; + return databaseEntries; + } + + public void Update(KeyBinding keyBinding) + { + Connection.Update(keyBinding); } } } diff --git a/osu.Game/OsuGameBase.cs b/osu.Game/OsuGameBase.cs index d72716b5d6..54b091d7d9 100644 --- a/osu.Game/OsuGameBase.cs +++ b/osu.Game/OsuGameBase.cs @@ -108,7 +108,7 @@ namespace osu.Game dependencies.Cache(FileStore = new FileStore(connection, Host.Storage)); dependencies.Cache(BeatmapManager = new BeatmapManager(Host.Storage, FileStore, connection, RulesetStore, Host)); dependencies.Cache(ScoreStore = new ScoreStore(Host.Storage, connection, Host, BeatmapManager)); - dependencies.Cache(KeyBindingStore = new KeyBindingStore(connection)); + dependencies.Cache(KeyBindingStore = new KeyBindingStore(connection, RulesetStore)); dependencies.Cache(new OsuColour()); //this completely overrides the framework default. will need to change once we make a proper FontStore.