|
19 | 19 | package io.github.retrooper.packetevents.injector; |
20 | 20 |
|
21 | 21 | import com.github.retrooper.packetevents.PacketEvents; |
| 22 | +import com.github.retrooper.packetevents.PacketEventsAPI; |
22 | 23 | import com.github.retrooper.packetevents.injector.ChannelInjector; |
23 | 24 | import com.github.retrooper.packetevents.protocol.player.User; |
24 | 25 | import com.github.retrooper.packetevents.util.reflection.Reflection; |
|
29 | 30 | import io.github.retrooper.packetevents.handlers.PacketEventsEncoder; |
30 | 31 | import io.netty.channel.Channel; |
31 | 32 | import io.netty.channel.ChannelInitializer; |
| 33 | +import org.jetbrains.annotations.ApiStatus; |
| 34 | +import org.spongepowered.configurate.util.CheckedConsumer; |
32 | 35 |
|
| 36 | +import java.lang.reflect.Field; |
33 | 37 | import java.lang.reflect.InvocationTargetException; |
34 | 38 | import java.lang.reflect.Method; |
35 | 39 | import java.util.function.Supplier; |
36 | 40 |
|
| 41 | +@ApiStatus.Internal |
37 | 42 | public class VelocityPipelineInjector implements ChannelInjector { |
| 43 | + |
38 | 44 | private static Class<?> CONNECTION_MANAGER_CLASS, SERVER_INITIALIZER_HOLDER_CLASS, BACKEND_INITIALIZER_HOLDER_CLASS; |
39 | 45 | private static Method SET_SERVER_INTIIALIZER, SET_BACKEND_INITIALIZER; |
| 46 | + |
| 47 | + private final PacketEventsAPI<?> packetevents; |
40 | 48 | private final ProxyServer server; |
41 | 49 |
|
42 | | - public VelocityPipelineInjector(ProxyServer server) { |
| 50 | + public VelocityPipelineInjector(PacketEventsAPI<?> packetevents, ProxyServer server) { |
| 51 | + this.packetevents = packetevents; |
43 | 52 | this.server = server; |
44 | 53 | } |
45 | 54 |
|
@@ -76,17 +85,38 @@ public void inject() { |
76 | 85 |
|
77 | 86 | @Override |
78 | 87 | public void uninject() { |
79 | | - Supplier<ChannelInitializer<Channel>> initializerHolder = getServerChannelInitializerHolder(); |
80 | | - ChannelInitializer<Channel> wrapper = initializerHolder.get(); |
81 | | - // Check if it's our initializer, could be wrapped by other plugins |
82 | | - if (wrapper instanceof VelocityChannelInitializer) { |
83 | | - try { |
84 | | - SET_SERVER_INTIIALIZER.invoke(initializerHolder, ((VelocityChannelInitializer) wrapper).getWrappedInitializer()); |
85 | | - } catch (IllegalAccessException | InvocationTargetException exception) { |
86 | | - throw new RuntimeException("Failed to uninject from frontend pipeline", exception); |
| 88 | + Supplier<ChannelInitializer<Channel>> holder = this.getServerChannelInitializerHolder(); |
| 89 | + ChannelInitializer<?> wrapper = holder.get(); |
| 90 | + CheckedConsumer<ChannelInitializer<Channel>, ReflectiveOperationException> uninjector = (initializer) -> { |
| 91 | + this.packetevents.getLogManager().info("Uninjecting from Velocity channel initializer..."); |
| 92 | + SET_SERVER_INTIIALIZER.invoke(holder, initializer); |
| 93 | + }; |
| 94 | + |
| 95 | + try { |
| 96 | + while (true) { |
| 97 | + // Check if it's our initializer, could be wrapped by other plugins |
| 98 | + if (wrapper instanceof VelocityChannelInitializer) { |
| 99 | + uninjector.accept(((VelocityChannelInitializer) wrapper).getWrappedInitializer()); |
| 100 | + break; |
| 101 | + } else { |
| 102 | + // walk up wrapper tree, if possible to find a single matching field |
| 103 | + // this accounts for other plugins (e.g. ViaVersion) also replacing the injector, which may |
| 104 | + // wrap our already wrapped injector |
| 105 | + Field field = Reflection.getField(wrapper.getClass(), ChannelInitializer.class, 0); |
| 106 | + if (field == null) { |
| 107 | + throw new IllegalStateException("Can't unwrap foreign channel initializer: " + wrapper); |
| 108 | + } |
| 109 | + field.setAccessible(true); |
| 110 | + ChannelInitializer<?> thisWrapper = wrapper; |
| 111 | + wrapper = (ChannelInitializer<?>) field.get(thisWrapper); |
| 112 | + uninjector = initializer -> { |
| 113 | + field.set(thisWrapper, initializer); |
| 114 | + this.packetevents.getLogManager().info("Uninjected from plugin channel initializer " + thisWrapper); |
| 115 | + }; |
| 116 | + } |
87 | 117 | } |
88 | | - } else { |
89 | | - throw new IllegalStateException("Failed to uninject from frontend pipeline: can't unwrap foreign channel initializer"); |
| 118 | + } catch (ReflectiveOperationException exception) { |
| 119 | + throw new RuntimeException("Failed to uninject from frontend pipeline", exception); |
90 | 120 | } |
91 | 121 | } |
92 | 122 |
|
|
0 commit comments