3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-26 00:00:28 +01:00

Fix Bungee Support (Excluding Chunks) + Rename Handlers

Dieser Commit ist enthalten in:
Myles 2016-09-29 15:25:18 +01:00
Ursprung 0454169588
Commit 36301a595b
18 geänderte Dateien mit 115 neuen und 160 gelöschten Zeilen

Datei anzeigen

@ -3,17 +3,17 @@ package us.myles.ViaVersion.bukkit.classgenerator;
import io.netty.handler.codec.ByteToMessageDecoder; 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.bukkit.handlers.ViaDecodeHandler; import us.myles.ViaVersion.bukkit.handlers.BukkitDecodeHandler;
import us.myles.ViaVersion.bukkit.handlers.ViaEncodeHandler; import us.myles.ViaVersion.bukkit.handlers.BukkitEncodeHandler;
public class BasicHandlerConstructor implements HandlerConstructor { public class BasicHandlerConstructor implements HandlerConstructor {
@Override @Override
public ViaEncodeHandler newEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) { public BukkitEncodeHandler newEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) {
return new ViaEncodeHandler(info, minecraftEncoder); return new BukkitEncodeHandler(info, minecraftEncoder);
} }
@Override @Override
public ViaDecodeHandler newDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder) { public BukkitDecodeHandler newDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder) {
return new ViaDecodeHandler(info, minecraftDecoder); return new BukkitDecodeHandler(info, minecraftDecoder);
} }
} }

Datei anzeigen

@ -6,8 +6,8 @@ import javassist.expr.ExprEditor;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.ViaVersion;
import us.myles.ViaVersion.bukkit.handlers.ViaDecodeHandler; import us.myles.ViaVersion.bukkit.handlers.BukkitDecodeHandler;
import us.myles.ViaVersion.bukkit.handlers.ViaEncodeHandler; import us.myles.ViaVersion.bukkit.handlers.BukkitEncodeHandler;
import us.myles.ViaVersion.bukkit.util.NMSUtil; import us.myles.ViaVersion.bukkit.util.NMSUtil;
public class ClassGenerator { public class ClassGenerator {
@ -30,14 +30,14 @@ public class ClassGenerator {
Class decodeSuper = NMSUtil.nms("PacketDecoder"); Class decodeSuper = NMSUtil.nms("PacketDecoder");
Class encodeSuper = NMSUtil.nms("PacketEncoder"); Class encodeSuper = NMSUtil.nms("PacketEncoder");
// Generate the classes // Generate the classes
addSpigotCompatibility(pool, ViaDecodeHandler.class, decodeSuper); addSpigotCompatibility(pool, BukkitDecodeHandler.class, decodeSuper);
addSpigotCompatibility(pool, ViaEncodeHandler.class, encodeSuper); addSpigotCompatibility(pool, BukkitEncodeHandler.class, encodeSuper);
} else { } else {
Class decodeSuper = Class.forName(getPSPackage() + ".wrapped.WrappedDecoder"); Class decodeSuper = Class.forName(getPSPackage() + ".wrapped.WrappedDecoder");
Class encodeSuper = Class.forName(getPSPackage() + ".wrapped.WrappedEncoder"); Class encodeSuper = Class.forName(getPSPackage() + ".wrapped.WrappedEncoder");
// Generate the classes // Generate the classes
addPSCompatibility(pool, ViaDecodeHandler.class, decodeSuper); addPSCompatibility(pool, BukkitDecodeHandler.class, decodeSuper);
addPSCompatibility(pool, ViaEncodeHandler.class, encodeSuper); addPSCompatibility(pool, BukkitEncodeHandler.class, encodeSuper);
} }
@ -53,10 +53,10 @@ public class ClassGenerator {
pool.importPackage("io.netty.handler.codec"); pool.importPackage("io.netty.handler.codec");
// Implement Methods // Implement Methods
generated.addMethod(CtMethod.make("public MessageToByteEncoder newEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) {\n" + generated.addMethod(CtMethod.make("public MessageToByteEncoder newEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) {\n" +
" return new ViaEncodeHandler(info, minecraftEncoder);\n" + " return new BukkitEncodeHandler(info, minecraftEncoder);\n" +
" }", generated)); " }", generated));
generated.addMethod(CtMethod.make("public ByteToMessageDecoder newDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder) {\n" + generated.addMethod(CtMethod.make("public ByteToMessageDecoder newDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder) {\n" +
" return new ViaDecodeHandler(info, minecraftDecoder);\n" + " return new BukkitDecodeHandler(info, minecraftDecoder);\n" +
" }", generated)); " }", generated));
constructor = (HandlerConstructor) generated.toClass(HandlerConstructor.class.getClassLoader()).newInstance(); constructor = (HandlerConstructor) generated.toClass(HandlerConstructor.class.getClassLoader()).newInstance();

Datei anzeigen

@ -12,12 +12,12 @@ import us.myles.ViaVersion.bukkit.classgenerator.HandlerConstructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public class ViaVersionInitializer extends ChannelInitializer<SocketChannel> { public class BukkitChannelInitializer extends ChannelInitializer<SocketChannel> {
private final ChannelInitializer<SocketChannel> original; private final ChannelInitializer<SocketChannel> original;
private Method method; private Method method;
public ViaVersionInitializer(ChannelInitializer<SocketChannel> oldInit) { public BukkitChannelInitializer(ChannelInitializer<SocketChannel> oldInit) {
this.original = oldInit; this.original = oldInit;
try { try {
this.method = ChannelInitializer.class.getDeclaredMethod("initChannel", Channel.class); this.method = ChannelInitializer.class.getDeclaredMethod("initChannel", Channel.class);
@ -43,7 +43,7 @@ public class ViaVersionInitializer extends ChannelInitializer<SocketChannel> {
// 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"));
ViaPacketHandler chunkHandler = new ViaPacketHandler(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);

Datei anzeigen

@ -14,12 +14,12 @@ import us.myles.ViaVersion.util.PipelineUtil;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.List; import java.util.List;
public class ViaDecodeHandler extends ByteToMessageDecoder { public class BukkitDecodeHandler extends ByteToMessageDecoder {
private final ByteToMessageDecoder minecraftDecoder; private final ByteToMessageDecoder minecraftDecoder;
private final UserConnection info; private final UserConnection info;
public ViaDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder) { public BukkitDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder) {
this.info = info; this.info = info;
this.minecraftDecoder = minecraftDecoder; this.minecraftDecoder = minecraftDecoder;
} }

Datei anzeigen

@ -16,11 +16,11 @@ import us.myles.ViaVersion.util.PipelineUtil;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
public class ViaEncodeHandler extends MessageToByteEncoder { public class BukkitEncodeHandler extends MessageToByteEncoder {
private final UserConnection info; private final UserConnection info;
private final MessageToByteEncoder minecraftEncoder; private final MessageToByteEncoder minecraftEncoder;
public ViaEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) { public BukkitEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) {
this.info = info; this.info = info;
this.minecraftEncoder = minecraftEncoder; this.minecraftEncoder = minecraftEncoder;
} }

Datei anzeigen

@ -8,10 +8,10 @@ import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import java.util.List; import java.util.List;
public class ViaPacketHandler extends MessageToMessageEncoder { public class BukkitPacketHandler extends MessageToMessageEncoder {
private final UserConnection info; private final UserConnection info;
public ViaPacketHandler(UserConnection info) { public BukkitPacketHandler(UserConnection info) {
this.info = info; this.info = info;
} }

Datei anzeigen

@ -8,7 +8,7 @@ import org.bukkit.plugin.PluginDescriptionFile;
import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.Pair;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.platform.ViaInjector; import us.myles.ViaVersion.api.platform.ViaInjector;
import us.myles.ViaVersion.bukkit.handlers.ViaVersionInitializer; import us.myles.ViaVersion.bukkit.handlers.BukkitChannelInitializer;
import us.myles.ViaVersion.util.ConcurrentList; import us.myles.ViaVersion.util.ConcurrentList;
import us.myles.ViaVersion.util.ListWrapper; import us.myles.ViaVersion.util.ListWrapper;
import us.myles.ViaVersion.bukkit.util.NMSUtil; import us.myles.ViaVersion.bukkit.util.NMSUtil;
@ -75,7 +75,7 @@ public class BukkitViaInjector implements ViaInjector {
ChannelHandler bootstrapAcceptor = future.channel().pipeline().first(); ChannelHandler bootstrapAcceptor = future.channel().pipeline().first();
try { try {
ChannelInitializer<SocketChannel> oldInit = ReflectionUtil.get(bootstrapAcceptor, "childHandler", ChannelInitializer.class); ChannelInitializer<SocketChannel> oldInit = ReflectionUtil.get(bootstrapAcceptor, "childHandler", ChannelInitializer.class);
ChannelInitializer newInit = new ViaVersionInitializer(oldInit); ChannelInitializer newInit = new BukkitChannelInitializer(oldInit);
ReflectionUtil.set(bootstrapAcceptor, "childHandler", newInit); ReflectionUtil.set(bootstrapAcceptor, "childHandler", newInit);
injectedFutures.add(future); injectedFutures.add(future);
@ -103,8 +103,8 @@ public class BukkitViaInjector implements ViaInjector {
ChannelHandler bootstrapAcceptor = future.channel().pipeline().first(); ChannelHandler bootstrapAcceptor = future.channel().pipeline().first();
try { try {
ChannelInitializer<SocketChannel> oldInit = ReflectionUtil.get(bootstrapAcceptor, "childHandler", ChannelInitializer.class); ChannelInitializer<SocketChannel> oldInit = ReflectionUtil.get(bootstrapAcceptor, "childHandler", ChannelInitializer.class);
if (oldInit instanceof ViaVersionInitializer) { if (oldInit instanceof BukkitChannelInitializer) {
ReflectionUtil.set(bootstrapAcceptor, "childHandler", ((ViaVersionInitializer) oldInit).getOriginal()); ReflectionUtil.set(bootstrapAcceptor, "childHandler", ((BukkitChannelInitializer) oldInit).getOriginal());
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println("Failed to remove injection handler, reload won't work with connections, please reboot!"); System.out.println("Failed to remove injection handler, reload won't work with connections, please reboot!");

Datei anzeigen

@ -3,19 +3,17 @@ package us.myles.ViaVersion.bungee.handlers;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import net.md_5.bungee.protocol.MinecraftDecoder;
import net.md_5.bungee.protocol.MinecraftEncoder;
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 java.lang.reflect.Method; import java.lang.reflect.Method;
public class ViaVersionInitializer extends ChannelInitializer<SocketChannel> { public class BungeeChannelInitializer extends ChannelInitializer<SocketChannel> {
private final ChannelInitializer<Channel> original; private final ChannelInitializer<Channel> original;
private Method method; private Method method;
public ViaVersionInitializer(ChannelInitializer<Channel> oldInit) { public BungeeChannelInitializer(ChannelInitializer<Channel> oldInit) {
this.original = oldInit; this.original = oldInit;
try { try {
this.method = ChannelInitializer.class.getDeclaredMethod("initChannel", Channel.class); this.method = ChannelInitializer.class.getDeclaredMethod("initChannel", Channel.class);
@ -25,10 +23,6 @@ public class ViaVersionInitializer extends ChannelInitializer<SocketChannel> {
} }
} }
public ChannelInitializer<Channel> getOriginal() {
return original;
}
@Override @Override
protected void initChannel(SocketChannel socketChannel) throws Exception { protected void initChannel(SocketChannel socketChannel) throws Exception {
UserConnection info = new UserConnection(socketChannel); UserConnection info = new UserConnection(socketChannel);
@ -37,12 +31,11 @@ public class ViaVersionInitializer extends ChannelInitializer<SocketChannel> {
// Add originals // Add originals
this.method.invoke(this.original, socketChannel); this.method.invoke(this.original, socketChannel);
// Add our transformers // Add our transformers
ViaEncodeHandler encoder = new ViaEncodeHandler(info, (MinecraftEncoder) socketChannel.pipeline().get("packet-encoder")); BungeeEncodeHandler encoder = new BungeeEncodeHandler(info);
ViaDecodeHandler decoder = new ViaDecodeHandler(info, (MinecraftDecoder) socketChannel.pipeline().get("packet-decoder")); BungeeDecodeHandler decoder = new BungeeDecodeHandler(info);
// ViaPacketHandler chunkHandler = new ViaPacketHandler(info);
socketChannel.pipeline().addBefore("packet-encoder", "via-encoder", encoder);
socketChannel.pipeline().addBefore("packet-decoder", "via-decoder", decoder);
socketChannel.pipeline().replace("packet-encoder", "packet-encoder", encoder);
socketChannel.pipeline().replace("packet-decoder", "packet-decoder", decoder);
// socketChannel.pipeline().addAfter("packet_handler", "viaversion_packet_handler", chunkHandler);
} }
} }

Datei anzeigen

@ -1,35 +1,30 @@
package us.myles.ViaVersion.bungee.handlers; package us.myles.ViaVersion.bungee.handlers;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import net.md_5.bungee.api.ProxyServer; import io.netty.handler.codec.MessageToMessageDecoder;
import net.md_5.bungee.protocol.MinecraftDecoder;
import net.md_5.bungee.protocol.Protocol;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.bungee.util.BungeePipelineUtil;
import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.exception.CancelException;
import us.myles.ViaVersion.packets.Direction; import us.myles.ViaVersion.packets.Direction;
import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.util.PipelineUtil; import us.myles.ViaVersion.util.PipelineUtil;
import java.lang.reflect.InvocationTargetException;
import java.util.List; import java.util.List;
public class ViaDecodeHandler extends MinecraftDecoder { @ChannelHandler.Sharable
public class BungeeDecodeHandler extends MessageToMessageDecoder<ByteBuf> {
private final MinecraftDecoder minecraftDecoder;
private final UserConnection info; private final UserConnection info;
public ViaDecodeHandler(UserConnection info, MinecraftDecoder minecraftDecoder) { public BungeeDecodeHandler(UserConnection info) {
super(Protocol.HANDSHAKE, true, ProxyServer.getInstance().getProtocolVersion());
this.info = info; this.info = info;
this.minecraftDecoder = minecraftDecoder;
} }
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf bytebuf, List<Object> list) throws Exception { protected void decode(final ChannelHandlerContext ctx, ByteBuf bytebuf, List<Object> out) throws Exception {
// use transformers // use transformers
if (bytebuf.readableBytes() > 0) { if (bytebuf.readableBytes() > 0) {
// Ignore if pending disconnect // Ignore if pending disconnect
@ -68,20 +63,11 @@ public class ViaDecodeHandler extends MinecraftDecoder {
newPacket.release(); newPacket.release();
throw e; throw e;
} }
} else {
bytebuf.retain();
} }
// call minecraft decoder out.add(bytebuf);
try {
list.addAll(BungeePipelineUtil.callDecode(this.minecraftDecoder, ctx, bytebuf));
} catch (InvocationTargetException e) {
if (e.getCause() instanceof Exception) {
throw (Exception) e.getCause();
}
} finally {
if (info.isActive()) {
bytebuf.release();
}
}
} }
} }
@ -90,14 +76,4 @@ public class ViaDecodeHandler extends MinecraftDecoder {
if (PipelineUtil.containsCause(cause, CancelException.class)) return; if (PipelineUtil.containsCause(cause, CancelException.class)) return;
super.exceptionCaught(ctx, cause); super.exceptionCaught(ctx, cause);
} }
@Override
public void setProtocol(Protocol protocol) {
this.minecraftDecoder.setProtocol(protocol);
}
@Override
public void setProtocolVersion(int protocolVersion) {
this.minecraftDecoder.setProtocolVersion(protocolVersion);
}
} }

Datei anzeigen

@ -1,11 +1,9 @@
package us.myles.ViaVersion.bungee.handlers; package us.myles.ViaVersion.bungee.handlers;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import net.md_5.bungee.api.ProxyServer; import io.netty.handler.codec.MessageToMessageEncoder;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.MinecraftEncoder;
import net.md_5.bungee.protocol.Protocol;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
@ -15,32 +13,38 @@ import us.myles.ViaVersion.packets.Direction;
import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.util.PipelineUtil; import us.myles.ViaVersion.util.PipelineUtil;
import java.lang.reflect.InvocationTargetException; import java.util.List;
public class ViaEncodeHandler extends MinecraftEncoder { @ChannelHandler.Sharable
public class BungeeEncodeHandler extends MessageToMessageEncoder<ByteBuf> {
private final UserConnection info; private final UserConnection info;
private final MinecraftEncoder minecraftEncoder; private boolean handledCompression = false;
public ViaEncodeHandler(UserConnection info, MinecraftEncoder minecraftEncoder) { public BungeeEncodeHandler(UserConnection info) {
super(Protocol.HANDSHAKE, true, ProxyServer.getInstance().getProtocolVersion());
this.info = info; this.info = info;
this.minecraftEncoder = minecraftEncoder;
} }
@Override @Override
protected void encode(final ChannelHandlerContext ctx, DefinedPacket o, final ByteBuf bytebuf) throws Exception { protected void encode(final ChannelHandlerContext ctx, ByteBuf bytebuf, List<Object> out) throws Exception {
// call minecraft encoder
try {
BungeePipelineUtil.callEncode(this.minecraftEncoder, ctx, o, bytebuf);
} catch (InvocationTargetException e) {
if (e.getCause() instanceof Exception) {
throw (Exception) e.getCause();
}
}
if (bytebuf.readableBytes() == 0) { if (bytebuf.readableBytes() == 0) {
throw new CancelException(); throw new CancelException();
} }
boolean needsCompress = false;
if (!handledCompression) {
if (ctx.pipeline().names().indexOf("compress") > ctx.pipeline().names().indexOf("via-encoder")) {
// Need to decompress this packet due to bad order
bytebuf = BungeePipelineUtil.decompress(ctx, bytebuf);
ChannelHandler encoder = ctx.pipeline().get("via-decoder");
ChannelHandler decoder = ctx.pipeline().get("via-encoder");
ctx.pipeline().remove(encoder);
ctx.pipeline().remove(decoder);
ctx.pipeline().addAfter("decompress", "via-decoder", encoder);
ctx.pipeline().addAfter("compress", "via-encoder", decoder);
needsCompress = true;
handledCompression = true;
}
}
// Increment sent // Increment sent
info.incrementSent(); info.incrementSent();
if (info.isActive()) { if (info.isActive()) {
@ -62,6 +66,11 @@ public class ViaEncodeHandler extends MinecraftEncoder {
oldPacket.release(); oldPacket.release();
} }
} }
if (needsCompress) {
bytebuf = BungeePipelineUtil.compress(ctx, bytebuf);
}
out.add(bytebuf.retain());
} }
@Override @Override
@ -70,13 +79,4 @@ public class ViaEncodeHandler extends MinecraftEncoder {
super.exceptionCaught(ctx, cause); super.exceptionCaught(ctx, cause);
} }
@Override
public void setProtocol(Protocol protocol) {
this.minecraftEncoder.setProtocol(protocol);
}
@Override
public void setProtocolVersion(int protocolVersion) {
this.minecraftEncoder.setProtocolVersion(protocolVersion);
}
} }

Datei anzeigen

@ -1,35 +0,0 @@
package us.myles.ViaVersion.bungee.handlers;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import java.util.List;
public class ViaPacketHandler extends MessageToMessageEncoder {
private final UserConnection info;
public ViaPacketHandler(UserConnection info) {
this.info = info;
}
@Override
protected void encode(ChannelHandlerContext ctx, Object o, List list) throws Exception {
// Split chunks bulk packet up in to single chunks packets before it reached the encoder.
// This will prevent issues with several plugins and other protocol handlers due to the chunks being sent twice.
// It also sends the chunks in the right order possible resolving some issues with added chunks/block/entity data.
if (!(o instanceof ByteBuf)) {
info.setLastPacket(o);
/* This transformer is more for fixing issues which we find hard at packet level :) */
if (info.isActive()) {
if (info.get(ProtocolInfo.class).getPipeline().filter(o, list)) {
return;
}
}
}
list.add(o);
}
}

Datei anzeigen

@ -5,7 +5,7 @@ import io.netty.channel.ChannelInitializer;
import net.md_5.bungee.netty.PipelineUtils; import net.md_5.bungee.netty.PipelineUtils;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.platform.ViaInjector; import us.myles.ViaVersion.api.platform.ViaInjector;
import us.myles.ViaVersion.bungee.handlers.ViaVersionInitializer; import us.myles.ViaVersion.bungee.handlers.BungeeChannelInitializer;
import us.myles.ViaVersion.util.ReflectionUtil; import us.myles.ViaVersion.util.ReflectionUtil;
public class BungeeViaInjector implements ViaInjector { public class BungeeViaInjector implements ViaInjector {
@ -14,7 +14,7 @@ public class BungeeViaInjector implements ViaInjector {
try { try {
try { try {
ChannelInitializer<Channel> oldInit = PipelineUtils.SERVER_CHILD; ChannelInitializer<Channel> oldInit = PipelineUtils.SERVER_CHILD;
ChannelInitializer newInit = new ViaVersionInitializer(oldInit); ChannelInitializer newInit = new BungeeChannelInitializer(oldInit);
ReflectionUtil.setStatic(PipelineUtils.class, "SERVER_CHILD", newInit); ReflectionUtil.setStatic(PipelineUtils.class, "SERVER_CHILD", newInit);
} catch (NoSuchFieldException e) { } catch (NoSuchFieldException e) {
@ -41,6 +41,6 @@ public class BungeeViaInjector implements ViaInjector {
@Override @Override
public String getEncoderName() { public String getEncoderName() {
return "packet-encoder"; return "via-encoder";
} }
} }

Datei anzeigen

@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.MessageToMessageDecoder; import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.MessageToMessageEncoder;
import net.md_5.bungee.protocol.DefinedPacket; import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.MinecraftDecoder; import net.md_5.bungee.protocol.MinecraftDecoder;
import net.md_5.bungee.protocol.MinecraftEncoder; import net.md_5.bungee.protocol.MinecraftEncoder;
@ -19,14 +20,14 @@ public class BungeePipelineUtil {
static { static {
try { try {
DECODE_METHOD = MinecraftDecoder.class.getDeclaredMethod("decode", ChannelHandlerContext.class, ByteBuf.class, List.class); DECODE_METHOD = MessageToMessageDecoder.class.getDeclaredMethod("decode", ChannelHandlerContext.class, Object.class, List.class);
DECODE_METHOD.setAccessible(true); DECODE_METHOD.setAccessible(true);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
e.printStackTrace(); e.printStackTrace();
System.out.println("Netty issue?"); System.out.println("Netty issue?");
} }
try { try {
ENCODE_METHOD = MinecraftEncoder.class.getDeclaredMethod("encode", ChannelHandlerContext.class, DefinedPacket.class, ByteBuf.class); ENCODE_METHOD = MessageToByteEncoder.class.getDeclaredMethod("encode", ChannelHandlerContext.class, Object.class, ByteBuf.class);
ENCODE_METHOD.setAccessible(true); ENCODE_METHOD.setAccessible(true);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
e.printStackTrace(); e.printStackTrace();
@ -34,7 +35,7 @@ public class BungeePipelineUtil {
} }
} }
public static List<Object> callDecode(MessageToMessageDecoder decoder, ChannelHandlerContext ctx, Object input) throws InvocationTargetException { public static List<Object> callDecode(MessageToMessageDecoder decoder, ChannelHandlerContext ctx, ByteBuf input) throws InvocationTargetException {
List<Object> output = new ArrayList<>(); List<Object> output = new ArrayList<>();
try { try {
BungeePipelineUtil.DECODE_METHOD.invoke(decoder, ctx, input, output); BungeePipelineUtil.DECODE_METHOD.invoke(decoder, ctx, input, output);
@ -44,11 +45,31 @@ public class BungeePipelineUtil {
return output; return output;
} }
public static void callEncode(MessageToByteEncoder encoder, ChannelHandlerContext ctx, Object msg, ByteBuf output) throws InvocationTargetException { public static ByteBuf callEncode(MessageToByteEncoder encoder, ChannelHandlerContext ctx, ByteBuf input) throws InvocationTargetException {
ByteBuf output = ctx.alloc().buffer();
try { try {
BungeePipelineUtil.ENCODE_METHOD.invoke(encoder, ctx, msg, output); BungeePipelineUtil.ENCODE_METHOD.invoke(encoder, ctx, input, output);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
} }
return output;
}
public static ByteBuf decompress(ChannelHandlerContext ctx, ByteBuf bytebuf) {
try {
return (ByteBuf) callDecode((MessageToMessageDecoder) ctx.pipeline().get("decompress"), ctx.pipeline().context("decompress"), bytebuf).get(0);
} catch (InvocationTargetException e) {
e.printStackTrace();
return ctx.alloc().buffer();
}
}
public static ByteBuf compress(ChannelHandlerContext ctx, ByteBuf bytebuf) {
try {
return callEncode((MessageToByteEncoder) ctx.pipeline().get("compress"), ctx.pipeline().context("compress"), bytebuf);
} catch (InvocationTargetException e) {
e.printStackTrace();
return ctx.alloc().buffer();
}
} }
} }

Datei anzeigen

@ -10,12 +10,12 @@ import us.myles.ViaVersion.api.protocol.ProtocolPipeline;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public class ViaVersionInitializer extends ChannelInitializer<SocketChannel> { public class SpongeChannelInitializer extends ChannelInitializer<SocketChannel> {
private final ChannelInitializer<SocketChannel> original; private final ChannelInitializer<SocketChannel> original;
private Method method; private Method method;
public ViaVersionInitializer(ChannelInitializer<SocketChannel> oldInit) { public SpongeChannelInitializer(ChannelInitializer<SocketChannel> oldInit) {
this.original = oldInit; this.original = oldInit;
try { try {
this.method = ChannelInitializer.class.getDeclaredMethod("initChannel", Channel.class); this.method = ChannelInitializer.class.getDeclaredMethod("initChannel", Channel.class);
@ -37,9 +37,9 @@ public class ViaVersionInitializer extends ChannelInitializer<SocketChannel> {
// Add originals // Add originals
this.method.invoke(this.original, socketChannel); this.method.invoke(this.original, socketChannel);
// Add our transformers // Add our transformers
MessageToByteEncoder encoder = new ViaEncodeHandler(info, (MessageToByteEncoder) socketChannel.pipeline().get("encoder")); MessageToByteEncoder encoder = new SpongeEncodeHandler(info, (MessageToByteEncoder) socketChannel.pipeline().get("encoder"));
ByteToMessageDecoder decoder = new ViaDecodeHandler(info, (ByteToMessageDecoder) socketChannel.pipeline().get("decoder")); ByteToMessageDecoder decoder = new SpongeDecodeHandler(info, (ByteToMessageDecoder) socketChannel.pipeline().get("decoder"));
ViaPacketHandler chunkHandler = new ViaPacketHandler(info); 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);

Datei anzeigen

@ -14,12 +14,12 @@ import us.myles.ViaVersion.util.PipelineUtil;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.List; import java.util.List;
public class ViaDecodeHandler extends ByteToMessageDecoder { public class SpongeDecodeHandler extends ByteToMessageDecoder {
private final ByteToMessageDecoder minecraftDecoder; private final ByteToMessageDecoder minecraftDecoder;
private final UserConnection info; private final UserConnection info;
public ViaDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder) { public SpongeDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder) {
this.info = info; this.info = info;
this.minecraftDecoder = minecraftDecoder; this.minecraftDecoder = minecraftDecoder;
} }

Datei anzeigen

@ -13,11 +13,11 @@ import us.myles.ViaVersion.util.PipelineUtil;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
public class ViaEncodeHandler extends MessageToByteEncoder { public class SpongeEncodeHandler extends MessageToByteEncoder {
private final UserConnection info; private final UserConnection info;
private final MessageToByteEncoder minecraftEncoder; private final MessageToByteEncoder minecraftEncoder;
public ViaEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) { public SpongeEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) {
this.info = info; this.info = info;
this.minecraftEncoder = minecraftEncoder; this.minecraftEncoder = minecraftEncoder;
} }

Datei anzeigen

@ -8,10 +8,10 @@ import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import java.util.List; import java.util.List;
public class ViaPacketHandler extends MessageToMessageEncoder { public class SpongePacketHandler extends MessageToMessageEncoder {
private final UserConnection info; private final UserConnection info;
public ViaPacketHandler(UserConnection info) { public SpongePacketHandler(UserConnection info) {
this.info = info; this.info = info;
} }

Datei anzeigen

@ -9,7 +9,7 @@ import org.spongepowered.api.Sponge;
import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.Pair;
import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.platform.ViaInjector; import us.myles.ViaVersion.api.platform.ViaInjector;
import us.myles.ViaVersion.sponge.handlers.ViaVersionInitializer; import us.myles.ViaVersion.sponge.handlers.SpongeChannelInitializer;
import us.myles.ViaVersion.util.ListWrapper; import us.myles.ViaVersion.util.ListWrapper;
import us.myles.ViaVersion.util.ReflectionUtil; import us.myles.ViaVersion.util.ReflectionUtil;
@ -74,7 +74,7 @@ public class SpongeViaInjector implements ViaInjector {
ChannelHandler bootstrapAcceptor = future.channel().pipeline().first(); ChannelHandler bootstrapAcceptor = future.channel().pipeline().first();
try { try {
ChannelInitializer<SocketChannel> oldInit = ReflectionUtil.get(bootstrapAcceptor, "childHandler", ChannelInitializer.class); ChannelInitializer<SocketChannel> oldInit = ReflectionUtil.get(bootstrapAcceptor, "childHandler", ChannelInitializer.class);
ChannelInitializer newInit = new ViaVersionInitializer(oldInit); ChannelInitializer newInit = new SpongeChannelInitializer(oldInit);
ReflectionUtil.set(bootstrapAcceptor, "childHandler", newInit); ReflectionUtil.set(bootstrapAcceptor, "childHandler", newInit);
injectedFutures.add(future); injectedFutures.add(future);
@ -95,8 +95,8 @@ public class SpongeViaInjector implements ViaInjector {
ChannelHandler bootstrapAcceptor = future.channel().pipeline().first(); ChannelHandler bootstrapAcceptor = future.channel().pipeline().first();
try { try {
ChannelInitializer<SocketChannel> oldInit = ReflectionUtil.get(bootstrapAcceptor, "childHandler", ChannelInitializer.class); ChannelInitializer<SocketChannel> oldInit = ReflectionUtil.get(bootstrapAcceptor, "childHandler", ChannelInitializer.class);
if (oldInit instanceof ViaVersionInitializer) { if (oldInit instanceof SpongeChannelInitializer) {
ReflectionUtil.set(bootstrapAcceptor, "childHandler", ((ViaVersionInitializer) oldInit).getOriginal()); ReflectionUtil.set(bootstrapAcceptor, "childHandler", ((SpongeChannelInitializer) oldInit).getOriginal());
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println("Failed to remove injection handler, reload won't work with connections, please reboot!"); System.out.println("Failed to remove injection handler, reload won't work with connections, please reboot!");