Beispiel #1
0
        public override FieldUpdate MakeFieldUpdate(params object[] parameters)
        {
            // Do we have more than one parameter?
            if (parameters.Length != 1)
            {
                throw new ToffeeException("Field update {0}::{1} requires 1 parameter, got {3}.",
                                          Owner.Name, Identifier, parameters.Length);
            }

            // Is the parameter the correct Type?
            object parameter = parameters[0];

            if (parameter.GetType() != PropertyInfo.GetType())
            {
                throw new ToffeeException("Invalid parameter type for field update {0}::{1}", Owner.Name, Identifier);
            }

            // Write the parameter to a temporary packet
            ToffeePacket packet = new ToffeePacket(Owner.Network);

            packet.Write(parameter);

            // Return the FieldUpdate
            return(new FieldUpdate
            {
                FieldId = FieldId,
                Parameters = packet.GetBytes()
            });
        }
Beispiel #2
0
        public override FieldUpdate MakeFieldUpdate(params object[] parameters)
        {
            // Do we have the correct number of parameters?
            if (parameters.Length != Parameters.Length)
            {
                throw new ToffeeException("Field update {0}::{1} requires {2} parameters, got {3}.",
                                          Owner.Name, Identifier, Parameters.Length, parameters.Length);
            }

            // Write the parameters to a temporary packet
            ToffeePacket packet = new ToffeePacket(Owner.Network);

            for (int i = 0; i < parameters.Length; i++)
            {
                // Is the parameter the correct Type?
                if (Parameters[i].GetType() != parameters[i].GetType())
                {
                    throw new ToffeeException("Invalid parameter type for field update {0}::{1}", Identifier, Identifier);
                }
                packet.Write(parameters[i]);
            }

            // Return the FieldUpdate
            return(new FieldUpdate
            {
                FieldId = FieldId,
                Parameters = packet.GetBytes()
            });
        }
Beispiel #3
0
        /// <summary>
        /// Build a final compressed, encrypted, and encoded packet.
        /// </summary>
        /// <returns>The packet data.</returns>
        public byte[] BuildPacket()
        {
            // Get the packet data
            byte[] data = Data.ToArray();

            // Compression
            bool compressed = false;

            if (DataLength > 100)
            {
                byte[] compressedData = CompressionService.Compress(data);
                if (compressedData.Length < data.Length)
                {
                    data       = compressedData;
                    compressed = true;
                }
            }
            ushort dataLength = (ushort)data.Length;

            // Create the packet header
            ClientPacketHeader header = new ClientPacketHeader
            {
                OpCode     = OpCode,
                Compressed = compressed,
                Length     = dataLength,
                Timestamp  = TimeUtil.GetUnixTimestamp()
            };

            LastHeader = header;

            // Append the header and CRC to the compressed data
            ToffeePacket packet = new ToffeePacket(Sender);

            packet.Write(header);
            packet.Write(data, true);
            packet.Write(CRC.CalculateCRC32(data));
            data = packet.GetBytes();

            // Encryption
            bool encrypted = ((Encrypted && Sender.UseEncryption) || (Sender.ForceEncryption));

            if ((encrypted) && (!Sender.UseEncryption))
            {
                Sender.Disconnect();
                return(new byte[0]);
            }
            if (encrypted)
            {
                data = Sender.Encryption.Encrypt(data);
            }

            // Create the final packet
            packet = new ToffeePacket(Sender);
            packet.Write(encrypted);
            packet.Write(data, true);
            return(packet.GetBytes());
        }
Beispiel #4
0
        /// <summary>
        /// Build a final compressed, encrypted, and encoded packet.
        /// </summary>
        /// <returns>The packet data.</returns>
        public byte[] BuildPacket()
        {
            // Get the packet data
            byte[] data = Data.ToArray();

            // Compression
            bool compressed = false;

            if (DataLength > 100)
            {
                byte[] compressedData = CompressionService.Compress(data);
                if (compressedData.Length < data.Length)
                {
                    data       = compressedData;
                    compressed = true;
                }
            }
            ushort dataLength = (ushort)data.Length;

            // Create the packet header
            InternalPacketHeader header = new InternalPacketHeader
            {
                OpCode     = OpCode,
                Compressed = compressed,
                Length     = dataLength
            };

            LastHeader = header;

            // Append the header and CRC to the compressed data
            ToffeePacket packet = new ToffeePacket(Sender);

            packet.Write(header);
            packet.Write(data, true);
            packet.Write(CRC.CalculateCRC32(data));
            return(packet.GetBytes());
        }