From 187c557ea8105dc1db329cdf6828bc2a79fc41dc Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Sun, 22 Aug 2021 02:21:45 +0900 Subject: [PATCH] Begin migrating settings implementation across to realm --- osu.Game/Configuration/RealmSetting.cs | 33 +++++++++++++++++++++++++ osu.Game/Configuration/SettingsStore.cs | 25 ++++++++++--------- osu.Game/OsuGameBase.cs | 4 +-- osu.Game/Rulesets/RulesetConfigCache.cs | 4 +-- 4 files changed, 50 insertions(+), 16 deletions(-) create mode 100644 osu.Game/Configuration/RealmSetting.cs diff --git a/osu.Game/Configuration/RealmSetting.cs b/osu.Game/Configuration/RealmSetting.cs new file mode 100644 index 0000000000..b773796067 --- /dev/null +++ b/osu.Game/Configuration/RealmSetting.cs @@ -0,0 +1,33 @@ +// 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; + +namespace osu.Game.Configuration +{ + [MapTo(@"Setting")] + public class RealmSetting : RealmObject, IHasGuidPrimaryKey + { + [PrimaryKey] + public Guid ID { get; set; } = Guid.NewGuid(); + + public int? RulesetID { get; set; } + + public int? Variant { get; set; } + + public string Key { get; set; } + + [MapTo(nameof(Value))] + public string ValueString { get; set; } + + public object Value + { + get => ValueString; + set => ValueString = value.ToString(); + } + + public override string ToString() => $"{Key}=>{Value}"; + } +} diff --git a/osu.Game/Configuration/SettingsStore.cs b/osu.Game/Configuration/SettingsStore.cs index 86e84b0732..864fa3cf53 100644 --- a/osu.Game/Configuration/SettingsStore.cs +++ b/osu.Game/Configuration/SettingsStore.cs @@ -1,31 +1,34 @@ // 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 System.Collections.Generic; using System.Linq; using osu.Game.Database; +using Realms; namespace osu.Game.Configuration { - public class SettingsStore : DatabaseBackedStore + public class RealmSettingsStore { - public event Action SettingChanged; + private readonly RealmContextFactory realmFactory; - public SettingsStore(DatabaseContextFactory contextFactory) - : base(contextFactory) + public RealmSettingsStore(RealmContextFactory realmFactory) { + this.realmFactory = realmFactory; } /// - /// Retrieve s for a specified ruleset/variant content. + /// Retrieve s for a specified ruleset/variant content. /// /// The ruleset's internal ID. /// An optional variant. - public List Query(int? rulesetId = null, int? variant = null) => - ContextFactory.Get().DatabasedSetting.Where(b => b.RulesetID == rulesetId && b.Variant == variant).ToList(); + public List Query(int? rulesetId = null, int? variant = null) + { + using (var context = realmFactory.GetForRead()) + return context.Realm.All().Where(b => b.RulesetID == rulesetId && b.Variant == variant).ToList(); + } - public void Update(DatabasedSetting setting) + public void Update(RealmSetting setting) { using (ContextFactory.GetForWrite()) { @@ -33,11 +36,9 @@ namespace osu.Game.Configuration Refresh(ref setting); setting.Value = newValue; } - - SettingChanged?.Invoke(); } - public void Delete(DatabasedSetting setting) + public void Delete(RealmSetting setting) { using (var usage = ContextFactory.GetForWrite()) usage.Context.Remove(setting); diff --git a/osu.Game/OsuGameBase.cs b/osu.Game/OsuGameBase.cs index f4db0f2603..12f53df6e8 100644 --- a/osu.Game/OsuGameBase.cs +++ b/osu.Game/OsuGameBase.cs @@ -140,7 +140,7 @@ namespace osu.Game private FileStore fileStore; - private SettingsStore settingsStore; + private RealmSettingsStore settingsStore; private RulesetConfigCache rulesetConfigCache; @@ -279,7 +279,7 @@ namespace osu.Game migrateDataToRealm(); - dependencies.Cache(settingsStore = new SettingsStore(contextFactory)); + dependencies.Cache(settingsStore = new RealmSettingsStore(realmFactory)); dependencies.Cache(rulesetConfigCache = new RulesetConfigCache(settingsStore)); var powerStatus = CreateBatteryInfo(); diff --git a/osu.Game/Rulesets/RulesetConfigCache.cs b/osu.Game/Rulesets/RulesetConfigCache.cs index d42428638c..885fa249df 100644 --- a/osu.Game/Rulesets/RulesetConfigCache.cs +++ b/osu.Game/Rulesets/RulesetConfigCache.cs @@ -16,9 +16,9 @@ namespace osu.Game.Rulesets public class RulesetConfigCache : Component { private readonly ConcurrentDictionary configCache = new ConcurrentDictionary(); - private readonly SettingsStore settingsStore; + private readonly RealmSettingsStore settingsStore; - public RulesetConfigCache(SettingsStore settingsStore) + public RulesetConfigCache(RealmSettingsStore settingsStore) { this.settingsStore = settingsStore; }