From cdab5b3f5b44ee1c7a3dab433d2edb8639a93f54 Mon Sep 17 00:00:00 2001 From: Myles Date: Mon, 26 Sep 2016 14:48:46 +0100 Subject: [PATCH] Inject into Bungee, (does not get past login from when i've tested) Also add commands --- .../main/java/us/myles/ViaVersion/Bungee.java | 7 +++ .../bungee/command/BungeeCommand.java | 19 +++++++ .../bungee/command/BungeeCommandHandler.java | 7 +++ .../bungee/config/BungeeConfigProvider.java | 3 +- .../bungee/handlers/ViaDecodeHandler.java | 24 ++++++-- .../bungee/handlers/ViaEncodeHandler.java | 39 ++++++++----- .../handlers/ViaVersionInitializer.java | 15 +++-- .../bungee/util/BungeePipelineUtil.java | 56 +++++++++++++++++++ .../myles/ViaVersion/util/ReflectionUtil.java | 5 ++ 9 files changed, 151 insertions(+), 24 deletions(-) create mode 100644 bungee/src/main/java/us/myles/ViaVersion/bungee/command/BungeeCommand.java create mode 100644 bungee/src/main/java/us/myles/ViaVersion/bungee/command/BungeeCommandHandler.java create mode 100644 bungee/src/main/java/us/myles/ViaVersion/bungee/util/BungeePipelineUtil.java diff --git a/bungee/src/main/java/us/myles/ViaVersion/Bungee.java b/bungee/src/main/java/us/myles/ViaVersion/Bungee.java index 0b825c6d2..a5ba702de 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/Bungee.java +++ b/bungee/src/main/java/us/myles/ViaVersion/Bungee.java @@ -1,6 +1,7 @@ package us.myles.ViaVersion; import com.google.gson.JsonObject; +import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.plugin.Plugin; @@ -10,6 +11,8 @@ import us.myles.ViaVersion.api.ViaVersionConfig; import us.myles.ViaVersion.api.command.ViaCommandSender; import us.myles.ViaVersion.api.configuration.ConfigurationProvider; import us.myles.ViaVersion.api.platform.ViaPlatform; +import us.myles.ViaVersion.bungee.command.BungeeCommand; +import us.myles.ViaVersion.bungee.command.BungeeCommandHandler; import us.myles.ViaVersion.bungee.BungeeViaAPI; import us.myles.ViaVersion.bungee.BungeeViaInjector; import us.myles.ViaVersion.bungee.BungeeViaLoader; @@ -23,16 +26,20 @@ public class Bungee extends Plugin implements ViaPlatform { private BungeeViaAPI api; private BungeeConfigProvider config; + private BungeeCommandHandler commandHandler; @Override public void onLoad() { api = new BungeeViaAPI(); config = new BungeeConfigProvider(); + commandHandler = new BungeeCommandHandler(); + ProxyServer.getInstance().getPluginManager().registerCommand(this, new BungeeCommand(commandHandler)); // Init platform Via.init(ViaManager.builder() .platform(this) .injector(new BungeeViaInjector()) .loader(new BungeeViaLoader()) + .commandHandler(commandHandler) .build()); } diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/command/BungeeCommand.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/command/BungeeCommand.java new file mode 100644 index 000000000..19b33f211 --- /dev/null +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/command/BungeeCommand.java @@ -0,0 +1,19 @@ +package us.myles.ViaVersion.bungee.command; + +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.plugin.Command; + +public class BungeeCommand extends Command { + private final BungeeCommandHandler handler; + + public BungeeCommand(BungeeCommandHandler handler) { + super("viaversion"); // TODO PERMS HERE + this.handler = handler; + } + + @Override + public void execute(CommandSender commandSender, String[] strings) { + handler.onCommand(new BungeeCommandSender(commandSender), strings); + } + +} diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/command/BungeeCommandHandler.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/command/BungeeCommandHandler.java new file mode 100644 index 000000000..f6d2b2a6d --- /dev/null +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/command/BungeeCommandHandler.java @@ -0,0 +1,7 @@ +package us.myles.ViaVersion.bungee.command; + +import us.myles.ViaVersion.commands.ViaCommandHandler; + +public class BungeeCommandHandler extends ViaCommandHandler { + +} diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/config/BungeeConfigProvider.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/config/BungeeConfigProvider.java index d2eb518f2..688d367a7 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/config/BungeeConfigProvider.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/config/BungeeConfigProvider.java @@ -3,6 +3,7 @@ package us.myles.ViaVersion.bungee.config; import us.myles.ViaVersion.api.ViaVersionConfig; import us.myles.ViaVersion.api.configuration.ConfigurationProvider; +import java.util.Arrays; import java.util.List; import java.util.Map; @@ -145,7 +146,7 @@ public class BungeeConfigProvider implements ViaVersionConfig, ConfigurationProv @Override public List getBlockedProtocols() { - return null; + return Arrays.asList(); } @Override diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaDecodeHandler.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaDecodeHandler.java index 017c6a7dd..cb7073118 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaDecodeHandler.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaDecodeHandler.java @@ -3,9 +3,14 @@ package us.myles.ViaVersion.bungee.handlers; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; +import io.netty.handler.codec.MessageToMessageDecoder; +import net.md_5.bungee.api.ProxyServer; +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.data.UserConnection; import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.bungee.util.BungeePipelineUtil; import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.packets.Direction; import us.myles.ViaVersion.protocols.base.ProtocolInfo; @@ -14,12 +19,13 @@ import us.myles.ViaVersion.util.PipelineUtil; import java.lang.reflect.InvocationTargetException; import java.util.List; -public class ViaDecodeHandler extends ByteToMessageDecoder { +public class ViaDecodeHandler extends MinecraftDecoder { - private final ByteToMessageDecoder minecraftDecoder; + private final MinecraftDecoder minecraftDecoder; private final UserConnection info; - public ViaDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder) { + public ViaDecodeHandler(UserConnection info, MinecraftDecoder minecraftDecoder) { + super(Protocol.HANDSHAKE, true, ProxyServer.getInstance().getProtocolVersion()); this.info = info; this.minecraftDecoder = minecraftDecoder; } @@ -68,7 +74,7 @@ public class ViaDecodeHandler extends ByteToMessageDecoder { // call minecraft decoder try { - list.addAll(PipelineUtil.callDecode(this.minecraftDecoder, ctx, bytebuf)); + list.addAll(BungeePipelineUtil.callDecode(this.minecraftDecoder, ctx, bytebuf)); } catch (InvocationTargetException e) { if (e.getCause() instanceof Exception) { throw (Exception) e.getCause(); @@ -86,4 +92,14 @@ public class ViaDecodeHandler extends ByteToMessageDecoder { if (PipelineUtil.containsCause(cause, CancelException.class)) return; super.exceptionCaught(ctx, cause); } + + @Override + public void setProtocol(Protocol protocol) { + this.minecraftDecoder.setProtocol(protocol); + } + + @Override + public void setProtocolVersion(int protocolVersion) { + this.minecraftDecoder.setProtocolVersion(protocolVersion); + } } diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaEncodeHandler.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaEncodeHandler.java index 119fb5b13..b6cdb8767 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaEncodeHandler.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaEncodeHandler.java @@ -3,9 +3,14 @@ package us.myles.ViaVersion.bungee.handlers; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; +import net.md_5.bungee.api.ProxyServer; +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.data.UserConnection; import us.myles.ViaVersion.api.type.Type; +import us.myles.ViaVersion.bungee.util.BungeePipelineUtil; import us.myles.ViaVersion.exception.CancelException; import us.myles.ViaVersion.packets.Direction; import us.myles.ViaVersion.protocols.base.ProtocolInfo; @@ -13,27 +18,25 @@ import us.myles.ViaVersion.util.PipelineUtil; import java.lang.reflect.InvocationTargetException; -public class ViaEncodeHandler extends MessageToByteEncoder { +public class ViaEncodeHandler extends MinecraftEncoder { private final UserConnection info; - private final MessageToByteEncoder minecraftEncoder; + private final MinecraftEncoder minecraftEncoder; - public ViaEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) { + public ViaEncodeHandler(UserConnection info, MinecraftEncoder minecraftEncoder) { + super(Protocol.HANDSHAKE, true, ProxyServer.getInstance().getProtocolVersion()); this.info = info; this.minecraftEncoder = minecraftEncoder; } @Override - protected void encode(final ChannelHandlerContext ctx, Object o, final ByteBuf bytebuf) throws Exception { - // handle the packet type - if (!(o instanceof ByteBuf)) { - // call minecraft encoder - try { - PipelineUtil.callEncode(this.minecraftEncoder, ctx, o, bytebuf); - } catch (InvocationTargetException e) { - if (e.getCause() instanceof Exception) { - throw (Exception) e.getCause(); - } + protected void encode(final ChannelHandlerContext ctx, DefinedPacket o, final ByteBuf bytebuf) 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) { @@ -67,4 +70,14 @@ public class ViaEncodeHandler extends MessageToByteEncoder { if (PipelineUtil.containsCause(cause, CancelException.class)) return; super.exceptionCaught(ctx, cause); } + + @Override + public void setProtocol(Protocol protocol) { + this.minecraftEncoder.setProtocol(protocol); + } + + @Override + public void setProtocolVersion(int protocolVersion) { + this.minecraftEncoder.setProtocolVersion(protocolVersion); + } } diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaVersionInitializer.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaVersionInitializer.java index ea5808344..bf1be261f 100644 --- a/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaVersionInitializer.java +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/handlers/ViaVersionInitializer.java @@ -5,6 +5,9 @@ import io.netty.channel.ChannelInitializer; import io.netty.channel.socket.SocketChannel; import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.MessageToByteEncoder; +import io.netty.handler.codec.MessageToMessageDecoder; +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.protocol.ProtocolPipeline; @@ -37,12 +40,12 @@ public class ViaVersionInitializer extends ChannelInitializer { // Add originals this.method.invoke(this.original, socketChannel); // Add our transformers - MessageToByteEncoder encoder = new ViaEncodeHandler(info, (MessageToByteEncoder) socketChannel.pipeline().get("encoder")); - ByteToMessageDecoder decoder = new ViaDecodeHandler(info, (ByteToMessageDecoder) socketChannel.pipeline().get("decoder")); - ViaPacketHandler chunkHandler = new ViaPacketHandler(info); + ViaEncodeHandler encoder = new ViaEncodeHandler(info, (MinecraftEncoder) socketChannel.pipeline().get("packet-encoder")); + ViaDecodeHandler decoder = new ViaDecodeHandler(info, (MinecraftDecoder) socketChannel.pipeline().get("packet-decoder")); +// ViaPacketHandler chunkHandler = new ViaPacketHandler(info); - socketChannel.pipeline().replace("encoder", "encoder", encoder); - socketChannel.pipeline().replace("decoder", "decoder", decoder); - socketChannel.pipeline().addAfter("packet_handler", "viaversion_packet_handler", chunkHandler); + 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); } } diff --git a/bungee/src/main/java/us/myles/ViaVersion/bungee/util/BungeePipelineUtil.java b/bungee/src/main/java/us/myles/ViaVersion/bungee/util/BungeePipelineUtil.java new file mode 100644 index 000000000..0b8934680 --- /dev/null +++ b/bungee/src/main/java/us/myles/ViaVersion/bungee/util/BungeePipelineUtil.java @@ -0,0 +1,56 @@ +package us.myles.ViaVersion.bungee.util; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.ByteToMessageDecoder; +import io.netty.handler.codec.MessageToByteEncoder; +import io.netty.handler.codec.MessageToMessageDecoder; +import net.md_5.bungee.protocol.DefinedPacket; +import net.md_5.bungee.protocol.MinecraftDecoder; +import net.md_5.bungee.protocol.MinecraftEncoder; +import us.myles.ViaVersion.Bungee; +import us.myles.ViaVersion.util.PipelineUtil; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +public class BungeePipelineUtil { + private static Method DECODE_METHOD; + private static Method ENCODE_METHOD; + + static { + try { + DECODE_METHOD = MinecraftDecoder.class.getDeclaredMethod("decode", ChannelHandlerContext.class, ByteBuf.class, List.class); + DECODE_METHOD.setAccessible(true); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + System.out.println("Netty issue?"); + } + try { + ENCODE_METHOD = MinecraftEncoder.class.getDeclaredMethod("encode", ChannelHandlerContext.class, DefinedPacket.class, ByteBuf.class); + ENCODE_METHOD.setAccessible(true); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + System.out.println("Netty issue?"); + } + } + public static List callDecode(MessageToMessageDecoder decoder, ChannelHandlerContext ctx, Object input) throws InvocationTargetException { + List output = new ArrayList<>(); + try { + BungeePipelineUtil.DECODE_METHOD.invoke(decoder, ctx, input, output); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + return output; + } + + public static void callEncode(MessageToByteEncoder encoder, ChannelHandlerContext ctx, Object msg, ByteBuf output) throws InvocationTargetException { + try { + BungeePipelineUtil.ENCODE_METHOD.invoke(encoder, ctx, msg, output); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/util/ReflectionUtil.java b/common/src/main/java/us/myles/ViaVersion/util/ReflectionUtil.java index f44622297..6febc6c1a 100644 --- a/common/src/main/java/us/myles/ViaVersion/util/ReflectionUtil.java +++ b/common/src/main/java/us/myles/ViaVersion/util/ReflectionUtil.java @@ -5,6 +5,7 @@ import com.google.common.collect.Maps; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.lang.reflect.Modifier; import java.util.Collection; import java.util.Collections; import java.util.Map; @@ -30,6 +31,10 @@ public class ReflectionUtil { public static void setStatic(Class clazz, String f, Object value) throws NoSuchFieldException, IllegalAccessException { Field field = clazz.getDeclaredField(f); field.setAccessible(true); + // Remove any final stuff + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); field.set(null, value); }