1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 16:48:22 +08:00
osu-lazer/osu.Game/Rulesets/Configuration/RulesetConfigManager.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

112 lines
3.3 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using osu.Framework.Bindables;
using osu.Framework.Configuration;
using osu.Framework.Extensions;
using osu.Game.Configuration;
using osu.Game.Database;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Rulesets.Configuration
{
public abstract class RulesetConfigManager<TLookup> : ConfigManager<TLookup>, IRulesetConfigManager
where TLookup : struct, Enum
{
private readonly RealmAccess realm;
private readonly int variant;
private List<RealmRulesetSetting> databasedSettings = new List<RealmRulesetSetting>();
private readonly string rulesetName;
protected RulesetConfigManager(SettingsStore store, RulesetInfo ruleset, int? variant = null)
{
realm = store?.Realm;
2021-09-15 16:01:31 +08:00
rulesetName = ruleset.ShortName;
2021-09-15 16:01:31 +08:00
this.variant = variant ?? 0;
Load();
InitialiseDefaults();
}
protected override void PerformLoad()
{
if (realm != null)
{
// As long as RulesetConfigCache exists, there is no need to subscribe to realm events.
databasedSettings = realm.Realm.All<RealmRulesetSetting>().Where(b => b.RulesetName == rulesetName && b.Variant == variant).ToList();
}
}
private readonly HashSet<TLookup> pendingWrites = new HashSet<TLookup>();
protected override bool PerformSave()
{
TLookup[] changed;
lock (pendingWrites)
{
changed = pendingWrites.ToArray();
pendingWrites.Clear();
}
if (!changed.Any())
return true;
realm?.Write(r =>
{
foreach (var c in changed)
{
var setting = r.All<RealmRulesetSetting>().First(s => s.RulesetName == rulesetName && s.Variant == variant && s.Key == c.ToString());
setting.Value = ConfigStore[c].ToString(CultureInfo.InvariantCulture);
}
});
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)
{
2023-12-13 13:07:38 +08:00
bindable.Parse(setting.Value, CultureInfo.InvariantCulture);
}
else
{
setting = new RealmRulesetSetting
{
Key = lookup.ToString(),
Value = bindable.ToString(CultureInfo.InvariantCulture),
RulesetName = rulesetName,
Variant = variant,
};
realm?.Realm.Write(() => realm.Realm.Add(setting));
databasedSettings.Add(setting);
}
2022-06-24 20:25:23 +08:00
bindable.ValueChanged += _ =>
{
lock (pendingWrites)
pendingWrites.Add(lookup);
};
}
}
}