| package us.myles.ViaVersion.util; |
| |
| import io.netty.buffer.ByteBuf; |
| import io.netty.channel.ChannelHandlerContext; |
| import io.netty.handler.codec.ByteToMessageDecoder; |
| import io.netty.handler.codec.MessageToByteEncoder; |
| |
| import java.lang.reflect.InvocationTargetException; |
| import java.lang.reflect.Method; |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| public class PipelineUtil { |
| private static Method DECODE_METHOD; |
| private static Method ENCODE_METHOD; |
| |
| static { |
| try { |
| DECODE_METHOD = ByteToMessageDecoder.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 = MessageToByteEncoder.class.getDeclaredMethod("encode", ChannelHandlerContext.class, Object.class, ByteBuf.class); |
| ENCODE_METHOD.setAccessible(true); |
| } catch (NoSuchMethodException e) { |
| e.printStackTrace(); |
| System.out.println("Netty issue?"); |
| } |
| } |
| |
| public static List<Object> callDecode(ByteToMessageDecoder decoder, ChannelHandlerContext ctx, Object input) throws InvocationTargetException { |
| List<Object> output = new ArrayList<>(); |
| try { |
| PipelineUtil.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 { |
| PipelineUtil.ENCODE_METHOD.invoke(encoder, ctx, msg, output); |
| } catch (IllegalAccessException e) { |
| e.printStackTrace(); |
| } |
| } |
| |
| public static boolean containsCause(Throwable t, Class<? extends Throwable> c) { |
| while (t != null) { |
| t = t.getCause(); |
| if (t != null) |
| if (c.isAssignableFrom(t.getClass())) return true; |
| } |
| return false; |
| } |
| } |