From 38f92acf8e02acbff4688aaa4b35805afd86b984 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Wed, 21 Jun 2017 16:56:35 +0300 Subject: [PATCH] Bunch of fixes to null reference exceptions caused by removing characters mid-round --- Barotrauma/Source/Characters/AI/AITarget.cs | 7 ++++++- .../Source/Characters/Animation/Ragdoll.cs | 18 ++++++++++++------ Barotrauma/Source/Characters/Character.cs | 9 ++++++++- Barotrauma/Source/Networking/EntitySpawner.cs | 2 ++ 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Barotrauma/Source/Characters/AI/AITarget.cs b/Barotrauma/Source/Characters/AI/AITarget.cs index 1f5f47f69..f3038082f 100644 --- a/Barotrauma/Source/Characters/AI/AITarget.cs +++ b/Barotrauma/Source/Characters/AI/AITarget.cs @@ -11,7 +11,11 @@ namespace Barotrauma public static List List = new List(); - public readonly Entity Entity; + public Entity Entity + { + get; + private set; + } private float soundRange; private float sightRange; @@ -47,6 +51,7 @@ namespace Barotrauma public void Remove() { List.Remove(this); + Entity = null; } public void Draw(SpriteBatch spriteBatch) diff --git a/Barotrauma/Source/Characters/Animation/Ragdoll.cs b/Barotrauma/Source/Characters/Animation/Ragdoll.cs index b1d50472c..bde487a1e 100644 --- a/Barotrauma/Source/Characters/Animation/Ragdoll.cs +++ b/Barotrauma/Source/Characters/Animation/Ragdoll.cs @@ -1513,22 +1513,28 @@ namespace Barotrauma public void Remove() { - foreach (Limb l in Limbs) + if (Limbs != null) { - l.Remove(); + foreach (Limb l in Limbs) + { + l.Remove(); + } + Limbs = null; } - Limbs = null; foreach (PhysicsBody b in collider) { b.Remove(); } - foreach (RevoluteJoint joint in LimbJoints) + if (LimbJoints != null) { - GameMain.World.RemoveJoint(joint); + foreach (RevoluteJoint joint in LimbJoints) + { + GameMain.World.RemoveJoint(joint); + } + LimbJoints = null; } - LimbJoints = null; list.Remove(this); } diff --git a/Barotrauma/Source/Characters/Character.cs b/Barotrauma/Source/Characters/Character.cs index 3ce5c3c95..06449f436 100644 --- a/Barotrauma/Source/Characters/Character.cs +++ b/Barotrauma/Source/Characters/Character.cs @@ -1959,6 +1959,13 @@ namespace Barotrauma public override void Remove() { +#if DEBUG + if (Removed) + { + DebugConsole.ThrowError("Attempting to remove an already removed character\n" + Environment.StackTrace); + } +#endif + base.Remove(); if (info != null) info.Remove(); @@ -1969,7 +1976,7 @@ namespace Barotrauma if (GameMain.Client != null && GameMain.Client.Character == this) GameMain.Client.Character = null; - if (aiTarget != null) aiTarget.Remove(); + if (aiTarget != null) aiTarget.Remove(); if (AnimController != null) AnimController.Remove(); diff --git a/Barotrauma/Source/Networking/EntitySpawner.cs b/Barotrauma/Source/Networking/EntitySpawner.cs index 229fe6ab8..17a32b977 100644 --- a/Barotrauma/Source/Networking/EntitySpawner.cs +++ b/Barotrauma/Source/Networking/EntitySpawner.cs @@ -109,6 +109,7 @@ namespace Barotrauma public void AddToRemoveQueue(Entity entity) { if (GameMain.Client != null) return; + if (removeQueue.Contains(entity) || entity.Removed) return; removeQueue.Enqueue(entity); } @@ -116,6 +117,7 @@ namespace Barotrauma public void AddToRemoveQueue(Item item) { if (GameMain.Client != null) return; + if (removeQueue.Contains(item) || item.Removed) return; removeQueue.Enqueue(item); if (item.ContainedItems == null) return;