From 6325e256f209cb6d64b0c533ceb3256bf996d415 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 21 Sep 2016 23:27:21 +1000 Subject: [PATCH] Core: Fix very rare ZS crash when a player had been added twice to the infected or alive list. --- MCGalaxy/Games/ZombieSurvival/ZombieGame.Core.cs | 2 +- MCGalaxy/util/VolatileArray.cs | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/MCGalaxy/Games/ZombieSurvival/ZombieGame.Core.cs b/MCGalaxy/Games/ZombieSurvival/ZombieGame.Core.cs index a9cfc2564..5d9126a00 100644 --- a/MCGalaxy/Games/ZombieSurvival/ZombieGame.Core.cs +++ b/MCGalaxy/Games/ZombieSurvival/ZombieGame.Core.cs @@ -192,7 +192,7 @@ namespace MCGalaxy.Games { aliveList = Alive.Items; foreach (Player alive in aliveList) { - if (alive == null || alive == dead) continue; + if (alive == dead) continue; UpdatePlayerColor(alive, alive.color); int dx = Math.Abs(alive.pos[0] - dead.pos[0]); int dy = Math.Abs(alive.pos[1] - dead.pos[1]); diff --git a/MCGalaxy/util/VolatileArray.cs b/MCGalaxy/util/VolatileArray.cs index 787917bc7..ef31d7ec9 100644 --- a/MCGalaxy/util/VolatileArray.cs +++ b/MCGalaxy/util/VolatileArray.cs @@ -59,14 +59,24 @@ namespace MCGalaxy { if (Items.Length == 0) return; T[] newItems = new T[Items.Length - 1]; - for (int i = 0, j = 0; i < Items.Length; i++) { + int j = 0; + for (int i = 0; i < Items.Length; i++) { if (object.ReferenceEquals(Items[i], value)) continue; // For some reason item wasn't in the list if (j == newItems.Length) return; newItems[j] = Items[i]; j++; } - Items = newItems; + + // Handle very rare case when an item has been added twice + if (newItems.Length != j) { + T[] temp = new T[j]; + for (int i = 0; i < temp.Length; i++) + temp[i] = newItems[i]; + Items = temp; + } else { + Items = newItems; + } } }