Skip to content

Commit

Permalink
Add overload Get methods that auto read array lengths (#127)
Browse files Browse the repository at this point in the history
* Change intArray to intoArray in multiple places

* Add overload Get methods with only array no set amount, to Message

* Undo Weird vscode changes
  • Loading branch information
Gohan500 authored Dec 19, 2023
1 parent bdd09c3 commit 933cafd
Showing 1 changed file with 66 additions and 14 deletions.
80 changes: 66 additions & 14 deletions RiptideNetworking/RiptideNetworking/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ public Message AddVarULong(ulong value)
AddByte(byteValue);
}
while (value != 0);

return this;
}

Expand Down Expand Up @@ -684,7 +684,7 @@ public Message AddSBytes(sbyte[] array, bool includeLength = true)
Converter.SByteToBits(array[i], data, writeBit);
writeBit += BitsPerByte;
}

return this;
}

Expand All @@ -701,6 +701,10 @@ public byte[] GetBytes(int amount)
return array;
}
/// <summary>Populates a <see cref="byte"/> array with bytes retrieved from the message.</summary>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
public void GetBytes(byte[] intoArray, int startIndex = 0) => GetBytes((int)GetVarULong(), intoArray, startIndex);
/// <summary>Populates a <see cref="byte"/> array with bytes retrieved from the message.</summary>
/// <param name="amount">The amount of bytes to retrieve.</param>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
Expand All @@ -725,15 +729,19 @@ public sbyte[] GetSBytes(int amount)
return array;
}
/// <summary>Populates a <see cref="sbyte"/> array with bytes retrieved from the message.</summary>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating <paramref name="intoArray"/>.</param>
public void GetSBytes(sbyte[] intoArray, int startIndex = 0) => GetSBytes((int)GetVarULong(), intoArray, startIndex);
/// <summary>Populates a <see cref="sbyte"/> array with bytes retrieved from the message.</summary>
/// <param name="amount">The amount of sbytes to retrieve.</param>
/// <param name="intArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating <paramref name="intArray"/>.</param>
public void GetSBytes(int amount, sbyte[] intArray, int startIndex = 0)
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating <paramref name="intoArray"/>.</param>
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);
}

/// <summary>Reads a number of bytes from the message and writes them into the given array.</summary>
Expand Down Expand Up @@ -840,6 +848,10 @@ public bool[] GetBools(int amount)
return array;
}
/// <summary>Populates a <see cref="bool"/> array with bools retrieved from the message.</summary>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
public void GetBools(bool[] intoArray, int startIndex = 0) => GetBools((int)GetVarULong(), intoArray, startIndex);
/// <summary>Populates a <see cref="bool"/> array with bools retrieved from the message.</summary>
/// <param name="amount">The amount of bools to retrieve.</param>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
Expand All @@ -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++);
}
Expand Down Expand Up @@ -924,7 +936,7 @@ public ushort GetUShort()
readBit += sizeof(ushort) * BitsPerByte;
return value;
}

/// <summary>Adds a <see cref="short"/> array to the message.</summary>
/// <param name="array">The array to add.</param>
/// <param name="includeLength">Whether or not to include the length of the array in the message.</param>
Expand Down Expand Up @@ -980,6 +992,10 @@ public short[] GetShorts(int amount)
return array;
}
/// <summary>Populates a <see cref="short"/> array with shorts retrieved from the message.</summary>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
public void GetShorts(short[] intoArray, int startIndex = 0) => GetShorts((int)GetVarULong(), intoArray, startIndex);
/// <summary>Populates a <see cref="short"/> array with shorts retrieved from the message.</summary>
/// <param name="amount">The amount of shorts to retrieve.</param>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
Expand All @@ -1004,6 +1020,10 @@ public ushort[] GetUShorts(int amount)
return array;
}
/// <summary>Populates a <see cref="ushort"/> array with ushorts retrieved from the message.</summary>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
public void GetUShorts(ushort[] intoArray, int startIndex = 0) => GetUShorts((int)GetVarULong(), intoArray, startIndex);
/// <summary>Populates a <see cref="ushort"/> array with ushorts retrieved from the message.</summary>
/// <param name="amount">The amount of ushorts to retrieve.</param>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
Expand Down Expand Up @@ -1166,6 +1186,10 @@ public int[] GetInts(int amount)
return array;
}
/// <summary>Populates an <see cref="int"/> array with ints retrieved from the message.</summary>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
public void GetInts(int[] intoArray, int startIndex = 0) => GetInts((int)GetVarULong(), intoArray, startIndex);
/// <summary>Populates an <see cref="int"/> array with ints retrieved from the message.</summary>
/// <param name="amount">The amount of ints to retrieve.</param>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
Expand All @@ -1190,6 +1214,10 @@ public uint[] GetUInts(int amount)
return array;
}
/// <summary>Populates a <see cref="uint"/> array with uints retrieved from the message.</summary>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
public void GetUInts(uint[] intoArray, int startIndex = 0) => GetUInts((int)GetVarULong(), intoArray, startIndex);
/// <summary>Populates a <see cref="uint"/> array with uints retrieved from the message.</summary>
/// <param name="amount">The amount of uints to retrieve.</param>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
Expand Down Expand Up @@ -1352,6 +1380,10 @@ public long[] GetLongs(int amount)
return array;
}
/// <summary>Populates a <see cref="long"/> array with longs retrieved from the message.</summary>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
public void GetLongs(long[] intoArray, int startIndex = 0) => GetLongs((int)GetVarULong(), intoArray, startIndex);
/// <summary>Populates a <see cref="long"/> array with longs retrieved from the message.</summary>
/// <param name="amount">The amount of longs to retrieve.</param>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
Expand All @@ -1376,6 +1408,10 @@ public ulong[] GetULongs(int amount)
return array;
}
/// <summary>Populates a <see cref="ulong"/> array with ulongs retrieved from the message.</summary>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
public void GetULongs(ulong[] intoArray, int startIndex = 0) => GetULongs((int)GetVarULong(), intoArray, startIndex);
/// <summary>Populates a <see cref="ulong"/> array with ulongs retrieved from the message.</summary>
/// <param name="amount">The amount of ulongs to retrieve.</param>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
Expand Down Expand Up @@ -1489,6 +1525,10 @@ public float[] GetFloats(int amount)
return array;
}
/// <summary>Populates a <see cref="float"/> array with floats retrieved from the message.</summary>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
public void GetFloats(float[] intoArray, int startIndex = 0) => GetFloats((int)GetVarULong(), intoArray, startIndex);
/// <summary>Populates a <see cref="float"/> array with floats retrieved from the message.</summary>
/// <param name="amount">The amount of floats to retrieve.</param>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
Expand Down Expand Up @@ -1583,6 +1623,10 @@ public double[] GetDoubles(int amount)
return array;
}
/// <summary>Populates a <see cref="double"/> array with doubles retrieved from the message.</summary>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
public void GetDoubles(double[] intoArray, int startIndex = 0) => GetDoubles((int)GetVarULong(), intoArray, startIndex);
/// <summary>Populates a <see cref="double"/> array with doubles retrieved from the message.</summary>
/// <param name="amount">The amount of doubles to retrieve.</param>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
Expand Down Expand Up @@ -1674,6 +1718,10 @@ public string[] GetStrings(int amount)
return array;
}
/// <summary>Populates a <see cref="string"/> array with strings retrieved from the message.</summary>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
public void GetStrings(string[] intoArray, int startIndex = 0) => GetStrings((int)GetVarULong(), intoArray, startIndex);
/// <summary>Populates a <see cref="string"/> array with strings retrieved from the message.</summary>
/// <param name="amount">The amount of strings to retrieve.</param>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
Expand Down Expand Up @@ -1734,6 +1782,10 @@ public Message AddSerializables<T>(T[] array, bool includeLength = true) where T
return array;
}
/// <summary>Populates an array of serializables retrieved from the message.</summary>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
public void GetSerializables<T>(T[] intoArray, int startIndex = 0) where T : IMessageSerializable, new() => GetSerializables<T>((int)GetVarULong(), intoArray, startIndex);
/// <summary>Populates an array of serializables retrieved from the message.</summary>
/// <param name="amount">The amount of serializables to retrieve.</param>
/// <param name="intoArray">The array to populate.</param>
/// <param name="startIndex">The position at which to start populating the array.</param>
Expand All @@ -1747,12 +1799,12 @@ public Message AddSerializables<T>(T[] array, bool includeLength = true) where T

/// <summary>Reads a number of serializables from the message and writes them into the given array.</summary>
/// <param name="amount">The amount of serializables to read.</param>
/// <param name="intArray">The array to write the serializables into.</param>
/// <param name="startIndex">The position at which to start writing into <paramref name="intArray"/>.</param>
private void ReadSerializables<T>(int amount, T[] intArray, int startIndex = 0) where T : IMessageSerializable, new()
/// <param name="intoArray">The array to write the serializables into.</param>
/// <param name="startIndex">The position at which to start writing into <paramref name="intoArray"/>.</param>
private void ReadSerializables<T>(int amount, T[] intoArray, int startIndex = 0) where T : IMessageSerializable, new()
{
for (int i = 0; i < amount; i++)
intArray[startIndex + i] = GetSerializable<T>();
intoArray[startIndex + i] = GetSerializable<T>();
}
#endregion

Expand Down

0 comments on commit 933cafd

Please sign in to comment.