// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. #nullable disable using System; using System.Collections.Generic; using System.Linq; using osu.Framework.Bindables; using osu.Framework.Configuration; using osu.Game.Configuration; using osu.Game.Database; namespace osu.Game.Rulesets.Configuration { public abstract class RulesetConfigManager : ConfigManager, IRulesetConfigManager where TLookup : struct, Enum { private readonly RealmAccess realm; private readonly int variant; private List databasedSettings = new List(); private readonly string rulesetName; protected RulesetConfigManager(SettingsStore store, RulesetInfo ruleset, int? variant = null) { realm = store?.Realm; rulesetName = ruleset.ShortName; 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().Where(b => b.RulesetName == rulesetName && b.Variant == variant).ToList(); } } private readonly HashSet pendingWrites = new HashSet(); protected override bool PerformSave() { TLookup[] changed; lock (pendingWrites) { changed = pendingWrites.ToArray(); pendingWrites.Clear(); } realm?.Write(r => { foreach (var c in changed) { var setting = r.All().First(s => s.RulesetName == rulesetName && s.Variant == variant && s.Key == c.ToString()); setting.Value = ConfigStore[c].ToString(); } }); return true; } protected override void AddBindable(TLookup lookup, Bindable 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.ToString(), RulesetName = rulesetName, Variant = variant, }; realm?.Realm.Write(() => realm.Realm.Add(setting)); databasedSettings.Add(setting); } bindable.ValueChanged += b => { lock (pendingWrites) pendingWrites.Add(lookup); }; } } }