Skip to content

Commit

Permalink
feat: adding extra string methods to use different encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Nov 2, 2022
1 parent 58012c0 commit 586717b
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions Assets/Mirage/Runtime/Serialization/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ public static class StringExtensions
/// <para>Can be changed by user if they need to</para>
/// </summary>
public static int MaxStringLength = 1300;
private static readonly UTF8Encoding encoding = new UTF8Encoding(false, true);

private static readonly UTF8Encoding defaultEncoding = new UTF8Encoding(false, true);
private static readonly byte[] stringBuffer = new byte[MaxStringLength];

/// <param name="value">string or null</param>
public static void WriteString(this NetworkWriter writer, string value)
public static void WriteString(this NetworkWriter writer, string value) => WriteString(writer, value, defaultEncoding);

/// <returns>string or null</returns>
/// <exception cref="ArgumentException">Throws if invalid utf8 string is received</exception>
public static string ReadString(this NetworkReader reader) => ReadString(reader, defaultEncoding);

/// <param name="encoding">Use this for encoding other than the default (UTF8). Make sure to use same encoding for ReadString</param>
/// <param name="value">string or null</param>
public static void WriteString(this NetworkWriter writer, string value, Encoding encoding)
{
// write 0 for null support, increment real size by 1
// (note: original HLAPI would write "" for null strings, but if a
Expand All @@ -42,10 +51,10 @@ public static void WriteString(this NetworkWriter writer, string value)
writer.WriteBytes(stringBuffer, 0, size);
}


/// <param name="encoding">Use this for encoding other than the default (UTF8). Make sure to use same encoding for WriterString</param>
/// <returns>string or null</returns>
/// <exception cref="ArgumentException">Throws if invalid utf8 string is received</exception>
public static string ReadString(this NetworkReader reader)
public static string ReadString(this NetworkReader reader, Encoding encoding)
{
// read number of bytes
var size = reader.ReadUInt16();
Expand Down

0 comments on commit 586717b

Please sign in to comment.