Begin migrating settings implementation across to realm

This commit is contained in:
Dean Herbert 2021-08-22 02:21:45 +09:00
parent 1ba716d9f1
commit 187c557ea8
4 changed files with 50 additions and 16 deletions

View File

@ -0,0 +1,33 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. 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}";
}
}

View File

@ -1,31 +1,34 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. 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;
}
/// <summary>
/// Retrieve <see cref="DatabasedSetting"/>s for a specified ruleset/variant content.
/// Retrieve <see cref="RealmSetting"/>s for a specified ruleset/variant content.
/// </summary>
/// <param name="rulesetId">The ruleset's internal ID.</param>
/// <param name="variant">An optional variant.</param>
public List<DatabasedSetting> Query(int? rulesetId = null, int? variant = null) =>
ContextFactory.Get().DatabasedSetting.Where(b => b.RulesetID == rulesetId && b.Variant == variant).ToList();
public List<RealmSetting> Query(int? rulesetId = null, int? variant = null)
{
using (var context = realmFactory.GetForRead())
return context.Realm.All<RealmSetting>().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 @@ public void Update(DatabasedSetting setting)
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);

View File

@ -140,7 +140,7 @@ public virtual string Version
private FileStore fileStore;
private SettingsStore settingsStore;
private RealmSettingsStore settingsStore;
private RulesetConfigCache rulesetConfigCache;
@ -279,7 +279,7 @@ List<ScoreInfo> getBeatmapScores(BeatmapSetInfo set)
migrateDataToRealm();
dependencies.Cache(settingsStore = new SettingsStore(contextFactory));
dependencies.Cache(settingsStore = new RealmSettingsStore(realmFactory));
dependencies.Cache(rulesetConfigCache = new RulesetConfigCache(settingsStore));
var powerStatus = CreateBatteryInfo();

View File

@ -16,9 +16,9 @@ namespace osu.Game.Rulesets
public class RulesetConfigCache : Component
{
private readonly ConcurrentDictionary<int, IRulesetConfigManager> configCache = new ConcurrentDictionary<int, IRulesetConfigManager>();
private readonly SettingsStore settingsStore;
private readonly RealmSettingsStore settingsStore;
public RulesetConfigCache(SettingsStore settingsStore)
public RulesetConfigCache(RealmSettingsStore settingsStore)
{
this.settingsStore = settingsStore;
}