1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 21:17:33 +08:00
osu-lazer/osu.Game/Rulesets/RulesetConfigCache.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

64 lines
2.2 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.Generic;
2018-06-11 14:07:42 +08:00
using osu.Framework.Graphics;
using osu.Game.Configuration;
using osu.Game.Database;
using osu.Game.Extensions;
2018-06-11 14:07:42 +08:00
using osu.Game.Rulesets.Configuration;
namespace osu.Game.Rulesets
{
public class RulesetConfigCache : Component, IRulesetConfigCache
2018-06-11 14:07:42 +08:00
{
private readonly RealmAccess realm;
private readonly RulesetStore rulesets;
2018-06-11 14:07:42 +08:00
private readonly Dictionary<string, IRulesetConfigManager> configCache = new Dictionary<string, IRulesetConfigManager>();
public RulesetConfigCache(RealmAccess realm, RulesetStore rulesets)
{
this.realm = realm;
this.rulesets = rulesets;
}
protected override void LoadComplete()
2018-06-11 14:07:42 +08:00
{
base.LoadComplete();
var settingsStore = new SettingsStore(realm);
// let's keep things simple for now and just retrieve all the required configs at startup..
foreach (var ruleset in rulesets.AvailableRulesets)
{
if (string.IsNullOrEmpty(ruleset.ShortName))
continue;
configCache[ruleset.ShortName] = ruleset.CreateInstance().CreateConfig(settingsStore);
}
2018-06-11 14:07:42 +08:00
}
public IRulesetConfigManager GetConfigFor(Ruleset ruleset)
{
if (!IsLoaded)
throw new InvalidOperationException($@"Cannot retrieve {nameof(IRulesetConfigManager)} before {nameof(RulesetConfigCache)} has loaded");
if (!configCache.TryGetValue(ruleset.RulesetInfo.ShortName, out var config))
throw new InvalidOperationException($@"Attempted to retrieve {nameof(IRulesetConfigManager)} for an unavailable ruleset {ruleset.GetDisplayString()}");
return config;
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
}
}