Search in sources :

Example 1 with ChannelHandler

use of net.minecraft.util.io.netty.channel.ChannelHandler in project packetevents by retrooper.

the class EarlyChannelInjectorLegacy method getHandler.

private PlayerChannelHandlerLegacy getHandler(Object rawChannel) {
    Channel channel = (Channel) rawChannel;
    ChannelHandler handler = channel.pipeline().get(PacketEvents.get().getHandlerName());
    if (handler instanceof PlayerChannelHandlerLegacy) {
        return (PlayerChannelHandlerLegacy) handler;
    } else {
        return null;
    }
}
Also used : PlayerChannelHandlerLegacy(io.github.retrooper.packetevents.injector.legacy.PlayerChannelHandlerLegacy) Channel(net.minecraft.util.io.netty.channel.Channel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) NioSocketChannel(net.minecraft.util.io.netty.channel.socket.nio.NioSocketChannel) ChannelHandler(net.minecraft.util.io.netty.channel.ChannelHandler)

Example 2 with ChannelHandler

use of net.minecraft.util.io.netty.channel.ChannelHandler in project packetevents by retrooper.

the class EarlyChannelInjectorLegacy method eject.

@Override
public void eject() {
    Field childHandlerField = null;
    for (ChannelFuture future : injectedFutures) {
        List<String> names = future.channel().pipeline().names();
        ChannelHandler bootstrapAcceptor = null;
        // Pick best
        for (String name : names) {
            try {
                ChannelHandler handler = future.channel().pipeline().get(name);
                if (childHandlerField == null) {
                    childHandlerField = handler.getClass().getDeclaredField("childHandler");
                    childHandlerField.setAccessible(true);
                }
                ChannelInitializer<Channel> oldInit = (ChannelInitializer<Channel>) childHandlerField.get(handler);
                if (oldInit instanceof PEChannelInitializerLegacy) {
                    bootstrapAcceptor = handler;
                }
            } catch (Exception e) {
            // Not this one
            }
        }
        // Default to first
        if (bootstrapAcceptor == null) {
            bootstrapAcceptor = future.channel().pipeline().first();
        }
        try {
            ChannelInitializer<Channel> oldInit = (ChannelInitializer<Channel>) childHandlerField.get(bootstrapAcceptor);
            if (oldInit instanceof PEChannelInitializerLegacy) {
                childHandlerField.setAccessible(true);
                childHandlerField.set(bootstrapAcceptor, ((PEChannelInitializerLegacy) oldInit).getOldChannelInitializer());
            }
        } catch (Exception e) {
            PacketEvents.get().getPlugin().getLogger().severe("PacketEvents failed to eject the injection handler! Please reboot!");
        }
    }
    injectedFutures.clear();
    for (Map<Field, Object> map : injectedLists) {
        try {
            for (Field key : map.keySet()) {
                key.setAccessible(true);
                Object o = map.get(key);
                if (o instanceof ListWrapper) {
                    key.set(o, ((ListWrapper) o).getOriginalList());
                }
            }
        } catch (IllegalAccessException e) {
            PacketEvents.get().getPlugin().getLogger().severe("PacketEvents failed to eject the injection handler! Please reboot!!");
        }
    }
    injectedLists.clear();
}
Also used : ChannelFuture(net.minecraft.util.io.netty.channel.ChannelFuture) Channel(net.minecraft.util.io.netty.channel.Channel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) NioSocketChannel(net.minecraft.util.io.netty.channel.socket.nio.NioSocketChannel) ListWrapper(io.github.retrooper.packetevents.utils.list.ListWrapper) ChannelHandler(net.minecraft.util.io.netty.channel.ChannelHandler) Field(java.lang.reflect.Field) ChannelInitializer(net.minecraft.util.io.netty.channel.ChannelInitializer)

Example 3 with ChannelHandler

use of net.minecraft.util.io.netty.channel.ChannelHandler in project packetevents by retrooper.

the class EarlyChannelInjectorLegacy method injectChannelFuture.

private void injectChannelFuture(ChannelFuture channelFuture) {
    List<String> channelHandlerNames = channelFuture.channel().pipeline().names();
    ChannelHandler bootstrapAcceptor = null;
    Field bootstrapAcceptorField = null;
    for (String handlerName : channelHandlerNames) {
        ChannelHandler handler = channelFuture.channel().pipeline().get(handlerName);
        try {
            bootstrapAcceptorField = handler.getClass().getDeclaredField("childHandler");
            bootstrapAcceptorField.setAccessible(true);
            bootstrapAcceptorField.get(handler);
            bootstrapAcceptor = handler;
        } catch (Exception ignored) {
        }
    }
    if (bootstrapAcceptor == null) {
        bootstrapAcceptor = channelFuture.channel().pipeline().first();
    }
    ChannelInitializer<?> oldChannelInitializer = null;
    try {
        oldChannelInitializer = (ChannelInitializer<?>) bootstrapAcceptorField.get(bootstrapAcceptor);
        ChannelInitializer<?> channelInitializer = new PEChannelInitializerLegacy(oldChannelInitializer);
        // Replace the old channel initializer with our own.
        bootstrapAcceptorField.setAccessible(true);
        bootstrapAcceptorField.set(bootstrapAcceptor, channelInitializer);
        injectedFutures.add(channelFuture);
    } catch (IllegalAccessException e) {
        ClassLoader cl = bootstrapAcceptor.getClass().getClassLoader();
        if (cl.getClass().getName().equals("org.bukkit.plugin.java.PluginClassLoader")) {
            PluginDescriptionFile yaml = null;
            try {
                yaml = (PluginDescriptionFile) PluginDescriptionFile.class.getDeclaredField("description").get(cl);
            } catch (IllegalAccessException | NoSuchFieldException e2) {
                e2.printStackTrace();
            }
            throw new IllegalStateException("PacketEvents failed to inject, because of " + bootstrapAcceptor.getClass().getName() + ", you might want to try running without " + yaml.getName() + "?");
        } else {
            throw new IllegalStateException("PacketEvents failed to find core component 'childHandler', please check your plugins. issue: " + bootstrapAcceptor.getClass().getName());
        }
    }
}
Also used : Field(java.lang.reflect.Field) PluginDescriptionFile(org.bukkit.plugin.PluginDescriptionFile) ChannelHandler(net.minecraft.util.io.netty.channel.ChannelHandler)

Aggregations

ChannelHandler (net.minecraft.util.io.netty.channel.ChannelHandler)3 EpollSocketChannel (io.netty.channel.epoll.EpollSocketChannel)2 Field (java.lang.reflect.Field)2 Channel (net.minecraft.util.io.netty.channel.Channel)2 NioSocketChannel (net.minecraft.util.io.netty.channel.socket.nio.NioSocketChannel)2 PlayerChannelHandlerLegacy (io.github.retrooper.packetevents.injector.legacy.PlayerChannelHandlerLegacy)1 ListWrapper (io.github.retrooper.packetevents.utils.list.ListWrapper)1 ChannelFuture (net.minecraft.util.io.netty.channel.ChannelFuture)1 ChannelInitializer (net.minecraft.util.io.netty.channel.ChannelInitializer)1 PluginDescriptionFile (org.bukkit.plugin.PluginDescriptionFile)1