1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 17:43:05 +08:00

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. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using osu.Game.Database; using osu.Game.Database;
using Realms;
namespace osu.Game.Configuration namespace osu.Game.Configuration
{ {
public class SettingsStore : DatabaseBackedStore public class RealmSettingsStore
{ {
public event Action SettingChanged; private readonly RealmContextFactory realmFactory;
public SettingsStore(DatabaseContextFactory contextFactory) public RealmSettingsStore(RealmContextFactory realmFactory)
: base(contextFactory)
{ {
this.realmFactory = realmFactory;
} }
/// <summary> /// <summary>
/// Retrieve <see cref="DatabasedSetting"/>s for a specified ruleset/variant content. /// Retrieve <see cref="RealmSetting"/>s for a specified ruleset/variant content.
/// </summary> /// </summary>
/// <param name="rulesetId">The ruleset's internal ID.</param> /// <param name="rulesetId">The ruleset's internal ID.</param>
/// <param name="variant">An optional variant.</param> /// <param name="variant">An optional variant.</param>
public List<DatabasedSetting> Query(int? rulesetId = null, int? variant = null) => public List<RealmSetting> Query(int? rulesetId = null, int? variant = null)
ContextFactory.Get().DatabasedSetting.Where(b => b.RulesetID == rulesetId && b.Variant == variant).ToList(); {
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()) using (ContextFactory.GetForWrite())
{ {
@ -33,11 +36,9 @@ namespace osu.Game.Configuration
Refresh(ref setting); Refresh(ref setting);
setting.Value = newValue; setting.Value = newValue;
} }
SettingChanged?.Invoke();
} }
public void Delete(DatabasedSetting setting) public void Delete(RealmSetting setting)
{ {
using (var usage = ContextFactory.GetForWrite()) using (var usage = ContextFactory.GetForWrite())
usage.Context.Remove(setting); usage.Context.Remove(setting);

View File

@ -140,7 +140,7 @@ namespace osu.Game
private FileStore fileStore; private FileStore fileStore;
private SettingsStore settingsStore; private RealmSettingsStore settingsStore;
private RulesetConfigCache rulesetConfigCache; private RulesetConfigCache rulesetConfigCache;
@ -279,7 +279,7 @@ namespace osu.Game
migrateDataToRealm(); migrateDataToRealm();
dependencies.Cache(settingsStore = new SettingsStore(contextFactory)); dependencies.Cache(settingsStore = new RealmSettingsStore(realmFactory));
dependencies.Cache(rulesetConfigCache = new RulesetConfigCache(settingsStore)); dependencies.Cache(rulesetConfigCache = new RulesetConfigCache(settingsStore));
var powerStatus = CreateBatteryInfo(); var powerStatus = CreateBatteryInfo();

View File

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