From affb43e7a542d0c4a08340f3b05759d8c8547fc8 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Wed, 26 Jul 2017 21:35:50 +0300 Subject: [PATCH] Workaround to null reference exceptions in Character.WriteStatus: it seems AnimController can be null when reading the message, which suggests that the character has been removed (e.g. turned to a husk, eaten by something). --- .../Source/Characters/Character.cs | 11 ++++++-- .../Source/Characters/CharacterNetworking.cs | 27 ++++++++++++------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/Barotrauma/BarotraumaShared/Source/Characters/Character.cs b/Barotrauma/BarotraumaShared/Source/Characters/Character.cs index 5e26c452e..bf550d137 100644 --- a/Barotrauma/BarotraumaShared/Source/Characters/Character.cs +++ b/Barotrauma/BarotraumaShared/Source/Characters/Character.cs @@ -21,11 +21,18 @@ namespace Barotrauma { get { - return enabled; + return enabled && !Removed; } set { if (value == enabled) return; + + if (Removed) + { + enabled = false; + return; + } + enabled = value; foreach (Limb limb in AnimController.Limbs) @@ -980,7 +987,7 @@ namespace Barotrauma public bool CanInteractWith(Character c, float maxDist = 200.0f) { - if (c == this || !c.enabled || c.info == null || !c.IsHumanoid || !c.CanBeSelected) return false; + if (c == this || !c.Enabled || c.info == null || !c.IsHumanoid || !c.CanBeSelected) return false; maxDist = ConvertUnits.ToSimUnits(maxDist); if (Vector2.DistanceSquared(SimPosition, c.SimPosition) > maxDist * maxDist) return false; diff --git a/Barotrauma/BarotraumaShared/Source/Characters/CharacterNetworking.cs b/Barotrauma/BarotraumaShared/Source/Characters/CharacterNetworking.cs index dfef47bf7..5f83c52f5 100644 --- a/Barotrauma/BarotraumaShared/Source/Characters/CharacterNetworking.cs +++ b/Barotrauma/BarotraumaShared/Source/Characters/CharacterNetworking.cs @@ -474,18 +474,27 @@ namespace Barotrauma if (isDead) { msg.Write((byte)causeOfDeath); - List severedJointIndices = new List(); - for (int i = 0; i < AnimController.LimbJoints.Length; i++) + + if (AnimController?.LimbJoints == null) { - if (AnimController.LimbJoints[i] != null && AnimController.LimbJoints[i].IsSevered) - { - severedJointIndices.Add(i); - } + //0 limbs severed + msg.Write((byte)0); } - msg.Write((byte)severedJointIndices.Count); - foreach (int jointIndex in severedJointIndices) + else { - msg.Write((byte)jointIndex); + List severedJointIndices = new List(); + for (int i = 0; i < AnimController.LimbJoints.Length; i++) + { + if (AnimController.LimbJoints[i] != null && AnimController.LimbJoints[i].IsSevered) + { + severedJointIndices.Add(i); + } + } + msg.Write((byte)severedJointIndices.Count); + foreach (int jointIndex in severedJointIndices) + { + msg.Write((byte)jointIndex); + } } } else