Ejemplo n.º 1
0
        public byte[] ToArray()
        {
            using (MemoryStream temp = RMSManager.Get().GetStream()) {
                if (!Client.CompressionEnabled)
                {
                    VarInt varInt = new(Id);

                    if (Length <= 0)
                    {
                        varInt.Write(temp);
                        Data.BaseStream.WriteTo(temp);

                        Length = (int)temp.Length;

                        temp.Position = 0;
                        temp.SetLength(0);
                    }

                    varInt.Value = Length;
                    varInt.Write(temp);

                    varInt.Value = Id;
                    varInt.Write(temp);

                    Data.BaseStream.WriteTo(temp);
                }
                else
                {
                    byte[] output;
                    int    dataLength;

                    using (MemoryStream input = RMSManager.Get().GetStream()) {
                        new VarInt(Id).Write(input);
                        Data.BaseStream.WriteTo(input);

                        dataLength = (int)input.Position;

                        if (dataLength >= Nylium.Server.Configuration.CompressionThreshold)
                        {
                            CompressionUtils.ZLibCompress(input.ToArray(), out output);
                        }
                        else
                        {
                            dataLength = 0;
                            output     = input.ToArray();
                        }
                    }

                    new VarInt(dataLength).Write(temp);
                    int dataLengthLength = (int)temp.Position;

                    temp.Position = 0;
                    temp.SetLength(0);

                    new VarInt(output.Length + dataLengthLength).Write(temp);
                    new VarInt(dataLength).Write(temp);
                    temp.Write(output);
                }

                return(Client.EncryptionEnabled ? Client.Encryptor.ProcessBytes(temp.ToArray(), 0, (int)temp.Length) : temp.ToArray());
            }
        }