/// <summary> /// Compress position to integers and push it to the buffer, required bytes count: <see cref="PositionRequiredBytes"/> /// </summary> /// <param name="bytesStack">Buffer where position will be pushed</param> /// <param name="position">Position to be compressed and pushed</param> public static void PushCompressedPosition(this BytesStack bytesStack, Vector3 position) { //Reverse order when writing to stack //Always use little-endian so compression and decompression are machines independent var z = CompressPositionZ(position.z); if (!BitConverter.IsLittleEndian) { z = SwapEndianness(z); } bytesStack.PushInt(z, PositionZRequiredBytes); var y = CompressPositionY(position.y); if (!BitConverter.IsLittleEndian) { y = SwapEndianness(y); } bytesStack.PushInt(y, PositionYRequiredBytes); var x = CompressPositionX(position.x); if (!BitConverter.IsLittleEndian) { x = SwapEndianness(x); } bytesStack.PushInt(x, PositionXRequiredBytes); }
/// <summary> /// Compress vector3 to integers and push it to the buffer /// </summary> /// <param name="bytesStack">Buffer where vector3 will be pushed</param> /// <param name="vector3">Vector3 to be compressed and pushed</param> /// <param name="minElementValue">Minimal value of the encoded vector3</param> /// <param name="maxElementValue">Maximal value of the encoded vector3</param> /// <param name="bytesPerElement">Bytes count that will be used per each element</param> public static void PushCompressedVector3(this BytesStack bytesStack, Vector3 vector3, float minElementValue, float maxElementValue, int bytesPerElement) { //Reverse order when writing to stack //Always use little-endian so compression and decompression are machines independent var z = CompressFloatToInt(vector3.z, minElementValue, maxElementValue, bytesPerElement); if (!BitConverter.IsLittleEndian) { z = SwapEndianness(z); } bytesStack.PushInt(z, bytesPerElement); var y = CompressFloatToInt(vector3.y, minElementValue, maxElementValue, bytesPerElement); if (!BitConverter.IsLittleEndian) { y = SwapEndianness(y); } bytesStack.PushInt(y, bytesPerElement); var x = CompressFloatToInt(vector3.x, minElementValue, maxElementValue, bytesPerElement); if (!BitConverter.IsLittleEndian) { x = SwapEndianness(x); } bytesStack.PushInt(x, bytesPerElement); }
/// <summary> /// Pushes uncompressed vector3 to the buffer /// </summary> /// <param name="bytesStack">Buffer where vector3 will be pushed</param> /// <param name="vector3">Vector3 to be pushed</param> public static void PushUncompressedVector3(this BytesStack bytesStack, Vector3 vector3) { //Reverse order when writing to stack //Always use little-endian so compression and decompression are machines independent bytesStack.PushFloat(vector3.z); bytesStack.PushFloat(vector3.y); bytesStack.PushFloat(vector3.x); }
/// <summary> /// Copy constructor /// </summary> /// <param name="origin">Origin distributed message that will be copied</param> public DistributedMessage(DistributedMessage origin) { OriginPool = origin.OriginPool; Sender = origin.Sender; AddressKey = origin.AddressKey; Content = new BytesStack(origin.Content); Type = origin.Type; ServerTimestamp = origin.ServerTimestamp; Timestamp = origin.Timestamp; TimeTicksDifference = origin.TimeTicksDifference; }
/// <summary> /// Decompress position from the integers in the buffer, required bytes count: <see cref="PositionRequiredBytes"/> /// </summary> /// <param name="bytesStack">Buffer where position is pushed</param> /// <returns>Decompressed position</returns> public static Vector3 PopDecompressedPosition(this BytesStack bytesStack) { //Always use little-endian so compression and decompression are machines independent var intX = bytesStack.PopInt(PositionXRequiredBytes); var intY = bytesStack.PopInt(PositionYRequiredBytes); var intZ = bytesStack.PopInt(PositionZRequiredBytes); return(BitConverter.IsLittleEndian ? new Vector3(DecompressPositionX(intX), DecompressPositionY(intY), DecompressPositionZ(intZ)) : new Vector3(DecompressPositionX(SwapEndianness(intX)), DecompressPositionY(SwapEndianness(intY)), DecompressPositionZ(SwapEndianness(intZ)))); }
/// <summary> /// Decompress vector3 from the integers in the buffer /// </summary> /// <param name="bytesStack">Buffer where vector3 is pushed</param> /// <param name="minElementValue">Minimal value of the encoded vector3</param> /// <param name="maxElementValue">Maximal value of the encoded vector3</param> /// <param name="bytesPerElement">Bytes count that will be used per each element</param> /// <returns>Decompressed vector3</returns> public static Vector3 PopDecompressedVector3(this BytesStack bytesStack, float minElementValue, float maxElementValue, int bytesPerElement) { //Always use little-endian so compression and decompression are machines independent var intX = bytesStack.PopInt(bytesPerElement); var intY = bytesStack.PopInt(bytesPerElement); var intZ = bytesStack.PopInt(bytesPerElement); return(BitConverter.IsLittleEndian ? new Vector3(DecompressFloatFromInt(intX, minElementValue, maxElementValue, bytesPerElement), DecompressFloatFromInt(intY, minElementValue, maxElementValue, bytesPerElement), DecompressFloatFromInt(intZ, minElementValue, maxElementValue, bytesPerElement)) : new Vector3( DecompressFloatFromInt(SwapEndianness(intX), minElementValue, maxElementValue, bytesPerElement), DecompressFloatFromInt(SwapEndianness(intY), minElementValue, maxElementValue, bytesPerElement), DecompressFloatFromInt(SwapEndianness(intZ), minElementValue, maxElementValue, bytesPerElement))); }
/// <summary> /// Decompress color from the integers in the buffer /// </summary> /// <param name="bytesStack">Buffer where color is pushed</param> /// <param name="bytesPerElement">Bytes count that will be used per each element</param> /// <returns>Decompressed vector3</returns> public static Color PopDecompressedColor(this BytesStack bytesStack, int bytesPerElement) { //Always use little-endian so compression and decompression are machines independent var intR = bytesStack.PopInt(bytesPerElement); var intG = bytesStack.PopInt(bytesPerElement); var intB = bytesStack.PopInt(bytesPerElement); var intA = bytesStack.PopInt(bytesPerElement); return(BitConverter.IsLittleEndian ? new Color(DecompressFloatFromInt(intR, 0.0f, 1.0f, bytesPerElement), DecompressFloatFromInt(intG, 0.0f, 1.0f, bytesPerElement), DecompressFloatFromInt(intB, 0.0f, 1.0f, bytesPerElement), DecompressFloatFromInt(intA, 0.0f, 1.0f, bytesPerElement)) : new Color( DecompressFloatFromInt(SwapEndianness(intR), 0.0f, 1.0f, bytesPerElement), DecompressFloatFromInt(SwapEndianness(intG), 0.0f, 1.0f, bytesPerElement), DecompressFloatFromInt(SwapEndianness(intB), 0.0f, 1.0f, bytesPerElement), DecompressFloatFromInt(SwapEndianness(intA), 0.0f, 1.0f, bytesPerElement))); }
/// <summary> /// Compress color to integers and push it to the buffer /// </summary> /// <param name="bytesStack">Buffer where color will be pushed</param> /// <param name="color">Color to be compressed and pushed</param> /// <param name="bytesPerElement">Bytes count that will be used per each element</param> public static void PushCompressedColor(this BytesStack bytesStack, Color color, int bytesPerElement) { //Reverse order when writing to stack //Always use little-endian so compression and decompression are machines independent var a = CompressFloatToInt(color.a, 0.0f, 1.0f, bytesPerElement); if (!BitConverter.IsLittleEndian) { a = SwapEndianness(a); } bytesStack.PushInt(a, bytesPerElement); var b = CompressFloatToInt(color.b, 0.0f, 1.0f, bytesPerElement); if (!BitConverter.IsLittleEndian) { b = SwapEndianness(b); } bytesStack.PushInt(b, bytesPerElement); var g = CompressFloatToInt(color.g, 0.0f, 1.0f, bytesPerElement); if (!BitConverter.IsLittleEndian) { g = SwapEndianness(g); } bytesStack.PushInt(g, bytesPerElement); var r = CompressFloatToInt(color.r, 0.0f, 1.0f, bytesPerElement); if (!BitConverter.IsLittleEndian) { r = SwapEndianness(r); } bytesStack.PushInt(r, bytesPerElement); }
/// <summary> /// Pops uncompressed vector3 from the buffer /// </summary> /// <param name="bytesStack">Buffer where vector3 is pushed</param> /// <returns>Decoded vector3</returns> public static Vector3 PopUncompressedVector3(this BytesStack bytesStack) { return(new Vector3(bytesStack.PopFloat(), bytesStack.PopFloat(), bytesStack.PopFloat())); }
/// <summary> /// Pops the enum value from the bytes stack /// </summary> /// <param name="bytesStack">Buffer where enum is pushed</param> /// <typeparam name="T">Enum type</typeparam> /// <returns>Enum value decompressed from the bytes stack</returns> public static T PopEnum <T>(this BytesStack bytesStack) where T : IComparable, IConvertible, IFormattable { var intValue = bytesStack.PopInt(RequiredBytes <T>()); return((T)Enum.ToObject(typeof(T), intValue)); }
/// <summary> /// Pushes the enum integer value with minimum required bytes /// </summary> /// <param name="bytesStack">Buffer where enum is pushed</param> /// <param name="intValue">Enum value casted to integer</param> /// <typeparam name="T">Enum type</typeparam> public static void PushEnum <T>(this BytesStack bytesStack, int intValue) where T : IComparable, IConvertible, IFormattable { bytesStack.PushInt(intValue, RequiredBytes <T>()); }
/// <summary> /// Constructor /// </summary> /// <param name="addressKey">Address key defining where message should be passed</param> /// <param name="content">The content of message</param> /// <param name="distributedMessageType">Type defining how message should be delivered</param> public DistributedMessage(string addressKey, BytesStack content, DistributedMessageType distributedMessageType) { AddressKey = addressKey; Content = content; Type = distributedMessageType; }
/// <summary> /// Constructor /// </summary> /// <param name="content">The content of message</param> public DistributedMessage(BytesStack content) { Content = content; }