Exemple #1
0
        /// <summary>
        /// Read the next packet from the network
        /// </summary>
        /// <param name="packetID">will contain packet ID</param>
        /// <param name="packetData">will contain raw packet Data</param>

        private void readNextPacket(ref int packetID, ref byte[] packetData)
        {
            int size = readNextVarIntRAW(); //Packet size

            packetData = readDataRAW(size); //Packet contents

            if (compression_treshold > 0)   //Handle packet decompression
            {
                int size_uncompressed = readNextVarInt(ref packetData);
                if (size_uncompressed != 0) // != 0 means compressed, let's decompress
                {
                    packetData = ZlibUtils.Decompress(packetData, size_uncompressed);
                }
            }

            packetID = readNextVarInt(ref packetData); //Packet ID
        }
Exemple #2
0
        private void readNextPacket(TcpClient c, ref int packetID, ref byte[] packetData, ref byte[] packetRawData)
        {
            int size = readNextVarIntRAW(c);

            packetData    = readDataRAW(c, size);
            packetRawData = concatBytes(getVarInt(size), packetData);

            if (compression_treshold > 0)
            {
                int size_uncompressed = readNextVarInt(ref packetData);
                if (size_uncompressed != 0)
                {
                    packetData = ZlibUtils.Decompress(packetData, size_uncompressed);
                }
            }

            packetID = readNextVarInt(ref packetData);
        }
Exemple #3
0
        /// <summary>
        /// Send a packet to the server.  Compression and encryption will be handled automatically.
        /// </summary>
        /// <param name="packetID">packet ID</param>
        /// <param name="packetData">packet Data</param>
        private void SendPacket(int packetID, IEnumerable <byte> packetData)
        {
            //The inner packet
            byte[] the_packet = dataTypes.ConcatBytes(dataTypes.GetVarInt(packetID), packetData.ToArray());

            if (compression_treshold > 0)                      //Compression enabled?
            {
                if (the_packet.Length >= compression_treshold) //Packet long enough for compressing?
                {
                    byte[] compressed_packet = ZlibUtils.Compress(the_packet);
                    the_packet = dataTypes.ConcatBytes(dataTypes.GetVarInt(the_packet.Length), compressed_packet);
                }
                else
                {
                    byte[] uncompressed_length = dataTypes.GetVarInt(0); //Not compressed (short packet)
                    the_packet = dataTypes.ConcatBytes(uncompressed_length, the_packet);
                }
            }

            socketWrapper.SendDataRAW(dataTypes.ConcatBytes(dataTypes.GetVarInt(the_packet.Length), the_packet));
        }
Exemple #4
0
        /// <summary>
        /// Read the next packet from the network
        /// </summary>
        /// <param name="packetID">will contain packet ID</param>
        /// <param name="packetData">will contain raw packet Data</param>
        internal void ReadNextPacket(ref int packetID, List <byte> packetData)
        {
            packetData.Clear();
            int size = dataTypes.ReadNextVarIntRAW(socketWrapper); //Packet size

            packetData.AddRange(socketWrapper.ReadDataRAW(size));  //Packet contents

            //Handle packet decompression
            if (protocolversion >= MC18Version &&
                compression_treshold > 0)
            {
                int sizeUncompressed = dataTypes.ReadNextVarInt(packetData);
                if (sizeUncompressed != 0) // != 0 means compressed, let's decompress
                {
                    byte[] toDecompress = packetData.ToArray();
                    byte[] uncompressed = ZlibUtils.Decompress(toDecompress, sizeUncompressed);
                    packetData.Clear();
                    packetData.AddRange(uncompressed);
                }
            }

            packetID = dataTypes.ReadNextVarInt(packetData); //Packet ID
        }
Exemple #5
0
        /// <summary>
        /// Send a packet to the server, compression and encryption will be handled automatically
        /// </summary>
        /// <param name="packetID">packet ID</param>
        /// <param name="packetData">packet Data</param>

        private void SendPacket(int packetID, byte[] packetData)
        {
            //The inner packet
            byte[] the_packet = concatBytes(getVarInt(packetID), packetData);

            if (compression_treshold > 0)                     //Compression enabled?
            {
                if (the_packet.Length > compression_treshold) //Packet long enough for compressing?
                {
                    byte[] uncompressed_length      = getVarInt(the_packet.Length);
                    byte[] compressed_packet        = ZlibUtils.Compress(the_packet);
                    byte[] compressed_packet_length = getVarInt(compressed_packet.Length);
                    the_packet = concatBytes(compressed_packet_length, compressed_packet);
                }
                else
                {
                    byte[] uncompressed_length = getVarInt(0); //Not compressed (short packet)
                    the_packet = concatBytes(uncompressed_length, the_packet);
                }
            }

            SendRAW(concatBytes(getVarInt(the_packet.Length), the_packet));
        }
Exemple #6
0
        /// <summary>
        /// Handle the given packet
        /// </summary>
        /// <param name="packetID">Packet ID</param>
        /// <param name="packetData">Packet contents</param>
        /// <returns>TRUE if the packet was processed, FALSE if ignored or unknown</returns>
        internal bool HandlePacket(int packetID, List <byte> packetData)
        {
            try
            {
                if (login_phase)
                {
                    switch (packetID) //Packet IDs are different while logging in
                    {
                    case 0x03:
                        if (protocolversion >= MC18Version)
                        {
                            compression_treshold = dataTypes.ReadNextVarInt(packetData);
                        }
                        break;

                    default:
                        return(false);    //Ignored packet
                    }
                }
                // Regular in-game packets
                switch (Protocol18PacketTypes.GetPacketIncomingType(packetID, protocolversion))
                {
                case PacketIncomingType.KeepAlive:
                    SendPacket(PacketOutgoingType.KeepAlive, packetData);
                    break;

                case PacketIncomingType.JoinGame:
                    handler.OnGameJoined();
                    dataTypes.ReadNextInt(packetData);
                    dataTypes.ReadNextByte(packetData);
                    if (protocolversion >= MC191Version)
                    {
                        this.currentDimension = dataTypes.ReadNextInt(packetData);
                    }
                    else
                    {
                        this.currentDimension = (sbyte)dataTypes.ReadNextByte(packetData);
                    }
                    if (protocolversion < MC114Version)
                    {
                        dataTypes.ReadNextByte(packetData);               // Difficulty - 1.13 and below
                    }
                    dataTypes.ReadNextByte(packetData);
                    dataTypes.ReadNextString(packetData);
                    if (protocolversion >= MC114Version)
                    {
                        dataTypes.ReadNextVarInt(packetData);             // View distance - 1.14 and above
                    }
                    if (protocolversion >= MC18Version)
                    {
                        dataTypes.ReadNextBool(packetData);               // Reduced debug info - 1.8 and above
                    }
                    break;

                case PacketIncomingType.ChatMessage:
                    string message = dataTypes.ReadNextString(packetData);
                    try
                    {
                        //Hide system messages or xp bar messages?
                        byte messageType = dataTypes.ReadNextByte(packetData);
                        if ((messageType == 1 && !Settings.DisplaySystemMessages) ||
                            (messageType == 2 && !Settings.DisplayXPBarMessages))
                        {
                            break;
                        }
                    }
                    catch (ArgumentOutOfRangeException) { /* No message type */ }
                    handler.OnTextReceived(message, true);
                    break;

                case PacketIncomingType.Respawn:
                    this.currentDimension = dataTypes.ReadNextInt(packetData);
                    if (protocolversion < MC114Version)
                    {
                        dataTypes.ReadNextByte(packetData);               // Difficulty - 1.13 and below
                    }
                    dataTypes.ReadNextByte(packetData);
                    dataTypes.ReadNextString(packetData);
                    handler.OnRespawn();
                    break;

                case PacketIncomingType.PlayerPositionAndLook:
                    if (handler.GetTerrainEnabled())
                    {
                        double x       = dataTypes.ReadNextDouble(packetData);
                        double y       = dataTypes.ReadNextDouble(packetData);
                        double z       = dataTypes.ReadNextDouble(packetData);
                        float  yaw     = dataTypes.ReadNextFloat(packetData);
                        float  pitch   = dataTypes.ReadNextFloat(packetData);
                        byte   locMask = dataTypes.ReadNextByte(packetData);

                        if (protocolversion >= MC18Version)
                        {
                            Location location = handler.GetCurrentLocation();
                            location.X = (locMask & 1 << 0) != 0 ? location.X + x : x;
                            location.Y = (locMask & 1 << 1) != 0 ? location.Y + y : y;
                            location.Z = (locMask & 1 << 2) != 0 ? location.Z + z : z;
                            handler.UpdateLocation(location, yaw, pitch);
                        }
                        else
                        {
                            handler.UpdateLocation(new Location(x, y, z), yaw, pitch);
                        }
                    }

                    if (protocolversion >= MC19Version)
                    {
                        int teleportID = dataTypes.ReadNextVarInt(packetData);
                        // Teleport confirm packet
                        SendPacket(PacketOutgoingType.TeleportConfirm, dataTypes.GetVarInt(teleportID));
                    }
                    break;

                case PacketIncomingType.ChunkData:
                    if (handler.GetTerrainEnabled())
                    {
                        int    chunkX           = dataTypes.ReadNextInt(packetData);
                        int    chunkZ           = dataTypes.ReadNextInt(packetData);
                        bool   chunksContinuous = dataTypes.ReadNextBool(packetData);
                        ushort chunkMask        = protocolversion >= MC19Version
                                ? (ushort)dataTypes.ReadNextVarInt(packetData)
                                : dataTypes.ReadNextUShort(packetData);
                        if (protocolversion < MC18Version)
                        {
                            ushort addBitmap          = dataTypes.ReadNextUShort(packetData);
                            int    compressedDataSize = dataTypes.ReadNextInt(packetData);
                            byte[] compressed         = dataTypes.ReadData(compressedDataSize, packetData);
                            byte[] decompressed       = ZlibUtils.Decompress(compressed);
                            pTerrain.ProcessChunkColumnData(chunkX, chunkZ, chunkMask, addBitmap, currentDimension == 0, chunksContinuous, currentDimension, new List <byte>(decompressed));
                        }
                        else
                        {
                            if (protocolversion >= MC114Version)
                            {
                                dataTypes.ReadNextNbt(packetData);      // Heightmaps - 1.14 and above
                            }
                            int dataSize = dataTypes.ReadNextVarInt(packetData);
                            pTerrain.ProcessChunkColumnData(chunkX, chunkZ, chunkMask, 0, false, chunksContinuous, currentDimension, packetData);
                        }
                    }
                    break;

                case PacketIncomingType.MultiBlockChange:
                    if (handler.GetTerrainEnabled())
                    {
                        int chunkX      = dataTypes.ReadNextInt(packetData);
                        int chunkZ      = dataTypes.ReadNextInt(packetData);
                        int recordCount = protocolversion < MC18Version
                                ? (int)dataTypes.ReadNextShort(packetData)
                                : dataTypes.ReadNextVarInt(packetData);

                        for (int i = 0; i < recordCount; i++)
                        {
                            byte   locationXZ;
                            ushort blockIdMeta;
                            int    blockY;

                            if (protocolversion < MC18Version)
                            {
                                blockIdMeta = dataTypes.ReadNextUShort(packetData);
                                blockY      = (ushort)dataTypes.ReadNextByte(packetData);
                                locationXZ  = dataTypes.ReadNextByte(packetData);
                            }
                            else
                            {
                                locationXZ  = dataTypes.ReadNextByte(packetData);
                                blockY      = (ushort)dataTypes.ReadNextByte(packetData);
                                blockIdMeta = (ushort)dataTypes.ReadNextVarInt(packetData);
                            }

                            int   blockX = locationXZ >> 4;
                            int   blockZ = locationXZ & 0x0F;
                            Block block  = new Block(blockIdMeta);
                            handler.GetWorld().SetBlock(new Location(chunkX, chunkZ, blockX, blockY, blockZ), block);
                        }
                    }
                    break;

                case PacketIncomingType.BlockChange:
                    if (handler.GetTerrainEnabled())
                    {
                        if (protocolversion < MC18Version)
                        {
                            int   blockX    = dataTypes.ReadNextInt(packetData);
                            int   blockY    = dataTypes.ReadNextByte(packetData);
                            int   blockZ    = dataTypes.ReadNextInt(packetData);
                            short blockId   = (short)dataTypes.ReadNextVarInt(packetData);
                            byte  blockMeta = dataTypes.ReadNextByte(packetData);
                            handler.GetWorld().SetBlock(new Location(blockX, blockY, blockZ), new Block(blockId, blockMeta));
                        }
                        else
                        {
                            handler.GetWorld().SetBlock(dataTypes.ReadNextLocation(packetData), new Block((ushort)dataTypes.ReadNextVarInt(packetData)));
                        }
                    }
                    break;

                case PacketIncomingType.MapChunkBulk:
                    if (protocolversion < MC19Version && handler.GetTerrainEnabled())
                    {
                        int         chunkCount;
                        bool        hasSkyLight;
                        List <byte> chunkData = packetData;

                        //Read global fields
                        if (protocolversion < MC18Version)
                        {
                            chunkCount = dataTypes.ReadNextShort(packetData);
                            int compressedDataSize = dataTypes.ReadNextInt(packetData);
                            hasSkyLight = dataTypes.ReadNextBool(packetData);
                            byte[] compressed   = dataTypes.ReadData(compressedDataSize, packetData);
                            byte[] decompressed = ZlibUtils.Decompress(compressed);
                            chunkData = new List <byte>(decompressed);
                        }
                        else
                        {
                            hasSkyLight = dataTypes.ReadNextBool(packetData);
                            chunkCount  = dataTypes.ReadNextVarInt(packetData);
                        }

                        //Read chunk records
                        int[]    chunkXs    = new int[chunkCount];
                        int[]    chunkZs    = new int[chunkCount];
                        ushort[] chunkMasks = new ushort[chunkCount];
                        ushort[] addBitmaps = new ushort[chunkCount];
                        for (int chunkColumnNo = 0; chunkColumnNo < chunkCount; chunkColumnNo++)
                        {
                            chunkXs[chunkColumnNo]    = dataTypes.ReadNextInt(packetData);
                            chunkZs[chunkColumnNo]    = dataTypes.ReadNextInt(packetData);
                            chunkMasks[chunkColumnNo] = dataTypes.ReadNextUShort(packetData);
                            addBitmaps[chunkColumnNo] = protocolversion < MC18Version
                                    ? dataTypes.ReadNextUShort(packetData)
                                    : (ushort)0;
                        }

                        //Process chunk records
                        for (int chunkColumnNo = 0; chunkColumnNo < chunkCount; chunkColumnNo++)
                        {
                            pTerrain.ProcessChunkColumnData(chunkXs[chunkColumnNo], chunkZs[chunkColumnNo], chunkMasks[chunkColumnNo], addBitmaps[chunkColumnNo], hasSkyLight, true, currentDimension, chunkData);
                        }
                    }
                    break;

                case PacketIncomingType.UnloadChunk:
                    if (protocolversion >= MC19Version && handler.GetTerrainEnabled())
                    {
                        int chunkX = dataTypes.ReadNextInt(packetData);
                        int chunkZ = dataTypes.ReadNextInt(packetData);
                        handler.GetWorld()[chunkX, chunkZ] = null;
                    }
                    break;

                case PacketIncomingType.PlayerListUpdate:
                    if (protocolversion >= MC18Version)
                    {
                        int action     = dataTypes.ReadNextVarInt(packetData);
                        int numActions = dataTypes.ReadNextVarInt(packetData);
                        for (int i = 0; i < numActions; i++)
                        {
                            Guid uuid = dataTypes.ReadNextUUID(packetData);
                            switch (action)
                            {
                            case 0x00:         //Player Join
                                string name    = dataTypes.ReadNextString(packetData);
                                int    propNum = dataTypes.ReadNextVarInt(packetData);
                                for (int p = 0; p < propNum; p++)
                                {
                                    string key = dataTypes.ReadNextString(packetData);
                                    string val = dataTypes.ReadNextString(packetData);
                                    if (dataTypes.ReadNextBool(packetData))
                                    {
                                        dataTypes.ReadNextString(packetData);
                                    }
                                }
                                dataTypes.ReadNextVarInt(packetData);
                                dataTypes.ReadNextVarInt(packetData);
                                if (dataTypes.ReadNextBool(packetData))
                                {
                                    dataTypes.ReadNextString(packetData);
                                }
                                handler.OnPlayerJoin(uuid, name);
                                break;

                            case 0x01:         //Update gamemode
                            case 0x02:         //Update latency
                                dataTypes.ReadNextVarInt(packetData);
                                break;

                            case 0x03:         //Update display name
                                if (dataTypes.ReadNextBool(packetData))
                                {
                                    dataTypes.ReadNextString(packetData);
                                }
                                break;

                            case 0x04:         //Player Leave
                                handler.OnPlayerLeave(uuid);
                                break;

                            default:
                                //Unknown player list item type
                                break;
                            }
                        }
                    }
                    else     //MC 1.7.X does not provide UUID in tab-list updates
                    {
                        string name     = dataTypes.ReadNextString(packetData);
                        bool   online   = dataTypes.ReadNextBool(packetData);
                        short  ping     = dataTypes.ReadNextShort(packetData);
                        Guid   FakeUUID = new Guid(MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(name)).Take(16).ToArray());
                        if (online)
                        {
                            handler.OnPlayerJoin(FakeUUID, name);
                        }
                        else
                        {
                            handler.OnPlayerLeave(FakeUUID);
                        }
                    }
                    break;

                case PacketIncomingType.TabCompleteResult:
                    if (protocolversion >= MC113Version)
                    {
                        autocomplete_transaction_id = dataTypes.ReadNextVarInt(packetData);
                        dataTypes.ReadNextVarInt(packetData);     // Start of text to replace
                        dataTypes.ReadNextVarInt(packetData);     // Length of text to replace
                    }

                    int autocomplete_count = dataTypes.ReadNextVarInt(packetData);
                    autocomplete_result.Clear();

                    for (int i = 0; i < autocomplete_count; i++)
                    {
                        autocomplete_result.Add(dataTypes.ReadNextString(packetData));
                        if (protocolversion >= MC113Version)
                        {
                            // Skip optional tooltip for each tab-complete result
                            if (dataTypes.ReadNextBool(packetData))
                            {
                                dataTypes.ReadNextString(packetData);
                            }
                        }
                    }

                    autocomplete_received = true;
                    break;

                case PacketIncomingType.PluginMessage:
                    String channel = dataTypes.ReadNextString(packetData);
                    // Length is unneeded as the whole remaining packetData is the entire payload of the packet.
                    if (protocolversion < MC18Version)
                    {
                        pForge.ReadNextVarShort(packetData);
                    }
                    handler.OnPluginChannelMessage(channel, packetData.ToArray());
                    return(pForge.HandlePluginMessage(channel, packetData, ref currentDimension));

                case PacketIncomingType.KickPacket:
                    handler.OnConnectionLost(ChatBot.DisconnectReason.InGameKick, ChatParser.ParseText(dataTypes.ReadNextString(packetData)));
                    return(false);

                case PacketIncomingType.NetworkCompressionTreshold:
                    if (protocolversion >= MC18Version && protocolversion < MC19Version)
                    {
                        compression_treshold = dataTypes.ReadNextVarInt(packetData);
                    }
                    break;

                case PacketIncomingType.ResourcePackSend:
                    string url  = dataTypes.ReadNextString(packetData);
                    string hash = dataTypes.ReadNextString(packetData);
                    //Send back "accepted" and "successfully loaded" responses for plugins making use of resource pack mandatory
                    byte[] responseHeader = new byte[0];
                    if (protocolversion < MC110Version)     //MC 1.10 does not include resource pack hash in responses
                    {
                        responseHeader = dataTypes.ConcatBytes(dataTypes.GetVarInt(hash.Length), Encoding.UTF8.GetBytes(hash));
                    }
                    SendPacket(PacketOutgoingType.ResourcePackStatus, dataTypes.ConcatBytes(responseHeader, dataTypes.GetVarInt(3)));     //Accepted pack
                    SendPacket(PacketOutgoingType.ResourcePackStatus, dataTypes.ConcatBytes(responseHeader, dataTypes.GetVarInt(0)));     //Successfully loaded
                    break;

                default:
                    return(false); //Ignored packet
                }
                return(true);      //Packet processed
            }
            catch (Exception innerException)
            {
                throw new System.IO.InvalidDataException(
                          String.Format("Failed to process incoming packet of type {0}. (PacketID: {1}, Protocol: {2}, LoginPhase: {3}, InnerException: {4}).",
                                        Protocol18PacketTypes.GetPacketIncomingType(packetID, protocolversion),
                                        packetID,
                                        protocolversion,
                                        login_phase,
                                        innerException.GetType()),
                          innerException);
            }
        }