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

76 lines
3.1 KiB
Java
Raw Normal View History

/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
2024-01-01 12:39:45 +01:00
* Copyright (C) 2016-2024 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
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
2021-04-26 22:54:43 +02:00
import com.viaversion.viaversion.connection.UserConnectionImpl;
2021-10-01 11:57:35 +02:00
import com.viaversion.viaversion.platform.WrappedChannelInitializer;
import com.viaversion.viaversion.protocol.ProtocolPipelineImpl;
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 java.lang.reflect.Method;
2021-10-01 11:57:35 +02:00
public class SpongeChannelInitializer extends ChannelInitializer<Channel> implements WrappedChannelInitializer {
2016-09-26 02:44:21 +02:00
2021-10-01 11:57:35 +02:00
private static final Method INIT_CHANNEL_METHOD;
2018-06-14 20:21:49 +02:00
private final ChannelInitializer<Channel> original;
2016-09-26 02:44:21 +02:00
2021-10-01 11:57:35 +02:00
static {
2016-09-26 02:44:21 +02:00
try {
2021-10-01 11:57:35 +02:00
INIT_CHANNEL_METHOD = ChannelInitializer.class.getDeclaredMethod("initChannel", Channel.class);
INIT_CHANNEL_METHOD.setAccessible(true);
2016-09-26 02:44:21 +02:00
} catch (NoSuchMethodException e) {
2021-10-01 11:57:35 +02:00
throw new RuntimeException(e);
2016-09-26 02:44:21 +02:00
}
}
2021-10-01 11:57:35 +02:00
public SpongeChannelInitializer(ChannelInitializer<Channel> oldInit) {
this.original = oldInit;
}
2016-09-26 02:44:21 +02:00
@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
2021-10-01 11:57:35 +02:00
INIT_CHANNEL_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"));
2018-06-14 20:21:49 +02:00
channel.pipeline().replace("encoder", "encoder", encoder);
channel.pipeline().replace("decoder", "decoder", decoder);
} else {
2021-10-01 11:57:35 +02:00
INIT_CHANNEL_METHOD.invoke(this.original, channel);
}
2016-09-26 02:44:21 +02:00
}
2020-04-13 20:58:24 +02:00
2021-10-01 11:57:35 +02:00
@Override
public ChannelInitializer<Channel> original() {
return original;
}
2016-09-26 02:44:21 +02:00
}