1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-13 08:32:57 +08:00

Remove double fetch/binding of parent source

This commit is contained in:
Dean Herbert 2021-07-06 16:41:48 +09:00
parent 93ef783339
commit 935fbe7cc6
2 changed files with 35 additions and 26 deletions

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Allocation;
@ -47,22 +48,19 @@ namespace osu.Game.Skinning
};
}
private ISkinSource parentSource;
private ResourceStoreBackedSkin rulesetResourcesSkin;
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
parentSource = parent.Get<ISkinSource>();
parentSource.SourceChanged += OnSourceChanged;
if (Ruleset.CreateResourceStore() is IResourceStore<byte[]> resources)
rulesetResourcesSkin = new ResourceStoreBackedSkin(resources, parent.Get<GameHost>(), parent.Get<AudioManager>());
var dependencies = base.CreateChildDependencies(parent);
// ensure sources are populated and ready for use before childrens' asynchronous load flow.
UpdateSkinSources();
return base.CreateChildDependencies(parent);
return dependencies;
}
protected override void OnSourceChanged()
@ -77,7 +75,9 @@ namespace osu.Game.Skinning
var skinSources = new List<ISkin>();
foreach (var skin in parentSource.AllSources)
Debug.Assert(ParentSource != null);
foreach (var skin in ParentSource.AllSources)
{
switch (skin)
{
@ -121,9 +121,6 @@ namespace osu.Game.Skinning
{
base.Dispose(isDisposing);
if (parentSource != null)
parentSource.SourceChanged -= OnSourceChanged;
rulesetResourcesSkin?.Dispose();
}
}

View File

@ -35,7 +35,7 @@ namespace osu.Game.Skinning
private readonly Dictionary<ISkin, DisableableSkinSource> skinSources = new Dictionary<ISkin, DisableableSkinSource>();
[CanBeNull]
private ISkinSource fallbackSource;
protected ISkinSource ParentSource { get; private set; }
/// <summary>
/// Whether falling back to parent <see cref="ISkinSource"/>s is allowed in this container.
@ -101,7 +101,10 @@ namespace osu.Game.Skinning
return skin;
}
return fallbackSource?.FindProvider(lookupFunction);
if (!AllowFallingBackToParent)
return null;
return ParentSource?.FindProvider(lookupFunction);
}
public IEnumerable<ISkin> AllSources
@ -111,9 +114,9 @@ namespace osu.Game.Skinning
foreach (var skin in SkinSources)
yield return skin;
if (fallbackSource != null)
if (AllowFallingBackToParent && ParentSource != null)
{
foreach (var skin in fallbackSource.AllSources)
foreach (var skin in ParentSource.AllSources)
yield return skin;
}
}
@ -128,7 +131,10 @@ namespace osu.Game.Skinning
return sourceDrawable;
}
return fallbackSource?.GetDrawableComponent(component);
if (!AllowFallingBackToParent)
return null;
return ParentSource?.GetDrawableComponent(component);
}
public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT)
@ -140,7 +146,10 @@ namespace osu.Game.Skinning
return sourceTexture;
}
return fallbackSource?.GetTexture(componentName, wrapModeS, wrapModeT);
if (!AllowFallingBackToParent)
return null;
return ParentSource?.GetTexture(componentName, wrapModeS, wrapModeT);
}
public ISample GetSample(ISampleInfo sampleInfo)
@ -152,7 +161,10 @@ namespace osu.Game.Skinning
return sourceSample;
}
return fallbackSource?.GetSample(sampleInfo);
if (!AllowFallingBackToParent)
return null;
return ParentSource?.GetSample(sampleInfo);
}
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup)
@ -164,7 +176,10 @@ namespace osu.Game.Skinning
return bindable;
}
return fallbackSource?.GetConfig<TLookup, TValue>(lookup);
if (!AllowFallingBackToParent)
return null;
return ParentSource?.GetConfig<TLookup, TValue>(lookup);
}
protected virtual void OnSourceChanged() => SourceChanged?.Invoke();
@ -173,12 +188,9 @@ namespace osu.Game.Skinning
{
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
if (AllowFallingBackToParent)
{
fallbackSource = dependencies.Get<ISkinSource>();
if (fallbackSource != null)
fallbackSource.SourceChanged += OnSourceChanged;
}
ParentSource = dependencies.Get<ISkinSource>();
if (ParentSource != null)
ParentSource.SourceChanged += OnSourceChanged;
dependencies.CacheAs<ISkinSource>(this);
@ -192,8 +204,8 @@ namespace osu.Game.Skinning
base.Dispose(isDisposing);
if (fallbackSource != null)
fallbackSource.SourceChanged -= OnSourceChanged;
if (ParentSource != null)
ParentSource.SourceChanged -= OnSourceChanged;
foreach (var source in SkinSources.OfType<ISkinSource>())
source.SourceChanged -= OnSourceChanged;