From 933cafd6208b379eed4837f6e395e911f37a93d7 Mon Sep 17 00:00:00 2001 From: T Boots <51194461+Gohan500@users.noreply.github.com> Date: Tue, 19 Dec 2023 20:58:18 +0100 Subject: [PATCH] Add overload Get methods that auto read array lengths (#127) * Change intArray to intoArray in multiple places * Add overload Get methods with only array no set amount, to Message * Undo Weird vscode changes --- .../RiptideNetworking/Message.cs | 80 +++++++++++++++---- 1 file changed, 66 insertions(+), 14 deletions(-) diff --git a/RiptideNetworking/RiptideNetworking/Message.cs b/RiptideNetworking/RiptideNetworking/Message.cs index 41330bb0..a33972c5 100644 --- a/RiptideNetworking/RiptideNetworking/Message.cs +++ b/RiptideNetworking/RiptideNetworking/Message.cs @@ -543,7 +543,7 @@ public Message AddVarULong(ulong value) AddByte(byteValue); } while (value != 0); - + return this; } @@ -684,7 +684,7 @@ public Message AddSBytes(sbyte[] array, bool includeLength = true) Converter.SByteToBits(array[i], data, writeBit); writeBit += BitsPerByte; } - + return this; } @@ -701,6 +701,10 @@ public byte[] GetBytes(int amount) return array; } /// Populates a array with bytes retrieved from the message. + /// The array to populate. + /// The position at which to start populating the array. + public void GetBytes(byte[] intoArray, int startIndex = 0) => GetBytes((int)GetVarULong(), intoArray, startIndex); + /// Populates a array with bytes retrieved from the message. /// The amount of bytes to retrieve. /// The array to populate. /// The position at which to start populating the array. @@ -725,15 +729,19 @@ public sbyte[] GetSBytes(int amount) return array; } /// Populates a array with bytes retrieved from the message. + /// The array to populate. + /// The position at which to start populating . + public void GetSBytes(sbyte[] intoArray, int startIndex = 0) => GetSBytes((int)GetVarULong(), intoArray, startIndex); + /// Populates a array with bytes retrieved from the message. /// The amount of sbytes to retrieve. - /// The array to populate. - /// The position at which to start populating . - public void GetSBytes(int amount, sbyte[] intArray, int startIndex = 0) + /// The array to populate. + /// The position at which to start populating . + public void GetSBytes(int amount, sbyte[] intoArray, int startIndex = 0) { - if (startIndex + amount > intArray.Length) - throw new ArgumentException(nameof(amount), ArrayNotLongEnoughError(amount, intArray.Length, startIndex, SByteName)); + if (startIndex + amount > intoArray.Length) + throw new ArgumentException(nameof(amount), ArrayNotLongEnoughError(amount, intoArray.Length, startIndex, SByteName)); - ReadSBytes(amount, intArray, startIndex); + ReadSBytes(amount, intoArray, startIndex); } /// Reads a number of bytes from the message and writes them into the given array. @@ -840,6 +848,10 @@ public bool[] GetBools(int amount) return array; } /// Populates a array with bools retrieved from the message. + /// The array to populate. + /// The position at which to start populating the array. + public void GetBools(bool[] intoArray, int startIndex = 0) => GetBools((int)GetVarULong(), intoArray, startIndex); + /// Populates a array with bools retrieved from the message. /// The amount of bools to retrieve. /// The array to populate. /// The position at which to start populating the array. @@ -862,7 +874,7 @@ private void ReadBools(int amount, bool[] intoArray, int startIndex = 0) RiptideLogger.Log(LogType.Error, NotEnoughBitsError(amount, BoolName)); amount = UnreadBits; } - + for (int i = 0; i < amount; i++) intoArray[startIndex + i] = Converter.BoolFromBit(data, readBit++); } @@ -924,7 +936,7 @@ public ushort GetUShort() readBit += sizeof(ushort) * BitsPerByte; return value; } - + /// Adds a array to the message. /// The array to add. /// Whether or not to include the length of the array in the message. @@ -980,6 +992,10 @@ public short[] GetShorts(int amount) return array; } /// Populates a array with shorts retrieved from the message. + /// The array to populate. + /// The position at which to start populating the array. + public void GetShorts(short[] intoArray, int startIndex = 0) => GetShorts((int)GetVarULong(), intoArray, startIndex); + /// Populates a array with shorts retrieved from the message. /// The amount of shorts to retrieve. /// The array to populate. /// The position at which to start populating the array. @@ -1004,6 +1020,10 @@ public ushort[] GetUShorts(int amount) return array; } /// Populates a array with ushorts retrieved from the message. + /// The array to populate. + /// The position at which to start populating the array. + public void GetUShorts(ushort[] intoArray, int startIndex = 0) => GetUShorts((int)GetVarULong(), intoArray, startIndex); + /// Populates a array with ushorts retrieved from the message. /// The amount of ushorts to retrieve. /// The array to populate. /// The position at which to start populating the array. @@ -1166,6 +1186,10 @@ public int[] GetInts(int amount) return array; } /// Populates an array with ints retrieved from the message. + /// The array to populate. + /// The position at which to start populating the array. + public void GetInts(int[] intoArray, int startIndex = 0) => GetInts((int)GetVarULong(), intoArray, startIndex); + /// Populates an array with ints retrieved from the message. /// The amount of ints to retrieve. /// The array to populate. /// The position at which to start populating the array. @@ -1190,6 +1214,10 @@ public uint[] GetUInts(int amount) return array; } /// Populates a array with uints retrieved from the message. + /// The array to populate. + /// The position at which to start populating the array. + public void GetUInts(uint[] intoArray, int startIndex = 0) => GetUInts((int)GetVarULong(), intoArray, startIndex); + /// Populates a array with uints retrieved from the message. /// The amount of uints to retrieve. /// The array to populate. /// The position at which to start populating the array. @@ -1352,6 +1380,10 @@ public long[] GetLongs(int amount) return array; } /// Populates a array with longs retrieved from the message. + /// The array to populate. + /// The position at which to start populating the array. + public void GetLongs(long[] intoArray, int startIndex = 0) => GetLongs((int)GetVarULong(), intoArray, startIndex); + /// Populates a array with longs retrieved from the message. /// The amount of longs to retrieve. /// The array to populate. /// The position at which to start populating the array. @@ -1376,6 +1408,10 @@ public ulong[] GetULongs(int amount) return array; } /// Populates a array with ulongs retrieved from the message. + /// The array to populate. + /// The position at which to start populating the array. + public void GetULongs(ulong[] intoArray, int startIndex = 0) => GetULongs((int)GetVarULong(), intoArray, startIndex); + /// Populates a array with ulongs retrieved from the message. /// The amount of ulongs to retrieve. /// The array to populate. /// The position at which to start populating the array. @@ -1489,6 +1525,10 @@ public float[] GetFloats(int amount) return array; } /// Populates a array with floats retrieved from the message. + /// The array to populate. + /// The position at which to start populating the array. + public void GetFloats(float[] intoArray, int startIndex = 0) => GetFloats((int)GetVarULong(), intoArray, startIndex); + /// Populates a array with floats retrieved from the message. /// The amount of floats to retrieve. /// The array to populate. /// The position at which to start populating the array. @@ -1583,6 +1623,10 @@ public double[] GetDoubles(int amount) return array; } /// Populates a array with doubles retrieved from the message. + /// The array to populate. + /// The position at which to start populating the array. + public void GetDoubles(double[] intoArray, int startIndex = 0) => GetDoubles((int)GetVarULong(), intoArray, startIndex); + /// Populates a array with doubles retrieved from the message. /// The amount of doubles to retrieve. /// The array to populate. /// The position at which to start populating the array. @@ -1674,6 +1718,10 @@ public string[] GetStrings(int amount) return array; } /// Populates a array with strings retrieved from the message. + /// The array to populate. + /// The position at which to start populating the array. + public void GetStrings(string[] intoArray, int startIndex = 0) => GetStrings((int)GetVarULong(), intoArray, startIndex); + /// Populates a array with strings retrieved from the message. /// The amount of strings to retrieve. /// The array to populate. /// The position at which to start populating the array. @@ -1734,6 +1782,10 @@ public Message AddSerializables(T[] array, bool includeLength = true) where T return array; } /// Populates an array of serializables retrieved from the message. + /// The array to populate. + /// The position at which to start populating the array. + public void GetSerializables(T[] intoArray, int startIndex = 0) where T : IMessageSerializable, new() => GetSerializables((int)GetVarULong(), intoArray, startIndex); + /// Populates an array of serializables retrieved from the message. /// The amount of serializables to retrieve. /// The array to populate. /// The position at which to start populating the array. @@ -1747,12 +1799,12 @@ public Message AddSerializables(T[] array, bool includeLength = true) where T /// Reads a number of serializables from the message and writes them into the given array. /// The amount of serializables to read. - /// The array to write the serializables into. - /// The position at which to start writing into . - private void ReadSerializables(int amount, T[] intArray, int startIndex = 0) where T : IMessageSerializable, new() + /// The array to write the serializables into. + /// The position at which to start writing into . + private void ReadSerializables(int amount, T[] intoArray, int startIndex = 0) where T : IMessageSerializable, new() { for (int i = 0; i < amount; i++) - intArray[startIndex + i] = GetSerializable(); + intoArray[startIndex + i] = GetSerializable(); } #endregion