3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-03 14:50:30 +01:00

Only inject channels when server protocol is known #631

Dieser Commit ist enthalten in:
Myles 2017-02-13 17:22:52 +00:00
Ursprung 280da6b8e9
Commit 99e4ab22e3
2 geänderte Dateien mit 37 neuen und 25 gelöschten Zeilen

Datei anzeigen

@ -7,6 +7,7 @@ import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToByteEncoder;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.protocol.ProtocolPipeline; import us.myles.ViaVersion.api.protocol.ProtocolPipeline;
import us.myles.ViaVersion.api.protocol.ProtocolRegistry;
import us.myles.ViaVersion.bukkit.classgenerator.ClassGenerator; import us.myles.ViaVersion.bukkit.classgenerator.ClassGenerator;
import us.myles.ViaVersion.bukkit.classgenerator.HandlerConstructor; import us.myles.ViaVersion.bukkit.classgenerator.HandlerConstructor;
@ -33,20 +34,25 @@ public class BukkitChannelInitializer extends ChannelInitializer<SocketChannel>
@Override @Override
protected void initChannel(SocketChannel socketChannel) throws Exception { protected void initChannel(SocketChannel socketChannel) throws Exception {
UserConnection info = new UserConnection(socketChannel); // Ensure ViaVersion is loaded
// init protocol if (ProtocolRegistry.SERVER_PROTOCOL != -1) {
new ProtocolPipeline(info); UserConnection info = new UserConnection(socketChannel);
// Add originals // init protocol
this.method.invoke(this.original, socketChannel); new ProtocolPipeline(info);
// Add originals
this.method.invoke(this.original, socketChannel);
HandlerConstructor constructor = ClassGenerator.getConstructor(); HandlerConstructor constructor = ClassGenerator.getConstructor();
// Add our transformers // Add our transformers
MessageToByteEncoder encoder = constructor.newEncodeHandler(info, (MessageToByteEncoder) socketChannel.pipeline().get("encoder")); MessageToByteEncoder encoder = constructor.newEncodeHandler(info, (MessageToByteEncoder) socketChannel.pipeline().get("encoder"));
ByteToMessageDecoder decoder = constructor.newDecodeHandler(info, (ByteToMessageDecoder) socketChannel.pipeline().get("decoder")); ByteToMessageDecoder decoder = constructor.newDecodeHandler(info, (ByteToMessageDecoder) socketChannel.pipeline().get("decoder"));
BukkitPacketHandler chunkHandler = new BukkitPacketHandler(info); BukkitPacketHandler chunkHandler = new BukkitPacketHandler(info);
socketChannel.pipeline().replace("encoder", "encoder", encoder); socketChannel.pipeline().replace("encoder", "encoder", encoder);
socketChannel.pipeline().replace("decoder", "decoder", decoder); socketChannel.pipeline().replace("decoder", "decoder", decoder);
socketChannel.pipeline().addAfter("packet_handler", "viaversion_packet_handler", chunkHandler); socketChannel.pipeline().addAfter("packet_handler", "viaversion_packet_handler", chunkHandler);
} else {
this.method.invoke(this.original, socketChannel);
}
} }
} }

Datei anzeigen

@ -7,6 +7,7 @@ import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToByteEncoder;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.protocol.ProtocolPipeline; import us.myles.ViaVersion.api.protocol.ProtocolPipeline;
import us.myles.ViaVersion.api.protocol.ProtocolRegistry;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -31,18 +32,23 @@ public class SpongeChannelInitializer extends ChannelInitializer<SocketChannel>
@Override @Override
protected void initChannel(SocketChannel socketChannel) throws Exception { protected void initChannel(SocketChannel socketChannel) throws Exception {
UserConnection info = new UserConnection(socketChannel); // Ensure ViaVersion is loaded
// init protocol if (ProtocolRegistry.SERVER_PROTOCOL != -1) {
new ProtocolPipeline(info); UserConnection info = new UserConnection(socketChannel);
// Add originals // init protocol
this.method.invoke(this.original, socketChannel); new ProtocolPipeline(info);
// Add our transformers // Add originals
MessageToByteEncoder encoder = new SpongeEncodeHandler(info, (MessageToByteEncoder) socketChannel.pipeline().get("encoder")); this.method.invoke(this.original, socketChannel);
ByteToMessageDecoder decoder = new SpongeDecodeHandler(info, (ByteToMessageDecoder) socketChannel.pipeline().get("decoder")); // Add our transformers
SpongePacketHandler chunkHandler = new SpongePacketHandler(info); MessageToByteEncoder encoder = new SpongeEncodeHandler(info, (MessageToByteEncoder) socketChannel.pipeline().get("encoder"));
ByteToMessageDecoder decoder = new SpongeDecodeHandler(info, (ByteToMessageDecoder) socketChannel.pipeline().get("decoder"));
SpongePacketHandler chunkHandler = new SpongePacketHandler(info);
socketChannel.pipeline().replace("encoder", "encoder", encoder); socketChannel.pipeline().replace("encoder", "encoder", encoder);
socketChannel.pipeline().replace("decoder", "decoder", decoder); socketChannel.pipeline().replace("decoder", "decoder", decoder);
socketChannel.pipeline().addAfter("packet_handler", "viaversion_packet_handler", chunkHandler); socketChannel.pipeline().addAfter("packet_handler", "viaversion_packet_handler", chunkHandler);
} else {
this.method.invoke(this.original, socketChannel);
}
} }
} }