Avoid constructor overhead for realm RealmKeyBinding parameterless constructor

This commit is contained in:
Dean Herbert 2022-01-20 17:18:53 +09:00
parent 6c10531df2
commit 70cc03fe43
3 changed files with 22 additions and 26 deletions

View File

@ -63,23 +63,9 @@ namespace osu.Game.Tests.Database
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)
});
realm.Add(new RealmKeyBinding(GlobalAction.Back, new KeyCombination(InputKey.A)));
realm.Add(new RealmKeyBinding(GlobalAction.Back, new KeyCombination(InputKey.S)));
realm.Add(new RealmKeyBinding(GlobalAction.Back, new KeyCombination(InputKey.D)));
transaction.Commit();
}

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using JetBrains.Annotations;
using osu.Framework.Input.Bindings;
using osu.Game.Database;
using Realms;
@ -14,7 +15,7 @@ namespace osu.Game.Input.Bindings
public class RealmKeyBinding : RealmObject, IHasGuidPrimaryKey, IKeyBinding
{
[PrimaryKey]
public Guid ID { get; set; } = Guid.NewGuid();
public Guid ID { get; set; }
public string? RulesetName { get; set; }
@ -38,6 +39,21 @@ namespace osu.Game.Input.Bindings
public int ActionInt { get; set; }
[MapTo(nameof(KeyCombination))]
public string KeyCombinationString { get; set; } = string.Empty;
public string KeyCombinationString { get; set; } = null!;
public RealmKeyBinding(object action, KeyCombination keyCombination, string? rulesetName = null, int? variant = null)
{
Action = action;
KeyCombination = keyCombination;
RulesetName = rulesetName;
Variant = variant;
ID = Guid.NewGuid();
}
[UsedImplicitly] // Realm
private RealmKeyBinding()
{
}
}
}

View File

@ -92,13 +92,7 @@ namespace osu.Game.Input
if (defaultsCount > existingCount)
{
// insert any defaults which are missing.
realm.Add(defaultsForAction.Skip(existingCount).Select(k => new RealmKeyBinding
{
KeyCombinationString = k.KeyCombination.ToString(),
ActionInt = (int)k.Action,
RulesetName = rulesetName,
Variant = variant
}));
realm.Add(defaultsForAction.Skip(existingCount).Select(k => new RealmKeyBinding(k.Action, k.KeyCombination, rulesetName, variant)));
}
else if (defaultsCount < existingCount)
{