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);
            }
        }
 /// <summary>
 /// Shared area for SE and ME. So far it writes whether you have a controlled entity or not. In the latter case you get the spectator position
 /// </summary>
 /// <param name="stream"></param>
 /// <param name="validControlledEntity"></param>
 private void WriteShared(BitStream stream, MyEntity controlledEntity)
 {
     stream.WriteUInt32(ClientTimeStamp);
     stream.WriteBool(controlledEntity != null);
     if (controlledEntity == null)
     {
         Vector3D pos = MySpectatorCameraController.Static.Position;
         stream.Serialize(ref pos);
     }
     else
     {
         stream.WriteInt64(controlledEntity.EntityId);
     }
 }
        private InventoryDeltaInformation PrepareSendData(ref InventoryDeltaInformation packetInfo, BitStream stream, int maxBitPosition,out bool needsSplit)
        {
            needsSplit = false;
            int startStreamPosition = stream.BitPosition;

            InventoryDeltaInformation sentData = new InventoryDeltaInformation();

            sentData.HasChanges = false;
            stream.WriteBool(false);
            stream.WriteUInt32(packetInfo.MessageId);
            stream.WriteBool(packetInfo.ChangedItems != null);

            if (packetInfo.ChangedItems != null)
            {
                stream.WriteInt32(packetInfo.ChangedItems.Count);
                if (stream.BitPosition > maxBitPosition)
                {
                    needsSplit = true;
                }
                else
                {
                    sentData.ChangedItems = new Dictionary<uint, MyFixedPoint>();
                    foreach (var item in packetInfo.ChangedItems)
                    {
                        stream.WriteUInt32(item.Key);
                        stream.WriteInt64(item.Value.RawValue);

                        if (stream.BitPosition <= maxBitPosition)
                        {
                            sentData.ChangedItems[item.Key] = item.Value;
                            sentData.HasChanges = true;
                        }
                        else
                        {
                            needsSplit = true;
                        }
                    }
                }
            }

            stream.WriteBool(packetInfo.RemovedItems != null);
            if (packetInfo.RemovedItems != null)
            {
                stream.WriteInt32(packetInfo.RemovedItems.Count);
                if (stream.BitPosition > maxBitPosition)
                {
                    needsSplit = true;
                }
                else
                {
                    sentData.RemovedItems = new List<uint>();
                    foreach (var item in packetInfo.RemovedItems)
                    {
                        stream.WriteUInt32(item);

                        if (stream.BitPosition <= maxBitPosition)
                        {
                            sentData.RemovedItems.Add(item);
                            sentData.HasChanges = true;
                        }
                        else
                        {
                            needsSplit = true;
                        }
                    }
                }
            }

            stream.WriteBool(packetInfo.NewItems != null);
            if (packetInfo.NewItems != null)
            {
                stream.WriteInt32(packetInfo.NewItems.Count);
                if (stream.BitPosition > maxBitPosition)
                {
                    needsSplit = true;
                }
                else
                {
                    sentData.NewItems = new SortedDictionary<int, MyPhysicalInventoryItem>();

                    foreach (var item in  packetInfo.NewItems)
                    {
                        MyPhysicalInventoryItem inventoryItem = item.Value;
                        VRage.Serialization.MySerializer.Write(stream, ref inventoryItem, MyObjectBuilderSerializer.Dynamic);

                        if (stream.BitPosition <= maxBitPosition)
                        {
                            sentData.NewItems[item.Key] = inventoryItem;
                            sentData.HasChanges = true;
                        }
                        else
                        {
                            needsSplit = true;
                        }
                    }
                }
            }

            stream.WriteBool(packetInfo.SwappedItems != null);
            if (packetInfo.SwappedItems != null)
            {
                stream.WriteInt32(packetInfo.SwappedItems.Count);
                if (stream.BitPosition > maxBitPosition)
                {
                    needsSplit = true;
                }
                else
                {
                    sentData.SwappedItems = new Dictionary<int, int>();

                    foreach (var item in packetInfo.SwappedItems)
                    {
                        stream.WriteInt32(item.Key);
                        stream.WriteInt32(item.Value);

                        if (stream.BitPosition <= maxBitPosition)
                        {
                            sentData.SwappedItems[item.Key] = item.Value;
                            sentData.HasChanges = true;
                        }
                        else
                        {
                            needsSplit = true;
                        }
                    }
                }
            }
            stream.SetBitPositionWrite(startStreamPosition);
            return sentData;
        }
        private InventoryDeltaInformation WriteInventory(ref InventoryDeltaInformation packetInfo, BitStream stream, byte packetId, int maxBitPosition, out bool needsSplit)
        {
            InventoryDeltaInformation sendPacketInfo = PrepareSendData(ref packetInfo, stream, maxBitPosition, out needsSplit);
            if (sendPacketInfo.HasChanges == false)
            {
                stream.WriteBool(false);
                return sendPacketInfo;
            }

            sendPacketInfo.MessageId = packetInfo.MessageId;

            stream.WriteBool(true);
            stream.WriteUInt32(sendPacketInfo.MessageId);
            stream.WriteBool(sendPacketInfo.ChangedItems != null);
            if (sendPacketInfo.ChangedItems != null)
            {
                stream.WriteInt32(sendPacketInfo.ChangedItems.Count);
                foreach (var item in sendPacketInfo.ChangedItems)
                {
                    stream.WriteUInt32(item.Key);
                    stream.WriteInt64(item.Value.RawValue);
                }
            }

            stream.WriteBool(sendPacketInfo.RemovedItems != null);
            if (sendPacketInfo.RemovedItems != null)
            {
                stream.WriteInt32(sendPacketInfo.RemovedItems.Count);
                foreach (var item in sendPacketInfo.RemovedItems)
                {
                    stream.WriteUInt32(item);
                }
            }

            stream.WriteBool(sendPacketInfo.NewItems != null);
            if (sendPacketInfo.NewItems != null)
            {
                stream.WriteInt32(sendPacketInfo.NewItems.Count);
                foreach (var item in sendPacketInfo.NewItems)
                {
                    stream.WriteInt32(item.Key);
                    MyPhysicalInventoryItem itemTosend = item.Value;
                    VRage.Serialization.MySerializer.Write(stream, ref itemTosend, MyObjectBuilderSerializer.Dynamic);
                }
            }

            stream.WriteBool(sendPacketInfo.SwappedItems != null);
            if (sendPacketInfo.SwappedItems != null)
            {
                stream.WriteInt32(sendPacketInfo.SwappedItems.Count);
                foreach (var item in sendPacketInfo.SwappedItems)
                {
                    stream.WriteInt32(item.Key);
                    stream.WriteInt32(item.Value);
                }
            }

            return sendPacketInfo;
        }
        public static void Write(BitStream stream, Quaternion q)
        {
            float x = q.X;
            float y = q.Y;
            float z = q.Z;
            float w = q.W;

            float abs_x = Math.Abs(x);
            float abs_y = Math.Abs(y);
            float abs_z = Math.Abs(z);
            float abs_w = Math.Abs(w);

            uint largest = 0;
            float largest_value = abs_x;

            if (abs_y > largest_value)
            {
                largest = 1;
                largest_value = abs_y;
            }

            if (abs_z > largest_value)
            {
                largest = 2;
                largest_value = abs_z;
            }

            if (abs_w > largest_value)
            {
                largest = 3;
                largest_value = abs_w;
            }

            float a = 0;
            float b = 0;
            float c = 0;

            switch (largest)
            {
                case 0:
                    if (x >= 0)
                    {
                        a = y;
                        b = z;
                        c = w;
                    }
                    else
                    {
                        a = -y;
                        b = -z;
                        c = -w;
                    }
                    break;

                case 1:
                    if (y >= 0)
                    {
                        a = x;
                        b = z;
                        c = w;
                    }
                    else
                    {
                        a = -x;
                        b = -z;
                        c = -w;
                    }
                    break;

                case 2:
                    if (z >= 0)
                    {
                        a = x;
                        b = y;
                        c = w;
                    }
                    else
                    {
                        a = -x;
                        b = -y;
                        c = -w;
                    }
                    break;

                case 3:
                    if (w >= 0)
                    {
                        a = x;
                        b = y;
                        c = z;
                    }
                    else
                    {
                        a = -x;
                        b = -y;
                        c = -z;
                    }
                    break;

                default:
                    Debug.Fail("Error");
                    break;
            }

            float normal_a = (a - minimum) / (maximum - minimum);
            float normal_b = (b - minimum) / (maximum - minimum);
            float normal_c = (c - minimum) / (maximum - minimum);

            stream.WriteUInt32(largest, 2);
            stream.WriteUInt32((uint)Math.Floor(normal_a * scale + 0.5f), bits);
            stream.WriteUInt32((uint)Math.Floor(normal_b * scale + 0.5f), bits);
            stream.WriteUInt32((uint)Math.Floor(normal_c * scale + 0.5f), bits);
        }