Small rewrite of the Coroutine logic to stop it from going out of bounds and crashing.

This commit is contained in:
Sebastian Broberg
2016-08-30 22:51:40 +02:00
parent 9c116fc268
commit 8142cc734e
+16 -22
View File
@@ -56,17 +56,8 @@ namespace Barotrauma
{ {
Coroutines.RemoveAll(c => c == handle); Coroutines.RemoveAll(c => c == handle);
} }
private static bool IsDone(CoroutineHandle handle)
// Updating just means stepping through all the coroutines
public static void Update(float unscaledDeltaTime, float deltaTime)
{ {
UnscaledDeltaTime = unscaledDeltaTime;
DeltaTime = deltaTime;
for (int i = Coroutines.Count-1; i>=0; i--)
{
CoroutineHandle handle = Coroutines[i];
try try
{ {
if (handle.Coroutine.Current != null) if (handle.Coroutine.Current != null)
@@ -74,37 +65,40 @@ namespace Barotrauma
WaitForSeconds wfs = handle.Coroutine.Current as WaitForSeconds; WaitForSeconds wfs = handle.Coroutine.Current as WaitForSeconds;
if (wfs != null) if (wfs != null)
{ {
if (!wfs.CheckFinished(unscaledDeltaTime)) continue; if (!wfs.CheckFinished(UnscaledDeltaTime)) return false;
} }
else else
{ {
switch ((CoroutineStatus)handle.Coroutine.Current) switch ((CoroutineStatus)handle.Coroutine.Current)
{ {
case CoroutineStatus.Success: case CoroutineStatus.Success:
Coroutines.RemoveAt(i); return true;
continue;
case CoroutineStatus.Failure: case CoroutineStatus.Failure:
DebugConsole.ThrowError("Coroutine ''" + handle.Name + "'' has failed"); DebugConsole.ThrowError("Coroutine ''" + handle.Name + "'' has failed");
continue; return true;
} }
} }
} }
handle.Coroutine.MoveNext(); handle.Coroutine.MoveNext();
return false;
} }
catch (Exception e) catch (Exception e)
{ {
DebugConsole.ThrowError("Coroutine " + handle.Name + " threw an exception: " + e.Message); DebugConsole.ThrowError("Coroutine " + handle.Name + " threw an exception: " + e.Message);
return true;
//#if DEBUG
// throw e;
//#endif
Coroutines.Remove(handle);
} }
} }
// Updating just means stepping through all the coroutines
public static void Update(float unscaledDeltaTime, float deltaTime)
{
UnscaledDeltaTime = unscaledDeltaTime;
DeltaTime = deltaTime;
foreach (var x in Coroutines.ToList())
if(IsDone(x))
Coroutines.Remove(x);
} }
} }