1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 16:02:55 +08:00

Rename all databased setting classes to be specific to rulesets for now

This commit is contained in:
Dean Herbert 2021-09-15 16:55:42 +09:00
parent 520e550764
commit 80ecf81be3
4 changed files with 74 additions and 91 deletions

View File

@ -1,84 +0,0 @@
// 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.Framework.Bindables;
using osu.Framework.Configuration;
using osu.Game.Database;
using osu.Game.Rulesets;
namespace osu.Game.Configuration
{
public abstract class DatabasedConfigManager<TLookup> : ConfigManager<TLookup>
where TLookup : struct, Enum
{
private readonly RealmContextFactory realmFactory;
private readonly int? variant;
private List<RealmSetting> databasedSettings = new List<RealmSetting>();
private readonly RulesetInfo ruleset;
protected DatabasedConfigManager(SettingsStore store = null, RulesetInfo ruleset = null, int? variant = null)
{
realmFactory = store?.Realm;
this.ruleset = ruleset;
this.variant = variant;
Load();
InitialiseDefaults();
}
protected override void PerformLoad()
{
var rulesetID = ruleset?.ID;
if (realmFactory != null)
{
// As long as RulesetConfigCache exists, there is no need to subscribe to realm events.
databasedSettings = realmFactory.Context.All<RealmSetting>().Where(b => b.RulesetID == rulesetID && b.Variant == variant).ToList();
}
}
protected override bool PerformSave()
{
// do nothing, realm saves immediately
return true;
}
protected override void AddBindable<TBindable>(TLookup lookup, Bindable<TBindable> bindable)
{
base.AddBindable(lookup, bindable);
var setting = databasedSettings.Find(s => s.Key == lookup.ToString());
if (setting != null)
{
bindable.Parse(setting.Value);
}
else
{
setting = new RealmSetting
{
Key = lookup.ToString(),
Value = bindable.Value,
RulesetID = ruleset?.ID,
Variant = variant,
};
realmFactory?.Context.Write(() => realmFactory.Context.Add(setting));
databasedSettings.Add(setting);
}
bindable.ValueChanged += b =>
{
realmFactory?.Context.Write(() => setting.Value = b.NewValue);
};
}
}
}

View File

@ -7,8 +7,8 @@ using Realms;
namespace osu.Game.Configuration namespace osu.Game.Configuration
{ {
[MapTo(@"Setting")] [MapTo(@"RulesetSetting")]
public class RealmSetting : RealmObject, IHasGuidPrimaryKey public class RealmRulesetSetting : RealmObject, IHasGuidPrimaryKey
{ {
[PrimaryKey] [PrimaryKey]
public Guid ID { get; set; } = Guid.NewGuid(); public Guid ID { get; set; } = Guid.NewGuid();

View File

@ -454,11 +454,11 @@ namespace osu.Game
var existingSettings = db.Context.DatabasedSetting; var existingSettings = db.Context.DatabasedSetting;
// only migrate data if the realm database is empty. // only migrate data if the realm database is empty.
if (!usage.Realm.All<RealmSetting>().Any()) if (!usage.Realm.All<RealmRulesetSetting>().Any())
{ {
foreach (var dkb in existingSettings) foreach (var dkb in existingSettings)
{ {
usage.Realm.Add(new RealmSetting usage.Realm.Add(new RealmRulesetSetting
{ {
ValueString = dkb.StringValue, ValueString = dkb.StringValue,
Key = dkb.Key, Key = dkb.Key,

View File

@ -2,16 +2,83 @@
// 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;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Bindables;
using osu.Framework.Configuration;
using osu.Game.Configuration; using osu.Game.Configuration;
using osu.Game.Database;
namespace osu.Game.Rulesets.Configuration namespace osu.Game.Rulesets.Configuration
{ {
public abstract class RulesetConfigManager<TLookup> : DatabasedConfigManager<TLookup>, IRulesetConfigManager public abstract class RulesetConfigManager<TLookup> : ConfigManager<TLookup>, IRulesetConfigManager
where TLookup : struct, Enum where TLookup : struct, Enum
{ {
protected RulesetConfigManager(SettingsStore settings, RulesetInfo ruleset, int? variant = null) private readonly RealmContextFactory realmFactory;
: base(settings, ruleset, variant)
private readonly int? variant;
private List<RealmRulesetSetting> databasedSettings = new List<RealmRulesetSetting>();
private readonly RulesetInfo ruleset;
protected RulesetConfigManager(SettingsStore store, RulesetInfo ruleset, int? variant = null)
{ {
realmFactory = store?.Realm;
this.ruleset = ruleset;
this.variant = variant;
Load();
InitialiseDefaults();
}
protected override void PerformLoad()
{
var rulesetID = ruleset?.ID;
if (realmFactory != null)
{
// As long as RulesetConfigCache exists, there is no need to subscribe to realm events.
databasedSettings = realmFactory.Context.All<RealmRulesetSetting>().Where(b => b.RulesetID == rulesetID && b.Variant == variant).ToList();
}
}
protected override bool PerformSave()
{
// do nothing, realm saves immediately
return true;
}
protected override void AddBindable<TBindable>(TLookup lookup, Bindable<TBindable> bindable)
{
base.AddBindable(lookup, bindable);
var setting = databasedSettings.Find(s => s.Key == lookup.ToString());
if (setting != null)
{
bindable.Parse(setting.Value);
}
else
{
setting = new RealmRulesetSetting
{
Key = lookup.ToString(),
Value = bindable.Value,
RulesetID = ruleset?.ID,
Variant = variant,
};
realmFactory?.Context.Write(() => realmFactory.Context.Add(setting));
databasedSettings.Add(setting);
}
bindable.ValueChanged += b =>
{
realmFactory?.Context.Write(() => setting.Value = b.NewValue);
};
} }
} }
} }