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