Ejemplo n.º 1
0
        public override void ReadPacket()
        {
            ReadHeaders(Packet);

            // Read the byte flag representing which packets
            // are included in this update
            var dataPacketIdFlag = Packet.ReadUShort();
            // Keep track of value of current bit
            var currentTypeValue = 1;

            for (var i = 0; i < Enum.GetNames(typeof(ClientPacketId)).Length; i++)
            {
                // If this bit was set in our flag, we add the type to the list
                if ((dataPacketIdFlag & currentTypeValue) != 0)
                {
                    DataPacketIds.Add((ClientPacketId)i);
                }

                // Increase the value of current bit
                currentTypeValue *= 2;
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerConnect))
            {
                PlayerConnect.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerDisconnect))
            {
                PlayerDisconnect.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerEnterScene))
            {
                PlayerEnterScene.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerAlreadyInScene))
            {
                PlayerAlreadyInScene.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerLeaveScene))
            {
                PlayerLeaveScene.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerUpdate))
            {
                PlayerUpdates.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.EntityUpdate))
            {
                EntityUpdates.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerDeath))
            {
                PlayerDeath.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerTeamUpdate))
            {
                PlayerTeamUpdate.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerSkinUpdate))
            {
                PlayerSkinUpdate.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerEmoteUpdate))
            {
                PlayerEmoteUpdate.ReadData(Packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.GameSettingsUpdated))
            {
                GameSettingsUpdate.ReadData(Packet);
            }
        }
Ejemplo n.º 2
0
        public override Packet CreatePacket()
        {
            var packet = new Packet();

            WriteHeaders(packet);

            // Construct the ushort flag representing which packets are included
            // in this update, we need a ushort since we have more than 8 possible packet IDs
            ushort dataPacketIdFlag = 0;
            // Keep track of value of current bit
            ushort currentTypeValue = 1;

            for (var i = 0; i < Enum.GetNames(typeof(ClientPacketId)).Length; i++)
            {
                // Cast the current index of the loop to a ClientPacketId and check if it is
                // contained in the update type list, if so, we add the current bit to the flag
                if (DataPacketIds.Contains((ClientPacketId)i))
                {
                    dataPacketIdFlag |= currentTypeValue;
                }

                currentTypeValue *= 2;
            }

            packet.Write(dataPacketIdFlag);

            if (DataPacketIds.Contains(ClientPacketId.PlayerConnect))
            {
                PlayerConnect.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerDisconnect))
            {
                PlayerDisconnect.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerEnterScene))
            {
                PlayerEnterScene.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerAlreadyInScene))
            {
                PlayerAlreadyInScene.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerLeaveScene))
            {
                PlayerLeaveScene.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerUpdate))
            {
                PlayerUpdates.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.EntityUpdate))
            {
                EntityUpdates.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerDeath))
            {
                PlayerDeath.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerTeamUpdate))
            {
                PlayerTeamUpdate.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerSkinUpdate))
            {
                PlayerSkinUpdate.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.PlayerEmoteUpdate))
            {
                PlayerEmoteUpdate.WriteData(packet);
            }

            if (DataPacketIds.Contains(ClientPacketId.GameSettingsUpdated))
            {
                GameSettingsUpdate.WriteData(packet);
            }

            _containsReliableData = DataPacketIds.Contains(ClientPacketId.PlayerConnect) ||
                                    DataPacketIds.Contains(ClientPacketId.PlayerDisconnect) ||
                                    DataPacketIds.Contains(ClientPacketId.PlayerEnterScene) ||
                                    DataPacketIds.Contains(ClientPacketId.PlayerAlreadyInScene) ||
                                    DataPacketIds.Contains(ClientPacketId.PlayerLeaveScene) ||
                                    DataPacketIds.Contains(ClientPacketId.PlayerDeath) ||
                                    DataPacketIds.Contains(ClientPacketId.PlayerTeamUpdate) ||
                                    DataPacketIds.Contains(ClientPacketId.PlayerSkinUpdate) ||
                                    DataPacketIds.Contains(ClientPacketId.GameSettingsUpdated);

            packet.WriteLength();

            return(packet);
        }