protected override void ReadInternal(BitStream stream, MyNetworkClient sender, MyEntity controlledEntity)
 {
     Context = (MyContextKind)stream.ReadInt32(2);
     if (Context != MyContextKind.None)
     {
         long entityId = stream.ReadInt64();
         ContextEntity = MyEntities.GetEntityByIdOrDefault(entityId);
     }
     else
     {
         ContextEntity = null;
     }
 }
 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;
 }
Example #3
0
        void Read(BitStream stream)
        {
            // TODO: Read additional client data, context

            MyNetworkClient sender;
            if (!Sync.Clients.TryGetClient(EndpointId.Value, out sender))
            {
                Debug.Fail("Unknown sender");
                return;
            }

            var hasControlledEntity = stream.ReadBool();
            if (hasControlledEntity == false)
            {
                Vector3D pos = Vector3D.Zero;
                stream.Serialize(ref pos); // 24B
                Position = pos;
            }
            else
            {
                int numEntity = 0;
                if (stream.BytePosition < stream.ByteLength)
                {
                    var entityId = stream.ReadInt64();
                    MyEntity entity;
                    if (!MyEntities.TryGetEntityById(entityId, out entity))
                        return;

                    MySyncEntity syncEntity = entity.SyncObject as MySyncEntity;
                    if (syncEntity == null)
                        return;

                    Context = (MyContextKind)stream.ReadInt32(2);

                    switch (Context)
                    {
                        case MyContextKind.Inventory:
                            entityId = stream.ReadInt64();
                            break;
                        case MyContextKind.Terminal:
                            entityId = stream.ReadInt64();
                            break;
                        case MyContextKind.Production:
                            entityId = stream.ReadInt64();
                            break;
                        default:
                            entityId = stream.ReadInt64();
                            break;
                    }

                    MyEntities.TryGetEntityById(entityId, out entity);
                    ContextEntity = entity;

                    if (!syncEntity.ResponsibleForUpdate(sender))
                    {
                        // Also happens when entering cockpit due to order of operations and responsibility update change
                        //Debug.Fail("Server sending entity update for entity controlled by client, should happen only very rarely (packets out-of-order)");
                        return;
                    }
                    syncEntity.SerializePhysics(stream, sender);

                    if (numEntity == 0)
                    {
                        Position = syncEntity.Entity.WorldMatrix.Translation;
                    }
                    numEntity++;
                }
            }
        }
        private void ReadShared(BitStream stream, MyNetworkClient sender, out MyEntity controlledEntity)
        {
            controlledEntity = null;

            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;
            }
        }
        protected void ReadPlanetSectors(BitStream stream)
        {
            KnownSectors.Clear();

            if (stream.ReadInt32() != PlanetMagic)
            {
                throw new BitStreamException("Wrong magic when reading planet sectors from client state.");
            }

            int count = stream.ReadInt32();

            for (int i = 0; i < count; i++)
            {
                long p = stream.ReadInt64();
                Debug.Assert(p != ~0);

                HashSet<long> sectids = new HashSet<long>();

                KnownSectors.Add(p, sectids);

                while (true)
                {
                    long sectorid = stream.ReadInt64();
                    if (sectorid == ~0) break;

                    sectids.Add(sectorid);
                }
            }
        }
        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();
        }
        /// <summary>
        /// Serializes physics and takes into account support (what's entity standing on)
        /// </summary>
        private void SerializePhysicsWithSupport(BitStream stream, EndpointId forClient,uint timestamp, byte packetId, int maxBitPosition)
        {
            if (stream.Writing)
            {
                // TODO: only prototype implementation
                SetSupport(FindSupportDelegate());

                stream.WriteBool(m_supportPhysics != null);
                if (m_supportPhysics != null)
                {
                    stream.WriteInt64(m_supportPhysics.Entity.EntityId);
                    var localToParent = Entity.WorldMatrix * MatrixD.Invert(m_supportPhysics.Entity.PositionComp.WorldMatrix);
                    stream.Write(localToParent.Translation);
                    stream.Write(Quaternion.CreateFromForwardUp(localToParent.Forward, localToParent.Up).ToVector4());
                    bool moving = IsMoving(Entity);
                    stream.WriteBool(moving);

                    SerializeVelocities(stream, Entity, EffectiveSimulationRatio, !IsControlledLocally, moving);
                }
                else
                {
                    base.Serialize(stream, forClient,timestamp, packetId, maxBitPosition);
                }
            }
            else
            {
                if (stream.ReadBool())
                {
                    MyEntity support;
                    bool apply = MyEntities.TryGetEntityById(stream.ReadInt64(), out support) && !IsControlledLocally;

                    var pos = stream.ReadVector3D();
                    var orient = Quaternion.FromVector4(stream.ReadVector4());

                    if (apply)
                    {
                        var old = Entity.PositionComp.WorldMatrix;

                        MatrixD localToParent = MatrixD.CreateFromQuaternion(orient);
                        localToParent.Translation = pos;
                        MatrixD matrix = localToParent * support.WorldMatrix;
                        Entity.PositionComp.SetWorldMatrix(matrix, null, true);

                        SetSupport(MySupportHelper.FindPhysics(support));

                        var handler = MoveHandler;
                        if (handler != null)
                        {
                            handler(ref old, ref matrix);
                        }
                    }
                    else
                    {
                        SetSupport(null);
                    }
                    bool moving = stream.ReadBool();
                    SerializeVelocities(stream, Entity, EffectiveSimulationRatio, apply, moving);

                }
                else
                {
                    SetSupport(null);
                    base.Serialize(stream, forClient, timestamp, packetId, maxBitPosition);
                }
            }
        }
Example #8
0
        private void ReadPhysics(BitStream stream, MyNetworkClient sender, MyEntity controlledEntity,uint serverTimeStamp)
        {
            
            bool hasPhysics = stream.ReadBool();

            m_currentServerTimeStamp = serverTimeStamp;

            if (hasPhysics && MyEntityPhysicsStateGroup.ResponsibleForUpdate(controlledEntity, new EndpointId(sender.SteamUserId)))
            {
                IMyStateGroup stateGroup = null;
                SupportId = null;
                bool enableControlOnServer = stream.ReadBool();
                bool stateGroupFound = stream.ReadBool();
                if (stateGroupFound == false)
                {
                    return;
                }

              
                if (stream.ReadBool())
                {
                    SupportId = stream.ReadInt64();
                }
        
                if (enableControlOnServer)
                {
                    stateGroup = MyExternalReplicable.FindByObject(controlledEntity).FindStateGroup<MyEntityPositionVerificationStateGroup>();
                }
                else
                {
                    stateGroup = MyExternalReplicable.FindByObject(controlledEntity).FindStateGroup<MyEntityPhysicsStateGroup>();
                }

                if (stream.ReadBool())
                {
                    stateGroup.Serialize(stream, new EndpointId(sender.SteamUserId), ClientTimeStamp, 0, 65535);
                }
            }
        }
        private void ReadPlanetSectors(BitStream stream)
        {
            if (stream.ReadInt32() != 0x42424242)
            {
                throw new BitStreamException("Wrong magic when reading planet sectors from client state.");
            }

            int count = stream.ReadInt32();

            for (int i = 0; i < count; i++)
            {
                long p = stream.ReadInt64();

                long sectorid;

                HashSet<MyPlanetSectorId> sectids = new HashSet<MyPlanetSectorId>();

                KnownSectors.Add(p, sectids);

                while(true)
                {
                    sectorid = stream.ReadInt64();
                    if (sectorid == -1) break;

                    MyPlanetSectorId id;
                    MyPlanetSectorId.Unpack64(sectorid, out id);
                    sectids.Add(id);
                }

            }
        }