From 08661001d6524709b7ef2bb28235ffb153bce12f Mon Sep 17 00:00:00 2001 From: NorthFury Date: Wed, 3 Jan 2018 12:50:14 +0200 Subject: [PATCH] added null checks for PlayerCollection.sacks --- src/TQVaultAE.DAL/PlayerCollection.cs | 62 ++++++++++++++++----------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/src/TQVaultAE.DAL/PlayerCollection.cs b/src/TQVaultAE.DAL/PlayerCollection.cs index e818cc21..77f45cac 100644 --- a/src/TQVaultAE.DAL/PlayerCollection.cs +++ b/src/TQVaultAE.DAL/PlayerCollection.cs @@ -188,6 +188,10 @@ public int NumberOfSacks /// Each Sack in the sack array. public IEnumerator GetEnumerator() { + if (this.sacks == null) + { + yield break; + } foreach (SackCollection sack in this.sacks) { yield return sack; @@ -278,11 +282,12 @@ public byte[] Encode() /// Sack instace for the corresponding sack number public SackCollection GetSack(int sackNumber) { - if (sackNumber < this.sacks.Length){ - return this.sacks[sackNumber]; - }else{ + if (this.sacks == null || this.sacks.Length <= sackNumber) + { return null; } + + return this.sacks[sackNumber]; } /// @@ -294,7 +299,9 @@ public SackCollection GetSack(int sackNumber) public bool MoveSack(int source, int destination) { // Do a little bit of error handling - if (destination < 0 || destination > this.sacks.Length || source < 0 || source > this.sacks.Length || source == destination) + if (this.sacks == null || + destination < 0 || destination > this.sacks.Length || + source < 0 || source > this.sacks.Length || source == destination) { return false; } @@ -327,7 +334,9 @@ public bool MoveSack(int source, int destination) public bool CopySack(int source, int destination) { // Do a little bit of error handling - if (destination < 0 || destination > this.sacks.Length || source < 0 || source > this.sacks.Length || source == destination) + if (this.sacks == null || + destination < 0 || destination > this.sacks.Length || + source < 0 || source > this.sacks.Length || source == destination) { return false; } @@ -475,31 +484,34 @@ private void ParseRawData() outStream.WriteLine("Number of Sacks = {0}", this.numberOfSacks); int sackNumber = 0; - foreach (SackCollection sack in this.sacks) + if (this.sacks != null) { - if (!sack.IsEmpty) + foreach (SackCollection sack in this.sacks) { - outStream.WriteLine(); - outStream.WriteLine("SACK {0}", sackNumber); - - int itemNumber = 0; - foreach (Item item in sack) + if (!sack.IsEmpty) { - object[] params1 = new object[20]; - - params1[0] = itemNumber; - params1[1] = item.ToString(); - params1[2] = item.PositionX; - params1[3] = item.PositionY; - params1[4] = item.Seed; - ////params1[5] = - - outStream.WriteLine(" {0,5:n0} {1}", params1); - itemNumber++; + outStream.WriteLine(); + outStream.WriteLine("SACK {0}", sackNumber); + + int itemNumber = 0; + foreach (Item item in sack) + { + object[] params1 = new object[20]; + + params1[0] = itemNumber; + params1[1] = item.ToString(); + params1[2] = item.PositionX; + params1[3] = item.PositionY; + params1[4] = item.Seed; + ////params1[5] = + + outStream.WriteLine(" {0,5:n0} {1}", params1); + itemNumber++; + } } - } - sackNumber++; + sackNumber++; + } } } }