2019-01-24 08:43:03 +00: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-06-11 06:07:42 +00:00
using System ;
2021-09-15 05:39:47 +00:00
using System.Collections.Generic ;
2018-06-11 06:07:42 +00:00
using osu.Framework.Graphics ;
2021-09-15 07:08:31 +00:00
using osu.Game.Configuration ;
2021-09-15 05:39:47 +00:00
using osu.Game.Database ;
2018-06-11 06:07:42 +00:00
using osu.Game.Rulesets.Configuration ;
namespace osu.Game.Rulesets
{
/// <summary>
2018-06-11 06:44:59 +00:00
/// A cache that provides a single <see cref="IRulesetConfigManager"/> per-ruleset.
2018-06-11 06:07:42 +00: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
{
2021-09-15 05:39:47 +00:00
private readonly RealmContextFactory realmFactory ;
private readonly RulesetStore rulesets ;
2018-06-11 06:07:42 +00:00
2021-09-15 05:39:47 +00:00
private readonly Dictionary < int , IRulesetConfigManager > configCache = new Dictionary < int , IRulesetConfigManager > ( ) ;
public RulesetConfigCache ( RealmContextFactory realmFactory , RulesetStore rulesets )
{
this . realmFactory = realmFactory ;
this . rulesets = rulesets ;
}
protected override void LoadComplete ( )
2018-06-11 06:07:42 +00:00
{
2021-09-15 05:39:47 +00:00
base . LoadComplete ( ) ;
2021-09-15 07:08:31 +00:00
var settingsStore = new SettingsStore ( realmFactory ) ;
2021-09-15 05:39:47 +00:00
// let's keep things simple for now and just retrieve all the required configs at startup..
foreach ( var ruleset in rulesets . AvailableRulesets )
{
if ( ruleset . ID = = null )
continue ;
2021-09-15 07:08:31 +00:00
configCache [ ruleset . ID . Value ] = ruleset . CreateInstance ( ) . CreateConfig ( settingsStore ) ;
2021-09-15 05:39:47 +00:00
}
2018-06-11 06:07:42 +00:00
}
/// <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 )
2019-08-26 07:34:12 +00:00
return null ;
2018-06-11 06:07:42 +00:00
2021-09-15 05:39:47 +00:00
if ( ! configCache . TryGetValue ( ruleset . RulesetInfo . ID . Value , out var config ) )
2021-09-15 07:08:31 +00:00
// any ruleset request which wasn't initialised on startup should not be stored to realm.
// this should only be used by tests.
return ruleset . CreateConfig ( null ) ;
2021-09-15 05:39:47 +00:00
return config ;
2018-06-11 06:07:42 +00:00
}
2019-09-05 15:22:35 +00:00
protected override void Dispose ( bool isDisposing )
{
base . Dispose ( isDisposing ) ;
2019-09-05 16:14:37 +00:00
// ensures any potential database operations are finalised before game destruction.
2019-09-05 15:22:35 +00:00
foreach ( var c in configCache . Values )
2019-11-12 04:41:54 +00:00
c ? . Dispose ( ) ;
2019-09-05 15:22:35 +00:00
}
2018-06-11 06:07:42 +00:00
}
}