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-06-11 14:07:42 +08:00
|
|
|
|
|
2021-12-24 02:27:27 +08:00
|
|
|
|
using System;
|
2021-09-15 13:39:47 +08:00
|
|
|
|
using System.Collections.Generic;
|
2018-06-11 14:07:42 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
2021-09-15 15:08:31 +08:00
|
|
|
|
using osu.Game.Configuration;
|
2021-09-15 13:39:47 +08:00
|
|
|
|
using osu.Game.Database;
|
2021-12-24 02:27:27 +08:00
|
|
|
|
using osu.Game.Extensions;
|
2018-06-11 14:07:42 +08:00
|
|
|
|
using osu.Game.Rulesets.Configuration;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets
|
|
|
|
|
{
|
2022-11-24 13:32:20 +08:00
|
|
|
|
public partial class RulesetConfigCache : Component, IRulesetConfigCache
|
2018-06-11 14:07:42 +08:00
|
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
|
private readonly RealmAccess realm;
|
2021-09-15 13:39:47 +08:00
|
|
|
|
private readonly RulesetStore rulesets;
|
2018-06-11 14:07:42 +08:00
|
|
|
|
|
2022-07-10 10:01:56 +08:00
|
|
|
|
private readonly Dictionary<string, IRulesetConfigManager?> configCache = new Dictionary<string, IRulesetConfigManager?>();
|
2021-09-15 13:39:47 +08:00
|
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
|
public RulesetConfigCache(RealmAccess realm, RulesetStore rulesets)
|
2021-09-15 13:39:47 +08:00
|
|
|
|
{
|
2022-01-24 18:59:58 +08:00
|
|
|
|
this.realm = realm;
|
2021-09-15 13:39:47 +08:00
|
|
|
|
this.rulesets = rulesets;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
2018-06-11 14:07:42 +08:00
|
|
|
|
{
|
2021-09-15 13:39:47 +08:00
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
2022-01-24 18:59:58 +08:00
|
|
|
|
var settingsStore = new SettingsStore(realm);
|
2021-09-15 15:08:31 +08:00
|
|
|
|
|
2021-09-15 13:39:47 +08:00
|
|
|
|
// let's keep things simple for now and just retrieve all the required configs at startup..
|
|
|
|
|
foreach (var ruleset in rulesets.AvailableRulesets)
|
|
|
|
|
{
|
2021-11-23 11:52:17 +08:00
|
|
|
|
if (string.IsNullOrEmpty(ruleset.ShortName))
|
2021-09-15 13:39:47 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2021-11-23 11:52:17 +08:00
|
|
|
|
configCache[ruleset.ShortName] = ruleset.CreateInstance().CreateConfig(settingsStore);
|
2021-09-15 13:39:47 +08:00
|
|
|
|
}
|
2018-06-11 14:07:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-10 10:01:56 +08:00
|
|
|
|
public IRulesetConfigManager? GetConfigFor(Ruleset ruleset)
|
2018-06-11 14:07:42 +08:00
|
|
|
|
{
|
2021-12-24 02:27:27 +08:00
|
|
|
|
if (!IsLoaded)
|
|
|
|
|
throw new InvalidOperationException($@"Cannot retrieve {nameof(IRulesetConfigManager)} before {nameof(RulesetConfigCache)} has loaded");
|
|
|
|
|
|
2021-11-23 11:52:17 +08:00
|
|
|
|
if (!configCache.TryGetValue(ruleset.RulesetInfo.ShortName, out var config))
|
2021-12-24 02:27:27 +08:00
|
|
|
|
throw new InvalidOperationException($@"Attempted to retrieve {nameof(IRulesetConfigManager)} for an unavailable ruleset {ruleset.GetDisplayString()}");
|
2021-09-15 13:39:47 +08:00
|
|
|
|
|
|
|
|
|
return config;
|
2018-06-11 14:07:42 +08:00
|
|
|
|
}
|
2019-09-05 23:22:35 +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.
|
2019-09-05 23:22:35 +08:00
|
|
|
|
foreach (var c in configCache.Values)
|
2019-11-12 12:41:54 +08:00
|
|
|
|
c?.Dispose();
|
2019-09-05 23:22:35 +08:00
|
|
|
|
}
|
2018-06-11 14:07:42 +08:00
|
|
|
|
}
|
|
|
|
|
}
|