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

View File

@@ -56,55 +56,49 @@ namespace Barotrauma
{
Coroutines.RemoveAll(c => c == handle);
}
private static bool IsDone(CoroutineHandle handle)
{
try
{
if (handle.Coroutine.Current != null)
{
WaitForSeconds wfs = handle.Coroutine.Current as WaitForSeconds;
if (wfs != null)
{
if (!wfs.CheckFinished(UnscaledDeltaTime)) return false;
}
else
{
switch ((CoroutineStatus)handle.Coroutine.Current)
{
case CoroutineStatus.Success:
return true;
case CoroutineStatus.Failure:
DebugConsole.ThrowError("Coroutine ''" + handle.Name + "'' has failed");
return true;
}
}
}
handle.Coroutine.MoveNext();
return false;
}
catch (Exception e)
{
DebugConsole.ThrowError("Coroutine " + handle.Name + " threw an exception: " + e.Message);
return true;
}
}
// 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
{
if (handle.Coroutine.Current != null)
{
WaitForSeconds wfs = handle.Coroutine.Current as WaitForSeconds;
if (wfs != null)
{
if (!wfs.CheckFinished(unscaledDeltaTime)) continue;
}
else
{
switch ((CoroutineStatus)handle.Coroutine.Current)
{
case CoroutineStatus.Success:
Coroutines.RemoveAt(i);
continue;
case CoroutineStatus.Failure:
DebugConsole.ThrowError("Coroutine ''" + handle.Name + "'' has failed");
continue;
}
}
}
handle.Coroutine.MoveNext();
}
catch (Exception e)
{
DebugConsole.ThrowError("Coroutine " + handle.Name + " threw an exception: " + e.Message);
//#if DEBUG
// throw e;
//#endif
Coroutines.Remove(handle);
}
}
foreach (var x in Coroutines.ToList())
if(IsDone(x))
Coroutines.Remove(x);
}
}