diff --git a/Source/Engine/Networking/NetworkMessage.cs b/Source/Engine/Networking/NetworkMessage.cs
index 42b0fb09e..6f530c18a 100644
--- a/Source/Engine/Networking/NetworkMessage.cs
+++ b/Source/Engine/Networking/NetworkMessage.cs
@@ -246,7 +246,7 @@ namespace FlaxEngine.Networking
}
///
- /// Writes data of type into the message. UTF-8 encoded.
+ /// Writes data of type into the message. UTF-16 encoded.
///
public void WriteString(string value)
{
@@ -254,23 +254,21 @@ namespace FlaxEngine.Networking
var data = Encoding.Unicode.GetBytes(value);
WriteUInt16((ushort)data.Length); // TODO: Use 1-byte length when possible
- WriteBytes(data, data.Length);
+ WriteBytes(data, data.Length * sizeof(ushort));
}
///
- /// Reads and returns data of type from the message. UTF-8 encoded.
+ /// Reads and returns data of type from the message. UTF-16 encoded.
///
public string ReadString()
{
// Note: Make sure that this is consistent with the C++ message API!
- var stringLength = ReadUInt16();
- var bytes = new byte[stringLength];
- fixed (byte* bytesPtr = bytes)
- {
- ReadBytes(bytesPtr, stringLength);
- }
- return Encoding.Unicode.GetString(bytes);
+ var stringLength = ReadUInt16(); // In chars
+ var stringSize = stringLength * sizeof(ushort); // In bytes
+ var bytes = stackalloc char[stringSize];
+ ReadBytes((byte*)bytes, stringSize);
+ return new string(bytes);
}
///
diff --git a/Source/Engine/Networking/NetworkMessage.h b/Source/Engine/Networking/NetworkMessage.h
index 3acdbfa17..5b2b1e7ba 100644
--- a/Source/Engine/Networking/NetworkMessage.h
+++ b/Source/Engine/Networking/NetworkMessage.h
@@ -171,18 +171,24 @@ public:
}
public:
- FORCE_INLINE void WriteQuaternion(const String& value)
+ ///
+ /// Writes data of type String into the message. UTF-16 encoded.
+ ///
+ FORCE_INLINE void WriteString(const String& value)
{
WriteUInt16(value.Length()); // TODO: Use 1-byte length when possible
- WriteBytes((uint8*)value.Get(), value.Length() * 2);
+ WriteBytes((uint8*)value.Get(), value.Length() * sizeof(wchar_t));
}
+ ///
+ /// Reads and returns data of type String from the message. UTF-16 encoded.
+ ///
FORCE_INLINE String ReadString()
{
uint16 length = ReadUInt16();
String value;
value.Resize(length);
- ReadBytes((uint8*)value.Get(), value.Length() * 2);
+ ReadBytes((uint8*)value.Get(), length * sizeof(wchar_t));
return value;
}