public void Serialize(BitStream stream, Type baseType, ref Type obj)
        {
            if (stream.Reading)
            {
                var id = new TypeId(stream.ReadUInt32());

                obj = MyMultiplayer.Static.ReplicationLayer.GetType(id);
            }
            else
            {
                var id = MyMultiplayer.Static.ReplicationLayer.GetTypeId(obj);
                stream.WriteUInt32(id);
            }
        }
        private void ReadShared(BitStream stream, MyNetworkClient sender, out MyEntity controlledEntity)
        {
            controlledEntity = null;

            ClientTimeStamp = stream.ReadUInt32();
            var hasControlledEntity = stream.ReadBool();
            if (!hasControlledEntity)
            {
                Vector3D pos = Vector3D.Zero;
                stream.Serialize(ref pos); // 24B
                Position = pos;
            }
            else
            {
                var entityId = stream.ReadInt64();
                MyEntity entity;
                if (!MyEntities.TryGetEntityById(entityId, out entity))
                    return;

                Position = entity.WorldMatrix.Translation;

                // TODO: Obsolete check?
                MySyncEntity syncEntity = entity.SyncObject as MySyncEntity;
                if (syncEntity == null)
                    return;
                controlledEntity = entity;
            }
        }
        private void ReadInventory(BitStream stream)
        {
            if(stream.ReadBool() == false)       
            {
                return;
            }

            if(m_recievedPacketIds == null)
            {
                m_recievedPacketIds = new MyQueue<uint>(RECIEVED_PACKET_HISTORY);
            }

            uint packetId = stream.ReadUInt32();

            bool apply = true;
            if (m_recievedPacketIds.Count == RECIEVED_PACKET_HISTORY)
            {
                m_recievedPacketIds.Dequeue();
            }

            if (m_recievedPacketIds.InternalArray.Contains(packetId) == false)
            {
                m_recievedPacketIds.Enqueue(packetId);
            }
            else
            {
                apply = false;
            }

            bool hasItems = stream.ReadBool();
            if(hasItems)
            {
                int numItems = stream.ReadInt32();

                for (int i = 0; i < numItems; ++i)
                {
                    uint itemId = stream.ReadUInt32();
                    MyFixedPoint amout = new MyFixedPoint();
                    amout.RawValue = stream.ReadInt64();

                    if (apply)
                    {
                        Inventory.UpdateItemAmoutClient(itemId, amout);
                    }
                }
            }

            hasItems = stream.ReadBool();
            if (hasItems)
            {
                int numItems = stream.ReadInt32();
                for (int i = 0; i < numItems; ++i)
                {
                    uint itemId = stream.ReadUInt32();
                    if (apply)
                    {
                        Inventory.RemoveItemClient(itemId);
                    }
                }
            }

            hasItems = stream.ReadBool();
            if (hasItems)
            {
                int numItems = stream.ReadInt32();

                for (int i = 0; i < numItems; ++i)
                {
                    int position = stream.ReadInt32();
                    MyPhysicalInventoryItem item;
                    VRage.Serialization.MySerializer.CreateAndRead(stream, out item, MyObjectBuilderSerializer.Dynamic);
                    if (apply)
                    {
                        Inventory.AddItemClient(position, item);
                    }
                }
            }

            hasItems = stream.ReadBool();
            if (hasItems)
            {
                int numItems = stream.ReadInt32();

                for (int i = 0; i < numItems; ++i)
                {
                    int position = stream.ReadInt32();
                    int newPosition = stream.ReadInt32();
                    if (apply)
                    {
                        Inventory.SwapItemClient(position, newPosition);
                    }
                }
            }

            Inventory.Refresh();
        }
        public static Quaternion Read(BitStream stream)
        {
            float x, y, z, w;

            // note: you're going to want to normalize the quaternion returned from this function
            uint largest = stream.ReadUInt32(2);
            float a = stream.ReadUInt32(bits) * inverse_scale * (maximum - minimum) + minimum;
            float b = stream.ReadUInt32(bits) * inverse_scale * (maximum - minimum) + minimum;
            float c = stream.ReadUInt32(bits) * inverse_scale * (maximum - minimum) + minimum;

            switch (largest)
            {
                case 0:
                    x = (float)Math.Sqrt(1 - a * a - b * b - c * c);
                    y = a;
                    z = b;
                    w = c;
                    break;

                case 1:
                    x = a;
                    y = (float)Math.Sqrt(1 - a * a - b * b - c * c);
                    z = b;
                    w = c;
                    break;

                case 2:
                    x = a;
                    y = b;
                    z = (float)Math.Sqrt(1 - a * a - b * b - c * c);
                    w = c;
                    break;

                case 3:
                    x = a;
                    y = b;
                    z = c;
                    w = (float)Math.Sqrt(1 - a * a - b * b - c * c);
                    break;

                default:
                    Debug.Fail("Error");
                    x = 0;
                    y = 0;
                    z = 0;
                    w = 1;
                    break;
            }
            return Quaternion.Normalize(new Quaternion(x, y, z, w));
        }