mirror of
https://github.com/osukey/osukey.git
synced 2025-08-07 00:23:59 +09:00
Merge branch 'master' into test-score-refactoring
This commit is contained in:
@ -52,6 +52,45 @@ namespace osu.Game.Tests.Database
|
|||||||
Assert.That(queryCount(GlobalAction.Select), Is.EqualTo(2));
|
Assert.That(queryCount(GlobalAction.Select), Is.EqualTo(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestDefaultsPopulationRemovesExcess()
|
||||||
|
{
|
||||||
|
Assert.That(queryCount(), Is.EqualTo(0));
|
||||||
|
|
||||||
|
KeyBindingContainer testContainer = new TestKeyBindingContainer();
|
||||||
|
|
||||||
|
// Add some excess bindings for an action which only supports 1.
|
||||||
|
using (var realm = realmContextFactory.CreateContext())
|
||||||
|
using (var transaction = realm.BeginWrite())
|
||||||
|
{
|
||||||
|
realm.Add(new RealmKeyBinding
|
||||||
|
{
|
||||||
|
Action = GlobalAction.Back,
|
||||||
|
KeyCombination = new KeyCombination(InputKey.A)
|
||||||
|
});
|
||||||
|
|
||||||
|
realm.Add(new RealmKeyBinding
|
||||||
|
{
|
||||||
|
Action = GlobalAction.Back,
|
||||||
|
KeyCombination = new KeyCombination(InputKey.S)
|
||||||
|
});
|
||||||
|
|
||||||
|
realm.Add(new RealmKeyBinding
|
||||||
|
{
|
||||||
|
Action = GlobalAction.Back,
|
||||||
|
KeyCombination = new KeyCombination(InputKey.D)
|
||||||
|
});
|
||||||
|
|
||||||
|
transaction.Commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.That(queryCount(GlobalAction.Back), Is.EqualTo(3));
|
||||||
|
|
||||||
|
keyBindingStore.Register(testContainer, Enumerable.Empty<RulesetInfo>());
|
||||||
|
|
||||||
|
Assert.That(queryCount(GlobalAction.Back), Is.EqualTo(1));
|
||||||
|
}
|
||||||
|
|
||||||
private int queryCount(GlobalAction? match = null)
|
private int queryCount(GlobalAction? match = null)
|
||||||
{
|
{
|
||||||
using (var realm = realmContextFactory.CreateContext())
|
using (var realm = realmContextFactory.CreateContext())
|
||||||
|
@ -81,20 +81,37 @@ namespace osu.Game.Input
|
|||||||
// compare counts in database vs defaults for each action type.
|
// compare counts in database vs defaults for each action type.
|
||||||
foreach (var defaultsForAction in defaults.GroupBy(k => k.Action))
|
foreach (var defaultsForAction in defaults.GroupBy(k => k.Action))
|
||||||
{
|
{
|
||||||
// avoid performing redundant queries when the database is empty and needs to be re-filled.
|
IEnumerable<RealmKeyBinding> existing = existingBindings.Where(k =>
|
||||||
int existingCount = existingBindings.Count(k => k.RulesetName == rulesetName && k.Variant == variant && k.ActionInt == (int)defaultsForAction.Key);
|
k.RulesetName == rulesetName
|
||||||
|
&& k.Variant == variant
|
||||||
|
&& k.ActionInt == (int)defaultsForAction.Key);
|
||||||
|
|
||||||
if (defaultsForAction.Count() <= existingCount)
|
int defaultsCount = defaultsForAction.Count();
|
||||||
continue;
|
int existingCount = existing.Count();
|
||||||
|
|
||||||
// insert any defaults which are missing.
|
if (defaultsCount > existingCount)
|
||||||
realm.Add(defaultsForAction.Skip(existingCount).Select(k => new RealmKeyBinding
|
|
||||||
{
|
{
|
||||||
KeyCombinationString = k.KeyCombination.ToString(),
|
// insert any defaults which are missing.
|
||||||
ActionInt = (int)k.Action,
|
realm.Add(defaultsForAction.Skip(existingCount).Select(k => new RealmKeyBinding
|
||||||
RulesetName = rulesetName,
|
{
|
||||||
Variant = variant
|
KeyCombinationString = k.KeyCombination.ToString(),
|
||||||
}));
|
ActionInt = (int)k.Action,
|
||||||
|
RulesetName = rulesetName,
|
||||||
|
Variant = variant
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
else if (defaultsCount < existingCount)
|
||||||
|
{
|
||||||
|
// generally this shouldn't happen, but if the user has more key bindings for an action than we expect,
|
||||||
|
// remove the last entries until the count matches for sanity.
|
||||||
|
foreach (var k in existing.TakeLast(existingCount - defaultsCount).ToArray())
|
||||||
|
{
|
||||||
|
realm.Remove(k);
|
||||||
|
|
||||||
|
// Remove from the local flattened/cached list so future lookups don't query now deleted rows.
|
||||||
|
existingBindings.Remove(k);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user