public static void WriteString(this NetworkWriter writer, string value) { // write 0 for null support, increment real size by 1 // (note: original HLAPI would write "" for null strings, but if a // string is null on the server then it should also be null // on the client) if (value == null) { writer.WriteUInt16((ushort)0); return; } // write string with same method as NetworkReader // convert to byte[] int size = encoding.GetBytes(value, 0, value.Length, stringBuffer, 0); // check if within max size if (size >= NetworkWriter.MaxStringLength) { throw new IndexOutOfRangeException("NetworkWriter.Write(string) too long: " + size + ". Limit: " + NetworkWriter.MaxStringLength); } // write size and bytes writer.WriteUInt16(checked ((ushort)(size + 1))); writer.WriteBytes(stringBuffer, 0, size); }
[EditorBrowsable(EditorBrowsableState.Never)] // public only for tests public static void SerializeIntoWriter(NetworkWriter writer, Vector3 position, Quaternion rotation, Compression compressRotation, Vector3 scale) { // serialize position writer.WriteVector3(position); // serialize rotation // writing quaternion = 16 byte // writing euler angles = 12 byte // -> quaternion->euler->quaternion always works. // -> gimbal lock only occurs when adding. Vector3 euler = rotation.eulerAngles; if (compressRotation == Compression.None) { // write 3 floats = 12 byte writer.WriteSingle(euler.x); writer.WriteSingle(euler.y); writer.WriteSingle(euler.z); } else if (compressRotation == Compression.Much) { // write 3 byte. scaling [0,360] to [0,255] writer.WriteByte(FloatBytePacker.ScaleFloatToByte(euler.x, 0, 360, byte.MinValue, byte.MaxValue)); writer.WriteByte(FloatBytePacker.ScaleFloatToByte(euler.y, 0, 360, byte.MinValue, byte.MaxValue)); writer.WriteByte(FloatBytePacker.ScaleFloatToByte(euler.z, 0, 360, byte.MinValue, byte.MaxValue)); } else if (compressRotation == Compression.Lots) { // write 2 byte, 5 bits for each float writer.WriteUInt16(FloatBytePacker.PackThreeFloatsIntoUShort(euler.x, euler.y, euler.z, 0, 360)); } // serialize scale writer.WriteVector3(scale); }
/* Wappen compatibility layer //////////////////////////////////*/ public static void PackX <T>(T message, NetworkWriter writer) where T : IMessageBase { int msgType = GetId <T>( ); writer.WriteUInt16((ushort)msgType); // serialize message into writer message.Serialize(writer); }
// pack message before sending // -> NetworkWriter passed as arg so that we can use .ToArraySegment // and do an allocation free send before recycling it. public static void Pack <T>(T message, NetworkWriter writer) where T : struct, NetworkMessage { int msgType = GetId <T>(); writer.WriteUInt16((ushort)msgType); // serialize message into writer writer.Write(message); }
// pack message before sending // -> NetworkWriter passed as arg so that we can use .ToArraySegment // and do an allocation free send before recycling it. public static void Pack <T>(T message, NetworkWriter writer) where T : IMessageBase { // write message type int msgType = GetId(typeof(T).IsValueType ? typeof(T) : message.GetType()); writer.WriteUInt16((ushort)msgType); // serialize message into writer message.Serialize(writer); }
// pack message before sending // -> NetworkWriter passed as arg so that we can use .ToArraySegment // and do an allocation free send before recycling it. public static void Pack <T>(T message, NetworkWriter writer) where T : IMessageBase { // if it is a value type, just use typeof(T) to avoid boxing // this works because value types cannot be derived // if it is a reference type (for example IMessageBase), // ask the message for the real type int msgType = GetId(default(T) != null ? typeof(T) : message.GetType()); writer.WriteUInt16((ushort)msgType); // serialize message into writer message.Serialize(writer); }
// pack message before sending // -> NetworkWriter passed as arg so that we can use .ToArraySegment // and do an allocation free send before recycling it. public static void Pack <T>(T message, NetworkWriter writer) { // if it is a value type, just use typeof(T) to avoid boxing // this works because value types cannot be derived // if it is a reference type (for example IMessageBase), // ask the message for the real type Type mstType = default(T) == null && message != null?message.GetType() : typeof(T); int msgType = GetId(mstType); writer.WriteUInt16((ushort)msgType); writer.Write(message); }
// pack message before sending public static byte[] Pack <T>(T message) where T : IMessageBase { NetworkWriter writer = NetworkWriterPool.GetWriter(); try { // write message type int msgType = GetId <T>(); writer.WriteUInt16((ushort)msgType); // serialize message into writer message.Serialize(writer); // return byte[] return(writer.ToArray()); } finally { NetworkWriterPool.Recycle(writer); } }
public static void WriteInt16(this NetworkWriter writer, short value) => writer.WriteUInt16((ushort)value);
public static void WriteChar(this NetworkWriter writer, char value) => writer.WriteUInt16((ushort)value);