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

Merge pull request #15050 from peppy/fix-cross-thread-list-manipulation-skin-source-provider

Fix cross-thread list manipulation in `SkinProvidingContainer`
This commit is contained in:
Dan Balasescu 2021-10-12 21:59:49 +09:00 committed by GitHub
commit 60ba1987ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 42 deletions

View File

@ -6,7 +6,6 @@ using System.Linq;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Audio.Sample; using osu.Framework.Audio.Sample;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.OpenGL.Textures; using osu.Framework.Graphics.OpenGL.Textures;
using osu.Framework.Graphics.Textures; using osu.Framework.Graphics.Textures;
@ -65,10 +64,9 @@ namespace osu.Game.Tests.Skins
public new void TriggerSourceChanged() => base.TriggerSourceChanged(); public new void TriggerSourceChanged() => base.TriggerSourceChanged();
protected override void OnSourceChanged() protected override void RefreshSources()
{ {
ResetSources(); SetSources(sources);
sources.ForEach(AddSource);
} }
} }

View File

@ -12,6 +12,9 @@ namespace osu.Game.Skinning
/// </summary> /// </summary>
public interface ISkinSource : ISkin public interface ISkinSource : ISkin
{ {
/// <summary>
/// Fired whenever a source change occurs, signalling that consumers should re-query as required.
/// </summary>
event Action SourceChanged; event Action SourceChanged;
/// <summary> /// <summary>

View File

@ -58,10 +58,8 @@ namespace osu.Game.Skinning
return base.CreateChildDependencies(parent); return base.CreateChildDependencies(parent);
} }
protected override void OnSourceChanged() protected override void RefreshSources()
{ {
ResetSources();
// Populate a local list first so we can adjust the returned order as we go. // Populate a local list first so we can adjust the returned order as we go.
var sources = new List<ISkin>(); var sources = new List<ISkin>();
@ -91,8 +89,7 @@ namespace osu.Game.Skinning
else else
sources.Add(rulesetResourcesSkin); sources.Add(rulesetResourcesSkin);
foreach (var skin in sources) SetSources(sources);
AddSource(skin);
} }
protected ISkin GetLegacyRulesetTransformedSkin(ISkin legacySkin) protected ISkin GetLegacyRulesetTransformedSkin(ISkin legacySkin)

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations; using JetBrains.Annotations;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Audio.Sample; using osu.Framework.Audio.Sample;
@ -40,10 +41,12 @@ namespace osu.Game.Skinning
protected virtual bool AllowColourLookup => true; protected virtual bool AllowColourLookup => true;
private readonly object sourceSetLock = new object();
/// <summary> /// <summary>
/// A dictionary mapping each <see cref="ISkin"/> source to a wrapper which handles lookup allowances. /// A dictionary mapping each <see cref="ISkin"/> source to a wrapper which handles lookup allowances.
/// </summary> /// </summary>
private readonly List<(ISkin skin, DisableableSkinSource wrapped)> skinSources = new List<(ISkin, DisableableSkinSource)>(); private (ISkin skin, DisableableSkinSource wrapped)[] skinSources = Array.Empty<(ISkin skin, DisableableSkinSource wrapped)>();
/// <summary> /// <summary>
/// Constructs a new <see cref="SkinProvidingContainer"/> initialised with a single skin source. /// Constructs a new <see cref="SkinProvidingContainer"/> initialised with a single skin source.
@ -52,7 +55,7 @@ namespace osu.Game.Skinning
: this() : this()
{ {
if (skin != null) if (skin != null)
AddSource(skin); SetSources(new[] { skin });
} }
/// <summary> /// <summary>
@ -168,49 +171,42 @@ namespace osu.Game.Skinning
} }
/// <summary> /// <summary>
/// Add a new skin to this provider. Will be added to the end of the lookup order precedence. /// Replace the sources used for lookups in this container.
/// </summary> /// </summary>
/// <param name="skin">The skin to add.</param> /// <remarks>
protected void AddSource(ISkin skin) /// This does not implicitly fire a <see cref="SourceChanged"/> event. Consider calling <see cref="TriggerSourceChanged"/> if required.
/// </remarks>
/// <param name="sources">The new sources.</param>
protected void SetSources(IEnumerable<ISkin> sources)
{ {
skinSources.Add((skin, new DisableableSkinSource(skin, this))); lock (sourceSetLock)
{
foreach (var skin in skinSources)
{
if (skin.skin is ISkinSource source)
source.SourceChanged -= TriggerSourceChanged;
}
if (skin is ISkinSource source) skinSources = sources.Select(skin => (skin, new DisableableSkinSource(skin, this))).ToArray();
source.SourceChanged += TriggerSourceChanged;
foreach (var skin in skinSources)
{
if (skin.skin is ISkinSource source)
source.SourceChanged += TriggerSourceChanged;
}
}
} }
/// <summary> /// <summary>
/// Remove a skin from this provider. /// Invoked after any consumed source change, before the external <see cref="SourceChanged"/> event is fired.
/// </summary>
/// <param name="skin">The skin to remove.</param>
protected void RemoveSource(ISkin skin)
{
if (skinSources.RemoveAll(s => s.skin == skin) == 0)
return;
if (skin is ISkinSource source)
source.SourceChanged -= TriggerSourceChanged;
}
/// <summary>
/// Clears all skin sources.
/// </summary>
protected void ResetSources()
{
foreach (var i in skinSources.ToArray())
RemoveSource(i.skin);
}
/// <summary>
/// Invoked when any source has changed (either <see cref="ParentSource"/> or a source registered via <see cref="AddSource"/>).
/// This is also invoked once initially during <see cref="CreateChildDependencies"/> to ensure sources are ready for children consumption. /// This is also invoked once initially during <see cref="CreateChildDependencies"/> to ensure sources are ready for children consumption.
/// </summary> /// </summary>
protected virtual void OnSourceChanged() { } protected virtual void RefreshSources() { }
protected void TriggerSourceChanged() protected void TriggerSourceChanged()
{ {
// Expose to implementations, giving them a chance to react before notifying external consumers. // Expose to implementations, giving them a chance to react before notifying external consumers.
OnSourceChanged(); RefreshSources();
SourceChanged?.Invoke(); SourceChanged?.Invoke();
} }