diff --git a/osu.Game/Configuration/RealmRulesetSetting.cs b/osu.Game/Configuration/RealmRulesetSetting.cs index 07e56ad8dd..3fea35ee9d 100644 --- a/osu.Game/Configuration/RealmRulesetSetting.cs +++ b/osu.Game/Configuration/RealmRulesetSetting.cs @@ -1,8 +1,6 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -using System; -using osu.Game.Database; using Realms; #nullable enable @@ -10,13 +8,10 @@ using Realms; namespace osu.Game.Configuration { [MapTo(@"RulesetSetting")] - public class RealmRulesetSetting : RealmObject, IHasGuidPrimaryKey + public class RealmRulesetSetting : RealmObject { - [PrimaryKey] - public Guid ID { get; set; } = Guid.NewGuid(); - [Indexed] - public int RulesetID { get; set; } + public string RulesetName { get; set; } = string.Empty; [Indexed] public int Variant { get; set; } diff --git a/osu.Game/Database/RealmContextFactory.cs b/osu.Game/Database/RealmContextFactory.cs index 1d0da3ea5c..70ea24b581 100644 --- a/osu.Game/Database/RealmContextFactory.cs +++ b/osu.Game/Database/RealmContextFactory.cs @@ -11,6 +11,7 @@ using osu.Framework.Input.Bindings; using osu.Framework.Logging; using osu.Framework.Platform; using osu.Framework.Statistics; +using osu.Game.Configuration; using osu.Game.Input.Bindings; using osu.Game.Models; using osu.Game.Rulesets; @@ -40,8 +41,9 @@ namespace osu.Game.Database /// 7 2021-10-18 Changed OnlineID fields to non-nullable to add indexing support. /// 8 2021-10-29 Rebind scroll adjust keys to not have control modifier. /// 9 2021-11-04 Converted BeatmapMetadata.Author from string to RealmUser. + /// 10 2021-11-22 Use ShortName instead of RulesetID for ruleset settings. /// - private const int schema_version = 9; + private const int schema_version = 10; /// /// Lock object which is held during sections, blocking context creation during blocking periods. @@ -236,6 +238,28 @@ namespace osu.Game.Database }; } + break; + + case 10: + string rulesetSettingClassName = getMappedOrOriginalName(typeof(RealmRulesetSetting)); + + var oldSettings = migration.OldRealm.DynamicApi.All(rulesetSettingClassName); + var newSettings = migration.NewRealm.All().ToList(); + + for (int i = 0; i < newSettings.Count; i++) + { + dynamic? oldItem = oldSettings.ElementAt(i); + var newItem = newSettings.ElementAt(i); + + long rulesetId = oldItem.RulesetID; + string? rulesetName = rulesets?.GetRuleset((int)rulesetId)?.ShortName; + + if (string.IsNullOrEmpty(rulesetName)) + migration.NewRealm.Remove(newItem); + else + newItem.RulesetName = rulesetName; + } + break; } } diff --git a/osu.Game/OsuGameBase.cs b/osu.Game/OsuGameBase.cs index 3b2f397a72..d8050353c5 100644 --- a/osu.Game/OsuGameBase.cs +++ b/osu.Game/OsuGameBase.cs @@ -456,7 +456,8 @@ namespace osu.Game { Key = dkb.Key, Value = dkb.StringValue, - RulesetID = dkb.RulesetID.Value, + // important: this RulesetStore must be the EF one. + RulesetName = RulesetStore.GetRuleset(dkb.RulesetID.Value).ShortName, Variant = dkb.Variant ?? 0, }); } diff --git a/osu.Game/Rulesets/Configuration/RulesetConfigManager.cs b/osu.Game/Rulesets/Configuration/RulesetConfigManager.cs index eec71a3623..17678775e9 100644 --- a/osu.Game/Rulesets/Configuration/RulesetConfigManager.cs +++ b/osu.Game/Rulesets/Configuration/RulesetConfigManager.cs @@ -20,16 +20,13 @@ namespace osu.Game.Rulesets.Configuration private List databasedSettings = new List(); - private readonly int rulesetId; + private readonly string rulesetName; protected RulesetConfigManager(SettingsStore store, RulesetInfo ruleset, int? variant = null) { realmFactory = store?.Realm; - if (realmFactory != null && !ruleset.ID.HasValue) - throw new InvalidOperationException("Attempted to add databased settings for a non-databased ruleset"); - - rulesetId = ruleset.ID ?? -1; + rulesetName = ruleset.ShortName; this.variant = variant ?? 0; @@ -43,7 +40,7 @@ namespace osu.Game.Rulesets.Configuration if (realmFactory != null) { // As long as RulesetConfigCache exists, there is no need to subscribe to realm events. - databasedSettings = realmFactory.Context.All().Where(b => b.RulesetID == rulesetId && b.Variant == variant).ToList(); + databasedSettings = realmFactory.Context.All().Where(b => b.RulesetName == rulesetName && b.Variant == variant).ToList(); } } @@ -68,7 +65,7 @@ namespace osu.Game.Rulesets.Configuration { foreach (var c in changed) { - var setting = realm.All().First(s => s.RulesetID == rulesetId && s.Variant == variant && s.Key == c.ToString()); + var setting = realm.All().First(s => s.RulesetName == rulesetName && s.Variant == variant && s.Key == c.ToString()); setting.Value = ConfigStore[c].ToString(); } @@ -94,7 +91,7 @@ namespace osu.Game.Rulesets.Configuration { Key = lookup.ToString(), Value = bindable.Value.ToString(), - RulesetID = rulesetId, + RulesetName = rulesetName, Variant = variant, };