Search in sources :

Example 1 with ProtocolState

use of com.github.dirtpowered.dirtmv.data.translator.ProtocolState in project DirtMultiversion by DirtPowered.

the class NettyPacketDecoder method decode.

@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
    if (byteBuf.readableBytes() == 0)
        return;
    PacketInput inputBuffer = new NettyInputWrapper(byteBuf);
    int i = inputBuffer.readVarInt();
    boolean flag = packetDirection == PacketDirection.TO_CLIENT;
    ProtocolState protocolState = userData.getProtocolState();
    if (protocolState == ProtocolState.HANDSHAKE) {
        int readerIndex = byteBuf.readerIndex();
        int protocol = inputBuffer.readVarInt();
        byteBuf.readerIndex(readerIndex);
        userData.setClientVersion(MinecraftVersion.fromNettyProtocolId(protocol));
    }
    PacketData packet;
    if (flag) {
        packet = readModernPacket(main.getConfiguration().getServerVersion(), protocolState, inputBuffer, packetDirection, i);
    } else {
        packet = readModernPacket(userData.getClientVersion(), protocolState, inputBuffer, packetDirection, i);
    }
    int readableBytes = byteBuf.readableBytes();
    if (readableBytes > 0) {
        byteBuf.skipBytes(readableBytes);
        Logger.warn("skipping {} bytes for packet id: {}, direction: {}", readableBytes, i, packetDirection);
    } else {
        list.add(packet);
    }
}
Also used : PacketInput(com.github.dirtpowered.dirtmv.data.protocol.io.model.PacketInput) NettyInputWrapper(com.github.dirtpowered.dirtmv.data.protocol.io.NettyInputWrapper) ProtocolState(com.github.dirtpowered.dirtmv.data.translator.ProtocolState) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Example 2 with ProtocolState

use of com.github.dirtpowered.dirtmv.data.translator.ProtocolState in project DirtMultiversion by DirtPowered.

the class ServerSession method sendPacket.

/**
 * Translates and sends packet to server
 *
 * @param packet    {@link PacketData} Packet
 * @param direction {@link PacketDirection} sending direction (client/server)
 * @param from      Version to start from
 */
public void sendPacket(PacketData packet, PacketDirection direction, MinecraftVersion from) {
    MinecraftVersion version = main.getConfiguration().getServerVersion();
    if (from != null && from != version) {
        version = from;
    }
    List<ServerProtocol> protocols = main.getTranslatorRegistry().findProtocol(userData, version);
    boolean flag = direction == PacketDirection.TO_CLIENT;
    if (!flag) {
        Collections.reverse(protocols);
    }
    PacketData target = packet;
    for (ServerProtocol protocol : protocols) {
        boolean isNetty = userData.getClientVersion().isNettyProtocol();
        ProtocolState state = isNetty ? userData.getProtocolState() : ProtocolState.PRE_NETTY;
        if (target == null) {
            return;
        }
        // packet queue workaround
        state = packet.getNettyState() != null ? packet.getNettyState() : state;
        if (!protocol.getFrom().isNettyProtocol()) {
            state = ProtocolState.PRE_NETTY;
        }
        PacketTranslator translator = protocol.getTranslatorFor(target.getOpCode(), state, direction);
        if (translator != null) {
            if (from == null || from != protocol.getFrom()) {
                try {
                    target = translator.translate(this, target);
                } catch (IOException e) {
                    disconnect(e.getMessage());
                    return;
                }
                if (target.getOpCode() == -1) {
                    return;
                }
            }
        }
    }
    if (flag) {
        sendPacket(target);
    } else {
        ClientSession clientSession = getClientSession();
        if (target.getOpCode() == 2 && /* handshake */
        !hasServerPingProtocol()) {
            connectToServer();
        }
        if (clientSession != null && clientSession.getChannel().isActive()) {
            clientSession.sendPacket(target);
            return;
        }
        // queued packets are always login packets
        target.setNettyState(ProtocolState.LOGIN);
        initialPacketQueue.add(target);
    }
}
Also used : PacketTranslator(com.github.dirtpowered.dirtmv.data.translator.PacketTranslator) ClientSession(com.github.dirtpowered.dirtmv.network.client.ClientSession) IOException(java.io.IOException) ServerProtocol(com.github.dirtpowered.dirtmv.data.translator.ServerProtocol) ProtocolState(com.github.dirtpowered.dirtmv.data.translator.ProtocolState) MinecraftVersion(com.github.dirtpowered.dirtmv.data.MinecraftVersion) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Aggregations

PacketData (com.github.dirtpowered.dirtmv.data.protocol.PacketData)2 ProtocolState (com.github.dirtpowered.dirtmv.data.translator.ProtocolState)2 MinecraftVersion (com.github.dirtpowered.dirtmv.data.MinecraftVersion)1 NettyInputWrapper (com.github.dirtpowered.dirtmv.data.protocol.io.NettyInputWrapper)1 PacketInput (com.github.dirtpowered.dirtmv.data.protocol.io.model.PacketInput)1 PacketTranslator (com.github.dirtpowered.dirtmv.data.translator.PacketTranslator)1 ServerProtocol (com.github.dirtpowered.dirtmv.data.translator.ServerProtocol)1 ClientSession (com.github.dirtpowered.dirtmv.network.client.ClientSession)1 IOException (java.io.IOException)1