Refactor disallowing in SkinProvidingContainer to become per source

Fixes `FindProvider` becoming completely broken, because of no way to perform the checks on one skin source.
This commit is contained in:
Salman Ahmed
2021-06-11 12:29:28 +03:00
parent 2e01e61177
commit 9e16359f18

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Linq; using System.Linq;
using JetBrains.Annotations; using JetBrains.Annotations;
@ -28,11 +29,15 @@ namespace osu.Game.Skinning
/// </summary> /// </summary>
protected readonly BindableList<ISkin> SkinSources = new BindableList<ISkin>(); protected readonly BindableList<ISkin> SkinSources = new BindableList<ISkin>();
/// <summary>
/// A dictionary mapping each <see cref="ISkin"/> from the <see cref="SkinSources"/>
/// to one that performs the "allow lookup" checks before proceeding with a lookup.
/// </summary>
private readonly Dictionary<ISkin, DisableableSkinSource> disableableSkinSources = new Dictionary<ISkin, DisableableSkinSource>();
[CanBeNull] [CanBeNull]
private ISkinSource fallbackSource; private ISkinSource fallbackSource;
private readonly NoFallbackProxy noFallbackLookupProxy;
protected virtual bool AllowDrawableLookup(ISkinComponent component) => true; protected virtual bool AllowDrawableLookup(ISkinComponent component) => true;
protected virtual bool AllowTextureLookup(string componentName) => true; protected virtual bool AllowTextureLookup(string componentName) => true;
@ -60,31 +65,49 @@ namespace osu.Game.Skinning
{ {
RelativeSizeAxes = Axes.Both; RelativeSizeAxes = Axes.Both;
noFallbackLookupProxy = new NoFallbackProxy(this);
SkinSources.BindCollectionChanged(((_, args) => SkinSources.BindCollectionChanged(((_, args) =>
{ {
switch (args.Action) switch (args.Action)
{ {
case NotifyCollectionChangedAction.Add: case NotifyCollectionChangedAction.Add:
foreach (var source in args.NewItems.Cast<ISkin>().OfType<ISkinSource>()) foreach (var skin in args.NewItems.Cast<ISkin>())
{
disableableSkinSources.Add(skin, new DisableableSkinSource(skin, this));
if (skin is ISkinSource source)
source.SourceChanged += OnSourceChanged; source.SourceChanged += OnSourceChanged;
}
break; break;
case NotifyCollectionChangedAction.Reset: case NotifyCollectionChangedAction.Reset:
case NotifyCollectionChangedAction.Remove: case NotifyCollectionChangedAction.Remove:
foreach (var source in args.OldItems.Cast<ISkin>().OfType<ISkinSource>()) foreach (var skin in args.OldItems.Cast<ISkin>())
{
disableableSkinSources.Remove(skin);
if (skin is ISkinSource source)
source.SourceChanged -= OnSourceChanged; source.SourceChanged -= OnSourceChanged;
}
break; break;
case NotifyCollectionChangedAction.Replace: case NotifyCollectionChangedAction.Replace:
foreach (var source in args.OldItems.Cast<ISkin>().OfType<ISkinSource>()) foreach (var skin in args.OldItems.Cast<ISkin>())
source.SourceChanged -= OnSourceChanged; {
disableableSkinSources.Remove(skin);
foreach (var source in args.NewItems.Cast<ISkin>().OfType<ISkinSource>()) if (skin is ISkinSource source)
source.SourceChanged -= OnSourceChanged;
}
foreach (var skin in args.NewItems.Cast<ISkin>())
{
disableableSkinSources.Add(skin, new DisableableSkinSource(skin, this));
if (skin is ISkinSource source)
source.SourceChanged += OnSourceChanged; source.SourceChanged += OnSourceChanged;
}
break; break;
} }
@ -95,8 +118,7 @@ namespace osu.Game.Skinning
{ {
foreach (var skin in SkinSources) foreach (var skin in SkinSources)
{ {
// a proxy must be used here to correctly pass through the "Allow" checks without implicitly falling back to the fallbackSource. if (lookupFunction(disableableSkinSources[skin]))
if (lookupFunction(noFallbackLookupProxy))
return skin; return skin;
} }
@ -104,93 +126,49 @@ namespace osu.Game.Skinning
} }
public Drawable GetDrawableComponent(ISkinComponent component) public Drawable GetDrawableComponent(ISkinComponent component)
=> GetDrawableComponent(component, true);
public Drawable GetDrawableComponent(ISkinComponent component, bool fallback)
{
if (AllowDrawableLookup(component))
{ {
foreach (var skin in SkinSources) foreach (var skin in SkinSources)
{ {
Drawable sourceDrawable; Drawable sourceDrawable;
if ((sourceDrawable = skin?.GetDrawableComponent(component)) != null) if ((sourceDrawable = disableableSkinSources[skin]?.GetDrawableComponent(component)) != null)
return sourceDrawable; return sourceDrawable;
} }
}
if (!fallback)
return null;
return fallbackSource?.GetDrawableComponent(component); return fallbackSource?.GetDrawableComponent(component);
} }
public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT) public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT)
=> GetTexture(componentName, wrapModeS, wrapModeT, true);
public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT, bool fallback)
{
if (AllowTextureLookup(componentName))
{ {
foreach (var skin in SkinSources) foreach (var skin in SkinSources)
{ {
Texture sourceTexture; Texture sourceTexture;
if ((sourceTexture = skin?.GetTexture(componentName, wrapModeS, wrapModeT)) != null) if ((sourceTexture = disableableSkinSources[skin]?.GetTexture(componentName, wrapModeS, wrapModeT)) != null)
return sourceTexture; return sourceTexture;
} }
}
if (!fallback)
return null;
return fallbackSource?.GetTexture(componentName, wrapModeS, wrapModeT); return fallbackSource?.GetTexture(componentName, wrapModeS, wrapModeT);
} }
public ISample GetSample(ISampleInfo sampleInfo) public ISample GetSample(ISampleInfo sampleInfo)
=> GetSample(sampleInfo, true);
public ISample GetSample(ISampleInfo sampleInfo, bool fallback)
{
if (AllowSampleLookup(sampleInfo))
{ {
foreach (var skin in SkinSources) foreach (var skin in SkinSources)
{ {
ISample sourceSample; ISample sourceSample;
if ((sourceSample = skin?.GetSample(sampleInfo)) != null) if ((sourceSample = disableableSkinSources[skin]?.GetSample(sampleInfo)) != null)
return sourceSample; return sourceSample;
} }
}
if (!fallback)
return null;
return fallbackSource?.GetSample(sampleInfo); return fallbackSource?.GetSample(sampleInfo);
} }
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup) public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup)
=> GetConfig<TLookup, TValue>(lookup, true);
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup, bool fallback)
{
if (lookup is GlobalSkinColours || lookup is SkinCustomColourLookup)
return lookupWithFallback<TLookup, TValue>(lookup, AllowColourLookup, fallback);
return lookupWithFallback<TLookup, TValue>(lookup, AllowConfigurationLookup, fallback);
}
private IBindable<TValue> lookupWithFallback<TLookup, TValue>(TLookup lookup, bool canUseSkinLookup, bool canUseFallback)
{
if (canUseSkinLookup)
{ {
foreach (var skin in SkinSources) foreach (var skin in SkinSources)
{ {
IBindable<TValue> bindable; IBindable<TValue> bindable;
if ((bindable = skin?.GetConfig<TLookup, TValue>(lookup)) != null) if ((bindable = disableableSkinSources[skin]?.GetConfig<TLookup, TValue>(lookup)) != null)
return bindable; return bindable;
} }
}
if (!canUseFallback)
return null;
return fallbackSource?.GetConfig<TLookup, TValue>(lookup); return fallbackSource?.GetConfig<TLookup, TValue>(lookup);
} }
@ -224,35 +202,61 @@ namespace osu.Game.Skinning
source.SourceChanged -= OnSourceChanged; source.SourceChanged -= OnSourceChanged;
} }
private class NoFallbackProxy : ISkinSource private class DisableableSkinSource : ISkin
{ {
private readonly ISkin skin;
private readonly SkinProvidingContainer provider; private readonly SkinProvidingContainer provider;
public NoFallbackProxy(SkinProvidingContainer provider) public DisableableSkinSource(ISkin skin, SkinProvidingContainer provider)
{ {
this.skin = skin;
this.provider = provider; this.provider = provider;
} }
public Drawable GetDrawableComponent(ISkinComponent component) public Drawable GetDrawableComponent(ISkinComponent component)
=> provider.GetDrawableComponent(component, false); {
if (provider.AllowDrawableLookup(component))
return skin.GetDrawableComponent(component);
return null;
}
public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT) public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT)
=> provider.GetTexture(componentName, wrapModeS, wrapModeT, false); {
if (provider.AllowTextureLookup(componentName))
return skin.GetTexture(componentName, wrapModeS, wrapModeT);
return null;
}
public ISample GetSample(ISampleInfo sampleInfo) public ISample GetSample(ISampleInfo sampleInfo)
=> provider.GetSample(sampleInfo, false); {
if (provider.AllowSampleLookup(sampleInfo))
return skin.GetSample(sampleInfo);
return null;
}
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup) public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup)
=> provider.GetConfig<TLookup, TValue>(lookup, false);
public event Action SourceChanged
{ {
add => provider.SourceChanged += value; switch (lookup)
remove => provider.SourceChanged -= value; {
case GlobalSkinColours _:
case SkinCustomColourLookup _:
if (provider.AllowColourLookup)
return skin.GetConfig<TLookup, TValue>(lookup);
break;
default:
if (provider.AllowConfigurationLookup)
return skin.GetConfig<TLookup, TValue>(lookup);
break;
} }
public ISkin FindProvider(Func<ISkin, bool> lookupFunction) => return null;
provider.FindProvider(lookupFunction); }
} }
} }
} }