ViaVersion/sponge/src/main/java/com/viaversion/viaversion/sponge/handlers/SpongeChannelInitializer.java

75 lines
3.1 KiB
Java
Raw Normal View History

/*
* 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/>.
*/
package com.viaversion.viaversion.sponge.handlers;
2016-09-26 02:44:21 +02:00
2021-04-26 22:54:43 +02:00
import com.viaversion.viaversion.protocol.ProtocolPipelineImpl;
import com.viaversion.viaversion.connection.UserConnectionImpl;
2016-09-26 02:44:21 +02:00
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToByteEncoder;
import com.viaversion.viaversion.api.Via;
2021-04-26 21:35:29 +02:00
import com.viaversion.viaversion.api.connection.UserConnection;
2016-09-26 02:44:21 +02:00
import java.lang.reflect.Method;
2018-06-14 20:21:49 +02:00
public class SpongeChannelInitializer extends ChannelInitializer<Channel> {
2016-09-26 02:44:21 +02:00
2018-06-14 20:21:49 +02:00
private final ChannelInitializer<Channel> original;
2016-09-26 02:44:21 +02:00
private Method method;
2018-06-14 20:21:49 +02:00
public SpongeChannelInitializer(ChannelInitializer<Channel> oldInit) {
2016-09-26 02:44:21 +02:00
this.original = oldInit;
try {
this.method = ChannelInitializer.class.getDeclaredMethod("initChannel", Channel.class);
this.method.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
@Override
2018-06-14 20:21:49 +02:00
protected void initChannel(Channel channel) throws Exception {
// Ensure ViaVersion is loaded
2021-03-26 12:51:38 +01:00
if (Via.getAPI().getServerVersion().isKnown()
2018-06-14 20:21:49 +02:00
&& channel instanceof SocketChannel) { // channel can be LocalChannel on internal server
2021-04-26 22:54:43 +02:00
UserConnection info = new UserConnectionImpl((SocketChannel) channel);
// init protocol
2021-04-26 22:54:43 +02:00
new ProtocolPipelineImpl(info);
// Add originals
2018-06-14 20:21:49 +02:00
this.method.invoke(this.original, channel);
// Add our transformers
2018-06-14 20:21:49 +02:00
MessageToByteEncoder encoder = new SpongeEncodeHandler(info, (MessageToByteEncoder) channel.pipeline().get("encoder"));
ByteToMessageDecoder decoder = new SpongeDecodeHandler(info, (ByteToMessageDecoder) channel.pipeline().get("decoder"));
SpongePacketHandler chunkHandler = new SpongePacketHandler(info);
2018-06-14 20:21:49 +02:00
channel.pipeline().replace("encoder", "encoder", encoder);
channel.pipeline().replace("decoder", "decoder", decoder);
channel.pipeline().addAfter("packet_handler", "viaversion_packet_handler", chunkHandler);
} else {
2018-06-14 20:21:49 +02:00
this.method.invoke(this.original, channel);
}
2016-09-26 02:44:21 +02:00
}
2020-04-13 20:58:24 +02:00
public ChannelInitializer<Channel> getOriginal() {
return original;
}
2016-09-26 02:44:21 +02:00
}