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")] [Table("KeyBinding")]
public class DatabasedKeyBinding : KeyBinding public class DatabasedKeyBinding : KeyBinding
{ {
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
[ForeignKey(typeof(RulesetInfo))] [ForeignKey(typeof(RulesetInfo))]
public int? RulesetID { get; set; } public int? RulesetID { get; set; }
@ -28,7 +31,7 @@ namespace osu.Game.Input.Bindings
public new int Action public new int Action
{ {
get { return (int)base.Action; } get { return (int)base.Action; }
private set { base.Action = value; } set { base.Action = value; }
} }
} }
} }

View File

@ -44,8 +44,7 @@ namespace osu.Game.Input.Bindings
protected override void ReloadMappings() protected override void ReloadMappings()
{ {
KeyBindings.Clear(); KeyBindings = store.GetProcessedList(DefaultMappings, ruleset?.ID, variant);
KeyBindings.AddRange(store.GetProcessedList(DefaultMappings, ruleset?.ID, variant));
} }
} }
} }

View File

@ -3,22 +3,35 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using osu.Framework.Input.Bindings; using osu.Framework.Input.Bindings;
using osu.Framework.Platform; using osu.Framework.Platform;
using osu.Game.Database; using osu.Game.Database;
using osu.Game.Input.Bindings; using osu.Game.Input.Bindings;
using osu.Game.Rulesets;
using SQLite.Net; using SQLite.Net;
namespace osu.Game.Input namespace osu.Game.Input
{ {
public class KeyBindingStore : DatabaseBackedStore public class KeyBindingStore : DatabaseBackedStore
{ {
public KeyBindingStore(SQLiteConnection connection, Storage storage = null) public KeyBindingStore(SQLiteConnection connection, RulesetStore rulesets, Storage storage = null)
: base(connection, storage) : 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) protected override void PerformMigration(int currentVersion, int targetVersion)
{ {
@ -29,6 +42,8 @@ namespace osu.Game.Input
switch (currentVersion) switch (currentVersion)
{ {
case 1: case 1:
case 2:
case 3:
// cannot migrate; breaking underlying changes. // cannot migrate; breaking underlying changes.
Reset(); Reset();
break; break;
@ -38,6 +53,11 @@ namespace osu.Game.Input
protected override void Prepare(bool reset = false) protected override void Prepare(bool reset = false)
{ {
if (reset)
{
Connection.DropTable<DatabasedKeyBinding>();
}
Connection.CreateTable<DatabasedKeyBinding>(); Connection.CreateTable<DatabasedKeyBinding>();
} }
@ -46,16 +66,28 @@ namespace osu.Game.Input
typeof(DatabasedKeyBinding) 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 var databaseEntries = Query<DatabasedKeyBinding>(b => b.RulesetID == rulesetId && b.Variant == variant);
List<KeyBinding> bindings = new List<KeyBinding>(defaults);
// load from database if present. if (!databaseEntries.Any())
foreach (var b in Query<DatabasedKeyBinding>(b => b.RulesetID == rulesetId && b.Variant == variant)) {
bindings.Add(b); // 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 @@ namespace osu.Game
dependencies.Cache(FileStore = new FileStore(connection, Host.Storage)); dependencies.Cache(FileStore = new FileStore(connection, Host.Storage));
dependencies.Cache(BeatmapManager = new BeatmapManager(Host.Storage, FileStore, connection, RulesetStore, Host)); dependencies.Cache(BeatmapManager = new BeatmapManager(Host.Storage, FileStore, connection, RulesetStore, Host));
dependencies.Cache(ScoreStore = new ScoreStore(Host.Storage, connection, Host, BeatmapManager)); 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()); dependencies.Cache(new OsuColour());
//this completely overrides the framework default. will need to change once we make a proper FontStore. //this completely overrides the framework default. will need to change once we make a proper FontStore.