Beispiel #1
0
        void Write(BitStream stream)
        {
            // TODO: Make sure sleeping, server controlled entities are not moving locally (or they can be but eventually their position should be corrected)

            stream.WriteBool(MySession.ControlledEntity != null);
            if (MySession.ControlledEntity == null)
            {
                Vector3D pos = MySpectatorCameraController.Static.Position;
                stream.WriteDouble(pos.X);
                stream.WriteDouble(pos.Y);
                stream.WriteDouble(pos.Z);
            }
            else
            {
                var controlledEntity = MySession.ControlledEntity.Entity.GetTopMostParent();

                // Send controlled entity every other frame to server
                if (MyMultiplayer.Static.FrameCounter % 2 == 0)
                {
                    // TODO: Write additional client data, context

                    if (controlledEntity != null && controlledEntity.SyncFlag && ((MySyncEntity)controlledEntity.SyncObject).ResponsibleForUpdate(Sync.Clients.LocalClient))
                    {
                        stream.WriteInt64(controlledEntity.EntityId);
                        switch (MyGuiScreenTerminal.GetCurrentScreen())
                        {
                            case MyTerminalPageEnum.Inventory:
                                stream.WriteInt32((int)MyContextKind.Inventory, 2);
                                break;
                            case MyTerminalPageEnum.ControlPanel:
                                stream.WriteInt32((int)MyContextKind.Terminal, 2);
                                break;
                            case MyTerminalPageEnum.Production:
                                stream.WriteInt32((int)MyContextKind.Production, 2);
                                break;
                            default:
                                stream.WriteInt32((int)MyContextKind.None, 2);
                                break;
                        }

                        if (MyGuiScreenTerminal.InteractedEntity != null)
                        {
                            stream.WriteInt64(MyGuiScreenTerminal.InteractedEntity.EntityId);
                        }
                        else
                        {
                            stream.WriteInt64(0);
                        }

                        ((MySyncEntity)controlledEntity.SyncObject).SerializePhysics(stream, null);
                    }
                }
            }
        }
        protected override void WriteInternal(BitStream stream, MyEntity controlledEntity)
        {
            MyContextKind context = GetContextByPage(MyGuiScreenTerminal.GetCurrentScreen());

            stream.WriteInt32((int)context, 2);
            if (context != MyContextKind.None)
            {
                var entityId = MyGuiScreenTerminal.InteractedEntity != null ? MyGuiScreenTerminal.InteractedEntity.EntityId : 0;
                stream.WriteInt64(entityId);
            }
        }
        private void WritePlanetSectors(BitStream stream)
        {
            stream.WriteInt32(0x42424242);

            var planets = MyPlanets.GetPlanets();

            stream.WriteInt32(planets.Count);

            foreach (var planet in planets)
            {
                stream.WriteInt64(planet.EntityId);

                foreach (var sector in planet.EnvironmentSectors.Values)
                {
                    if (sector.HasPhysics || sector.ServerOwned)
                    {
                        stream.WriteInt64(sector.SectorId.Pack64());
                    }
                }

                // don't know how many in advance so I will use -1 termination instead of count.
                stream.WriteInt64(-1);
            }
        }
 public bool Serialize(BitStream stream, bool validate)
 {
     if (stream.Reading)
     {
         SenderUserId = stream.ReadInt64();
         NumElements = stream.ReadInt32();
         stream.ReadBytes(CompressedVoiceBuffer, 0,NumElements);
     }
     else
     {
         stream.WriteInt64(SenderUserId);
         stream.WriteInt32(NumElements);
         stream.WriteBytes(CompressedVoiceBuffer, 0, NumElements);
     }
     return true;
 }
        protected void WritePlanetSectors(BitStream stream)
        {
            stream.WriteInt32(PlanetMagic);

            var planets = MyPlanets.GetPlanets();

            // Planets are not enabled if session component is not loaded.
            if (planets == null)
            {
                stream.WriteInt32(0);
                return;
    }

            stream.WriteInt32(planets.Count);

            foreach (var planet in planets)
            {
                stream.WriteInt64(planet.EntityId);

                MyPlanetEnvironmentComponent env = planet.Components.Get<MyPlanetEnvironmentComponent>();

                var syncLod = env.EnvironmentDefinition.SyncLod;

                foreach (var provider in env.Providers)
                {
                    foreach (var sector in provider.LogicalSectors)
                        if (sector.MinLod <= syncLod)
                        {
                            stream.WriteInt64(sector.Id);
}
                }

                // don't know how many in advance so I will use ~0 termination instead of count.
                stream.WriteInt64(~0);
            }
        }
        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;
        }
Beispiel #8
0
        /// <summary>
        /// Serializes id to hash list.
        /// Server sends the hashlist to client, client reorders type table to same order as server.
        /// </summary>
        public void Serialize(BitStream stream)
        {
            if (stream.Writing)
            {
                stream.WriteVariant((uint)m_idToType.Count);
                for (int i = 0; i < m_idToType.Count; i++)
                {
                    stream.WriteInt32(m_idToType[i].TypeHash);
                }
            }
            else
            {
                int count = (int)stream.ReadUInt32Variant();

                Debug.Assert(m_idToType.Count == count, "Number of received types does not match number of registered types");

                m_staticEventTable = new MyEventTable(null);
                for (int i = 0; i < count; i++)
                {
                    int typeHash = stream.ReadInt32();
                    Debug.Assert(m_hashLookup.ContainsKey(typeHash), "Type hash not found!");
                    var type = m_hashLookup[typeHash];
                    m_idToType[i] = type;
                    m_staticEventTable.AddStaticEvents(type.Type);
                }
            }
        }