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.bukkit.handlers;
|
2016-03-02 16:21:07 +01:00
|
|
|
|
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.bukkit.util.NMSUtil;
|
|
|
|
import com.viaversion.viaversion.exception.CancelCodecException;
|
|
|
|
import com.viaversion.viaversion.exception.CancelDecoderException;
|
|
|
|
import com.viaversion.viaversion.exception.InformativeException;
|
|
|
|
import com.viaversion.viaversion.util.PipelineUtil;
|
2021-04-27 13:41:39 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
2022-10-16 12:27:26 +02:00
|
|
|
import io.netty.channel.ChannelHandler;
|
2021-04-27 13:41:39 +02:00
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
2022-10-16 12:27:26 +02:00
|
|
|
import io.netty.channel.ChannelPipeline;
|
|
|
|
import io.netty.handler.codec.MessageToMessageDecoder;
|
2016-03-02 16:21:07 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2022-10-16 12:27:26 +02:00
|
|
|
@ChannelHandler.Sharable
|
|
|
|
public final class BukkitDecodeHandler extends MessageToMessageDecoder<ByteBuf> {
|
|
|
|
private final UserConnection connection;
|
2016-03-02 16:21:07 +01:00
|
|
|
|
2022-10-16 12:27:26 +02:00
|
|
|
public BukkitDecodeHandler(final UserConnection connection) {
|
|
|
|
this.connection = connection;
|
2016-03-02 16:21:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-10-16 12:27:26 +02:00
|
|
|
protected void decode(final ChannelHandlerContext ctx, final ByteBuf bytebuf, final List<Object> out) throws Exception {
|
|
|
|
if (!connection.checkServerboundPacket()) {
|
2020-06-07 10:35:27 +02:00
|
|
|
throw CancelDecoderException.generate(null);
|
|
|
|
}
|
2022-10-16 12:27:26 +02:00
|
|
|
if (!connection.shouldTransformPacket()) {
|
|
|
|
out.add(bytebuf.retain());
|
|
|
|
return;
|
|
|
|
}
|
2016-03-15 01:34:20 +01:00
|
|
|
|
2022-10-16 12:27:26 +02:00
|
|
|
final ByteBuf transformedBuf = ctx.alloc().buffer().writeBytes(bytebuf);
|
2021-02-06 16:29:36 +01:00
|
|
|
try {
|
2022-10-16 12:27:26 +02:00
|
|
|
connection.transformIncoming(transformedBuf, CancelDecoderException::generate);
|
|
|
|
out.add(transformedBuf.retain());
|
2021-02-09 17:32:31 +01:00
|
|
|
} finally {
|
2022-10-16 12:27:26 +02:00
|
|
|
transformedBuf.release();
|
2016-03-02 16:21:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-02 19:41:47 +01:00
|
|
|
@Override
|
2022-10-16 12:27:26 +02:00
|
|
|
public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) throws Exception {
|
|
|
|
if (PipelineUtil.containsCause(cause, CancelCodecException.class)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-09 21:29:19 +02:00
|
|
|
|
2016-03-03 20:24:37 +01:00
|
|
|
super.exceptionCaught(ctx, cause);
|
2023-01-06 20:33:17 +01:00
|
|
|
if (NMSUtil.isDebugPropertySet()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print if CB doesn't already do it
|
|
|
|
final InformativeException exception = PipelineUtil.getCause(cause, InformativeException.class);
|
|
|
|
if (exception != null && exception.shouldBePrinted()) {
|
|
|
|
cause.printStackTrace();
|
|
|
|
exception.setShouldBePrinted(false);
|
2020-06-07 10:35:27 +02:00
|
|
|
}
|
2016-03-02 19:41:47 +01:00
|
|
|
}
|
2022-10-16 12:27:26 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void userEventTriggered(final ChannelHandlerContext ctx, final Object event) throws Exception {
|
|
|
|
if (BukkitChannelInitializer.COMPRESSION_ENABLED_EVENT == null || event != BukkitChannelInitializer.COMPRESSION_ENABLED_EVENT) {
|
|
|
|
super.userEventTriggered(ctx, event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// When compression handlers are added, the order becomes Minecraft Encoder -> Compressor -> Via Encoder; fix the order again
|
|
|
|
final ChannelPipeline pipeline = ctx.pipeline();
|
|
|
|
pipeline.addAfter(BukkitChannelInitializer.MINECRAFT_COMPRESSOR, BukkitChannelInitializer.VIA_ENCODER, pipeline.remove(BukkitChannelInitializer.VIA_ENCODER));
|
|
|
|
pipeline.addAfter(BukkitChannelInitializer.MINECRAFT_DECOMPRESSOR, BukkitChannelInitializer.VIA_DECODER, pipeline.remove(BukkitChannelInitializer.VIA_DECODER));
|
|
|
|
super.userEventTriggered(ctx, event);
|
|
|
|
}
|
2016-03-02 16:21:07 +01:00
|
|
|
}
|