1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 00:52:59 +08:00
osu-lazer/osu.Game/Rulesets/RulesetConfigCache.cs

50 lines
2.1 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-06-11 14:07:42 +08:00
using System;
using System.Collections.Concurrent;
2018-06-11 14:07:42 +08:00
using osu.Framework.Graphics;
using osu.Game.Configuration;
using osu.Game.Rulesets.Configuration;
namespace osu.Game.Rulesets
{
/// <summary>
2018-06-11 14:44:59 +08:00
/// A cache that provides a single <see cref="IRulesetConfigManager"/> per-ruleset.
2018-06-11 14:07:42 +08:00
/// This is done to support referring to and updating ruleset configs from multiple locations in the absence of inter-config bindings.
/// </summary>
public class RulesetConfigCache : Component
{
private readonly ConcurrentDictionary<int, IRulesetConfigManager> configCache = new ConcurrentDictionary<int, IRulesetConfigManager>();
2018-06-11 14:07:42 +08:00
private readonly SettingsStore settingsStore;
public RulesetConfigCache(SettingsStore settingsStore)
{
this.settingsStore = settingsStore;
}
/// <summary>
/// Retrieves the <see cref="IRulesetConfigManager"/> for a <see cref="Ruleset"/>.
/// </summary>
/// <param name="ruleset">The <see cref="Ruleset"/> to retrieve the <see cref="IRulesetConfigManager"/> for.</param>
/// <returns>The <see cref="IRulesetConfigManager"/> defined by <paramref name="ruleset"/>, null if <paramref name="ruleset"/> doesn't define one.</returns>
/// <exception cref="InvalidOperationException">If <paramref name="ruleset"/> doesn't have a valid <see cref="RulesetInfo.ID"/>.</exception>
public IRulesetConfigManager GetConfigFor(Ruleset ruleset)
{
if (ruleset.RulesetInfo.ID == null)
return null;
2018-06-11 14:07:42 +08:00
return configCache.GetOrAdd(ruleset.RulesetInfo.ID.Value, _ => ruleset.CreateConfig(settingsStore));
2018-06-11 14:07:42 +08:00
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
2019-09-06 00:14:37 +08:00
// ensures any potential database operations are finalised before game destruction.
foreach (var c in configCache.Values)
2019-11-12 12:41:54 +08:00
c?.Dispose();
}
2018-06-11 14:07:42 +08:00
}
}