Search in sources :

Example 1 with ServerProtocol

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

the class ClientSession method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, PacketData packetData) {
    // server to client packets
    serverSession.sendPacket(packetData, PacketDirection.TO_CLIENT, null);
    // notify other sessions
    if (!stateLock) {
        boolean postNettyFlag = serverSession.getUserData().getProtocolState() == ProtocolState.PLAY;
        boolean preNettyFlag = serverSession.getUserData().getPreNettyProtocolState() == PreNettyProtocolState.IN_GAME;
        if (preNettyFlag || postNettyFlag) {
            MinecraftVersion server = main.getConfiguration().getServerVersion();
            MinecraftVersion client = serverSession.getUserData().getClientVersion();
            // call #onConnect only in user protocols
            for (ServerProtocol t : main.getTranslatorRegistry().getAllProtocolsBetween(server, client)) {
                t.onConnect(serverSession);
            }
            stateLock = true;
        }
    }
}
Also used : ServerProtocol(com.github.dirtpowered.dirtmv.data.translator.ServerProtocol) MinecraftVersion(com.github.dirtpowered.dirtmv.data.MinecraftVersion)

Example 2 with ServerProtocol

use of com.github.dirtpowered.dirtmv.data.translator.ServerProtocol 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)

Example 3 with ServerProtocol

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

the class TranslatorRegistry method findProtocol.

/**
 * Returns all protocols translators between client and server version
 *
 * @param data      User data
 * @param versionTo Server version
 * @return {@link List<ServerProtocol> List} with ordered protocol pipeline classes
 */
public List<ServerProtocol> findProtocol(UserData data, MinecraftVersion versionTo) {
    List<ServerProtocol> serverProtocols = new LinkedList<>();
    MinecraftVersion from = data.getClientVersion();
    Configuration c = main.getConfiguration();
    GlobalProtocolHandler globalProtocolHandler = new GlobalProtocolHandler(from, versionTo);
    // check if translating is needed
    if (from == c.getServerVersion()) {
        ServerProtocol serverProtocol;
        // starting from r1.3 the whole connections is encrypted
        if (from.getRegistryId() >= MinecraftVersion.R1_3_1.getRegistryId() && !from.isNettyProtocol()) {
            serverProtocol = new ProtocolPassthroughEncrypted(from, versionTo);
        } else if (from.isNettyProtocol()) {
            serverProtocol = new ProtocolStateHandler(from, versionTo);
        } else {
            serverProtocol = new ProtocolPassthrough(from, versionTo);
        }
        return Arrays.asList(serverProtocol, globalProtocolHandler);
    } else {
        if (from.getRegistryId() < 39) {
            serverProtocols.add(new ProtocolPassthrough(from, versionTo));
        }
        if ((from.getRegistryId() >= 39 && c.getServerVersion().getRegistryId() >= 39) && !from.isNettyProtocol()) {
            // add encryption translators to pipeline
            serverProtocols.add(new ProtocolPassthroughEncrypted(from, versionTo));
        }
    }
    int clientProtocol = from.getRegistryId();
    int serverProtocol = versionTo.getRegistryId();
    for (int i = serverProtocol; i <= clientProtocol; i++) {
        if (MinecraftVersion.fromRegistryId(i) != null) {
            ServerProtocol target = protocols.get(i);
            if (target != null && !(i <= serverProtocol)) {
                serverProtocols.add(target);
            }
        }
    }
    if (from.isNettyProtocol()) {
        serverProtocols.add(new ProtocolStateHandler(from, versionTo));
    }
    // track packets in all protocols
    serverProtocols.add(globalProtocolHandler);
    return serverProtocols;
}
Also used : ProtocolStateHandler(com.github.dirtpowered.dirtmv.network.versions.ProtocolStateHandler) Configuration(com.github.dirtpowered.dirtmv.api.Configuration) ProtocolPassthroughEncrypted(com.github.dirtpowered.dirtmv.network.versions.ProtocolPassthroughEncrypted) ProtocolPassthrough(com.github.dirtpowered.dirtmv.network.versions.ProtocolPassthrough) GlobalProtocolHandler(com.github.dirtpowered.dirtmv.network.versions.handler.GlobalProtocolHandler) ServerProtocol(com.github.dirtpowered.dirtmv.data.translator.ServerProtocol) LinkedList(java.util.LinkedList) MinecraftVersion(com.github.dirtpowered.dirtmv.data.MinecraftVersion)

Example 4 with ServerProtocol

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

the class TranslatorRegistry method getAllProtocolsBetween.

/**
 * Gets all protocols between given versions
 *
 * @param server - server version
 * @param client - client version
 * @return List of {@link ServerProtocol protocol translators}
 */
public List<ServerProtocol> getAllProtocolsBetween(MinecraftVersion server, MinecraftVersion client) {
    int serverProtocol = server.getRegistryId();
    int clientProtocol = client.getRegistryId();
    List<ServerProtocol> serverProtocols = new LinkedList<>();
    for (int i = serverProtocol; i <= clientProtocol; i++) {
        if (MinecraftVersion.fromRegistryId(i) != null) {
            ServerProtocol target = protocols.get(i);
            if (target != null && !(i <= serverProtocol)) {
                serverProtocols.add(target);
            }
        }
    }
    return serverProtocols;
}
Also used : ServerProtocol(com.github.dirtpowered.dirtmv.data.translator.ServerProtocol) LinkedList(java.util.LinkedList)

Aggregations

ServerProtocol (com.github.dirtpowered.dirtmv.data.translator.ServerProtocol)4 MinecraftVersion (com.github.dirtpowered.dirtmv.data.MinecraftVersion)3 LinkedList (java.util.LinkedList)2 Configuration (com.github.dirtpowered.dirtmv.api.Configuration)1 PacketData (com.github.dirtpowered.dirtmv.data.protocol.PacketData)1 PacketTranslator (com.github.dirtpowered.dirtmv.data.translator.PacketTranslator)1 ProtocolState (com.github.dirtpowered.dirtmv.data.translator.ProtocolState)1 ClientSession (com.github.dirtpowered.dirtmv.network.client.ClientSession)1 ProtocolPassthrough (com.github.dirtpowered.dirtmv.network.versions.ProtocolPassthrough)1 ProtocolPassthroughEncrypted (com.github.dirtpowered.dirtmv.network.versions.ProtocolPassthroughEncrypted)1 ProtocolStateHandler (com.github.dirtpowered.dirtmv.network.versions.ProtocolStateHandler)1 GlobalProtocolHandler (com.github.dirtpowered.dirtmv.network.versions.handler.GlobalProtocolHandler)1 IOException (java.io.IOException)1