Example #1
0
        public unsafe void Serialize(ref byte[] buffer, ref int offset, T[] value)
        {
            int count = value.Length;

            // Ensure capacity
            int size       = _itemSize;
            int neededSize = (count * size) + 5;

            SerializerBinary.EnsureCapacity(ref buffer, offset, neededSize);

            // Count
            SerializerBinary.WriteUInt32NoCheck(buffer, ref offset, (uint)count);

            int bytes = count * size;

            if (bytes == 0)
            {
                return;

                // Write
                fixed(T *srcAr = &value[0])
                fixed(byte *destAr = &buffer[0])
                {
                    byte *src  = (byte *)srcAr;
                    byte *dest = destAr + offset;

                    SerializerBinary.FastCopy(src, dest, (uint)bytes);
                }


                offset += bytes;
        }
Example #2
0
            public unsafe void Serialize(ref byte[] buffer, ref int offset, Vector3 value)
            {
                const int v3Size = 3 * 4;

                SerializerBinary.EnsureCapacity(ref buffer, offset, v3Size);

                var bufferLocal = buffer;

                fixed(byte *pBuffer = buffer)
                {
                    var ptr = (float *)(pBuffer + offset);

                    *ptr = value.X;
                }

                fixed(byte *pBuffer = buffer)
                {
                    var ptr = (float *)(pBuffer + offset + 4);

                    *ptr = value.Y;
                }

                fixed(byte *pBuffer = buffer)
                {
                    var ptr = (float *)(pBuffer + offset + 8);

                    *ptr = value.Z;
                }

                offset += v3Size;
            }
Example #3
0
        public unsafe void Serialize(ref byte[] buffer, ref int offset, T[] value)
        {
            // Ensure capacity
            int size       = _size;
            int neededSize = size + 5;

            SerializerBinary.EnsureCapacity(ref buffer, offset, neededSize);

            // Count
            int count = value.Length;

            SerializerBinary.WriteUInt32NoCheck(buffer, ref offset, (uint)count);

            int bytes = count * size;

            if (bytes == 0)
            {
                return;

                // Write
                fixed(T *p = value)
                Marshal.Copy(new IntPtr(p), buffer, offset, count);

                offset += bytes;
        }
Example #4
0
        public void Serialize(ref byte[] buffer, ref int offset, T value)
        {
            SerializerBinary.EnsureCapacity(ref buffer, offset, _size);

            Write_Raw(buffer, offset, ref value);

            offset += _size;
        }
Example #5
0
        public void Serialize(ref byte[] buffer, ref int offset, T value)
        {
            SerializerBinary.EnsureCapacity(ref buffer, offset, Unsafe.SizeOf <T>());

            Write_Raw(buffer, offset, ref value);

            offset += Unsafe.SizeOf <T>();
        }
Example #6
0
            public void Serialize(ref byte[] buffer, ref int offset, Vector3 value)
            {
                const int v3Size = 3 * 4;

                SerializerBinary.EnsureCapacity(ref buffer, offset, v3Size);

                SerializerBinary.WriteFloat32FixedNoCheck(ref buffer, ref offset, value.X);
                SerializerBinary.WriteFloat32FixedNoCheck(ref buffer, ref offset, value.Y);
                SerializerBinary.WriteFloat32FixedNoCheck(ref buffer, ref offset, value.Z);
            }
Example #7
0
        public void Serialize(ref byte[] buffer, ref int offset, Ulid value)
        {
            // Ensures that we have enough space to cram in an Ulid
            SerializerBinary.EnsureCapacity(ref buffer, offset, 16);
            var byteSpan = buffer.AsSpan(offset, 16);

            value.TryWriteBytes(byteSpan);
            // Move 128 bits further
            offset += 16;
        }
Example #8
0
            public void Serialize(ref byte[] buffer, ref int offset, decimal value)
            {
                SerializerBinary.EnsureCapacity(ref buffer, offset, 16);
                fixed(byte *dst = &buffer[offset])
                {
                    var src = &value;

                    *(decimal *)(dst) = *src;

                    offset += 16;
                }
            }
Example #9
0
        public void Serialize(ref byte[] buffer, ref int offset, T value)
        {
            SerializerBinary.EnsureCapacity(ref buffer, offset, _size);

            fixed(byte *pBuffer = buffer)
            {
                var ptr = (T *)(pBuffer + offset);

                *ptr = value;
            }

            offset += _size;
        }
Example #10
0
        public void Serialize(ref byte[] buffer, ref int offset, Bitmap img)
        {
            // Let the image serialize itself to the memory stream
            // Its unfortunate that there's only a stream-based api...
            // The alternative would be manually locking the bits.
            // That would be easy, but we'd potentially lose some information (animation frames etc?)

            // Prepare buffer stream
            if (_sharedMemoryStream == null)
            {
                _sharedMemoryStream = new MemoryStream(200 * 1024);
            }

            var stream = _sharedMemoryStream;

            // Encode image into stream
            stream.Position = 0;
            var format = BitmapModeToImgFormat(BitmapMode);

            img.Save(stream, format);

            long sizeLong = stream.Length;

            if (sizeLong > int.MaxValue)
            {
                throw new InvalidOperationException("image too large");
            }
            int size = (int)sizeLong;

            stream.Position = 0;
            var streamBuffer = stream.GetBuffer();

            // Write Size
            SerializerBinary.WriteUInt32Fixed(ref buffer, ref offset, (uint)size);

            // Write stream data to serialization buffer
            if (size > 0)
            {
                SerializerBinary.EnsureCapacity(ref buffer, offset, size);
                SerializerBinary.FastCopy(streamBuffer, 0, buffer, (uint)offset, (uint)size);
            }

            offset += size;
        }
Example #11
0
        public void Serialize(ref byte[] buffer, ref int offset, Bitmap img)
        {
            // Let the image serialize itself to the memory stream
            // Its unfortunate that there's only a stream-based api...
            // The alternative would be manually locking the bits.
            // That would be easy, but we'd potentially lose some information (animation frames etc?)

            var mode   = BitmapMode;
            var format = BitmapModeToImgFormat(mode);

            if (_sharedMemoryStream == null)
            {
                _sharedMemoryStream = new MemoryStream((int)(4 * (img.Width * img.Height) * 1.35));
            }
            var ms = _sharedMemoryStream;

            ms.Position = 0;
            img.Save(ms, format);

            long sizeLong = ms.Position;

            if (sizeLong > int.MaxValue)
            {
                throw new InvalidOperationException("image too large");
            }
            int size = (int)sizeLong;

            ms.Position = 0;
            var memoryStreamBuffer = ms.GetBuffer();

            // Write Size
            SerializerBinary.WriteUInt32Fixed(ref buffer, ref offset, (uint)size);

            // Write data into serialization buffer
            if (size > 0)
            {
                SerializerBinary.EnsureCapacity(ref buffer, offset, size);
                SerializerBinary.FastCopy(memoryStreamBuffer, 0, buffer, offset, size);
            }

            offset += size;
        }
Example #12
0
        public void Serialize(ref byte[] buffer, ref int offset, byte[] ar)
        {
            if (ar == null)
            {
                SerializerBinary.WriteUInt32Bias(ref buffer, ref offset, -1, 1);
                return;
            }

            var len = ar.Length;

            // Ensure we have enough space for the worst-case VarInt plus the byte array itself
            SerializerBinary.EnsureCapacity(ref buffer, offset, 5 + len);

            // Write the length, no need to check the capacity (we did that here)
            SerializerBinary.WriteUInt32BiasNoCheck(ref buffer, ref offset, len, 1);

            // Blit the array
            System.Array.Copy(ar, 0, buffer, offset, len);
            offset += len;
        }
Example #13
0
        public unsafe void Serialize(ref byte[] buffer, ref int offset, T[] value)
        {
            // Ensure capacity
            int size       = _size;
            int neededSize = size + 5;

            SerializerBinary.EnsureCapacity(ref buffer, offset, neededSize);

            // Count
            int count = value.Length;

            SerializerBinary.WriteUInt32NoCheck(buffer, ref offset, (uint)count);

            int bytes = count * size;

            if (bytes == 0)
            {
                return;
            }

            // Write
#if NETSTANDARD2_0 || NET47
            fixed(T *srcAr = &value[0])
            fixed(byte *dest = &buffer[offset])
            {
                byte *srcByteAr = (byte *)srcAr;

                Buffer.MemoryCopy(srcByteAr, dest, bytes, bytes);
            }
#else
            fixed(T *p = &value[0])
            Marshal.Copy(new IntPtr(p), buffer, offset, count);
#endif

            offset += bytes;
        }