Search in sources :

Example 1 with ListWrapper

use of io.github.retrooper.packetevents.utils.list.ListWrapper in project packetevents by retrooper.

the class EarlyChannelInjectorModern method eject.

@Override
public void eject() {
    // TODO: Uninject from players currently online to prevent protocol lib issues.
    /* if (PaperChannelInjector.PAPER_INJECTION_METHOD) {
            try {
                PaperChannelInjector.removePaperChannelInitializeListener();
            } catch (ReflectiveOperationException e) {
                e.printStackTrace();
            }
        }*/
    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 PEChannelInitializerModern) {
                    bootstrapAcceptor = handler;
                }
            } catch (Exception ignored) {
            // 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 PEChannelInitializerModern) {
                childHandlerField.setAccessible(true);
                childHandlerField.set(bootstrapAcceptor, ((PEChannelInitializerModern) 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(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) ListWrapper(io.github.retrooper.packetevents.utils.list.ListWrapper) ChannelHandler(io.netty.channel.ChannelHandler) Field(java.lang.reflect.Field) ChannelInitializer(io.netty.channel.ChannelInitializer)

Example 2 with ListWrapper

use of io.github.retrooper.packetevents.utils.list.ListWrapper 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 ListWrapper

use of io.github.retrooper.packetevents.utils.list.ListWrapper in project packetevents by retrooper.

the class EarlyChannelInjectorLegacy method inject.

@Override
public void inject() {
    try {
        Object serverConnection = NMSUtils.getMinecraftServerConnection();
        for (Field field : serverConnection.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            Object value = null;
            try {
                value = field.get(serverConnection);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            if (value instanceof List) {
                // Get the list.
                List<?> listWrapper = new ListWrapper((List<?>) value) {

                    @Override
                    public void processAdd(Object o) {
                        if (o instanceof ChannelFuture) {
                            try {
                                injectChannelFuture((ChannelFuture) o);
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                };
                HashMap<Field, Object> map = new HashMap<>();
                map.put(field, serverConnection);
                injectedLists.add(map);
                field.set(serverConnection, listWrapper);
                synchronized (listWrapper) {
                    for (Object serverChannel : (List<?>) value) {
                        // Is this the server channel future list?
                        if (serverChannel instanceof ChannelFuture) {
                            // Yes it is...
                            injectChannelFuture((ChannelFuture) serverChannel);
                        } else {
                            // Wrong list
                            break;
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
        PacketEvents.get().getPlugin().getLogger().severe("PacketEvents failed to inject!");
        ex.printStackTrace();
    }
    // Player channels might have been registered already. Let us add our handlers. We are a little late though.
    // This only happens when you join extremely early on older versions of minecraft.
    List<Object> networkManagers = NMSUtils.getNetworkManagers();
    synchronized (networkManagers) {
        for (Object networkManager : networkManagers) {
            WrappedPacket networkManagerWrapper = new WrappedPacket(new NMSPacket(networkManager), NMSUtils.networkManagerClass);
            Channel channel = (Channel) networkManagerWrapper.readObject(0, NMSUtils.nettyChannelClass);
            if (channel == null || (!(channel.getClass().equals(NioSocketChannel.class))) && !(channel.getClass().equals(EpollSocketChannel.class))) {
                continue;
            }
            if (channel.pipeline().get(PacketEvents.get().getHandlerName()) != null) {
                channel.pipeline().remove(PacketEvents.get().getHandlerName());
            }
            if (channel.pipeline().get("packet_handler") != null) {
                channel.pipeline().addBefore("packet_handler", PacketEvents.get().getHandlerName(), new PlayerChannelHandlerLegacy());
            }
        }
    }
}
Also used : ChannelFuture(net.minecraft.util.io.netty.channel.ChannelFuture) HashMap(java.util.HashMap) WrappedPacket(io.github.retrooper.packetevents.packetwrappers.WrappedPacket) 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) NioSocketChannel(net.minecraft.util.io.netty.channel.socket.nio.NioSocketChannel) Field(java.lang.reflect.Field) PlayerChannelHandlerLegacy(io.github.retrooper.packetevents.injector.legacy.PlayerChannelHandlerLegacy) ArrayList(java.util.ArrayList) List(java.util.List) NMSPacket(io.github.retrooper.packetevents.packetwrappers.NMSPacket)

Example 4 with ListWrapper

use of io.github.retrooper.packetevents.utils.list.ListWrapper in project packetevents by retrooper.

the class EarlyChannelInjectorModern method inject.

@Override
public void inject() {
    try {
        /* if (PaperChannelInjector.PAPER_INJECTION_METHOD) {
                PaperChannelInjector.setPaperChannelInitializeListener();
                return;
            }*/
        Object serverConnection = NMSUtils.getMinecraftServerConnection();
        for (Field field : serverConnection.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            Object value = null;
            try {
                value = field.get(serverConnection);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            if (value instanceof List) {
                // Get the list.
                List listWrapper = new ListWrapper((List) value) {

                    @Override
                    public void processAdd(Object o) {
                        if (o instanceof ChannelFuture) {
                            try {
                                injectChannelFuture((ChannelFuture) o);
                            } catch (Exception ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                };
                HashMap<Field, Object> map = new HashMap<>();
                map.put(field, serverConnection);
                injectedLists.add(map);
                field.set(serverConnection, listWrapper);
                synchronized (listWrapper) {
                    for (Object serverChannel : (List) value) {
                        // Is this the server channel future list?
                        if (serverChannel instanceof ChannelFuture) {
                            // Yes it is...
                            injectChannelFuture((ChannelFuture) serverChannel);
                        } else {
                            // Wrong list
                            break;
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
        throw new IllegalStateException("PacketEvents failed to inject!", ex);
    }
    // Player channels might have been registered already. Let us add our handlers. We are a little late though.
    // This only happens when you join extremely early on older versions of minecraft.
    List<Object> networkManagers = NMSUtils.getNetworkManagers();
    synchronized (networkManagers) {
        for (Object networkManager : networkManagers) {
            WrappedPacket networkManagerWrapper = new WrappedPacket(new NMSPacket(networkManager), NMSUtils.networkManagerClass);
            Channel channel = (Channel) networkManagerWrapper.readObject(0, NMSUtils.nettyChannelClass);
            if (channel == null || (!(channel.getClass().equals(NioSocketChannel.class))) && !(channel.getClass().equals(EpollSocketChannel.class))) {
                continue;
            }
            if (channel.pipeline().get(PacketEvents.get().getHandlerName()) != null) {
                channel.pipeline().remove(PacketEvents.get().getHandlerName());
            }
            if (channel.pipeline().get("packet_handler") != null) {
                channel.pipeline().addBefore("packet_handler", PacketEvents.get().getHandlerName(), new PlayerChannelHandlerModern());
            }
        }
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) PlayerChannelHandlerModern(io.github.retrooper.packetevents.injector.modern.PlayerChannelHandlerModern) HashMap(java.util.HashMap) WrappedPacket(io.github.retrooper.packetevents.packetwrappers.WrappedPacket) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) ListWrapper(io.github.retrooper.packetevents.utils.list.ListWrapper) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) List(java.util.List) NMSPacket(io.github.retrooper.packetevents.packetwrappers.NMSPacket)

Aggregations

ListWrapper (io.github.retrooper.packetevents.utils.list.ListWrapper)4 EpollSocketChannel (io.netty.channel.epoll.EpollSocketChannel)4 Field (java.lang.reflect.Field)4 NMSPacket (io.github.retrooper.packetevents.packetwrappers.NMSPacket)2 WrappedPacket (io.github.retrooper.packetevents.packetwrappers.WrappedPacket)2 Channel (io.netty.channel.Channel)2 ChannelFuture (io.netty.channel.ChannelFuture)2 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Channel (net.minecraft.util.io.netty.channel.Channel)2 ChannelFuture (net.minecraft.util.io.netty.channel.ChannelFuture)2 NioSocketChannel (net.minecraft.util.io.netty.channel.socket.nio.NioSocketChannel)2 PlayerChannelHandlerLegacy (io.github.retrooper.packetevents.injector.legacy.PlayerChannelHandlerLegacy)1 PlayerChannelHandlerModern (io.github.retrooper.packetevents.injector.modern.PlayerChannelHandlerModern)1 ChannelHandler (io.netty.channel.ChannelHandler)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 ChannelHandler (net.minecraft.util.io.netty.channel.ChannelHandler)1 ChannelInitializer (net.minecraft.util.io.netty.channel.ChannelInitializer)1