1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-20 05:03:20 +08:00

No more statics + better unregistration

This commit is contained in:
smoogipoo 2018-01-18 17:40:05 +09:00
parent 89f4bfa7b5
commit dee298c395
4 changed files with 28 additions and 21 deletions

View File

@ -161,11 +161,19 @@ namespace osu.Game.Rulesets.Mania.UI
judgements.Scale = Scale;
}
private Bindable<double> scrollTime;
[BackgroundDependencyLoader]
private void load(OsuColour colours, ManiaConfigManager maniaConfig)
{
maniaConfig.BindWith(ManiaSetting.ScrollTime, VisibleTimeRange);
// Todo: The following two lines shouldn't be required, but is an effect of not having config databased
// 1. ValueChanged is run prior to values being propagated
// 2. We want the config to be saved ASAP, in-case a new ManiaPlayfield is instantiated
scrollTime = maniaConfig.GetBindable<double>(ManiaSetting.ScrollTime);
scrollTime.ValueChanged += v => maniaConfig.Save();
normalColumnColours = new List<Color4>
{
colours.RedDark,

View File

@ -118,16 +118,16 @@ namespace osu.Game.Overlays
[BackgroundDependencyLoader]
private void load(FrameworkConfigManager frameworkConfig)
{
Register(frameworkConfig);
Register(this, frameworkConfig);
}
private readonly Dictionary<object, TrackedSettings> trackedConfigManagers = new Dictionary<object, TrackedSettings>();
private readonly Dictionary<(IDisposable, IConfigManager), TrackedSettings> trackedConfigManagers = new Dictionary<(IDisposable, IConfigManager), TrackedSettings>();
public void Register(ITrackableConfigManager configManager)
public void Register(IDisposable source, ITrackableConfigManager configManager)
{
if (configManager == null) throw new ArgumentNullException(nameof(configManager));
if (trackedConfigManagers.ContainsKey(configManager))
if (trackedConfigManagers.ContainsKey((source, configManager)))
return;
var trackedSettings = configManager.CreateTrackedSettings();
@ -138,20 +138,20 @@ namespace osu.Game.Overlays
trackedSettings.SettingChanged += display;
trackedConfigManagers.Add(configManager, trackedSettings);
trackedConfigManagers.Add((source, configManager), trackedSettings);
}
public void Unregister(ITrackableConfigManager configManager)
public void Unregister(IDisposable source, ITrackableConfigManager configManager)
{
if (configManager == null) throw new ArgumentNullException(nameof(configManager));
if (!trackedConfigManagers.TryGetValue(configManager, out var existing))
if (!trackedConfigManagers.TryGetValue((source, configManager), out var existing))
return;
existing.Unload();
existing.SettingChanged -= display;
trackedConfigManagers.Remove(configManager);
trackedConfigManagers.Remove((source, configManager));
}
private void display(SettingDescription description)

View File

@ -1,12 +1,11 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Configuration;
using osu.Framework.Configuration.Tracking;
namespace osu.Game.Rulesets.Configuration
{
public interface IRulesetConfigManager : ITrackableConfigManager, IConfigManager
public interface IRulesetConfigManager : ITrackableConfigManager
{
}
}

View File

@ -68,6 +68,7 @@ namespace osu.Game.Rulesets.UI
protected readonly Ruleset Ruleset;
private IRulesetConfigManager rulesetConfig;
private OnScreenDisplay onScreenDisplay;
private DependencyContainer dependencies;
@ -89,23 +90,17 @@ namespace osu.Game.Rulesets.UI
[BackgroundDependencyLoader]
private void load(Storage storage, OnScreenDisplay onScreenDisplay)
{
rulesetConfig = getConfig(storage);
this.onScreenDisplay = onScreenDisplay;
rulesetConfig = CreateConfig(Ruleset, storage);
if (rulesetConfig != null)
{
dependencies.Cache(rulesetConfig);
onScreenDisplay?.Register(rulesetConfig);
onScreenDisplay?.Register(this, rulesetConfig);
}
}
private static readonly Dictionary<Type, IRulesetConfigManager> config_cache = new Dictionary<Type, IRulesetConfigManager>();
private IRulesetConfigManager getConfig(Storage storage)
{
if (config_cache.TryGetValue(GetType(), out var existing))
return existing;
return config_cache[GetType()] = CreateConfig(Ruleset, storage);
}
public abstract ScoreProcessor CreateScoreProcessor();
/// <summary>
@ -148,7 +143,12 @@ namespace osu.Game.Rulesets.UI
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
rulesetConfig.Save();
if (rulesetConfig != null)
{
onScreenDisplay?.Unregister(this, rulesetConfig);
rulesetConfig = null;
}
}
}