2021-03-22 23:06:40 +01:00
|
|
|
/*
|
|
|
|
* 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
|
2021-03-22 23:06:40 +01:00
|
|
|
*
|
|
|
|
* 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.bungee.handlers;
|
2016-09-26 14:50:20 +02:00
|
|
|
|
2024-03-09 13:57:41 +01:00
|
|
|
import com.viaversion.viaversion.api.Via;
|
2021-04-27 13:41:39 +02:00
|
|
|
import com.viaversion.viaversion.api.connection.UserConnection;
|
2021-04-26 22:54:43 +02:00
|
|
|
import com.viaversion.viaversion.connection.UserConnectionImpl;
|
2021-04-27 13:41:39 +02:00
|
|
|
import com.viaversion.viaversion.protocol.ProtocolPipelineImpl;
|
2016-09-26 14:50:20 +02:00
|
|
|
import io.netty.channel.Channel;
|
|
|
|
import io.netty.channel.ChannelInitializer;
|
|
|
|
import java.lang.reflect.Method;
|
2024-03-09 13:57:41 +01:00
|
|
|
import java.util.logging.Level;
|
2016-09-26 14:50:20 +02:00
|
|
|
|
2019-03-18 12:30:02 +01:00
|
|
|
public class BungeeChannelInitializer extends ChannelInitializer<Channel> {
|
2016-09-26 14:50:20 +02:00
|
|
|
private final ChannelInitializer<Channel> original;
|
|
|
|
private Method method;
|
|
|
|
|
2016-09-29 16:25:18 +02:00
|
|
|
public BungeeChannelInitializer(ChannelInitializer<Channel> oldInit) {
|
2016-09-26 14:50:20 +02:00
|
|
|
this.original = oldInit;
|
|
|
|
try {
|
|
|
|
this.method = ChannelInitializer.class.getDeclaredMethod("initChannel", Channel.class);
|
|
|
|
this.method.setAccessible(true);
|
|
|
|
} catch (NoSuchMethodException e) {
|
2024-03-09 13:57:41 +01:00
|
|
|
Via.getPlatform().getLogger().log(Level.WARNING, "Failed to get initChannel method", e);
|
2016-09-26 14:50:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-18 12:30:02 +01:00
|
|
|
protected void initChannel(Channel socketChannel) throws Exception {
|
2021-04-24 18:30:08 +02:00
|
|
|
if (!socketChannel.isActive()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-26 22:54:43 +02:00
|
|
|
UserConnection info = new UserConnectionImpl(socketChannel);
|
2016-09-26 14:50:20 +02:00
|
|
|
// init protocol
|
2021-04-26 22:54:43 +02:00
|
|
|
new ProtocolPipelineImpl(info);
|
2016-09-26 14:50:20 +02:00
|
|
|
// Add originals
|
|
|
|
this.method.invoke(this.original, socketChannel);
|
2019-01-20 17:47:50 +01:00
|
|
|
|
2021-05-02 15:52:07 +02:00
|
|
|
if (!socketChannel.isActive()) return; // Don't inject if inactive
|
2019-01-20 17:47:50 +01:00
|
|
|
if (socketChannel.pipeline().get("packet-encoder") == null) return; // Don't inject if no packet-encoder
|
|
|
|
if (socketChannel.pipeline().get("packet-decoder") == null) return; // Don't inject if no packet-decoder
|
2016-09-26 14:50:20 +02:00
|
|
|
// Add our transformers
|
2016-09-29 16:25:18 +02:00
|
|
|
BungeeEncodeHandler encoder = new BungeeEncodeHandler(info);
|
|
|
|
BungeeDecodeHandler decoder = new BungeeDecodeHandler(info);
|
|
|
|
|
|
|
|
socketChannel.pipeline().addBefore("packet-encoder", "via-encoder", encoder);
|
|
|
|
socketChannel.pipeline().addBefore("packet-decoder", "via-decoder", decoder);
|
2020-04-13 20:58:24 +02:00
|
|
|
}
|
2016-09-26 14:50:20 +02:00
|
|
|
|
2020-04-13 20:58:24 +02:00
|
|
|
public ChannelInitializer<Channel> getOriginal() {
|
|
|
|
return original;
|
2016-09-26 14:50:20 +02:00
|
|
|
}
|
|
|
|
}
|