Search in sources :

Example 1 with HandlerConstructor

use of com.viaversion.viaversion.classgenerator.generated.HandlerConstructor in project ViaVersion by ViaVersion.

the class ClassGenerator method generate.

public static void generate() {
    if (ViaVersionPlugin.getInstance().isCompatSpigotBuild() || ViaVersionPlugin.getInstance().isProtocolSupport()) {
        try {
            ClassPool pool = ClassPool.getDefault();
            pool.insertClassPath(new LoaderClassPath(Bukkit.class.getClassLoader()));
            for (Plugin p : Bukkit.getPluginManager().getPlugins()) {
                pool.insertClassPath(new LoaderClassPath(p.getClass().getClassLoader()));
            }
            if (ViaVersionPlugin.getInstance().isCompatSpigotBuild()) {
                Class decodeSuper = NMSUtil.nms("PacketDecoder", "net.minecraft.network.PacketDecoder");
                Class encodeSuper = NMSUtil.nms("PacketEncoder", "net.minecraft.network.PacketEncoder");
                // Generate the classes
                addSpigotCompatibility(pool, BukkitDecodeHandler.class, decodeSuper);
                addSpigotCompatibility(pool, BukkitEncodeHandler.class, encodeSuper);
            } else {
                // ProtocolSupport compatibility
                Class encodeSuper;
                Class decodeSuper;
                if (isMultiplatformPS()) {
                    psConnectListener = makePSConnectListener(pool);
                    return;
                } else {
                    String psPackage = getOldPSPackage();
                    decodeSuper = Class.forName(psPackage.equals("unknown") ? "protocolsupport.protocol.pipeline.common.PacketDecoder" : psPackage + ".wrapped.WrappedDecoder");
                    encodeSuper = Class.forName(psPackage.equals("unknown") ? "protocolsupport.protocol.pipeline.common.PacketEncoder" : psPackage + ".wrapped.WrappedEncoder");
                }
                // Generate the classes
                addPSCompatibility(pool, BukkitDecodeHandler.class, decodeSuper);
                addPSCompatibility(pool, BukkitEncodeHandler.class, encodeSuper);
            }
            // Implement Constructor
            CtClass generated = pool.makeClass("com.viaversion.viaversion.classgenerator.generated.GeneratedConstructor");
            CtClass handlerInterface = pool.get(HandlerConstructor.class.getName());
            generated.setInterfaces(new CtClass[] { handlerInterface });
            // Import required classes
            pool.importPackage("com.viaversion.viaversion.classgenerator.generated");
            pool.importPackage("com.viaversion.viaversion.classgenerator");
            pool.importPackage("com.viaversion.viaversion.api.connection");
            pool.importPackage("io.netty.handler.codec");
            // Implement Methods
            generated.addMethod(CtMethod.make("public MessageToByteEncoder newEncodeHandler(UserConnection info, MessageToByteEncoder minecraftEncoder) {\n" + "        return new BukkitEncodeHandler(info, minecraftEncoder);\n" + "    }", generated));
            generated.addMethod(CtMethod.make("public ByteToMessageDecoder newDecodeHandler(UserConnection info, ByteToMessageDecoder minecraftDecoder) {\n" + "        return new BukkitDecodeHandler(info, minecraftDecoder);\n" + "    }", generated));
            constructor = (HandlerConstructor) toClass(generated).getConstructor().newInstance();
        } catch (ReflectiveOperationException | CannotCompileException | NotFoundException e) {
            e.printStackTrace();
        }
    }
}
Also used : CtClass(javassist.CtClass) BasicHandlerConstructor(com.viaversion.viaversion.classgenerator.generated.BasicHandlerConstructor) HandlerConstructor(com.viaversion.viaversion.classgenerator.generated.HandlerConstructor) ClassPool(javassist.ClassPool) LoaderClassPath(javassist.LoaderClassPath) NotFoundException(javassist.NotFoundException) CtClass(javassist.CtClass) CannotCompileException(javassist.CannotCompileException) Plugin(org.bukkit.plugin.Plugin) ViaVersionPlugin(com.viaversion.viaversion.ViaVersionPlugin)

Example 2 with HandlerConstructor

use of com.viaversion.viaversion.classgenerator.generated.HandlerConstructor in project ViaVersion by ViaVersion.

the class BukkitChannelInitializer method afterChannelInitialize.

public static void afterChannelInitialize(Channel channel) {
    UserConnection connection = new UserConnectionImpl(channel);
    new ProtocolPipelineImpl(connection);
    if (PaperViaInjector.PAPER_PACKET_LIMITER) {
        connection.setPacketLimiterEnabled(false);
    }
    // Add our transformers
    HandlerConstructor constructor = ClassGenerator.getConstructor();
    MessageToByteEncoder encoder = constructor.newEncodeHandler(connection, (MessageToByteEncoder) channel.pipeline().get("encoder"));
    ByteToMessageDecoder decoder = constructor.newDecodeHandler(connection, (ByteToMessageDecoder) channel.pipeline().get("decoder"));
    channel.pipeline().replace("encoder", "encoder", encoder);
    channel.pipeline().replace("decoder", "decoder", decoder);
}
Also used : UserConnectionImpl(com.viaversion.viaversion.connection.UserConnectionImpl) HandlerConstructor(com.viaversion.viaversion.classgenerator.generated.HandlerConstructor) ProtocolPipelineImpl(com.viaversion.viaversion.protocol.ProtocolPipelineImpl) ByteToMessageDecoder(io.netty.handler.codec.ByteToMessageDecoder) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) MessageToByteEncoder(io.netty.handler.codec.MessageToByteEncoder)

Aggregations

HandlerConstructor (com.viaversion.viaversion.classgenerator.generated.HandlerConstructor)2 ViaVersionPlugin (com.viaversion.viaversion.ViaVersionPlugin)1 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)1 BasicHandlerConstructor (com.viaversion.viaversion.classgenerator.generated.BasicHandlerConstructor)1 UserConnectionImpl (com.viaversion.viaversion.connection.UserConnectionImpl)1 ProtocolPipelineImpl (com.viaversion.viaversion.protocol.ProtocolPipelineImpl)1 ByteToMessageDecoder (io.netty.handler.codec.ByteToMessageDecoder)1 MessageToByteEncoder (io.netty.handler.codec.MessageToByteEncoder)1 CannotCompileException (javassist.CannotCompileException)1 ClassPool (javassist.ClassPool)1 CtClass (javassist.CtClass)1 LoaderClassPath (javassist.LoaderClassPath)1 NotFoundException (javassist.NotFoundException)1 Plugin (org.bukkit.plugin.Plugin)1