From 36d2199a0241173a8931fab9f17ef56a2191970e Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Fri, 18 Jun 2021 19:20:57 +0900 Subject: [PATCH] Add exception on Apply() while loading --- .../Pooling/PoolableDrawableWithLifetime.cs | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/osu.Game/Rulesets/Objects/Pooling/PoolableDrawableWithLifetime.cs b/osu.Game/Rulesets/Objects/Pooling/PoolableDrawableWithLifetime.cs index 3ab85aa214..faa82786cd 100644 --- a/osu.Game/Rulesets/Objects/Pooling/PoolableDrawableWithLifetime.cs +++ b/osu.Game/Rulesets/Objects/Pooling/PoolableDrawableWithLifetime.cs @@ -28,12 +28,9 @@ namespace osu.Game.Rulesets.Objects.Pooling public TEntry? Entry { get => entry; - set { - if (LoadState < LoadState.Ready) - entry = value; - else if (value != null) + if (value != null) Apply(value); else if (HasEntryApplied) free(); @@ -86,7 +83,7 @@ namespace osu.Game.Rulesets.Objects.Pooling // Apply the initial entry. if (Entry != null && !HasEntryApplied) - Apply(Entry); + apply(Entry); } /// @@ -95,16 +92,10 @@ namespace osu.Game.Rulesets.Objects.Pooling /// public void Apply(TEntry entry) { - if (HasEntryApplied) - free(); + if (LoadState == LoadState.Loading) + throw new InvalidOperationException($"Cannot apply a new {nameof(TEntry)} while currently loading."); - this.entry = entry; - entry.LifetimeChanged += setLifetimeFromEntry; - setLifetimeFromEntry(entry); - - OnApply(entry); - - HasEntryApplied = true; + apply(entry); } protected sealed override void FreeAfterUse() @@ -130,6 +121,20 @@ namespace osu.Game.Rulesets.Objects.Pooling { } + private void apply(TEntry entry) + { + if (HasEntryApplied) + free(); + + this.entry = entry; + entry.LifetimeChanged += setLifetimeFromEntry; + setLifetimeFromEntry(entry); + + OnApply(entry); + + HasEntryApplied = true; + } + private void free() { Debug.Assert(Entry != null && HasEntryApplied);