Skip to content

Commit b4f9c50

Browse files
committed
1.12 hotfix: delay injection for network manager
Fixes #332, fixes #330
1 parent 1738c6f commit b4f9c50

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

modules/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ProtocolInjector.java

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,11 @@
1616
*/
1717
package com.comphenix.protocol.injector.netty;
1818

19-
import io.netty.channel.Channel;
20-
import io.netty.channel.ChannelFuture;
21-
import io.netty.channel.ChannelHandler;
22-
import io.netty.channel.ChannelHandlerContext;
23-
import io.netty.channel.ChannelInboundHandler;
24-
import io.netty.channel.ChannelInboundHandlerAdapter;
25-
import io.netty.channel.ChannelInitializer;
26-
2719
import java.io.InputStream;
2820
import java.lang.reflect.Field;
2921
import java.lang.reflect.InvocationTargetException;
3022
import java.lang.reflect.Method;
23+
import java.lang.reflect.ParameterizedType;
3124
import java.net.InetSocketAddress;
3225
import java.util.List;
3326
import java.util.Set;
@@ -57,6 +50,14 @@
5750
import com.comphenix.protocol.utility.MinecraftReflection;
5851
import com.google.common.collect.Lists;
5952

53+
import io.netty.channel.Channel;
54+
import io.netty.channel.ChannelFuture;
55+
import io.netty.channel.ChannelHandler;
56+
import io.netty.channel.ChannelHandlerContext;
57+
import io.netty.channel.ChannelInboundHandler;
58+
import io.netty.channel.ChannelInboundHandlerAdapter;
59+
import io.netty.channel.ChannelInitializer;
60+
6061
public class ProtocolInjector implements ChannelListener {
6162
public static final ReportType REPORT_CANNOT_INJECT_INCOMING_CHANNEL = new ReportType("Unable to inject incoming channel %s.");
6263

@@ -139,11 +140,12 @@ public synchronized void inject() {
139140
// Handle connected channels
140141
final ChannelInboundHandler endInitProtocol = new ChannelInitializer<Channel>() {
141142
@Override
142-
protected void initChannel(Channel channel) throws Exception {
143+
protected void initChannel(final Channel channel) throws Exception {
143144
try {
144-
// This can take a while, so we need to stop the main thread from interfering
145+
// TODO I don't like this
145146
synchronized (networkManagers) {
146-
injectionFactory.fromChannel(channel, ProtocolInjector.this, playerFactory).inject();
147+
channel.eventLoop().submit(() ->
148+
injectionFactory.fromChannel(channel, ProtocolInjector.this, playerFactory).inject());
147149
}
148150
} catch (Exception e) {
149151
reporter.reportDetailed(ProtocolInjector.this, Report.newBuilder(REPORT_CANNOT_INJECT_INCOMING_CHANNEL).messageParam(channel).error(e));
@@ -171,9 +173,26 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
171173
ctx.fireChannelRead(msg);
172174
}
173175
};
176+
177+
FuzzyReflection fuzzy = FuzzyReflection.fromObject(serverConnection, true);
178+
179+
try {
180+
List<Field> fields = fuzzy.getFieldListByType(List.class);
181+
for (Field field : fields) {
182+
ParameterizedType param = (ParameterizedType) field.getGenericType();
183+
if (param.getActualTypeArguments()[0].equals(MinecraftReflection.getNetworkManagerClass())) {
184+
field.setAccessible(true);
185+
networkManagers = (List<Object>) field.get(serverConnection);
186+
}
187+
}
188+
} catch (Exception ex) {
189+
networkManagers = (List<Object>) fuzzy.getMethodByParameters("getNetworkManagers", List.class, serverConnection.getClass())
190+
.invoke(null, serverConnection);
191+
}
174192

175-
// Get the current NetworkMananger list
176-
networkManagers = (List<Object>) FuzzyReflection.fromObject(serverConnection, true).invokeMethod(null, "getNetworkManagers", List.class, serverConnection);
193+
if (networkManagers == null) {
194+
throw new RuntimeException("Failed to obtain list of network managers.");
195+
}
177196

178197
// Insert ProtocolLib's connection interceptor
179198
bootstrapFields = getBootstrapFields(serverConnection);
@@ -191,7 +210,6 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
191210
}
192211

193212
injected = true;
194-
195213
} catch (Exception e) {
196214
throw new RuntimeException("Unable to inject channel futures.", e);
197215
}

0 commit comments

Comments
 (0)