Load defaults, pass around live IEnumerable, add PK for updating

This commit is contained in:
Dean Herbert 2017-08-14 22:31:23 +09:00
parent 7c9d6c9c83
commit 46bfa4db29
4 changed files with 47 additions and 13 deletions

View File

@ -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 @@ public string KeysString
public new int Action
{
get { return (int)base.Action; }
private set { base.Action = value; }
set { base.Action = value; }
}
}
}

View File

@ -44,8 +44,7 @@ private void load(KeyBindingStore keyBindings)
protected override void ReloadMappings()
{
KeyBindings.Clear();
KeyBindings.AddRange(store.GetProcessedList(DefaultMappings, ruleset?.ID, variant));
KeyBindings = store.GetProcessedList(DefaultMappings, ruleset?.ID, variant);
}
}
}

View File

@ -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<RulesetInfo>())
{
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 @@ protected override void PerformMigration(int currentVersion, int targetVersion)
switch (currentVersion)
{
case 1:
case 2:
case 3:
// cannot migrate; breaking underlying changes.
Reset();
break;
@ -38,6 +53,11 @@ protected override void PerformMigration(int currentVersion, int targetVersion)
protected override void Prepare(bool reset = false)
{
if (reset)
{
Connection.DropTable<DatabasedKeyBinding>();
}
Connection.CreateTable<DatabasedKeyBinding>();
}
@ -46,16 +66,28 @@ protected override void Prepare(bool reset = false)
typeof(DatabasedKeyBinding)
};
public List<KeyBinding> GetProcessedList(IEnumerable<KeyBinding> defaults, int? rulesetId, int? variant)
public IEnumerable<KeyBinding> GetProcessedList(IEnumerable<KeyBinding> defaults, int? rulesetId = null, int? variant = null)
{
//todo: cache and share reference
List<KeyBinding> bindings = new List<KeyBinding>(defaults);
var databaseEntries = Query<DatabasedKeyBinding>(b => b.RulesetID == rulesetId && b.Variant == variant);
// load from database if present.
foreach (var b in Query<DatabasedKeyBinding>(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);
}
}
}

View File

@ -108,7 +108,7 @@ private void load()
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.