From bb889595b777cec2d819c234142f15700f076893 Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 14 Feb 2016 01:59:58 -0800 Subject: [PATCH] bug fixes. coroutine changes from before were a mess --- Nez-PCL/Utils/Coroutines/CoroutineManager.cs | 54 ++++++++++++++------ Nez-PCL/Utils/NezContentManager.cs | 2 +- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/Nez-PCL/Utils/Coroutines/CoroutineManager.cs b/Nez-PCL/Utils/Coroutines/CoroutineManager.cs index 3e8d67cb5..8abbc9565 100644 --- a/Nez-PCL/Utils/Coroutines/CoroutineManager.cs +++ b/Nez-PCL/Utils/Coroutines/CoroutineManager.cs @@ -34,11 +34,18 @@ public void stop() } - internal void reset() + internal void prepareForReuse() { - waitTimer = 0; isDone = false; + } + + + internal void recycle() + { + isDone = true; + waitTimer = 0; waitForCoroutine = null; + enumerator = null; } } @@ -61,15 +68,19 @@ public ICoroutine startCoroutine( IEnumerator enumerator ) { // find or create a CoroutineImpl var coroutine = QuickCache.pop(); + coroutine.prepareForReuse(); // setup the coroutine and add it coroutine.enumerator = enumerator; - coroutine.reset(); - tickCoroutine( coroutine ); + var shouldContinueCoroutine = tickCoroutine( coroutine ); // guard against empty coroutines - if( coroutine.isDone ) + if( !shouldContinueCoroutine || coroutine.isDone ) + { + coroutine.recycle(); + QuickCache.push( coroutine ); return null; + } if( _isInUpdate ) _shouldRunNextFrame.Add( coroutine ); @@ -90,8 +101,7 @@ public void update() // check for stopped coroutines if( coroutine.isDone ) { - coroutine.isDone = true; - coroutine.enumerator = null; + coroutine.recycle(); QuickCache.push( coroutine ); continue; } @@ -119,7 +129,8 @@ public void update() continue; } - tickCoroutine( coroutine ); + if( tickCoroutine( coroutine ) ) + _shouldRunNextFrame.Add( coroutine ); } _unblockedCoroutines.Clear(); @@ -130,38 +141,47 @@ public void update() } - void tickCoroutine( CoroutineImpl coroutine ) + /// + /// ticks a coroutine. returns true if the coroutine should continue to run next frame. + /// + /// true, if coroutine was ticked, false otherwise. + /// Coroutine. + bool tickCoroutine( CoroutineImpl coroutine ) { // This coroutine has finished if( !coroutine.enumerator.MoveNext() ) { - coroutine.isDone = true; - coroutine.enumerator = null; + coroutine.recycle(); QuickCache.push( coroutine ); - return; + return false; } - if( coroutine.enumerator.Current is int ) + if( coroutine.enumerator.Current == null ) + { + // yielded null. run again next frame + return true; + } + else if( coroutine.enumerator.Current is int ) { var wait = (int)coroutine.enumerator.Current; coroutine.waitTimer = wait; - _shouldRunNextFrame.Add( coroutine ); + return true; } else if( coroutine.enumerator.Current is float ) { var wait = (float)coroutine.enumerator.Current; coroutine.waitTimer = wait; - _shouldRunNextFrame.Add( coroutine ); + return true; } else if( coroutine.enumerator.Current is CoroutineImpl ) { coroutine.waitForCoroutine = coroutine.enumerator.Current as CoroutineImpl; - _shouldRunNextFrame.Add( coroutine ); + return true; } else { // This coroutine yielded null, or some other value we don't understand. run it next frame. - _shouldRunNextFrame.Add( coroutine ); + return true; } } diff --git a/Nez-PCL/Utils/NezContentManager.cs b/Nez-PCL/Utils/NezContentManager.cs index 6fd9fbb13..73df9b0b7 100644 --- a/Nez-PCL/Utils/NezContentManager.cs +++ b/Nez-PCL/Utils/NezContentManager.cs @@ -156,7 +156,7 @@ public void loadAsync( string assetName, Action onLoaded = null ) /// The 1st type parameter. public void loadAsync( string assetName, Action onLoaded = null, object context = null ) { - var syncContext = context as SynchronizationContext; + var syncContext = SynchronizationContext.Current; ThreadPool.QueueUserWorkItem( state => { var asset = Load( assetName );