Beispiel #1
0
        /// <summary>
        /// Resets stream for reading.
        /// </summary>
        public void ResetRead(IntPtr buffer, int bitLength, bool copy)
        {
            if (copy)
            {
                int byteLen       = MyLibraryUtils.GetDivisionCeil(bitLength, 8);
                int allocByteSize = Math.Max(byteLen, m_defaultByteSize);

                if (m_ownedBuffer == null)
                {
                    m_ownedBuffer          = (ulong *)(void *)SharpDX.Utilities.AllocateClearedMemory(allocByteSize);
                    m_ownedBufferBitLength = allocByteSize * 8;
                }
                else if (m_ownedBufferBitLength < bitLength)
                {
                    SharpDX.Utilities.FreeMemory((IntPtr)(void *)m_ownedBuffer);
                    m_ownedBuffer          = (ulong *)(void *)SharpDX.Utilities.AllocateClearedMemory(allocByteSize);
                    m_ownedBufferBitLength = allocByteSize * 8;
                }
                SharpDX.Utilities.CopyMemory((IntPtr)(void *)m_ownedBuffer, buffer, byteLen);

                m_buffer      = m_ownedBuffer;
                m_bitLength   = bitLength;
                m_bitPosition = 0;
                m_writing     = false;
            }
            else
            {
                m_buffer      = (ulong *)(void *)buffer;
                m_bitLength   = bitLength;
                m_bitPosition = 0;
                m_writing     = false;
            }
        }
        private void WritePart(ref int bitsToSend, StreamClientData clientData, byte packetId, ref VRage.Library.Collections.BitStream stream)
        {
            bitsToSend = Math.Min(m_streamSize, clientData.RemainingBits);
            StreamPartInfo info = new StreamPartInfo();

            info.StartIndex = clientData.LastPosition;
            info.NumBits    = bitsToSend;

            clientData.LastPosition          = info.StartIndex + MyLibraryUtils.GetDivisionCeil(m_streamSize, 8);
            clientData.SendPackets[packetId] = info;
            clientData.RemainingBits         = Math.Max(0, clientData.RemainingBits - m_streamSize);

            stream.WriteInt16(clientData.NumParts);
            stream.WriteInt16(clientData.CurrentPart);
            info.Position = clientData.CurrentPart;

            clientData.CurrentPart++;

            stream.WriteInt32(bitsToSend);

            unsafe
            {
                fixed(byte *dataPtr = &clientData.ObjectData[info.StartIndex])
                {
                    stream.WriteMemory(dataPtr, bitsToSend);
                }
            }
        }
        private void ProcessWrite(int maxBitPosition, ref VRage.Library.Collections.BitStream stream, EndpointId forClient, byte packetId)
        {
            m_streamSize = MyLibraryUtils.GetDivisionCeil(maxBitPosition - stream.BitPosition - HEADER_SIZE - SAFE_VALUE, 8) * 8;
            StreamClientData clientData = m_clientStreamData[forClient.Value];

            if (clientData.FailedIncompletePackets.Count > 0)
            {
                stream.WriteBool(true);
                WriteIncompletePacket(clientData, packetId, ref stream);
                return;
            }

            int  bitsToSend = 0;
            bool incomplete = false;

            if (clientData.ObjectData == null)
            {
                SaveReplicable(clientData);
            }
            else
            {
                incomplete = true;
            }

            clientData.NumParts = (short)MyLibraryUtils.GetDivisionCeil(clientData.ObjectData.Length * 8, m_streamSize);

            bitsToSend = clientData.RemainingBits;

            if (bitsToSend == 0)
            {
                clientData.ForceSend = false;
                clientData.Dirty     = false;

                stream.WriteBool(false);
                return;
            }

            stream.WriteBool(true);
            stream.WriteInt32(clientData.UncompressedSize);

            if (bitsToSend > m_streamSize || incomplete)
            {
                WritePart(ref bitsToSend, clientData, packetId, ref stream);
            }
            else
            {
                WriteWhole(bitsToSend, clientData, packetId, ref stream);
            }

            if (clientData.RemainingBits == 0)
            {
                clientData.Dirty     = false;
                clientData.ForceSend = false;
            }
        }
Beispiel #4
0
        void CreateFromString(string s)
        {
            if (!Enum.TryParse <T>(s, out EnumType))
            {
                EnumType = (T)Enum.ToObject(typeof(T), MyLibraryUtils.GetHash(s));
            }

#if SUPPORT_STRING_SAVE
            EnumString = s;
#endif
            m_enumInt = ((IConvertible)EnumType).ToInt32(System.Globalization.CultureInfo.InvariantCulture);
        }
Beispiel #5
0
        /// <summary>
        /// Resets stream for writing.
        /// Uses internal buffer for writing, it's available as DataPointer.
        /// </summary>
        public void ResetWrite()
        {
            if (m_ownedBuffer == null)
            {
                m_ownedBuffer          = (ulong *)(void *)SharpDX.Utilities.AllocateClearedMemory(m_defaultByteSize);
                m_ownedBufferBitLength = m_defaultByteSize * 8;
            }
            else
            {
                SharpDX.Utilities.ClearMemory((IntPtr)(void *)m_ownedBuffer, 0, MyLibraryUtils.GetDivisionCeil(m_ownedBufferBitLength, 8));
            }

            m_buffer      = m_ownedBuffer;
            m_bitLength   = m_ownedBufferBitLength;
            m_bitPosition = 0;
            m_writing     = true;
        }
        private bool ReadPart(ref VRage.Library.Collections.BitStream stream)
        {
            m_numPartsToRecive = stream.ReadInt16();
            short currentPacket  = stream.ReadInt16();
            int   bitsToRecieve  = stream.ReadInt32();
            int   bytesToRecieve = MyLibraryUtils.GetDivisionCeil(bitsToRecieve, 8);

            int numBitsInStream = stream.BitLength - stream.BitPosition;

            if (numBitsInStream < bitsToRecieve)
            {
                Debug.Fail("trying to read more than there is in stream. Why ?");
                MyLog.Default.WriteLine("trying to read more than there is in stream. Total num parts : " + m_numPartsToRecive.ToString() + " current part : " + currentPacket.ToString() + " bits to read : " + bitsToRecieve.ToString() + " bits in stream : " + numBitsInStream.ToString() + " replicable : " + Instance.ToString());
                //what now ?
                return(false);
            }

            if (m_recivedParts == null)
            {
                m_recivedParts = new SortedList <StreamPartInfo, byte[]>();
            }


            m_recievedBytes += bytesToRecieve;
            byte[] partData = new byte[bytesToRecieve];

            unsafe
            {
                fixed(byte *dataPtr = partData)
                {
                    stream.ReadMemory(dataPtr, bitsToRecieve);
                }
            }

            StreamPartInfo info = new StreamPartInfo();

            info.NumBits         = bitsToRecieve;
            info.StartIndex      = currentPacket;
            m_recivedParts[info] = partData;

            return(true);
        }
Beispiel #7
0
        public void ResetWrite(BitStream stream)
        {
            int bitLength = stream.m_writing ? stream.m_bitPosition : stream.BitLength;

            if (m_ownedBuffer != null && m_ownedBufferBitLength < bitLength)
            {
                SharpDX.Utilities.FreeMemory((IntPtr)(void *)m_ownedBuffer);
                m_ownedBuffer = null;
            }
            if (m_ownedBuffer == null)
            {
                int byteSize = Math.Max(MyLibraryUtils.GetDivisionCeil(bitLength, 64) * 8, m_defaultByteSize);
                m_ownedBuffer          = (ulong *)(void *)SharpDX.Utilities.AllocateClearedMemory(byteSize);
                m_ownedBufferBitLength = byteSize * 8;
            }
            m_buffer    = m_ownedBuffer;
            m_bitLength = m_ownedBufferBitLength;
            SharpDX.Utilities.CopyMemory(DataPointer, stream.DataPointer, MyLibraryUtils.GetDivisionCeil(bitLength, 8));
            m_bitPosition = bitLength;
            m_writing     = true;
        }
Beispiel #8
0
        void Resize(int bitSize)
        {
            if (!OwnsBuffer)
            {
                throw new BitStreamException("BitStream cannot write more data. Buffer is full and it's not owned by BitStream", new System.IO.EndOfStreamException());
            }

            // Always at least double the size
            int newBitSize = Math.Max(m_bitLength * 2, bitSize);
            int newByteLen = MyLibraryUtils.GetDivisionCeil(newBitSize, 64) * 8;

            var newBuffer = SharpDX.Utilities.AllocateClearedMemory(newByteLen);

            SharpDX.Utilities.CopyMemory(newBuffer, (IntPtr)(void *)m_buffer, BytePosition);
            SharpDX.Utilities.FreeMemory((IntPtr)(void *)m_buffer);

            m_buffer    = (ulong *)(void *)newBuffer;
            m_bitLength = newBitSize;

            m_ownedBuffer          = m_buffer;
            m_ownedBufferBitLength = m_bitLength;
        }
        public void AddEdgeInfo(ref Vector3 point0, ref Vector3 point1, ref Vector3 normal0, ref Vector3 normal1, Color color, MySlimBlock owner)
        {
            var      hash      = CalculateEdgeHash(point0, point1);
            var      pos       = (point0 + point1) * 0.5f;
            Vector3I direction = Vector3I.Round((point0 - point1) / m_gridRender.GridSize);

            MyEdgeInfo info = new MyEdgeInfo(ref pos, ref direction, ref normal0, ref normal1, ref color, MyLibraryUtils.GetHash(owner.BlockDefinition.EdgeType));
            var        cell = GetCell(pos);

            if (cell.AddEdgeInfo(hash, info, owner))
            {
                m_dirtyCells.Add(cell);
            }
        }
 /// <summary>
 /// Writes uniform-spaced float within -1,1 range with specified number of bits.
 /// </summary>
 public void WriteNormalizedSignedFloat(float value, int bits)
 {
     WriteUInt32(MyLibraryUtils.NormalizeFloatCenter(value, -1, 1, bits), bits);
 }
Beispiel #11
0
 /// <summary>
 /// Reads uniform-spaced float within -1,1 range with specified number of bits.
 /// </summary>
 public float ReadNormalizedSignedFloat(int bits)
 {
     return(MyLibraryUtils.DenormalizeFloatCenter(ReadUInt32(bits), -1, 1, bits));
 }
Beispiel #12
0
 public BitStream(int defaultByteSize = 1536)
 {
     // At least 16 bytes
     m_defaultByteSize = Math.Max(16, MyLibraryUtils.GetDivisionCeil(defaultByteSize, 8) * 8);
 }
Beispiel #13
0
 public BlitSerializer()
 {
     MyLibraryUtils.ThrowNonBlittable <T>();
 }