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();
}
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();
}
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());
}
}
}
}
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());
}
}
}
}
Aggregations