1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-13 10:03:05 +08:00
osu-lazer/osu.Game/Configuration/DatabasedConfigManager.cs
Dean Herbert 520e550764 Bring back SettingsStore to avoid changing ruleset API for now
Also fixes some remaining test failures due to locally constructed
rulesets that are not being tracked by the game.
2021-09-15 17:12:02 +09:00

85 lines
2.5 KiB
C#

// 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);
};
}
}
}