// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Concurrent; using osu.Framework.Graphics; using osu.Game.Configuration; using osu.Game.Rulesets.Configuration; namespace osu.Game.Rulesets { /// /// A cache that provides a single per-ruleset. /// This is done to support referring to and updating ruleset configs from multiple locations in the absence of inter-config bindings. /// public class RulesetConfigCache : Component { private readonly ConcurrentDictionary configCache = new ConcurrentDictionary(); private readonly SettingsStore settingsStore; public RulesetConfigCache(SettingsStore settingsStore) { this.settingsStore = settingsStore; } /// /// Retrieves the for a . /// /// The to retrieve the for. /// The defined by , null if doesn't define one. /// If doesn't have a valid . public IRulesetConfigManager GetConfigFor(Ruleset ruleset) { if (ruleset.RulesetInfo.ID == null) return null; return configCache.GetOrAdd(ruleset.RulesetInfo.ID.Value, _ => ruleset.CreateConfig(settingsStore)); } protected override void Dispose(bool isDisposing) { base.Dispose(isDisposing); // ensures any potential database operations are finalised before game destruction. foreach (var c in configCache.Values) c?.Dispose(); } } }