Skip to content

Commit

Permalink
bug fixes. coroutine changes from before were a mess
Browse files Browse the repository at this point in the history
  • Loading branch information
prime31 committed Feb 14, 2016
1 parent 04f20c5 commit bb88959
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
54 changes: 37 additions & 17 deletions Nez-PCL/Utils/Coroutines/CoroutineManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand All @@ -61,15 +68,19 @@ public ICoroutine startCoroutine( IEnumerator enumerator )
{
// find or create a CoroutineImpl
var coroutine = QuickCache<CoroutineImpl>.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<CoroutineImpl>.push( coroutine );
return null;
}

if( _isInUpdate )
_shouldRunNextFrame.Add( coroutine );
Expand All @@ -90,8 +101,7 @@ public void update()
// check for stopped coroutines
if( coroutine.isDone )
{
coroutine.isDone = true;
coroutine.enumerator = null;
coroutine.recycle();
QuickCache<CoroutineImpl>.push( coroutine );
continue;
}
Expand Down Expand Up @@ -119,7 +129,8 @@ public void update()
continue;
}

tickCoroutine( coroutine );
if( tickCoroutine( coroutine ) )
_shouldRunNextFrame.Add( coroutine );
}

_unblockedCoroutines.Clear();
Expand All @@ -130,38 +141,47 @@ public void update()
}


void tickCoroutine( CoroutineImpl coroutine )
/// <summary>
/// ticks a coroutine. returns true if the coroutine should continue to run next frame.
/// </summary>
/// <returns><c>true</c>, if coroutine was ticked, <c>false</c> otherwise.</returns>
/// <param name="coroutine">Coroutine.</param>
bool tickCoroutine( CoroutineImpl coroutine )
{
// This coroutine has finished
if( !coroutine.enumerator.MoveNext() )
{
coroutine.isDone = true;
coroutine.enumerator = null;
coroutine.recycle();
QuickCache<CoroutineImpl>.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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion Nez-PCL/Utils/NezContentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void loadAsync<T>( string assetName, Action<T> onLoaded = null )
/// <typeparam name="T">The 1st type parameter.</typeparam>
public void loadAsync<T>( string assetName, Action<object,T> onLoaded = null, object context = null )
{
var syncContext = context as SynchronizationContext;
var syncContext = SynchronizationContext.Current;
ThreadPool.QueueUserWorkItem( state =>
{
var asset = Load<T>( assetName );
Expand Down

0 comments on commit bb88959

Please sign in to comment.