Beispiel #1
0
        public void Write(BitStream stream)
        {
            // TODO: this is suboptimal
            if (stream == null || m_writeData == null)
            {
                if (stream == null)
                {
                    Debug.Fail("BitReaderWriter - Write - stream is null");
                }
                if (m_writeData == null)
                {
                    Debug.Fail("BitReaderWriter - Write - m_writeData is null");
                }
                return;
            }

            // Store old bit position
            int pos = stream.BitPosition;

            // Write data
            m_writeData.Serialize(stream, false);

            // Measure data len
            int len = stream.BitPosition - pos;

            // Restore old position
            stream.SetBitPositionWrite(pos);

            // Write data len
            stream.WriteVariant((uint)len);

            // Write data again
            m_writeData.Serialize(stream, false);
        }
        public void Write(BitStream stream)
        {
            // TODO: this is suboptimal
            if (stream == null || m_writeData == null)
            {
                if (stream == null)
                    Debug.Fail("BitReaderWriter - Write - stream is null");
                if ( m_writeData == null)
                    Debug.Fail("BitReaderWriter - Write - m_writeData is null");
                return;
            }

            // Store old bit position
            int pos = stream.BitPosition;

            // Write data
            m_writeData.Serialize(stream, false);

            // Measure data len
            int len = stream.BitPosition - pos;

            // Restore old position
            stream.SetBitPositionWrite(pos);

            // Write data len
            stream.WriteVariant((uint)len);

            // Write data again
            m_writeData.Serialize(stream, false);
        }
Beispiel #3
0
        public void Write(BitStream stream)
        {
            // TODO: this is suboptimal

            // Store old bit position
            int pos = stream.BitPosition;

            // Write data
            m_writeData.Serialize(stream, false);

            // Measure data len
            int len = stream.BitPosition - pos;

            // Restore old position
            stream.SetBitPositionWrite(pos);

            // Write data len
            stream.WriteVariant((uint)len);

            // Write data again
            m_writeData.Serialize(stream, false);
        }
        public void Write(BitStream stream)
        {
            // TODO: this is suboptimal

            // Store old bit position
            int pos = stream.BitPosition;

            // Write data
            m_writeData.Serialize(stream, false);

            // Measure data len
            int len = stream.BitPosition - pos;

            // Restore old position
            stream.SetBitPositionWrite(pos);

            // Write data len
            stream.WriteVariant((uint)len);

            // Write data again
            m_writeData.Serialize(stream, false);
        }
        public override bool Serialize(BitStream stream, EndpointId forClient,uint timestamp, byte packetId, int maxBitPosition)
        {
            // Client does not care about slave grids, he always synced group through controlled object
            Debug.Assert(stream.Reading || !Sync.IsServer || Entity == GetMasterGrid(Entity), "Writing data from SlaveGrid!");

            bool apply = !IsControlledLocally;

            bool moving = false;
            if (stream.Writing)
            {
                moving = IsMoving(Entity);
                stream.WriteBool(moving);
            }
            else
            {
                moving = stream.ReadBool();
            }


            // Serialize this grid
            apply = SerializeTransform(stream, Entity, null, m_lowPrecisionOrientation, apply,moving, timestamp, m_positionValidation, MoveHandler);
            SerializeVelocities(stream, Entity, EffectiveSimulationRatio, apply, moving,VelocityHandler);

     
            // Serialize other grids in group
            Vector3D basePos = Entity.WorldMatrix.Translation;
            if (stream.Writing)
            {
                bool fullyWritten = true;
                UpdateGridMaxSpeed(Entity, Sync.IsServer);
                var g = MyCubeGridGroups.Static.PhysicalDynamic.GetGroup(Entity);
                if (g == null)
                {
                    stream.WriteBool(false);
                }
                else
                {
                    m_groups.Clear();
                    int i = 0;
                    foreach (var node in g.Nodes)
                    {                        
                        i++;
                        if (ResponsibleForUpdate(node.NodeData, forClient))
                        {
                            continue;
                        } 

                        if(i < m_currentSentPosition)
                        {
                            continue;
                        }


                        var target = MyMultiplayer.Static.ReplicationLayer.GetProxyTarget((IMyEventProxy)node.NodeData);
                        
                        int pos = stream.BitPosition;

                        if (node.NodeData != Entity && !node.NodeData.IsStatic && target != null)
                        {                 
                            stream.WriteBool(true);
                            // ~26.5 bytes per grid, not bad
                            NetworkId networkId = MyMultiplayer.Static.ReplicationLayer.GetNetworkIdByObject(target);
                            stream.WriteNetworkId(networkId); // ~2 bytes
             
                            moving = IsMoving(node.NodeData);
                            stream.WriteBool(moving);

                            SerializeTransform(stream, node.NodeData, basePos, m_lowPrecisionOrientation, apply, moving, timestamp, null, null); // 12.5 bytes
                            SerializeVelocities(stream, node.NodeData, EffectiveSimulationRatio, apply, moving); // 12 byte
                            UpdateGridMaxSpeed(node.NodeData, Sync.IsServer);
                            m_groups.Add(node.NodeData);

                            m_currentSentPosition++;
                        }

                        if (stream.BitPosition > maxBitPosition)
                        {
                            stream.SetBitPositionWrite(pos);
                            fullyWritten = false;
                            m_currentSentPosition--;
                            break;
                        }

                        if (i == g.Nodes.Count)
                        {
                            m_currentSentPosition = 0;
                        }
                    }

                    stream.WriteBool(false);
                }

                stream.WriteBool(fullyWritten);

                if (fullyWritten)
                {
                    SerializeRopeData(stream, apply, gridsGroup: m_groups);
                }
                return fullyWritten;

            }
            else
            {
                UpdateGridMaxSpeed(Entity, !Sync.IsServer);

                while (stream.ReadBool())
                {               
                    NetworkId networkId = stream.ReadNetworkId(); // ~2 bytes
                    MyCubeGridReplicable replicable = MyMultiplayer.Static.ReplicationLayer.GetObjectByNetworkId(networkId) as MyCubeGridReplicable;
                    MyCubeGrid grid = replicable != null ? replicable.Grid : null;

                    moving = stream.ReadBool();
                    SerializeTransform(stream, grid, basePos, m_lowPrecisionOrientation, apply && grid != null, moving, timestamp, null, null); // 12.5 bytes
                    SerializeVelocities(stream, grid, EffectiveSimulationRatio, apply && grid != null, moving); // 12 bytes
                   
                    UpdateGridMaxSpeed(grid,!Sync.IsServer);
                }

                if (stream.ReadBool())
                {
                    SerializeRopeData(stream, apply);
                }
            }
            return true;
        }
        public static  bool WriteSubgrids(MyCubeGrid masterGrid, BitStream stream, ref EndpointId forClient, uint timestamp, int maxBitPosition, bool lowPrecisionOrientation,ref Vector3D basePos, ref int currentSentPosition)
        {
            bool fullyWritten = true;
            var g = MyCubeGridGroups.Static.PhysicalDynamic.GetGroup(masterGrid);
            if (g == null)
            {
                stream.WriteBool(false);
            }
            else
            {
                m_groups.Clear();
                int i = 0;
                foreach (var node in g.Nodes)
                {
                    i++;

                    if (i < currentSentPosition)
                    {
                        continue;
                    }


                    var target = MyMultiplayer.Static.ReplicationLayer.GetProxyTarget((IMyEventProxy)node.NodeData);

                    int pos = stream.BitPosition;

                    if (node.NodeData != masterGrid && node.NodeData.Physics != null && !node.NodeData.Physics.IsStatic && target != null)
                    {
                        stream.WriteBool(true);
                        // ~26.5 bytes per grid, not bad
                        NetworkId networkId = MyMultiplayer.Static.ReplicationLayer.GetNetworkIdByObject(target);
                        stream.WriteNetworkId(networkId); // ~2 bytes

                        bool moving = IsMovingSubGrid(node.NodeData);
                        stream.WriteBool(moving);

                        SerializeTransform(stream, node.NodeData, basePos, lowPrecisionOrientation, false, moving, timestamp, null, null); // 12.5 bytes
                        SerializeVelocities(stream, node.NodeData, EffectiveSimulationRatio, false, moving); // 12 byte
                        UpdateGridMaxSpeed(node.NodeData, Sync.IsServer);
                        m_groups.Add(node.NodeData);

                        currentSentPosition++;
                    }

                    if (stream.BitPosition > maxBitPosition)
                    {
                        stream.SetBitPositionWrite(pos);
                        fullyWritten = false;
                        currentSentPosition--;
                        break;
                    }

                    if (i == g.Nodes.Count)
                    {
                        currentSentPosition = 0;
                    }
                }

                stream.WriteBool(false);
            }
            return fullyWritten;
        }
        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 InventoryPartInfo WriteInventory(ref InventoryPartInfo packetInfo, BitStream stream, byte packetId, int maxBitPosition, bool resend = false)
        {
            if (m_items == null)
            {
                m_items = new List<MyPhysicalInventoryItem>();
            }

            Console.WriteLine(String.Format("sending: {0}, {1}", packetId, Inventory.Owner.ToString()));
            InventoryPartInfo sendPacketInfo = new InventoryPartInfo();
            sendPacketInfo.AllItemsSend = true;

            List<MyPhysicalInventoryItem> items = Inventory.GetItems();
            int numItems = items.Count;

            int sentItems = packetInfo.StartItemIndex + (resend ? 0 : packetInfo.NumItems);
            sendPacketInfo.StartItemIndex = sentItems;

            bool startNewTransfer = (resend == false) && sentItems == 0;
            VRage.Serialization.MySerializer.Write(stream, ref startNewTransfer);
            int startStreamPosition = stream.BitPosition;
            VRage.Serialization.MySerializer.Write(stream, ref numItems);

            int maxNumItems = (resend ? packetInfo.NumItems : numItems);
            for (; sentItems < numItems; ++sentItems)
            {
                MyPhysicalInventoryItem item = items[sentItems];
                m_items.Add(item);
                VRage.Serialization.MySerializer.Write(stream, ref item, MyObjectBuilderSerializer.Dynamic);
                if (stream.BitPosition > maxBitPosition)
                {
                    m_items.RemoveAt(m_items.Count - 1);
                    break;
                }
                else if (sentItems >= maxNumItems)
                {
                    break;
                }

            }

            if (m_items.Count < numItems)
            {
                sendPacketInfo.AllItemsSend = false;
                stream.SetBitPositionWrite(startStreamPosition);
                numItems = m_items.Count;
                VRage.Serialization.MySerializer.Write(stream, ref numItems);

                for (int i = 0; i < m_items.Count; ++i)
                {
                    MyPhysicalInventoryItem item = m_items[i];
                    VRage.Serialization.MySerializer.Write(stream, ref item, MyObjectBuilderSerializer.Dynamic);
                }
            }

            VRage.Serialization.MySerializer.Write(stream, ref sendPacketInfo.StartItemIndex);
            sendPacketInfo.NumItems = m_items.Count;
            m_items.Clear();
            return sendPacketInfo;
        }