Esempio n. 1
0
 public override void Encode(MinecraftStream stream)
 {
     stream.WriteVarInt(SharedSecret.Length);
     stream.Write(SharedSecret);
     stream.WriteVarInt(VerifyToken.Length);
     stream.Write(VerifyToken);
 }
Esempio n. 2
0
 public State WriteTo(MinecraftStream stream, State state)
 {
     stream.Write(WindowID);
     stream.Write(NumberOfSlots);
     stream.Write(EntityID);
     return(state);
 }
Esempio n. 3
0
 public override void Encode(MinecraftStream stream)
 {
     stream.WriteString(ServerId);
     stream.WriteVarInt(PublicKey.Length);
     stream.Write(PublicKey);
     stream.WriteVarInt(VerifyToken.Length);
     stream.Write(VerifyToken);
 }
Esempio n. 4
0
        public void SendPacket(Packet packet)
        {
            if (packet.PacketId == -1)
            {
                throw new Exception();
            }

            byte[] encodedPacket;
            using (MemoryStream ms = new MemoryStream())
            {
                using (MinecraftStream mc = new MinecraftStream(ms))
                {
                    mc.WriteVarInt(packet.PacketId);
                    packet.Encode(mc);

                    encodedPacket = ms.ToArray();

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

                    if (CompressionEnabled)
                    {
                        if (encodedPacket.Length >= CompressionThreshold)
                        {
                            byte[] compressed;
                            CompressData(encodedPacket, out compressed);

                            mc.WriteVarInt(encodedPacket.Length);
                            mc.Write(compressed);
                        }
                        else                         //Uncompressed
                        {
                            mc.WriteVarInt(0);
                            mc.Write(encodedPacket);
                        }

                        encodedPacket = ms.ToArray();
                    }
                }
            }

            PacketWriteQueue.Add(encodedPacket);

            /*using (MemoryStream ms = new MemoryStream())
             * {
             *      using (MinecraftStream mc = new MinecraftStream(ms))
             *      {
             *              if (EncryptionInitiated)
             *              {
             *                      mc.InitEncryption(SharedSecret);
             *              }
             *
             *                  mc.WriteVarInt(encodedPacket.Length);
             *                  mc.Write(encodedPacket);
             *          }
             *          SendDataInternal(ms.ToArray());
             * }*/
        }
Esempio n. 5
0
        public void WriteTo(MinecraftStream stream)
        {
            if (Cache != null)
            {
                stream.Write(Cache);
                return;
            }

            byte[] sectionData;
            int    sectionBitmask = 0;

            using (MemoryStream ms = new MemoryStream())
            {
                using (MinecraftStream mc = new MinecraftStream(ms))
                {
                    for (int i = 0; i < Sections.Length; i++)
                    {
                        ChunkSection section = Sections[i];
                        if (section.IsAllAir)
                        {
                            continue;
                        }

                        sectionBitmask |= 1 << i;

                        section.WriteTo(mc, true);
                    }
                }
                sectionData = ms.ToArray();
            }

            using (MemoryStream ms = new MemoryStream())
            {
                using (MinecraftStream mc = new MinecraftStream(ms))
                {
                    mc.WriteInt(Coordinates.X);
                    mc.WriteInt(Coordinates.Z);

                    mc.WriteBool(true);

                    mc.WriteVarInt(sectionBitmask);                     //Primary Bitmask

                    mc.WriteVarInt(sectionData.Length + 256);
                    mc.Write(sectionData, 0, sectionData.Length);
                    mc.Write(Biomes);

                    mc.WriteVarInt(0);                     //No block entities for now
                }

                Cache = ms.ToArray();
            }

            stream.Write(Cache);
        }
Esempio n. 6
0
 public State WriteTo(MinecraftStream stream, State state)
 {
     stream.Write(X);
     stream.Write(Y);
     stream.Write(Z);
     stream.Write(Yaw);
     stream.Write(Pitch);
     stream.WriteByte(Flags);
     stream.WriteVarInt(TeleportId);
     return(state);
 }
Esempio n. 7
0
        private void SendQueue()
        {
            using (NetworkStream ms = new NetworkStream(Socket))
            {
                using (MinecraftStream mc = new MinecraftStream(ms))
                {
                    _sendStream = mc;
                    while (!CancellationToken.IsCancellationRequested)
                    {
                        try
                        {
                            EnqueuedPacket packet = PacketWriteQueue.Take(CancellationToken.Token);
                            var            data   = EncodePacket(packet);

                            mc.WriteVarInt(data.Length);
                            mc.Write(data);
                        }
                        catch (EndOfStreamException) { }
                        catch (OperationCanceledException)
                        {
                            break;
                        }
                    }
                }
            }
        }
Esempio n. 8
0
        private void SendQueue()
        {
            using NetworkStream ms   = new NetworkStream(Socket);
            using MinecraftStream mc = new MinecraftStream(ms);

            _sendStream = mc;
            while (!CancellationToken.IsCancellationRequested)
            {
                try
                {
                    if (PacketWriteQueue.TryTake(out var packet, 3500))
                    {
                        //    Log.Info($"Sent: {packet.Packet}");
                        var data = EncodePacket(packet);

                        Interlocked.Increment(ref PacketsOut);
                        Interlocked.Add(ref PacketSizeOut, data.Length);

                        mc.WriteVarInt(data.Length);
                        mc.Write(data);
                    }
                    else
                    {
                        //   Log.Warn($"No data sent in the last 3.5 seconds... :/");
                    }
                    //  EnqueuedPacket packet = PacketWriteQueue.Take(CancellationToken.Token);
                }
                catch (EndOfStreamException) { }
                catch (OperationCanceledException)
                {
                    break;
                }
            }
        }
Esempio n. 9
0
        public void WriteTo(MinecraftStream stream, bool writeSkylight = true)
        {
            long[] types = Types.Backing;

            stream.WriteByte(13);             //Bits Per Block
            stream.WriteVarInt(0);            //Palette Length

            stream.WriteVarInt(types.Length);
            for (int i = 0; i < types.Length; i++)
            {
                stream.WriteLong(types[i]);
            }

            stream.Write(BlockLight.Data);

            if (writeSkylight)
            {
                stream.Write(SkyLight.Data);
            }
        }
Esempio n. 10
0
        private byte[] EncodePacket(EnqueuedPacket enqueued)
        {
            var packet = enqueued.Packet;

            byte[] encodedPacket;
            using (MemoryStream ms = new MemoryStream())
            {
                using (MinecraftStream mc = new MinecraftStream(ms))
                {
                    mc.WriteVarInt(packet.PacketId);
                    packet.Encode(mc);

                    encodedPacket = ms.ToArray();

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

                    if (enqueued.CompressionEnabled)
                    {
                        if (encodedPacket.Length >= CompressionThreshold)
                        {
                            byte[] compressed;
                            CompressData(encodedPacket, out compressed);

                            mc.WriteVarInt(encodedPacket.Length);
                            mc.Write(compressed);
                        }
                        else                     //Uncompressed
                        {
                            mc.WriteVarInt(0);
                            mc.Write(encodedPacket);
                        }

                        encodedPacket = ms.ToArray();
                    }
                }
            }

            return(encodedPacket);
        }
Esempio n. 11
0
 public override void Encode(MinecraftStream stream)
 {
     stream.WriteInt(ChunkX);
     stream.WriteInt(ChunkZ);
     stream.WriteBool(GroundUp);
     stream.WriteVarInt(PrimaryBitmask);
     stream.WriteVarInt(Buffer.Length);
     stream.Write(Buffer, 0, Buffer.Length);
     stream.WriteVarInt(TileEntities.Count);
     foreach (var tileEntity in TileEntities)
     {
         stream.WriteNbtCompound(tileEntity);
     }
 }
Esempio n. 12
0
 public State WriteTo(MinecraftStream stream, State state)
 {
     stream.Write(EntityId);
     stream.Write(Gamemode);
     stream.Write(Dimension);
     stream.Write(HashedSeed);
     stream.Write(MaxPlayers);
     stream.WriteString(LevelType);
     stream.WriteVarInt(ViewDistance);
     stream.Write(ReducedDebugInfo);
     stream.Write(EnableRespawnScreen);
     return(state);
 }
Esempio n. 13
0
        public MinecraftPacket(MinecraftClient client, Stream stream)
        {
            Client = client;
            Data   = new MinecraftStream((RecyclableMemoryStream)RMSManager.Get().GetStream(GetType().FullName));

            Length = Client.CompressionEnabled ? (int)stream.Length : new VarInt(stream).Value;

            long _pos = stream.Position;

            Id = new VarInt(stream).Value;

            byte[] data = new byte[Length - (stream.Position - _pos)];
            stream.Read(data, 0, data.Length);

            Data.Write(data);
            Data.Seek(0, SeekOrigin.Begin);
        }
Esempio n. 14
0
        private byte[] EncodePacket(EnqueuedPacket enqueued)
        {
            var packet = enqueued.Packet;

            byte[] encodedPacket;
            using (MemoryStream ms = new MemoryStream())
            {
                using (MinecraftStream mc = new MinecraftStream(ms))
                {
                    mc.WriteVarInt(packet.PacketId);
                    packet.Encode(mc);

                    encodedPacket = ms.ToArray();

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

                    if (enqueued.CompressionEnabled)
                    {
                        if (encodedPacket.Length >= CompressionThreshold)
                        {
                            //byte[] compressed;
                            //CompressData(encodedPacket, out compressed);

                            mc.WriteVarInt(encodedPacket.Length);
                            using (ZlibStream outZStream = new ZlibStream(mc, CompressionMode.Compress, CompressionLevel.Default, true))
                            {
                                outZStream.Write(encodedPacket, 0, encodedPacket.Length);
                            }
                            // mc.Write(compressed);
                        }
                        else                     //Uncompressed
                        {
                            mc.WriteVarInt(0);
                            mc.Write(encodedPacket);
                        }

                        encodedPacket = ms.ToArray();
                    }
                }
            }

            return(encodedPacket);
        }
Esempio n. 15
0
        public override void Write(MinecraftStream stream)
        {
            base.Write(stream);

            stream.WriteString(Name);
            stream.WriteVarInt(Properties.Count);

            foreach (var properties in Properties)
            {
                stream.Write(properties.ToArray());
            }

            stream.WriteVarInt(Gamemode);
            stream.WriteVarInt(Ping);
            stream.WriteBoolean(HasDisplayName);
            if (HasDisplayName)
            {
                stream.WriteChat(DisplayName);
            }
        }
Esempio n. 16
0
 public override void Encode(MinecraftStream stream)
 {
     stream.WriteString(Channel);
     stream.WriteVarInt(Data.Length);
     stream.Write(Data, 0, Data.Length);
 }
Esempio n. 17
0
 public override void Encode(MinecraftStream stream)
 {
     stream.Write(Data);
 }
Esempio n. 18
0
 public State WriteTo(MinecraftStream stream, State state)
 {
     stream.Write(Reason);
     stream.Write(Value);
     return(state);
 }
Esempio n. 19
0
 public State WriteTo(MinecraftStream stream, State state)
 {
     stream.Write(Threshold);
     return(state);
 }
Esempio n. 20
0
 public State WriteTo(MinecraftStream stream, State state)
 {
     stream.Write(KeepAliveID);
     return(state);
 }
Esempio n. 21
0
 public readonly State WriteTo(MinecraftStream stream, State state)
 {
     stream.Write(Time);
     return(state);
 }
Esempio n. 22
0
 public override void Encode(MinecraftStream stream)
 {
     stream.WriteVarInt(MessageId);
     stream.WriteString(Channel);
     stream.Write(Data);
 }
Esempio n. 23
0
 public override void Encode(MinecraftStream stream)
 {
     stream.WriteVarInt(MessageId);
     stream.WriteBool(Succesful);
     stream.Write(Data);
 }
Esempio n. 24
0
 public State WriteTo(MinecraftStream stream, State state)
 {
     stream.Write(X);
     stream.Write(Z);
     return(state);
 }
Esempio n. 25
0
 public State WriteTo(MinecraftStream stream, State state)
 {
     stream.Write(Position);
     return(state);
 }