From dd9b78f8ffdecb60ac6abcd33d0ef22990108a95 Mon Sep 17 00:00:00 2001 From: Regalis Date: Thu, 21 Jul 2016 21:20:21 +0300 Subject: [PATCH] Handling exceptions thrown at any point during coroutine update, not just at Coroutine.MoveNext, dirty workaround for a (rare?) pathfinding bug I haven't been able to reproduce --- Subsurface/Source/Characters/AI/PathFinder.cs | 8 ++++ Subsurface/Source/CoroutineManager.cs | 48 +++++++++---------- Subsurface/Source/Map/LinkedSubmarine.cs | 11 ++--- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/Subsurface/Source/Characters/AI/PathFinder.cs b/Subsurface/Source/Characters/AI/PathFinder.cs index 10e29310e..984cd01fe 100644 --- a/Subsurface/Source/Characters/AI/PathFinder.cs +++ b/Subsurface/Source/Characters/AI/PathFinder.cs @@ -346,6 +346,14 @@ namespace Barotrauma { finalPath.Add(pathNode.Waypoint); + //there was one bug report that seems to have been caused by this loop never terminating: + //couldn't reproduce or figure out what caused it, but here's a workaround that prevents the game from crashing in case it happens again + if (finalPath.Count > nodes.Count) + { + DebugConsole.ThrowError("Pathfinding error: constructing final path failed"); + return new SteeringPath(true); + } + path.Cost += pathNode.F; pathNode = pathNode.Parent; } diff --git a/Subsurface/Source/CoroutineManager.cs b/Subsurface/Source/CoroutineManager.cs index a6b9929d5..4e749126c 100644 --- a/Subsurface/Source/CoroutineManager.cs +++ b/Subsurface/Source/CoroutineManager.cs @@ -65,42 +65,42 @@ namespace Barotrauma for (int i = Coroutines.Count-1; i>=0; i--) { - if (Coroutines[i].Coroutine.Current != null) + try { - WaitForSeconds wfs = Coroutines[i].Coroutine.Current as WaitForSeconds; - if (wfs != null) + if (Coroutines[i].Coroutine.Current != null) { - if (!wfs.CheckFinished(unscaledDeltaTime)) continue; - } - else - { - switch ((CoroutineStatus)Coroutines[i].Coroutine.Current) + WaitForSeconds wfs = Coroutines[i].Coroutine.Current as WaitForSeconds; + if (wfs != null) { - case CoroutineStatus.Success: - Coroutines.RemoveAt(i); - continue; - case CoroutineStatus.Failure: - DebugConsole.ThrowError("Coroutine ''" + Coroutines[i]+ "'' has failed"); - break; + if (!wfs.CheckFinished(unscaledDeltaTime)) continue; + } + else + { + switch ((CoroutineStatus)Coroutines[i].Coroutine.Current) + { + case CoroutineStatus.Success: + Coroutines.RemoveAt(i); + continue; + case CoroutineStatus.Failure: + DebugConsole.ThrowError("Coroutine ''" + Coroutines[i]+ "'' has failed"); + break; + } } } + + Coroutines[i].Coroutine.MoveNext(); } - //try - //{ - Coroutines[i].Coroutine.MoveNext(); - //} - - //catch (Exception e) - //{ - // DebugConsole.ThrowError("Coroutine " + Coroutines[i].Name + " threw an exception: " + e.Message); + catch (Exception e) + { + DebugConsole.ThrowError("Coroutine " + Coroutines[i].Name + " threw an exception: " + e.Message); //#if DEBUG // throw e; //#endif -// Coroutines.RemoveAt(i); -// } + Coroutines.RemoveAt(i); + } } } diff --git a/Subsurface/Source/Map/LinkedSubmarine.cs b/Subsurface/Source/Map/LinkedSubmarine.cs index c98dcab0a..feabb4e4b 100644 --- a/Subsurface/Source/Map/LinkedSubmarine.cs +++ b/Subsurface/Source/Map/LinkedSubmarine.cs @@ -318,18 +318,17 @@ namespace Barotrauma if (leaveBehind) { - saveElement.Add(new XAttribute("location", Level.Loaded.Seed)); - saveElement.Add(new XAttribute("worldpos", ToolBox.Vector2ToString(sub.SubBody.Position))); + saveElement.SetAttributeValue("location", Level.Loaded.Seed); + saveElement.SetAttributeValue("worldpos", ToolBox.Vector2ToString(sub.SubBody.Position)); } else { if (saveElement.Attribute("location") != null) saveElement.Attribute("location").Remove(); if (saveElement.Attribute("worldpos") != null) saveElement.Attribute("worldpos").Remove(); - } - - if (saveElement.Attribute("pos") != null) saveElement.Attribute("pos").Remove(); - saveElement.Add(new XAttribute("pos", ToolBox.Vector2ToString(Position - Submarine.HiddenSubPosition))); + } + + saveElement.SetAttributeValue("pos", ToolBox.Vector2ToString(Position - Submarine.HiddenSubPosition)); }