Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public override Packet CreatePacket()
        {
            var packet = new Packet();

            WriteHeaders(packet);

            // Construct the byte flag representing which packets are included
            // in this update
            ushort dataPacketIdFlag = 0;
            // Keep track of value of current bit
            ushort currentTypeValue = 1;

            /*
             * foreach (var item in DataPacketIds)
             * {
             *  Logger.Info(this,$"creating packet with {Enum.GetName(typeof(ServerPacketId), item)}");
             * }
             */

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

                currentTypeValue *= 2;
            }

            packet.Write(dataPacketIdFlag);

            // TODO: this is a mess, we have an interface that exposes a write and read method
            // to packets, but we don't really use the abstraction since we still need to
            // write and read in a specific order to ensure consistency
            // The same holds then for determining whether this packet contains reliable data
            // and finding a way to elegantly copy reliable data to a new packet

            if (DataPacketIds.Contains(ServerPacketId.HelloServer))
            {
                HelloServer.WriteData(packet);
            }

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

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

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

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

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

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

            // Check whether there is reliable data written in this packet
            // and set the boolean value accordingly
            _containsReliableData = DataPacketIds.Contains(ServerPacketId.HelloServer) ||
                                    DataPacketIds.Contains(ServerPacketId.PlayerEnterScene) ||
                                    DataPacketIds.Contains(ServerPacketId.PlayerLeaveScene) ||
                                    DataPacketIds.Contains(ServerPacketId.PlayerDeath) ||
                                    DataPacketIds.Contains(ServerPacketId.PlayerTeamUpdate) ||
                                    DataPacketIds.Contains(ServerPacketId.PlayerSkinUpdate);

            packet.WriteLength();

            return(packet);
        }