From fa4d021368acefebbe302a57e5dc7187fd7a1e55 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 26 Mar 2020 02:30:29 +0100 Subject: [PATCH] fixed cancellation causing the wrong coroutines to be stopped --- Coroutine/ActiveCoroutine.cs | 39 +++++++++++++++++++++-------------- Coroutine/CoroutineHandler.cs | 16 ++++---------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/Coroutine/ActiveCoroutine.cs b/Coroutine/ActiveCoroutine.cs index 899377e..03a8812 100644 --- a/Coroutine/ActiveCoroutine.cs +++ b/Coroutine/ActiveCoroutine.cs @@ -14,36 +14,45 @@ namespace Coroutine { this.enumerator.MoveNext(); } + public bool Cancel() { + if (this.IsFinished || this.WasCanceled) + return false; + this.WasCanceled = true; + this.IsFinished = true; + this.OnFinished?.Invoke(this); + return true; + } + internal bool Tick(double deltaSeconds) { - var curr = this.enumerator.Current; - if (curr != null && curr.Tick(deltaSeconds)) - this.MoveNext(); + if (!this.WasCanceled) { + var curr = this.enumerator.Current; + if (curr != null && curr.Tick(deltaSeconds)) + this.MoveNext(); + } return this.IsFinished; } internal bool OnEvent(Event evt) { - var curr = this.enumerator.Current; - if (curr != null && curr.OnEvent(evt)) - this.MoveNext(); + if (!this.WasCanceled) { + var curr = this.enumerator.Current; + if (curr != null && curr.OnEvent(evt)) + this.MoveNext(); + } return this.IsFinished; } - internal void Finish(bool cancel) { - this.IsFinished = true; - this.WasCanceled = cancel; - this.OnFinished?.Invoke(this, cancel); - } - private void MoveNext() { - if (!this.enumerator.MoveNext()) - this.Finish(false); + if (!this.enumerator.MoveNext()) { + this.IsFinished = true; + this.OnFinished?.Invoke(this); + } } internal WaitType GetCurrentType() { return this.enumerator.Current.GetWaitType(); } - public delegate void FinishCallback(ActiveCoroutine coroutine, bool canceled); + public delegate void FinishCallback(ActiveCoroutine coroutine); } } \ No newline at end of file diff --git a/Coroutine/CoroutineHandler.cs b/Coroutine/CoroutineHandler.cs index a040dc0..43ba3ca 100644 --- a/Coroutine/CoroutineHandler.cs +++ b/Coroutine/CoroutineHandler.cs @@ -18,14 +18,6 @@ namespace Coroutine { return inst; } - public static bool Stop(ActiveCoroutine coroutine) { - if (TickingCoroutines.Remove(coroutine) || EventCoroutines.Remove(coroutine)) { - coroutine.Finish(true); - return true; - } - return false; - } - public static void InvokeLater(IWait wait, Action action) { Start(InvokeLaterImpl(wait, action)); } @@ -34,9 +26,9 @@ namespace Coroutine { for (var i = TickingCoroutines.Count - 1; i >= 0; i--) { var coroutine = TickingCoroutines[i]; if (coroutine.Tick(deltaSeconds)) { - TickingCoroutines.Remove(coroutine); + TickingCoroutines.RemoveAt(i); } else if (coroutine.GetCurrentType() != WaitType.Tick) { - TickingCoroutines.Remove(coroutine); + TickingCoroutines.RemoveAt(i); EventCoroutines.Add(coroutine); } } @@ -46,9 +38,9 @@ namespace Coroutine { for (var i = EventCoroutines.Count - 1; i >= 0; i--) { var coroutine = EventCoroutines[i]; if (coroutine.OnEvent(evt)) { - EventCoroutines.Remove(coroutine); + EventCoroutines.RemoveAt(i); } else if (coroutine.GetCurrentType() != WaitType.Event) { - EventCoroutines.Remove(coroutine); + EventCoroutines.RemoveAt(i); TickingCoroutines.Add(coroutine); } }