2021-03-22 23:06:40 +01:00
|
|
|
/*
|
|
|
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
|
|
|
* Copyright (C) 2016-2021 ViaVersion and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-04-26 20:52:34 +02:00
|
|
|
package com.viaversion.viaversion.bukkit.handlers;
|
2016-02-28 23:44:33 +01:00
|
|
|
|
|
|
|
import io.netty.channel.Channel;
|
|
|
|
import io.netty.channel.ChannelInitializer;
|
|
|
|
import io.netty.channel.socket.SocketChannel;
|
2016-03-02 16:21:07 +01:00
|
|
|
import io.netty.handler.codec.ByteToMessageDecoder;
|
|
|
|
import io.netty.handler.codec.MessageToByteEncoder;
|
2021-04-26 21:35:29 +02:00
|
|
|
import com.viaversion.viaversion.api.connection.UserConnection;
|
2021-04-26 20:52:34 +02:00
|
|
|
import com.viaversion.viaversion.api.protocol.ProtocolPipeline;
|
|
|
|
import com.viaversion.viaversion.bukkit.classgenerator.ClassGenerator;
|
|
|
|
import com.viaversion.viaversion.bukkit.classgenerator.HandlerConstructor;
|
2016-02-28 23:44:33 +01:00
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
2016-09-29 16:25:18 +02:00
|
|
|
public class BukkitChannelInitializer extends ChannelInitializer<SocketChannel> {
|
2016-03-07 15:43:31 +01:00
|
|
|
|
2016-03-31 23:45:15 +02:00
|
|
|
private final ChannelInitializer<SocketChannel> original;
|
2016-02-28 23:44:33 +01:00
|
|
|
private Method method;
|
|
|
|
|
2016-09-29 16:25:18 +02:00
|
|
|
public BukkitChannelInitializer(ChannelInitializer<SocketChannel> oldInit) {
|
2016-03-31 23:45:15 +02:00
|
|
|
this.original = oldInit;
|
2016-02-28 23:44:33 +01:00
|
|
|
try {
|
|
|
|
this.method = ChannelInitializer.class.getDeclaredMethod("initChannel", Channel.class);
|
|
|
|
this.method.setAccessible(true);
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 23:45:15 +02:00
|
|
|
public ChannelInitializer<SocketChannel> getOriginal() {
|
|
|
|
return original;
|
|
|
|
}
|
|
|
|
|
2016-02-28 23:44:33 +01:00
|
|
|
@Override
|
|
|
|
protected void initChannel(SocketChannel socketChannel) throws Exception {
|
2016-03-15 01:34:20 +01:00
|
|
|
UserConnection info = new UserConnection(socketChannel);
|
|
|
|
// init protocol
|
|
|
|
new ProtocolPipeline(info);
|
2016-02-28 23:44:33 +01:00
|
|
|
// Add originals
|
2016-03-31 23:45:15 +02:00
|
|
|
this.method.invoke(this.original, socketChannel);
|
2016-05-10 15:37:31 +02:00
|
|
|
|
|
|
|
HandlerConstructor constructor = ClassGenerator.getConstructor();
|
2016-02-28 23:44:33 +01:00
|
|
|
// Add our transformers
|
2016-05-10 15:37:31 +02:00
|
|
|
MessageToByteEncoder encoder = constructor.newEncodeHandler(info, (MessageToByteEncoder) socketChannel.pipeline().get("encoder"));
|
|
|
|
ByteToMessageDecoder decoder = constructor.newDecodeHandler(info, (ByteToMessageDecoder) socketChannel.pipeline().get("decoder"));
|
2016-09-29 16:25:18 +02:00
|
|
|
BukkitPacketHandler chunkHandler = new BukkitPacketHandler(info);
|
2016-02-28 23:44:33 +01:00
|
|
|
|
2016-03-02 16:21:07 +01:00
|
|
|
socketChannel.pipeline().replace("encoder", "encoder", encoder);
|
|
|
|
socketChannel.pipeline().replace("decoder", "decoder", decoder);
|
2016-03-22 16:36:11 +01:00
|
|
|
socketChannel.pipeline().addAfter("packet_handler", "viaversion_packet_handler", chunkHandler);
|
2016-02-28 23:44:33 +01:00
|
|
|
}
|
|
|
|
}
|