2017-08-09 14:15:41 +08:00
|
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using System;
|
2017-08-14 19:19:25 +08:00
|
|
|
|
using System.Collections.Generic;
|
2017-08-14 21:31:23 +08:00
|
|
|
|
using System.Linq;
|
2017-08-14 19:19:25 +08:00
|
|
|
|
using osu.Framework.Input.Bindings;
|
2017-08-09 13:50:10 +08:00
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
|
using osu.Game.Database;
|
2017-08-11 15:11:46 +08:00
|
|
|
|
using osu.Game.Input.Bindings;
|
2017-08-14 21:31:23 +08:00
|
|
|
|
using osu.Game.Rulesets;
|
2017-08-09 13:50:10 +08:00
|
|
|
|
using SQLite.Net;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Input
|
|
|
|
|
{
|
2017-08-14 19:19:25 +08:00
|
|
|
|
public class KeyBindingStore : DatabaseBackedStore
|
2017-08-09 13:50:10 +08:00
|
|
|
|
{
|
2017-08-14 21:31:23 +08:00
|
|
|
|
public KeyBindingStore(SQLiteConnection connection, RulesetStore rulesets, Storage storage = null)
|
2017-08-09 13:50:10 +08:00
|
|
|
|
: base(connection, storage)
|
|
|
|
|
{
|
2017-08-20 04:07:03 +08:00
|
|
|
|
foreach (var info in rulesets.AllRulesets)
|
2017-08-14 21:31:23 +08:00
|
|
|
|
{
|
|
|
|
|
var ruleset = info.CreateInstance();
|
|
|
|
|
foreach (var variant in ruleset.AvailableVariants)
|
2017-08-23 11:36:53 +08:00
|
|
|
|
insertDefaults(ruleset.GetDefaultKeyBindings(variant), info.ID, variant);
|
2017-08-14 21:31:23 +08:00
|
|
|
|
}
|
2017-08-09 13:50:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-16 16:34:49 +08:00
|
|
|
|
public void Register(KeyBindingInputManager manager) => insertDefaults(manager.DefaultKeyBindings);
|
2017-08-14 21:31:23 +08:00
|
|
|
|
|
|
|
|
|
protected override int StoreVersion => 3;
|
2017-08-10 15:08:43 +08:00
|
|
|
|
|
|
|
|
|
protected override void PerformMigration(int currentVersion, int targetVersion)
|
|
|
|
|
{
|
|
|
|
|
base.PerformMigration(currentVersion, targetVersion);
|
|
|
|
|
|
|
|
|
|
while (currentVersion++ < targetVersion)
|
|
|
|
|
{
|
|
|
|
|
switch (currentVersion)
|
|
|
|
|
{
|
|
|
|
|
case 1:
|
2017-08-14 21:31:23 +08:00
|
|
|
|
case 2:
|
|
|
|
|
case 3:
|
2017-08-10 15:08:43 +08:00
|
|
|
|
// cannot migrate; breaking underlying changes.
|
|
|
|
|
Reset();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 13:50:10 +08:00
|
|
|
|
protected override void Prepare(bool reset = false)
|
|
|
|
|
{
|
2017-08-14 21:31:23 +08:00
|
|
|
|
if (reset)
|
|
|
|
|
Connection.DropTable<DatabasedKeyBinding>();
|
|
|
|
|
|
2017-08-11 15:11:46 +08:00
|
|
|
|
Connection.CreateTable<DatabasedKeyBinding>();
|
2017-08-09 13:50:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 22:59:58 +08:00
|
|
|
|
private void insertDefaults(IEnumerable<KeyBinding> defaults, int? rulesetId = null, int? variant = null)
|
2017-08-14 19:19:25 +08:00
|
|
|
|
{
|
2017-08-15 22:59:58 +08:00
|
|
|
|
var query = Query(rulesetId, variant);
|
2017-08-14 19:19:25 +08:00
|
|
|
|
|
2017-08-15 22:59:58 +08:00
|
|
|
|
// compare counts in database vs defaults
|
|
|
|
|
foreach (var group in defaults.GroupBy(k => k.Action))
|
2017-08-14 21:31:23 +08:00
|
|
|
|
{
|
2017-08-15 22:59:58 +08:00
|
|
|
|
int count;
|
|
|
|
|
while (group.Count() > (count = query.Count(k => (int)k.Action == (int)group.Key)))
|
2017-08-14 21:31:23 +08:00
|
|
|
|
{
|
2017-08-15 22:59:58 +08:00
|
|
|
|
var insertable = group.Skip(count).First();
|
2017-08-14 19:19:25 +08:00
|
|
|
|
|
2017-08-15 22:59:58 +08:00
|
|
|
|
// insert any defaults which are missing.
|
|
|
|
|
Connection.Insert(new DatabasedKeyBinding
|
|
|
|
|
{
|
|
|
|
|
KeyCombination = insertable.KeyCombination,
|
2017-08-16 16:27:09 +08:00
|
|
|
|
Action = insertable.Action,
|
2017-08-15 22:59:58 +08:00
|
|
|
|
RulesetID = rulesetId,
|
|
|
|
|
Variant = variant
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-14 21:31:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 22:59:58 +08:00
|
|
|
|
protected override Type[] ValidTypes => new[]
|
|
|
|
|
{
|
|
|
|
|
typeof(DatabasedKeyBinding)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public IEnumerable<KeyBinding> Query(int? rulesetId = null, int? variant = null) =>
|
|
|
|
|
Query<DatabasedKeyBinding>(b => b.RulesetID == rulesetId && b.Variant == variant);
|
|
|
|
|
|
2017-08-16 16:34:49 +08:00
|
|
|
|
public void Update(KeyBinding keyBinding) => Connection.Update(keyBinding);
|
2017-08-09 13:50:10 +08:00
|
|
|
|
}
|
2017-08-14 19:19:25 +08:00
|
|
|
|
}
|