diff --git a/Assets/Mirror/Components/NetworkAnimator.cs b/Assets/Mirror/Components/NetworkAnimator.cs index 54e682385..c0b5080f9 100644 --- a/Assets/Mirror/Components/NetworkAnimator.cs +++ b/Assets/Mirror/Components/NetworkAnimator.cs @@ -234,12 +234,12 @@ bool WriteParameters(NetworkWriter writer) else if (par.type == AnimatorControllerParameterType.Float) { float newFloatValue = animator.GetFloat(par.nameHash); - writer.Write(newFloatValue); + writer.WriteSingle(newFloatValue); } else if (par.type == AnimatorControllerParameterType.Bool) { bool newBoolValue = animator.GetBool(par.nameHash); - writer.Write(newBoolValue); + writer.WriteBoolean(newBoolValue); } } return dirtyBits != 0; @@ -279,14 +279,14 @@ public override bool OnSerialize(NetworkWriter writer, bool forceAll) if (animator.IsInTransition(0)) { AnimatorStateInfo st = animator.GetNextAnimatorStateInfo(0); - writer.Write(st.fullPathHash); - writer.Write(st.normalizedTime); + writer.WriteInt32(st.fullPathHash); + writer.WriteSingle(st.normalizedTime); } else { AnimatorStateInfo st = animator.GetCurrentAnimatorStateInfo(0); - writer.Write(st.fullPathHash); - writer.Write(st.normalizedTime); + writer.WriteInt32(st.fullPathHash); + writer.WriteSingle(st.normalizedTime); } WriteParameters(writer); return true; diff --git a/Assets/Mirror/Components/NetworkTransformBase.cs b/Assets/Mirror/Components/NetworkTransformBase.cs index b18857a3a..4eaae3af5 100644 --- a/Assets/Mirror/Components/NetworkTransformBase.cs +++ b/Assets/Mirror/Components/NetworkTransformBase.cs @@ -59,7 +59,7 @@ public class DataPoint static void SerializeIntoWriter(NetworkWriter writer, Vector3 position, Quaternion rotation, Compression compressRotation) { // serialize position - writer.Write(position); + writer.WriteVector3(position); // serialize rotation // writing quaternion = 16 byte @@ -70,21 +70,21 @@ static void SerializeIntoWriter(NetworkWriter writer, Vector3 position, Quaterni if (compressRotation == Compression.None) { // write 3 floats = 12 byte - writer.Write(euler.x); - writer.Write(euler.y); - writer.Write(euler.z); + 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.Write(FloatBytePacker.ScaleFloatToByte(euler.x, 0, 360, byte.MinValue, byte.MaxValue)); - writer.Write(FloatBytePacker.ScaleFloatToByte(euler.y, 0, 360, byte.MinValue, byte.MaxValue)); - writer.Write(FloatBytePacker.ScaleFloatToByte(euler.z, 0, 360, byte.MinValue, byte.MaxValue)); + 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.Write(FloatBytePacker.PackThreeFloatsIntoUShort(euler.x, euler.y, euler.z, 0, 360)); + writer.WriteUInt16(FloatBytePacker.PackThreeFloatsIntoUShort(euler.x, euler.y, euler.z, 0, 360)); } } diff --git a/Assets/Mirror/Editor/PreprocessorDefine.cs b/Assets/Mirror/Editor/PreprocessorDefine.cs index 440f282c9..dd551785a 100644 --- a/Assets/Mirror/Editor/PreprocessorDefine.cs +++ b/Assets/Mirror/Editor/PreprocessorDefine.cs @@ -15,7 +15,8 @@ static void AddDefineSymbols() { "MIRROR", "MIRROR_1726_OR_NEWER", - "MIRROR_3_0_OR_NEWER" + "MIRROR_3_0_OR_NEWER", + "MIRROR_3_12_OR_NEWER" }; PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, string.Join(";", defines)); } diff --git a/Assets/Mirror/Editor/Weaver/Writers.cs b/Assets/Mirror/Editor/Weaver/Writers.cs index 0e8a05020..13cd3981d 100644 --- a/Assets/Mirror/Editor/Weaver/Writers.cs +++ b/Assets/Mirror/Editor/Weaver/Writers.cs @@ -17,36 +17,36 @@ public static void Init(AssemblyDefinition CurrentAssembly) writeFuncs = new Dictionary { - { Weaver.singleType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.singleType) }, - { Weaver.doubleType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.doubleType) }, - { Weaver.boolType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.boolType) }, - { Weaver.stringType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.stringType) }, + { Weaver.singleType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteSingle") }, + { Weaver.doubleType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteDouble") }, + { Weaver.boolType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteBoolean") }, + { Weaver.stringType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteString") }, { Weaver.int64Type.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WritePackedInt64") }, { Weaver.uint64Type.FullName, Weaver.NetworkWriterWritePackedUInt64 }, { Weaver.int32Type.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WritePackedInt32") }, { Weaver.uint32Type.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WritePackedUInt32") }, - { Weaver.int16Type.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.int16Type) }, - { Weaver.uint16Type.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.uint16Type) }, - { Weaver.byteType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.byteType) }, - { Weaver.sbyteType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.sbyteType) }, - { Weaver.charType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.charType) }, - { Weaver.decimalType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.decimalType) }, - { Weaver.vector2Type.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.vector2Type) }, - { Weaver.vector3Type.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.vector3Type) }, - { Weaver.vector4Type.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.vector4Type) }, - { Weaver.vector2IntType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.vector2IntType) }, - { Weaver.vector3IntType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.vector3IntType) }, - { Weaver.colorType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.colorType) }, - { Weaver.color32Type.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.color32Type) }, - { Weaver.quaternionType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.quaternionType) }, - { Weaver.rectType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.rectType) }, - { Weaver.planeType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.planeType) }, - { Weaver.rayType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.rayType) }, - { Weaver.matrixType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.matrixType) }, - { Weaver.guidType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.guidType) }, - { Weaver.gameObjectType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.gameObjectType) }, - { Weaver.NetworkIdentityType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.NetworkIdentityType) }, - { Weaver.transformType.FullName, Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "Write", Weaver.transformType) }, + { Weaver.int16Type.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteInt16") }, + { Weaver.uint16Type.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteUInt16") }, + { Weaver.byteType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteByte") }, + { Weaver.sbyteType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteSByte") }, + { Weaver.charType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteChar") }, + { Weaver.decimalType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteDecimal") }, + { Weaver.vector2Type.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteVector2") }, + { Weaver.vector3Type.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteVector3") }, + { Weaver.vector4Type.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteVector4") }, + { Weaver.vector2IntType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteVector2Int") }, + { Weaver.vector3IntType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteVector3Int") }, + { Weaver.colorType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteColor") }, + { Weaver.color32Type.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteColor32") }, + { Weaver.quaternionType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteQuaternion") }, + { Weaver.rectType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteRect") }, + { Weaver.planeType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WritePlane") }, + { Weaver.rayType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteRay") }, + { Weaver.matrixType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteMatrix4x4") }, + { Weaver.guidType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteGuid") }, + { Weaver.gameObjectType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteGameObject") }, + { Weaver.NetworkIdentityType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteNetworkIdentity") }, + { Weaver.transformType.FullName, Resolvers.ResolveMethod(networkWriterType, CurrentAssembly, "WriteTransform") }, { "System.Byte[]", Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "WriteBytesAndSize", "System.Byte[]") }, { "System.ArraySegment`1", Resolvers.ResolveMethodWithArg(networkWriterType, CurrentAssembly, "WriteBytesAndSizeSegment", "System.ArraySegment`1") } }; diff --git a/Assets/Mirror/Runtime/MessagePacker.cs b/Assets/Mirror/Runtime/MessagePacker.cs index b4e4bacf0..1cf93db9f 100644 --- a/Assets/Mirror/Runtime/MessagePacker.cs +++ b/Assets/Mirror/Runtime/MessagePacker.cs @@ -34,7 +34,7 @@ public static byte[] PackMessage(int msgType, MessageBase msg) try { // write message type - writer.Write((short)msgType); + writer.WriteInt16((short)msgType); // serialize message into writer msg.Serialize(writer); @@ -56,7 +56,7 @@ public static byte[] Pack(T message) where T : IMessageBase { // write message type int msgType = GetId(); - writer.Write((ushort)msgType); + writer.WriteUInt16((ushort)msgType); // serialize message into writer message.Serialize(writer); diff --git a/Assets/Mirror/Runtime/Messages.cs b/Assets/Mirror/Runtime/Messages.cs index df25be919..1bb91c9ac 100644 --- a/Assets/Mirror/Runtime/Messages.cs +++ b/Assets/Mirror/Runtime/Messages.cs @@ -39,7 +39,7 @@ public override void Deserialize(NetworkReader reader) public override void Serialize(NetworkWriter writer) { - writer.Write(value); + writer.WriteString(value); } } @@ -61,7 +61,7 @@ public override void Deserialize(NetworkReader reader) public override void Serialize(NetworkWriter writer) { - writer.Write(value); + writer.WriteByte(value); } } @@ -127,7 +127,7 @@ public override void Deserialize(NetworkReader reader) public override void Serialize(NetworkWriter writer) { - writer.Write(value); + writer.WriteDouble(value); } } @@ -169,9 +169,9 @@ public override void Deserialize(NetworkReader reader) public override void Serialize(NetworkWriter writer) { - writer.Write(sceneName); - writer.Write((byte)sceneMode); - writer.Write((byte)physicsMode); + writer.WriteString(sceneName); + writer.WriteByte((byte)sceneMode); + writer.WriteByte((byte)physicsMode); } } #endregion @@ -199,7 +199,7 @@ public override void Serialize(NetworkWriter writer) { writer.WritePackedUInt32(netId); writer.WritePackedUInt32((uint)componentIndex); - writer.Write(functionHash); + writer.WriteInt32(functionHash); writer.WriteBytesAndSizeSegment(payload); } } @@ -238,11 +238,11 @@ public override void Deserialize(NetworkReader reader) public override void Serialize(NetworkWriter writer) { writer.WritePackedUInt32(netId); - writer.Write(owner); - writer.Write(assetId); - writer.Write(position); - writer.Write(rotation); - writer.Write(scale); + writer.WriteBoolean(owner); + writer.WriteGuid(assetId); + writer.WriteVector3(position); + writer.WriteQuaternion(rotation); + writer.WriteVector3(scale); writer.WriteBytesAndSizeSegment(payload); } } @@ -273,11 +273,11 @@ public override void Deserialize(NetworkReader reader) public override void Serialize(NetworkWriter writer) { writer.WritePackedUInt32(netId); - writer.Write(owner); - writer.Write(sceneId); - writer.Write(position); - writer.Write(rotation); - writer.Write(scale); + writer.WriteBoolean(owner); + writer.WriteUInt64(sceneId); + writer.WriteVector3(position); + writer.WriteQuaternion(rotation); + writer.WriteVector3(scale); writer.WriteBytesAndSizeSegment(payload); } } @@ -330,7 +330,7 @@ public override void Deserialize(NetworkReader reader) public override void Serialize(NetworkWriter writer) { writer.WritePackedUInt32(netId); - writer.Write(authority); + writer.WriteBoolean(authority); } } @@ -378,8 +378,8 @@ public override void Deserialize(NetworkReader reader) public override void Serialize(NetworkWriter writer) { - writer.Write(clientTime); - writer.Write(serverTime); + writer.WriteDouble(clientTime); + writer.WriteDouble(serverTime); } } #endregion diff --git a/Assets/Mirror/Runtime/NetworkIdentity.cs b/Assets/Mirror/Runtime/NetworkIdentity.cs index 8749eb398..b6b6edf58 100644 --- a/Assets/Mirror/Runtime/NetworkIdentity.cs +++ b/Assets/Mirror/Runtime/NetworkIdentity.cs @@ -547,7 +547,7 @@ bool OnSerializeSafely(NetworkBehaviour comp, NetworkWriter writer, bool initial // (jumping back later is WAY faster than allocating a temporary // writer for the payload, then writing payload.size, payload) int headerPosition = writer.Position; - writer.Write((int)0); + writer.WriteInt32((int)0); int contentPosition = writer.Position; // write payload @@ -565,7 +565,7 @@ bool OnSerializeSafely(NetworkBehaviour comp, NetworkWriter writer, bool initial // fill in length now writer.Position = headerPosition; - writer.Write(endPosition - contentPosition); + writer.WriteInt32(endPosition - contentPosition); writer.Position = endPosition; if (LogFilter.Debug) Debug.Log("OnSerializeSafely written for object=" + comp.name + " component=" + comp.GetType() + " sceneId=" + m_SceneId.ToString("X") + "header@" + headerPosition + " content@" + contentPosition + " end@" + endPosition + " contentSize=" + (endPosition - contentPosition)); diff --git a/Assets/Mirror/Runtime/NetworkWriter.cs b/Assets/Mirror/Runtime/NetworkWriter.cs index 65e95d9e9..3a59647cf 100644 --- a/Assets/Mirror/Runtime/NetworkWriter.cs +++ b/Assets/Mirror/Runtime/NetworkWriter.cs @@ -56,57 +56,103 @@ public void SetLength(long value) stream.SetLength(value); } - public void Write(ushort value) + [Obsolete("Use WriteUInt16 instead")] + public void Write(ushort value) => WriteUInt16(value); + + public void WriteUInt16(ushort value) { - Write((byte)(value & 0xFF)); - Write((byte)(value >> 8)); - } - public void Write(uint value) - { - Write((byte)(value & 0xFF)); - Write((byte)((value >> 8) & 0xFF)); - Write((byte)((value >> 16) & 0xFF)); - Write((byte)((value >> 24) & 0xFF)); + WriteByte((byte)(value & 0xFF)); + WriteByte((byte)(value >> 8)); } - public void Write(ulong value) { - Write((byte)(value & 0xFF)); - Write((byte)((value >> 8) & 0xFF)); - Write((byte)((value >> 16) & 0xFF)); - Write((byte)((value >> 24) & 0xFF)); - Write((byte)((value >> 32) & 0xFF)); - Write((byte)((value >> 40) & 0xFF)); - Write((byte)((value >> 48) & 0xFF)); - Write((byte)((value >> 56) & 0xFF)); + [Obsolete("Use WriteUInt32 instead")] + public void Write(uint value) => WriteUInt32(value); + + public void WriteUInt32(uint value) + { + WriteByte((byte)(value & 0xFF)); + WriteByte((byte)((value >> 8) & 0xFF)); + WriteByte((byte)((value >> 16) & 0xFF)); + WriteByte((byte)((value >> 24) & 0xFF)); } + [Obsolete("Use WriteUInt64 instead")] + public void Write(ulong value) => WriteUInt64(value); + + public void WriteUInt64(ulong value) { + WriteByte((byte)(value & 0xFF)); + WriteByte((byte)((value >> 8) & 0xFF)); + WriteByte((byte)((value >> 16) & 0xFF)); + WriteByte((byte)((value >> 24) & 0xFF)); + WriteByte((byte)((value >> 32) & 0xFF)); + WriteByte((byte)((value >> 40) & 0xFF)); + WriteByte((byte)((value >> 48) & 0xFF)); + WriteByte((byte)((value >> 56) & 0xFF)); + } + + [Obsolete("Use WriteByte instead")] public void Write(byte value) => stream.WriteByte(value); - public void Write(sbyte value) => Write((byte)value); - // write char the same way that NetworkReader reads it (2 bytes) - public void Write(char value) => Write((ushort)value); - public void Write(bool value) => Write((byte)(value ? 1 : 0)); - public void Write(short value) => Write((ushort)value); - public void Write(int value) => Write((uint)value); - public void Write(long value) => Write((ulong)value); - public void Write(float value) { + public void WriteByte(byte value) => stream.WriteByte(value); + + [Obsolete("Use WriteSByte instead")] + public void Write(sbyte value) => WriteByte((byte)value); + + public void WriteSByte(sbyte value) => WriteByte((byte)value); + + // write char the same way that NetworkReader reads it (2 bytes) + [Obsolete("Use WriteChar instead")] + public void Write(char value) => WriteUInt16((ushort)value); + + public void WriteChar(char value) => WriteUInt16((ushort)value); + + [Obsolete("Use WriteBoolean instead")] + public void Write(bool value) => WriteByte((byte)(value ? 1 : 0)); + + public void WriteBoolean(bool value) => WriteByte((byte)(value ? 1 : 0)); + + [Obsolete("Use WriteInt16 instead")] + public void Write(short value) => WriteUInt16((ushort)value); + + public void WriteInt16(short value) => WriteUInt16((ushort)value); + + [Obsolete("Use WriteInt32 instead")] + public void Write(int value) => WriteUInt32((uint)value); + + public void WriteInt32(int value) => WriteUInt32((uint)value); + + [Obsolete("Use WriteInt64 instead")] + public void Write(long value) => WriteUInt64((ulong)value); + + public void WriteInt64(long value) => WriteUInt64((ulong)value); + + [Obsolete("Use WriteSingle instead")] + public void Write(float value) => WriteSingle(value); + + public void WriteSingle(float value) { UIntFloat converter = new UIntFloat { floatValue = value }; - Write(converter.intValue); + WriteUInt32(converter.intValue); } - public void Write(double value) + [Obsolete("Use WriteDouble instead")] + public void Write(double value) => WriteDouble(value); + + public void WriteDouble(double value) { UIntDouble converter = new UIntDouble { doubleValue = value }; - Write(converter.longValue); + WriteUInt64(converter.longValue); } - public void Write(decimal value) + [Obsolete("Use WriteDecimal instead")] + public void Write(decimal value) => WriteDecimal(value); + + public void WriteDecimal(decimal value) { // the only way to read it without allocations is to both read and // write it with the FloatConverter (which is not binary compatible @@ -115,11 +161,14 @@ public void Write(decimal value) { decimalValue = value }; - Write(converter.longValue1); - Write(converter.longValue2); + WriteUInt64(converter.longValue1); + WriteUInt64(converter.longValue2); } - public void Write(string value) + [Obsolete("Use WriteString instead")] + public void Write(string value) => WriteString(value); + + public void WriteString(string value) { // write 0 for null support, increment real size by 1 // (note: original HLAPI would write "" for null strings, but if a @@ -127,7 +176,7 @@ public void Write(string value) // on the client) if (value == null) { - Write((ushort)0); + WriteUInt16((ushort)0); return; } @@ -142,13 +191,16 @@ public void Write(string value) } // write size and bytes - Write(checked((ushort)(size + 1))); - Write(stringBuffer, 0, size); + WriteUInt16(checked((ushort)(size + 1))); + WriteBytes(stringBuffer, 0, size); } + [Obsolete("Use WriteBytes instead")] + public void Write(byte[] buffer, int offset, int count) => WriteBytes(buffer, offset, count); + // for byte arrays with consistent size, where the reader knows how many to read // (like a packet opcode that's always the same) - public void Write(byte[] buffer, int offset, int count) + public void WriteBytes(byte[] buffer, int offset, int count) { // no null check because we would need to write size info for that too (hence WriteBytesAndSize) stream.Write(buffer, offset, count); @@ -167,7 +219,7 @@ public void WriteBytesAndSize(byte[] buffer, int offset, int count) return; } WritePackedUInt32(checked((uint)count) + 1u); - Write(buffer, offset, count); + WriteBytes(buffer, offset, count); } // Weaver needs a write function with just one byte[] parameter @@ -209,192 +261,234 @@ public void WritePackedUInt64(ulong value) { if (value <= 240) { - Write((byte)value); + WriteByte((byte)value); return; } if (value <= 2287) { - Write((byte)(((value - 240) >> 8) + 241)); - Write((byte)((value - 240) & 0xFF)); + WriteByte((byte)(((value - 240) >> 8) + 241)); + WriteByte((byte)((value - 240) & 0xFF)); return; } if (value <= 67823) { - Write((byte)249); - Write((byte)((value - 2288) >> 8)); - Write((byte)((value - 2288) & 0xFF)); + WriteByte((byte)249); + WriteByte((byte)((value - 2288) >> 8)); + WriteByte((byte)((value - 2288) & 0xFF)); return; } if (value <= 16777215) { - Write((byte)250); - Write((byte)(value & 0xFF)); - Write((byte)((value >> 8) & 0xFF)); - Write((byte)((value >> 16) & 0xFF)); + WriteByte((byte)250); + WriteByte((byte)(value & 0xFF)); + WriteByte((byte)((value >> 8) & 0xFF)); + WriteByte((byte)((value >> 16) & 0xFF)); return; } if (value <= 4294967295) { - Write((byte)251); - Write((byte)(value & 0xFF)); - Write((byte)((value >> 8) & 0xFF)); - Write((byte)((value >> 16) & 0xFF)); - Write((byte)((value >> 24) & 0xFF)); + WriteByte((byte)251); + WriteByte((byte)(value & 0xFF)); + WriteByte((byte)((value >> 8) & 0xFF)); + WriteByte((byte)((value >> 16) & 0xFF)); + WriteByte((byte)((value >> 24) & 0xFF)); return; } if (value <= 1099511627775) { - Write((byte)252); - Write((byte)(value & 0xFF)); - Write((byte)((value >> 8) & 0xFF)); - Write((byte)((value >> 16) & 0xFF)); - Write((byte)((value >> 24) & 0xFF)); - Write((byte)((value >> 32) & 0xFF)); + WriteByte((byte)252); + WriteByte((byte)(value & 0xFF)); + WriteByte((byte)((value >> 8) & 0xFF)); + WriteByte((byte)((value >> 16) & 0xFF)); + WriteByte((byte)((value >> 24) & 0xFF)); + WriteByte((byte)((value >> 32) & 0xFF)); return; } if (value <= 281474976710655) { - Write((byte)253); - Write((byte)(value & 0xFF)); - Write((byte)((value >> 8) & 0xFF)); - Write((byte)((value >> 16) & 0xFF)); - Write((byte)((value >> 24) & 0xFF)); - Write((byte)((value >> 32) & 0xFF)); - Write((byte)((value >> 40) & 0xFF)); + WriteByte((byte)253); + WriteByte((byte)(value & 0xFF)); + WriteByte((byte)((value >> 8) & 0xFF)); + WriteByte((byte)((value >> 16) & 0xFF)); + WriteByte((byte)((value >> 24) & 0xFF)); + WriteByte((byte)((value >> 32) & 0xFF)); + WriteByte((byte)((value >> 40) & 0xFF)); return; } if (value <= 72057594037927935) { - Write((byte)254); - Write((byte)(value & 0xFF)); - Write((byte)((value >> 8) & 0xFF)); - Write((byte)((value >> 16) & 0xFF)); - Write((byte)((value >> 24) & 0xFF)); - Write((byte)((value >> 32) & 0xFF)); - Write((byte)((value >> 40) & 0xFF)); - Write((byte)((value >> 48) & 0xFF)); + WriteByte((byte)254); + WriteByte((byte)(value & 0xFF)); + WriteByte((byte)((value >> 8) & 0xFF)); + WriteByte((byte)((value >> 16) & 0xFF)); + WriteByte((byte)((value >> 24) & 0xFF)); + WriteByte((byte)((value >> 32) & 0xFF)); + WriteByte((byte)((value >> 40) & 0xFF)); + WriteByte((byte)((value >> 48) & 0xFF)); return; } // all others { - Write((byte)255); - Write((byte)(value & 0xFF)); - Write((byte)((value >> 8) & 0xFF)); - Write((byte)((value >> 16) & 0xFF)); - Write((byte)((value >> 24) & 0xFF)); - Write((byte)((value >> 32) & 0xFF)); - Write((byte)((value >> 40) & 0xFF)); - Write((byte)((value >> 48) & 0xFF)); - Write((byte)((value >> 56) & 0xFF)); + WriteByte((byte)255); + WriteByte((byte)(value & 0xFF)); + WriteByte((byte)((value >> 8) & 0xFF)); + WriteByte((byte)((value >> 16) & 0xFF)); + WriteByte((byte)((value >> 24) & 0xFF)); + WriteByte((byte)((value >> 32) & 0xFF)); + WriteByte((byte)((value >> 40) & 0xFF)); + WriteByte((byte)((value >> 48) & 0xFF)); + WriteByte((byte)((value >> 56) & 0xFF)); } } - public void Write(Vector2 value) + [Obsolete("Use WriteVector2 instead")] + public void Write(Vector2 value) => WriteVector2(value); + + public void WriteVector2(Vector2 value) { - Write(value.x); - Write(value.y); + WriteSingle(value.x); + WriteSingle(value.y); } - public void Write(Vector3 value) + [Obsolete("Use WriteVector3 instead")] + public void Write(Vector3 value) => WriteVector3(value); + + public void WriteVector3(Vector3 value) { - Write(value.x); - Write(value.y); - Write(value.z); + WriteSingle(value.x); + WriteSingle(value.y); + WriteSingle(value.z); } - public void Write(Vector4 value) + [Obsolete("Use WriteVector4 instead")] + public void Write(Vector4 value) => WriteVector4(value); + + public void WriteVector4(Vector4 value) { - Write(value.x); - Write(value.y); - Write(value.z); - Write(value.w); + WriteSingle(value.x); + WriteSingle(value.y); + WriteSingle(value.z); + WriteSingle(value.w); } - public void Write(Vector2Int value) + [Obsolete("Use WriteVector2Int instead")] + public void Write(Vector2Int value) => WriteVector2Int(value); + + public void WriteVector2Int(Vector2Int value) { WritePackedInt32(value.x); WritePackedInt32(value.y); } - public void Write(Vector3Int value) + [Obsolete("Use WriteVector3Int instead")] + public void Write(Vector3Int value) => WriteVector3Int(value); + + public void WriteVector3Int(Vector3Int value) { WritePackedInt32(value.x); WritePackedInt32(value.y); WritePackedInt32(value.z); } - public void Write(Color value) + [Obsolete("Use WriteColor instead")] + public void Write(Color value) => WriteColor(value); + + public void WriteColor(Color value) { - Write(value.r); - Write(value.g); - Write(value.b); - Write(value.a); + WriteSingle(value.r); + WriteSingle(value.g); + WriteSingle(value.b); + WriteSingle(value.a); } - public void Write(Color32 value) + [Obsolete("Use WriteColor32 instead")] + public void Write(Color32 value) => WriteColor32(value); + + public void WriteColor32(Color32 value) { - Write(value.r); - Write(value.g); - Write(value.b); - Write(value.a); + WriteByte(value.r); + WriteByte(value.g); + WriteByte(value.b); + WriteByte(value.a); } - public void Write(Quaternion value) + [Obsolete("Use WriteQuaternion instead")] + public void Write(Quaternion value) => WriteQuaternion(value); + + public void WriteQuaternion(Quaternion value) { - Write(value.x); - Write(value.y); - Write(value.z); - Write(value.w); + WriteSingle(value.x); + WriteSingle(value.y); + WriteSingle(value.z); + WriteSingle(value.w); } - public void Write(Rect value) + [Obsolete("Use WriteRect instead")] + public void Write(Rect value) => WriteRect(value); + + public void WriteRect(Rect value) { - Write(value.xMin); - Write(value.yMin); - Write(value.width); - Write(value.height); + WriteSingle(value.xMin); + WriteSingle(value.yMin); + WriteSingle(value.width); + WriteSingle(value.height); } - public void Write(Plane value) + [Obsolete("Use WritePlane instead")] + public void Write(Plane value) => WritePlane(value); + + public void WritePlane(Plane value) { - Write(value.normal); - Write(value.distance); + WriteVector3(value.normal); + WriteSingle(value.distance); } - public void Write(Ray value) + [Obsolete("Use WriteRay instead")] + public void Write(Ray value) => WriteRay(value); + + public void WriteRay(Ray value) { - Write(value.origin); - Write(value.direction); + WriteVector3(value.origin); + WriteVector3(value.direction); } - public void Write(Matrix4x4 value) + [Obsolete("Use WriteMatrix4x4 instead")] + public void Write(Matrix4x4 value) => WriteMatrix4x4(value); + + public void WriteMatrix4x4(Matrix4x4 value) { - Write(value.m00); - Write(value.m01); - Write(value.m02); - Write(value.m03); - Write(value.m10); - Write(value.m11); - Write(value.m12); - Write(value.m13); - Write(value.m20); - Write(value.m21); - Write(value.m22); - Write(value.m23); - Write(value.m30); - Write(value.m31); - Write(value.m32); - Write(value.m33); + WriteSingle(value.m00); + WriteSingle(value.m01); + WriteSingle(value.m02); + WriteSingle(value.m03); + WriteSingle(value.m10); + WriteSingle(value.m11); + WriteSingle(value.m12); + WriteSingle(value.m13); + WriteSingle(value.m20); + WriteSingle(value.m21); + WriteSingle(value.m22); + WriteSingle(value.m23); + WriteSingle(value.m30); + WriteSingle(value.m31); + WriteSingle(value.m32); + WriteSingle(value.m33); } - public void Write(Guid value) + [Obsolete("Use WriteGuid instead")] + public void Write(Guid value) => WriteGuid(value); + + public void WriteGuid(Guid value) { byte[] data = value.ToByteArray(); - Write(data, 0, data.Length); + WriteBytes(data, 0, data.Length); } - public void Write(NetworkIdentity value) + [Obsolete("Use WriteNetworkIdentity instead")] + public void Write(NetworkIdentity value) => WriteNetworkIdentity(value); + + public void WriteNetworkIdentity(NetworkIdentity value) { if (value == null) { @@ -404,7 +498,10 @@ public void Write(NetworkIdentity value) WritePackedUInt32(value.netId); } - public void Write(Transform value) + [Obsolete("Use WriteTransform instead")] + public void Write(Transform value) => WriteTransform(value); + + public void WriteTransform(Transform value) { if (value == null || value.gameObject == null) { @@ -423,7 +520,10 @@ public void Write(Transform value) } } - public void Write(GameObject value) + [Obsolete("Use WriteGameObject instead")] + public void Write(GameObject value) => WriteGameObject(value); + + public void WriteGameObject(GameObject value) { if (value == null) { diff --git a/Assets/Mirror/Runtime/SyncDictionary.cs b/Assets/Mirror/Runtime/SyncDictionary.cs index 2a3cac7c1..930b8e549 100644 --- a/Assets/Mirror/Runtime/SyncDictionary.cs +++ b/Assets/Mirror/Runtime/SyncDictionary.cs @@ -38,8 +38,8 @@ struct Change // so we need to skip them int changesAhead; - protected virtual void SerializeKey(NetworkWriter writer, TKey item) {} - protected virtual void SerializeItem(NetworkWriter writer, TValue item) {} + protected virtual void SerializeKey(NetworkWriter writer, TKey item) { } + protected virtual void SerializeItem(NetworkWriter writer, TValue item) { } protected virtual TKey DeserializeKey(NetworkReader reader) => default; protected virtual TValue DeserializeItem(NetworkReader reader) => default; @@ -63,7 +63,7 @@ protected SyncDictionary(IEqualityComparer eq) objects = new Dictionary(eq); } - protected SyncDictionary(IDictionary objects) + protected SyncDictionary(IDictionary objects) { this.objects = objects; } @@ -113,7 +113,7 @@ public void OnSerializeDelta(NetworkWriter writer) for (int i = 0; i < changes.Count; i++) { Change change = changes[i]; - writer.Write((byte)change.operation); + writer.WriteByte((byte)change.operation); switch (change.operation) { @@ -284,7 +284,7 @@ public void CopyTo(KeyValuePair[] array, int arrayIndex) } int i = arrayIndex; - foreach (KeyValuePair item in objects) + foreach (KeyValuePair item in objects) { array[i] = item; i++; diff --git a/Assets/Mirror/Runtime/SyncList.cs b/Assets/Mirror/Runtime/SyncList.cs index 25fa23848..83ddc024b 100644 --- a/Assets/Mirror/Runtime/SyncList.cs +++ b/Assets/Mirror/Runtime/SyncList.cs @@ -7,13 +7,13 @@ namespace Mirror { public class SyncListString : SyncList { - protected override void SerializeItem(NetworkWriter writer, string item) => writer.Write(item); + protected override void SerializeItem(NetworkWriter writer, string item) => writer.WriteString(item); protected override string DeserializeItem(NetworkReader reader) => reader.ReadString(); } public class SyncListFloat : SyncList { - protected override void SerializeItem(NetworkWriter writer, float item) => writer.Write(item); + protected override void SerializeItem(NetworkWriter writer, float item) => writer.WriteSingle(item); protected override float DeserializeItem(NetworkReader reader) => reader.ReadSingle(); } @@ -31,7 +31,7 @@ public class SyncListUInt : SyncList public class SyncListBool : SyncList { - protected override void SerializeItem(NetworkWriter writer, bool item) => writer.Write(item); + protected override void SerializeItem(NetworkWriter writer, bool item) => writer.WriteBoolean(item); protected override bool DeserializeItem(NetworkReader reader) => reader.ReadBoolean(); } @@ -80,7 +80,7 @@ struct Change // so we need to skip them int changesAhead; - protected virtual void SerializeItem(NetworkWriter writer, T item) {} + protected virtual void SerializeItem(NetworkWriter writer, T item) { } protected virtual T DeserializeItem(NetworkReader reader) => default; @@ -147,7 +147,7 @@ public void OnSerializeDelta(NetworkWriter writer) for (int i = 0; i < changes.Count; i++) { Change change = changes[i]; - writer.Write((byte)change.operation); + writer.WriteByte((byte)change.operation); switch (change.operation) { diff --git a/Assets/Mirror/Runtime/SyncSet.cs b/Assets/Mirror/Runtime/SyncSet.cs index 04d35797a..0a0635259 100644 --- a/Assets/Mirror/Runtime/SyncSet.cs +++ b/Assets/Mirror/Runtime/SyncSet.cs @@ -42,7 +42,7 @@ protected SyncSet(ISet objects) this.objects = objects; } - protected virtual void SerializeItem(NetworkWriter writer, T item) {} + protected virtual void SerializeItem(NetworkWriter writer, T item) { } protected virtual T DeserializeItem(NetworkReader reader) => default; public bool IsDirty => changes.Count > 0; @@ -96,7 +96,7 @@ public void OnSerializeDelta(NetworkWriter writer) for (int i = 0; i < changes.Count; i++) { Change change = changes[i]; - writer.Write((byte)change.operation); + writer.WriteByte((byte)change.operation); switch (change.operation) { @@ -318,13 +318,13 @@ public void UnionWith(IEnumerable other) public abstract class SyncHashSet : SyncSet { - protected SyncHashSet() : base(new HashSet()) {} + protected SyncHashSet() : base(new HashSet()) { } } public abstract class SyncSortedSet : SyncSet { - protected SyncSortedSet() : base(new SortedSet()) {} + protected SyncSortedSet() : base(new SortedSet()) { } - protected SyncSortedSet(IComparer comparer) : base (new SortedSet(comparer)) {} + protected SyncSortedSet(IComparer comparer) : base(new SortedSet(comparer)) { } } } diff --git a/Assets/Mirror/Tests/MessageBaseTests.cs b/Assets/Mirror/Tests/MessageBaseTests.cs index 99e0fd4b7..00ad90ebb 100644 --- a/Assets/Mirror/Tests/MessageBaseTests.cs +++ b/Assets/Mirror/Tests/MessageBaseTests.cs @@ -24,9 +24,9 @@ public void Deserialize(NetworkReader reader) public void Serialize(NetworkWriter writer) { - writer.Write(IntValue); - writer.Write(StringValue); - writer.Write(DoubleValue); + writer.WriteInt32(IntValue); + writer.WriteString(StringValue); + writer.WriteDouble(DoubleValue); } } diff --git a/Assets/Mirror/Tests/NetworkWriterTest.cs b/Assets/Mirror/Tests/NetworkWriterTest.cs index 7384668c0..3cc615798 100644 --- a/Assets/Mirror/Tests/NetworkWriterTest.cs +++ b/Assets/Mirror/Tests/NetworkWriterTest.cs @@ -13,7 +13,7 @@ public void TestWritingSmallMessage() // try serializing less than 32kb and see what happens NetworkWriter writer = new NetworkWriter(); for (int i = 0; i < 30000 / 4; ++i) - writer.Write(i); + writer.WriteInt32(i); Assert.That(writer.Position, Is.EqualTo(30000)); } @@ -23,7 +23,7 @@ public void TestWritingLargeMessage() // try serializing more than 32kb and see what happens NetworkWriter writer = new NetworkWriter(); for (int i = 0; i < 40000 / 4; ++i) - writer.Write(i); + writer.WriteInt32(i); Assert.That(writer.Position, Is.EqualTo(40000)); } @@ -45,7 +45,7 @@ public void TestWritingBytesSegment() { byte[] data = {1, 2, 3}; NetworkWriter writer = new NetworkWriter(); - writer.Write(data, 0, data.Length); + writer.WriteBytes(data, 0, data.Length); NetworkReader reader = new NetworkReader(writer.ToArray()); ArraySegment deserialized = reader.ReadBytesSegment(data.Length); @@ -89,20 +89,20 @@ public void TestWritingSegmentAndReadingSegment() public void TestOverwritingData() { NetworkWriter writer = new NetworkWriter(); - writer.Write(Matrix4x4.identity); - writer.Write(1.23456789m); + writer.WriteMatrix4x4(Matrix4x4.identity); + writer.WriteDecimal(1.23456789m); writer.Position += 10; - writer.Write(Vector3.negativeInfinity); + writer.WriteVector3(Vector3.negativeInfinity); writer.Position = 46; // write right at the boundary before SetLength - writer.Write(0xfeed_babe_c0ffee); + writer.WriteInt64(0xfeed_babe_c0ffee); // test that SetLength clears data beyond length writer.SetLength(50); // check that jumping leaves 0s between writer.Position = 100; - writer.Write("no worries, m8"); + writer.WriteString("no worries, m8"); writer.Position = 64; - writer.Write(true); + writer.WriteBoolean(true); // check that clipping off the end affect ToArray()'s length writer.SetLength(128); byte[] output = writer.ToArray(); @@ -129,9 +129,9 @@ public void TestOverwritingData() public void TestSetLengthZeroes() { NetworkWriter writer = new NetworkWriter(); - writer.Write("I saw"); - writer.Write(0xA_FADED_DEAD_EEL); - writer.Write("and ate it"); + writer.WriteString("I saw"); + writer.WriteInt64(0xA_FADED_DEAD_EEL); + writer.WriteString("and ate it"); int position = writer.Position; writer.SetLength(10); // Setting length should set position too @@ -178,7 +178,7 @@ public void TestReading0LengthBytesAndSize() public void TestReading0LengthBytes() { NetworkWriter writer = new NetworkWriter(); - writer.Write(new byte[]{}, 0, 0); + writer.WriteBytes(new byte[]{}, 0, 0); NetworkReader reader = new NetworkReader(writer.ToArray()); Assert.That(reader.ReadBytes(0).Length, Is.EqualTo(0)); } @@ -260,7 +260,7 @@ public void TestVector2() foreach (Vector2 input in inputs) { NetworkWriter writer = new NetworkWriter(); - writer.Write(input); + writer.WriteVector2(input); NetworkReader reader = new NetworkReader(writer.ToArray()); Vector2 output = reader.ReadVector2(); Assert.That(output, Is.EqualTo(input)); @@ -282,7 +282,7 @@ public void TestVector3() foreach (Vector3 input in inputs) { NetworkWriter writer = new NetworkWriter(); - writer.Write(input); + writer.WriteVector3(input); NetworkReader reader = new NetworkReader(writer.ToArray()); Vector3 output = reader.ReadVector3(); Assert.That(output, Is.EqualTo(input)); @@ -303,7 +303,7 @@ public void TestVector4() foreach (Vector4 input in inputs) { NetworkWriter writer = new NetworkWriter(); - writer.Write(input); + writer.WriteVector4(input); NetworkReader reader = new NetworkReader(writer.ToArray()); Vector4 output = reader.ReadVector4(); Assert.That(output, Is.EqualTo(input)); @@ -325,7 +325,7 @@ public void TestVector2Int() foreach (Vector2Int input in inputs) { NetworkWriter writer = new NetworkWriter(); - writer.Write(input); + writer.WriteVector2Int(input); NetworkReader reader = new NetworkReader(writer.ToArray()); Vector2Int output = reader.ReadVector2Int(); Assert.That(output, Is.EqualTo(input)); @@ -348,7 +348,7 @@ public void TestVector3Int() foreach (Vector3Int input in inputs) { NetworkWriter writer = new NetworkWriter(); - writer.Write(input); + writer.WriteVector3Int(input); NetworkReader reader = new NetworkReader(writer.ToArray()); Vector3Int output = reader.ReadVector3Int(); Assert.That(output, Is.EqualTo(input)); @@ -370,7 +370,7 @@ public void TestColor() foreach (Color input in inputs) { NetworkWriter writer = new NetworkWriter(); - writer.Write(input); + writer.WriteColor(input); NetworkReader reader = new NetworkReader(writer.ToArray()); Color output = reader.ReadColor(); Assert.That(output, Is.EqualTo(input)); @@ -393,7 +393,7 @@ public void TestColor32() foreach (Color32 input in inputs) { NetworkWriter writer = new NetworkWriter(); - writer.Write(input); + writer.WriteColor32(input); NetworkReader reader = new NetworkReader(writer.ToArray()); Color32 output = reader.ReadColor32(); Assert.That(output, Is.EqualTo(input)); @@ -412,7 +412,7 @@ public void TestQuaternion() foreach (Quaternion input in inputs) { NetworkWriter writer = new NetworkWriter(); - writer.Write(input); + writer.WriteQuaternion(input); NetworkReader reader = new NetworkReader(writer.ToArray()); Quaternion output = reader.ReadQuaternion(); Assert.That(output, Is.EqualTo(input)); @@ -432,7 +432,7 @@ public void TestRect() foreach (Rect input in inputs) { NetworkWriter writer = new NetworkWriter(); - writer.Write(input); + writer.WriteRect(input); NetworkReader reader = new NetworkReader(writer.ToArray()); Rect output = reader.ReadRect(); Assert.That(output, Is.EqualTo(input)); @@ -451,7 +451,7 @@ public void TestPlane() foreach (Plane input in inputs) { NetworkWriter writer = new NetworkWriter(); - writer.Write(input); + writer.WritePlane(input); NetworkReader reader = new NetworkReader(writer.ToArray()); Plane output = reader.ReadPlane(); // note: Plane constructor does math internally, resulting in @@ -473,7 +473,7 @@ public void TestRay() foreach (Ray input in inputs) { NetworkWriter writer = new NetworkWriter(); - writer.Write(input); + writer.WriteRay(input); NetworkReader reader = new NetworkReader(writer.ToArray()); Ray output = reader.ReadRay(); Assert.That((output.direction - input.direction).magnitude, Is.LessThan(1e-6f)); @@ -494,7 +494,7 @@ public void TestMatrix4x4() foreach (Matrix4x4 input in inputs) { NetworkWriter writer = new NetworkWriter(); - writer.Write(input); + writer.WriteMatrix4x4(input); NetworkReader reader = new NetworkReader(writer.ToArray()); Matrix4x4 output = reader.ReadMatrix4x4(); Assert.That(output, Is.EqualTo(input)); @@ -515,7 +515,7 @@ public void TestReadingInvalidString() foreach (byte invalid in invalidUTF8bytes) { NetworkWriter writer = new NetworkWriter(); - writer.Write("an uncorrupted string"); + writer.WriteString("an uncorrupted string"); byte[] data = writer.ToArray(); data[10] = invalid; NetworkReader reader = new NetworkReader(data); @@ -527,7 +527,7 @@ public void TestReadingInvalidString() public void TestReadingTruncatedString() { NetworkWriter writer = new NetworkWriter(); - writer.Write("a string longer than 10 bytes"); + writer.WriteString("a string longer than 10 bytes"); writer.SetLength(10); NetworkReader reader = new NetworkReader(writer.ToArray()); Assert.Throws(() => reader.ReadString()); @@ -538,8 +538,8 @@ public void TestToArray() { // write 2 bytes NetworkWriter writer = new NetworkWriter(); - writer.Write((byte)1); - writer.Write((byte)2); + writer.WriteByte((byte)1); + writer.WriteByte((byte)2); // .ToArray() length is 2? Assert.That(writer.ToArray().Length, Is.EqualTo(2)); @@ -555,8 +555,8 @@ public void TestToArray() public void TestToArraySegment() { NetworkWriter writer = new NetworkWriter(); - writer.Write("hello"); - writer.Write("world"); + writer.WriteString("hello"); + writer.WriteString("world"); NetworkReader reader = new NetworkReader(writer.ToArraySegment()); Assert.That(reader.ReadString(), Is.EqualTo("hello")); @@ -570,8 +570,8 @@ public void TestChar() char u = 'ⓤ'; NetworkWriter writer = new NetworkWriter(); - writer.Write(a); - writer.Write(u); + writer.WriteChar(a); + writer.WriteChar(u); NetworkReader reader = new NetworkReader(writer.ToArray()); char a2 = reader.ReadChar(); Assert.That(a2, Is.EqualTo(a)); @@ -622,7 +622,7 @@ public void TestUnicodeString() foreach (string weird in weirdUnicode) { NetworkWriter writer = new NetworkWriter(); - writer.Write(weird); + writer.WriteString(weird); byte[] data = writer.ToArray(); NetworkReader reader = new NetworkReader(data); string str = reader.ReadString(); @@ -821,7 +821,7 @@ public void TestGuid() { Guid originalGuid = new Guid("0123456789abcdef9876543210fedcba"); NetworkWriter writer = new NetworkWriter(); - writer.Write(originalGuid); + writer.WriteGuid(originalGuid); NetworkReader reader = new NetworkReader(writer.ToArray()); Guid readGuid = reader.ReadGuid(); @@ -852,7 +852,7 @@ public void TestFloats() foreach (float weird in weirdFloats) { NetworkWriter writer = new NetworkWriter(); - writer.Write(weird); + writer.WriteSingle(weird); NetworkReader reader = new NetworkReader(writer.ToArray()); float readFloat = reader.ReadSingle(); Assert.That(readFloat, Is.EqualTo(weird)); @@ -883,7 +883,7 @@ public void TestDoubles() foreach (double weird in weirdDoubles) { NetworkWriter writer = new NetworkWriter(); - writer.Write(weird); + writer.WriteDouble(weird); NetworkReader reader = new NetworkReader(writer.ToArray()); double readDouble = reader.ReadDouble(); Assert.That(readDouble, Is.EqualTo(weird)); @@ -904,7 +904,7 @@ public void TestDecimals() foreach (decimal weird in weirdDecimals) { NetworkWriter writer = new NetworkWriter(); - writer.Write(weird); + writer.WriteDecimal(weird); NetworkReader reader = new NetworkReader(writer.ToArray()); decimal readDecimal = reader.ReadDecimal(); Assert.That(readDecimal, Is.EqualTo(weird)); @@ -925,7 +925,7 @@ public void TestFloatBinaryCompatibility() NetworkWriter writer = new NetworkWriter(); foreach (float weird in weirdFloats) { - writer.Write(weird); + writer.WriteSingle(weird); } Assert.That(writer.ToArray(), Is.EqualTo(expected)); } @@ -944,7 +944,7 @@ public void TestDoubleBinaryCompatibility() NetworkWriter writer = new NetworkWriter(); foreach (double weird in weirdDoubles) { - writer.Write(weird); + writer.WriteDouble(weird); } Assert.That(writer.ToArray(), Is.EqualTo(expected)); } @@ -965,7 +965,7 @@ public void TestDecimalBinaryCompatibility() NetworkWriter writer = new NetworkWriter(); foreach (decimal weird in weirdDecimals) { - writer.Write(weird); + writer.WriteDecimal(weird); } //Debug.Log(BitConverter.ToString(writer.ToArray())); Assert.That(writer.ToArray(), Is.EqualTo(expected)); @@ -979,7 +979,7 @@ public void TestByteEndianness() NetworkWriter writer = new NetworkWriter(); foreach (byte value in values) { - writer.Write(value); + writer.WriteByte(value); } Assert.That(writer.ToArray(), Is.EqualTo(expected)); } @@ -992,7 +992,7 @@ public void TestUShortEndianness() NetworkWriter writer = new NetworkWriter(); foreach (ushort value in values) { - writer.Write(value); + writer.WriteUInt16(value); } Assert.That(writer.ToArray(), Is.EqualTo(expected)); } @@ -1005,7 +1005,7 @@ public void TestUIntEndianness() NetworkWriter writer = new NetworkWriter(); foreach (uint value in values) { - writer.Write(value); + writer.WriteUInt32(value); } Assert.That(writer.ToArray(), Is.EqualTo(expected)); } @@ -1018,7 +1018,7 @@ public void TestULongEndianness() NetworkWriter writer = new NetworkWriter(); foreach (ulong value in values) { - writer.Write(value); + writer.WriteUInt64(value); } Assert.That(writer.ToArray(), Is.EqualTo(expected)); } @@ -1031,7 +1031,7 @@ public void TestSbyteEndianness() NetworkWriter writer = new NetworkWriter(); foreach (byte value in values) { - writer.Write((sbyte) value); + writer.WriteSByte((sbyte) value); } Assert.That(writer.ToArray(), Is.EqualTo(expected)); } @@ -1044,7 +1044,7 @@ public void TestShortEndianness() NetworkWriter writer = new NetworkWriter(); foreach (ushort value in values) { - writer.Write((short) value); + writer.WriteInt16((short) value); } Assert.That(writer.ToArray(), Is.EqualTo(expected)); } @@ -1057,7 +1057,7 @@ public void TestIntEndianness() NetworkWriter writer = new NetworkWriter(); foreach (uint value in values) { - writer.Write((int) value); + writer.WriteInt32((int) value); } Assert.That(writer.ToArray(), Is.EqualTo(expected)); } @@ -1070,7 +1070,7 @@ public void TestLongEndianness() NetworkWriter writer = new NetworkWriter(); foreach (ulong value in values) { - writer.Write((long) value); + writer.WriteInt64((long) value); } Assert.That(writer.ToArray(), Is.EqualTo(expected)); } @@ -1080,23 +1080,23 @@ public void TestWritingAndReading() { // write all simple types once NetworkWriter writer = new NetworkWriter(); - writer.Write((char)1); - writer.Write((byte)2); - writer.Write((sbyte)3); - writer.Write(true); - writer.Write((short)4); - writer.Write((ushort)5); - writer.Write(6); - writer.Write(7U); - writer.Write(8L); - writer.Write(9UL); - writer.Write(10.0F); - writer.Write(11.0D); - writer.Write((decimal)12); - writer.Write((string)null); - writer.Write(""); - writer.Write("13"); - writer.Write(new byte[] { 14, 15 }, 0, 2); // just the byte array, no size info etc. + writer.WriteChar((char)1); + writer.WriteByte((byte)2); + writer.WriteSByte((sbyte)3); + writer.WriteBoolean(true); + writer.WriteInt16((short)4); + writer.WriteUInt16((ushort)5); + writer.WriteInt32(6); + writer.WriteUInt32(7U); + writer.WriteInt64(8L); + writer.WriteUInt64(9UL); + writer.WriteSingle(10.0F); + writer.WriteDouble(11.0D); + writer.WriteDecimal((decimal)12); + writer.WriteString((string)null); + writer.WriteString(""); + writer.WriteString("13"); + writer.WriteBytes(new byte[] { 14, 15 }, 0, 2); // just the byte array, no size info etc. writer.WriteBytesAndSize((byte[])null); // [SyncVar] struct values can have uninitialized byte arrays, null needs to be supported writer.WriteBytesAndSize(new byte[] { 17, 18 }, 0, 2); // buffer, no-offset, count writer.WriteBytesAndSize(new byte[] { 19, 20, 21 }, 1, 2); // buffer, offset, count