Exemple #1
0
        public override void Decode(MinecraftStream stream)
        {
            int sharedSecretLength = stream.ReadVarInt();

            SharedSecret = stream.Read(sharedSecretLength);
            int verifyTokenLength = stream.ReadVarInt();

            VerifyToken = stream.Read(verifyTokenLength);
        }
Exemple #2
0
        public override void Decode(MinecraftStream stream)
        {
            ServerId = stream.ReadString();
            int publicKeyLength = stream.ReadVarInt();

            PublicKey = stream.Read(publicKeyLength);
            int verifyTokenLength = stream.ReadVarInt();

            VerifyToken = stream.Read(verifyTokenLength);
        }
Exemple #3
0
        public override void Decode(MinecraftStream stream)
        {
            ChunkX   = stream.ReadInt();
            ChunkZ   = stream.ReadInt();
            GroundUp = stream.ReadBool();
            //IgnoreOldData = stream.ReadBool();
            PrimaryBitmask = stream.ReadVarInt();

            HeightMaps = stream.ReadNbtCompound();

            if (GroundUp)
            {
                int biomeCount = stream.ReadVarInt();

                int[] biomeIds = new int[biomeCount];
                for (int idx = 0; idx < biomeIds.Length; idx++)
                {
                    biomeIds[idx] = stream.ReadVarInt();
                }

                Biomes = biomeIds;
            }

            int i = stream.ReadVarInt();

            Buffer = new Memory <byte>(new byte[i]);
            stream.Read(Buffer.Span, Buffer.Length);

            int tileEntities = stream.ReadVarInt();

            for (int k = 0; k < tileEntities; k++)
            {
                TileEntities.Add(stream.ReadNbtCompound());
            }
        }
Exemple #4
0
        public override void Decode(MinecraftStream stream)
        {
            ChunkX         = stream.ReadInt();
            ChunkZ         = stream.ReadInt();
            GroundUp       = stream.ReadBool();
            PrimaryBitmask = stream.ReadVarInt();

            HeightMaps = stream.ReadNbtCompound();

            if (GroundUp)
            {
                int[] biomeIds = new int[1024];
                for (int idx = 0; idx < biomeIds.Length; idx++)
                {
                    biomeIds[idx] = stream.ReadInt();
                }
            }

            int i = stream.ReadVarInt();

            Buffer = new byte[i];
            stream.Read(Buffer, 0, Buffer.Length);

            int tileEntities = stream.ReadVarInt();

            for (int k = 0; k < tileEntities; k++)
            {
                TileEntities.Add(stream.ReadNbtCompound());
            }
        }
        public override void Decode(MinecraftStream stream)
        {
            Channel = stream.ReadString();
            var l = stream.ReadVarInt();

            Data = stream.Read(l);
        }
Exemple #6
0
        public override void Decode(MinecraftStream stream)
        {
            ChunkX              = stream.ReadVarInt();
            ChunkZ              = stream.ReadVarInt();
            SkyLightMask        = stream.ReadVarInt();
            BlockLightMask      = stream.ReadVarInt();
            EmptySkyLightMask   = stream.ReadVarInt();
            EmptyBlockLightMask = stream.ReadVarInt();

            //  List<byte[]> skyLightList = new List<byte[]>();
            SkyLightArrays = new byte[18][];
            for (int i = 0; i < 18; i++)
            {
                if (((SkyLightMask & 1) == 1) /*&& ((EmptySkyLightMask & 1) == 0)*/)
                {
                    byte[] data = stream.Read(2048);
                    SkyLightArrays[i] = data;
                }
                else
                {
                    SkyLightArrays[i] = null;
                }

                SkyLightMask      = SkyLightMask >> 1;
                EmptySkyLightMask = EmptySkyLightMask >> 1;
            }
            //SkyLightArrays = skyLightList.ToArray();

            // List<byte[]> blockLightList = new List<byte[]>();
            BlockLightArrays = new byte[18][];
            for (int i = 0; i < 18; i++)
            {
                if (((BlockLightMask & 1) == 1) /*&& ((EmptyBlockLightMask & 1) == 0)*/)
                {
                    byte[] data = stream.Read(2048);
                    BlockLightArrays[i] = data;
                }
                else
                {
                    BlockLightArrays[i] = null;
                }

                BlockLightMask      = BlockLightMask >> 1;
                EmptyBlockLightMask = EmptyBlockLightMask >> 1;
            }
            // BlockLightArrays = blockLightList.ToArray();
        }
Exemple #7
0
 public State ReadFrom(MinecraftStream stream, State state)
 {
     ProtocolVersion = stream.ReadVarInt();
     ServerHostname  = stream.ReadString();
     ServerPort      = stream.Read <ushort>();
     NextState       = (State)stream.ReadVarInt();
     return(NextState);
 }
        public override void Decode(MinecraftStream stream)
        {
            MessageId = stream.ReadVarInt();
            Channel   = stream.ReadString();

            long length = stream.Length - stream.Position;

            byte[] buffer = new byte[length];
            Data = stream.Read(buffer.Length);
        }
Exemple #9
0
 public State ReadFrom(MinecraftStream stream, State state)
 {
     Time = stream.Read <long>();
     return(state);
 }
Exemple #10
0
        private void ProcessNetwork()
        {
            int lastPacketId = 0;

            try
            {
                using (NetworkStream ns = new NetworkStream(Socket))
                {
                    using (MinecraftStream ms = new MinecraftStream(ns))
                    {
                        _readerStream = ms;

                        while (!CancellationToken.IsCancellationRequested)
                        {
                            Packets.Packet packet = null;
                            int            packetId;
                            byte[]         packetData;

                            if (!CompressionEnabled)
                            {
                                int length = ms.ReadVarInt();

                                int packetIdLength;
                                packetId     = ms.ReadVarInt(out packetIdLength);
                                lastPacketId = packetId;
                                if (length - packetIdLength > 0)
                                {
                                    /*packetData = new byte[length - packetIdLength];
                                     * int read = 0;
                                     * while (read < packetData.Length)
                                     * {
                                     *      read += ms.Read(packetData, read, packetData.Length - read);
                                     *
                                     *      if (CancellationToken.IsCancellationRequested) throw new OperationCanceledException();
                                     * }*/
                                    packetData = ms.Read(length - packetIdLength);
                                }
                                else
                                {
                                    packetData = new byte[0];
                                }
                            }
                            else
                            {
                                int packetLength = ms.ReadVarInt();

                                int br;
                                int dataLength = ms.ReadVarInt(out br);

                                int readMore;
                                if (dataLength == 0)
                                {
                                    packetId     = ms.ReadVarInt(out readMore);
                                    lastPacketId = packetId;
                                    packetData   = ms.Read(packetLength - (br + readMore));
                                }
                                else
                                {
                                    byte[] data = ms.Read(packetLength - br);
                                    byte[] decompressed;
                                    DecompressData(data, out decompressed);

                                    using (MemoryStream b = new MemoryStream(decompressed))
                                    {
                                        using (MinecraftStream a = new MinecraftStream(b))
                                        {
                                            int l;
                                            packetId     = a.ReadVarInt(out l);
                                            lastPacketId = packetId;
                                            packetData   = a.Read(dataLength - l);
                                        }
                                    }
                                }
                            }

                            packet = MCPacketFactory.GetPacket(Direction, ConnectionState, packetId);
                            if (packet == null)
                            {
                                if (UnhandledPacketsFilter[ConnectionState]
                                    .TryAdd(packetId, 1))
                                {
                                    Log.Debug($"Unhandled packet in {ConnectionState}! 0x{packetId.ToString("x2")} = {(ConnectionState == ConnectionState.Play ? MCPacketFactory.GetPlayPacketName(packetId) : "Unknown")}");
                                }
                                else
                                {
                                    UnhandledPacketsFilter[ConnectionState][packetId] = UnhandledPacketsFilter[ConnectionState][packetId] + 1;
                                }

                                continue;
                            }

                            if (ConnectionState == ConnectionState.Play)
                            {
                                HandlePacketQueue.Add(new TemporaryPacketData(packet, packetData));
                            }
                            else
                            {
                                packet.Decode(new MinecraftStream(new MemoryStream(packetData)));
                                HandlePacket(packet);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is OperationCanceledException)
                {
                    return;
                }
                if (ex is EndOfStreamException)
                {
                    return;
                }
                if (ex is IOException)
                {
                    return;
                }

                if (LogExceptions)
                {
                    Log.Warn($"Failed to process network (Last packet: 0x{lastPacketId:X2} State: {ConnectionState}): " + ex);
                }
            }
            finally
            {
                Disconnected(false);
            }
        }
Exemple #11
0
        public override void Decode(MinecraftStream stream)
        {
            ChunkX              = stream.ReadVarInt();
            ChunkZ              = stream.ReadVarInt();
            TrustEdges          = stream.ReadBool();
            SkyLightMask        = stream.ReadVarInt();
            BlockLightMask      = stream.ReadVarInt();
            EmptySkyLightMask   = stream.ReadVarInt();
            EmptyBlockLightMask = stream.ReadVarInt();

            //  List<byte[]> skyLightList = new List<byte[]>();
            SkyLightArrays = new byte[16][];
            for (int i = 0; i < SkyLightArrays.Length + 1; i++)
            {
                if (((SkyLightMask & 1) != 0) /*&& ((EmptySkyLightMask & 1) == 0)*/)
                {
                    stream.ReadVarInt();
                    byte[] data = stream.Read(2048);

                    if (i != 0)
                    {
                        SkyLightArrays[i - 1] = data;
                    }
                }
                else
                {
                    if (i != 0)
                    {
                        SkyLightArrays[i - 1] = null;
                    }
                }

                SkyLightMask      = SkyLightMask >> 1;
                EmptySkyLightMask = EmptySkyLightMask >> 1;
            }
            //SkyLightArrays = skyLightList.ToArray();

            // List<byte[]> blockLightList = new List<byte[]>();
            BlockLightArrays = new byte[16][];
            for (int i = 0; i < BlockLightArrays.Length + 1; i++)
            {
                if (((BlockLightMask & 1) != 0) /*&& ((EmptyBlockLightMask & 1) == 0)*/)
                {
                    stream.ReadVarInt();
                    byte[] data = stream.Read(2048);

                    if (i != 0)
                    {
                        BlockLightArrays[i - 1] = data;
                    }
                }
                else
                {
                    if (i != 0)
                    {
                        BlockLightArrays[i - 1] = null;
                    }
                }

                BlockLightMask      = BlockLightMask >> 1;
                EmptyBlockLightMask = EmptyBlockLightMask >> 1;
            }
            // BlockLightArrays = blockLightList.ToArray();
        }
Exemple #12
0
 public override void Decode(MinecraftStream stream)
 {
     EntityId = stream.ReadVarInt();
     Data     = stream.Read((int)(stream.Length - stream.Position));
     //TODO: Read metadata properly
 }
Exemple #13
0
        private bool TryReadPacket(MinecraftStream stream, out int lastPacketId)
        {
            Packets.Packet packet = null;
            int            packetId;

            byte[] packetData;

            if (!CompressionEnabled)
            {
                int length = stream.ReadVarInt();

                int packetIdLength;
                packetId     = stream.ReadVarInt(out packetIdLength);
                lastPacketId = packetId;

                if (length - packetIdLength > 0)
                {
                    packetData = stream.Read(length - packetIdLength);
                }
                else
                {
                    packetData = new byte[0];
                }
            }
            else
            {
                int packetLength = stream.ReadVarInt();

                int br;
                int dataLength = stream.ReadVarInt(out br);

                int readMore;

                if (dataLength == 0)
                {
                    packetId     = stream.ReadVarInt(out readMore);
                    lastPacketId = packetId;
                    packetData   = stream.Read(packetLength - (br + readMore));
                }
                else
                {
                    var data = stream.ReadToSpan(packetLength - br);

                    using (MinecraftStream a = new MinecraftStream())
                    {
                        using (ZlibStream outZStream = new ZlibStream(
                                   a, CompressionMode.Decompress, CompressionLevel.Default, true))
                        {
                            outZStream.Write(data);
                            //  outZStream.Write(data, 0, data.Length);
                        }

                        a.Seek(0, SeekOrigin.Begin);

                        int l;
                        packetId     = a.ReadVarInt(out l);
                        lastPacketId = packetId;
                        packetData   = a.Read(dataLength - l);
                    }
                }
            }

            packet = MCPacketFactory.GetPacket(PacketDirection, ConnectionState, packetId);

            Interlocked.Increment(ref PacketsIn);
            Interlocked.Add(ref PacketSizeIn, packetData.Length);

            if (packet == null)
            {
                if (UnhandledPacketsFilter[ConnectionState].TryAdd(packetId, 1))
                {
                    Log.Debug(
                        $"Unhandled packet in {ConnectionState}! 0x{packetId.ToString("x2")} = {(ConnectionState == ConnectionState.Play ? MCPacketFactory.GetPlayPacketName(packetId) : "Unknown")}");
                }
                else
                {
                    UnhandledPacketsFilter[ConnectionState][packetId] =
                        UnhandledPacketsFilter[ConnectionState][packetId] + 1;
                }

                return(false);
            }

            //    Log.Info($"Received: {packet}");

            if (ConnectionState == ConnectionState.Play)
            {
                if (ShouldAddToProcessing(packet))
                {
                    // Interlocked.Increment(ref _queued);

                    // ThreadPool.QueueUserWorkItem(
                    //	    () =>
                    //	    {
                    ProcessPacket(packet, packetData);
                    //   Interlocked.Decrement(ref _queued);
                    //	    });

                    return(true);
                }
            }
            else
            {
                ProcessPacket(packet, packetData);

                return(true);
            }

            return(false);
        }
Exemple #14
0
        private void ProcessNetwork()
        {
            try
            {
                using (NetworkStream ns = new NetworkStream(Socket))
                {
                    using (MinecraftStream ms = new MinecraftStream(ns))
                    {
                        _readerStream = ms;

                        while (!CancellationToken.IsCancellationRequested)
                        {
                            Packets.Packet packet = null;
                            int            packetId;
                            byte[]         packetData;

                            if (!CompressionEnabled)
                            {
                                int length = ms.ReadVarInt();

                                int packetIdLength;
                                packetId = ms.ReadVarInt(out packetIdLength);

                                if (length - packetIdLength > 0)
                                {
                                    /*packetData = new byte[length - packetIdLength];
                                     * int read = 0;
                                     * while (read < packetData.Length)
                                     * {
                                     *      read += ms.Read(packetData, read, packetData.Length - read);
                                     *
                                     *      if (CancellationToken.IsCancellationRequested) throw new OperationCanceledException();
                                     * }*/
                                    packetData = ms.Read(length - packetIdLength);
                                }
                                else
                                {
                                    packetData = new byte[0];
                                }
                            }
                            else
                            {
                                int packetLength = ms.ReadVarInt();

                                int br;
                                int dataLength = ms.ReadVarInt(out br);

                                int readMore;
                                if (dataLength == 0)
                                {
                                    packetId   = ms.ReadVarInt(out readMore);
                                    packetData = ms.Read(packetLength - (br + readMore));
                                }
                                else
                                {
                                    byte[] data = ms.Read(packetLength - br);
                                    byte[] decompressed;
                                    DecompressData(data, out decompressed);

                                    using (MemoryStream b = new MemoryStream(decompressed))
                                    {
                                        using (MinecraftStream a = new MinecraftStream(b))
                                        {
                                            int l;
                                            packetId   = a.ReadVarInt(out l);
                                            packetData = a.Read(dataLength - l);
                                        }
                                    }
                                }
                            }

                            packet = MCPacketFactory.GetPacket(ConnectionState, packetId);
                            if (packet == null)
                            {
                                Log.Warn($"Unhandled package! 0x{packetId.ToString("x2")}");
                                continue;
                            }
                            packet.Decode(new MinecraftStream(new MemoryStream(packetData)));
                            HandlePacket(packet);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Warn("OH NO", ex);
                if (ex is OperationCanceledException)
                {
                    return;
                }
                if (ex is EndOfStreamException)
                {
                    return;
                }
                if (ex is IOException)
                {
                    return;
                }

                Log.Error("An unhandled exception occured while processing network!", ex);
            }
            finally
            {
                Disconnected(false);
            }
        }
Exemple #15
0
        private void ProcessNetwork()
        {
            int lastPacketId = 0;

            try
            {
                using (NetworkStream ns = new NetworkStream(Socket))
                {
                    using (MinecraftStream ms = new MinecraftStream(ns))
                    {
                        _readerStream = ms;

                        while (!CancellationToken.IsCancellationRequested)
                        {
                            Packets.Packet packet = null;
                            int            packetId;
                            byte[]         packetData;

                            if (!CompressionEnabled)
                            {
                                int length = ms.ReadVarInt();

                                int packetIdLength;
                                packetId     = ms.ReadVarInt(out packetIdLength);
                                lastPacketId = packetId;
                                if (length - packetIdLength > 0)
                                {
                                    packetData = ms.Read(length - packetIdLength);
                                }
                                else
                                {
                                    packetData = new byte[0];
                                }
                            }
                            else
                            {
                                int packetLength = ms.ReadVarInt();

                                int br;
                                int dataLength = ms.ReadVarInt(out br);

                                int readMore;
                                if (dataLength == 0)
                                {
                                    packetId     = ms.ReadVarInt(out readMore);
                                    lastPacketId = packetId;
                                    packetData   = ms.Read(packetLength - (br + readMore));
                                }
                                else
                                {
                                    byte[] data = ms.Read(packetLength - br);
                                    using (MinecraftStream a = new MinecraftStream())
                                    {
                                        using (ZlibStream outZStream = new ZlibStream(a,
                                                                                      CompressionMode.Decompress, CompressionLevel.Default, true))
                                        {
                                            outZStream.Write(data, 0, data.Length);
                                        }

                                        a.Seek(0, SeekOrigin.Begin);

                                        int l;
                                        packetId     = a.ReadVarInt(out l);
                                        lastPacketId = packetId;
                                        packetData   = a.Read(dataLength - l);
                                    }
                                }
                            }

                            packet = MCPacketFactory.GetPacket(Direction, ConnectionState, packetId);
                            if (packet == null)
                            {
                                if (UnhandledPacketsFilter[ConnectionState]
                                    .TryAdd(packetId, 1))
                                {
                                    Log.Debug(
                                        $"Unhandled packet in {ConnectionState}! 0x{packetId.ToString("x2")} = {(ConnectionState == ConnectionState.Play ? MCPacketFactory.GetPlayPacketName(packetId) : "Unknown")}");
                                }
                                else
                                {
                                    UnhandledPacketsFilter[ConnectionState][packetId] =
                                        UnhandledPacketsFilter[ConnectionState][packetId] + 1;
                                }

                                continue;
                            }

                            if (ConnectionState == ConnectionState.Play)
                            {
                                if (ShouldAddToProcessing(packet))
                                {
                                    Interlocked.Increment(ref _queued);
                                    ThreadPool.QueueUserWorkItem(() =>
                                    {
                                        ProcessPacket(packet, packetData);
                                        Interlocked.Decrement(ref _queued);
                                    });
                                }
                            }
                            else
                            {
                                ProcessPacket(packet, packetData);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex is OperationCanceledException)
                {
                    return;
                }
                if (ex is EndOfStreamException)
                {
                    return;
                }
                if (ex is IOException)
                {
                    return;
                }

                if (LogExceptions)
                {
                    Log.Warn(
                        $"Failed to process network (Last packet: 0x{lastPacketId:X2} State: {ConnectionState}): " +
                        ex);
                }
            }
            finally
            {
                Disconnected(false);
            }
        }