Skip to content

Commit

Permalink
Made the debug addon a bit more dumb
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim203 committed Mar 19, 2024
1 parent d8b361a commit c702a66
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 238 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Singleton;
import java.util.concurrent.atomic.AtomicInteger;
import org.geysermc.floodgate.api.inject.InjectorAddon;
import org.geysermc.floodgate.core.addon.debug.ChannelInDebugHandler;
import org.geysermc.floodgate.core.addon.debug.ChannelOutDebugHandler;
import org.geysermc.floodgate.core.addon.debug.StateChangeDetector;
import org.geysermc.floodgate.core.config.FloodgateConfig;
import org.geysermc.floodgate.core.logger.FloodgateLogger;
import org.geysermc.floodgate.core.util.Utils;
Expand All @@ -59,16 +59,14 @@ public final class DebugAddon implements InjectorAddon {
public void onInject(Channel channel, boolean toServer) {
logger.info("Successfully called onInject. To server? {} ({})", toServer, channel.id());

StateChangeDetector changeDetector = new StateChangeDetector(
channel, packetEncoder, packetDecoder, logger
);
var packetCount = new AtomicInteger();

channel.pipeline().addBefore(
packetEncoder, "floodgate_debug_out",
new ChannelOutDebugHandler(implementationName, toServer, changeDetector, logger)
new ChannelOutDebugHandler(implementationName, toServer, packetCount, logger)
).addBefore(
packetDecoder, "floodgate_debug_in",
new ChannelInDebugHandler(implementationName, toServer, changeDetector, logger)
new ChannelInDebugHandler(implementationName, toServer, packetCount, logger)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,48 +30,46 @@
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.util.concurrent.atomic.AtomicInteger;
import org.geysermc.floodgate.core.logger.FloodgateLogger;
import org.geysermc.floodgate.core.util.Constants;

@Sharable
public final class ChannelInDebugHandler extends SimpleChannelInboundHandler<ByteBuf> {
private final String direction;
private final FloodgateLogger logger;

private final boolean toServer;
private final StateChangeDetector changeDetector;
private final AtomicInteger packetCount;

public ChannelInDebugHandler(
String implementationType,
boolean toServer,
StateChangeDetector changeDetector,
AtomicInteger packetCount,
FloodgateLogger logger) {
this.direction = (toServer ? "Server -> " : "Player -> ") + implementationType;
this.logger = logger;
this.toServer = toServer;
this.changeDetector = changeDetector;
this.packetCount = packetCount;
}

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
try {
int index = msg.readerIndex();

if (changeDetector.shouldPrintPacket(msg, !toServer)) {
if (packetCount.getAndIncrement() < Constants.MAX_DEBUG_PACKET_COUNT) {
logger.info("{} {}:\n{}",
direction,
changeDetector.getCurrentState(),
packetCount.get(),
ByteBufUtil.prettyHexDump(msg)
);

changeDetector.checkPacket(msg, !toServer);
}

// reset index
msg.readerIndex(index);

ctx.fireChannelRead(msg.retain());
} catch (Exception e) {
e.printStackTrace();
} catch (Exception exception) {
logger.error("Error in ChannelInDebugHandler", exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,50 +30,47 @@
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import java.util.concurrent.atomic.AtomicInteger;
import org.geysermc.floodgate.core.logger.FloodgateLogger;
import org.geysermc.floodgate.core.util.Constants;

@Sharable
public final class ChannelOutDebugHandler extends MessageToByteEncoder<ByteBuf> {
private final String direction;
private final FloodgateLogger logger;

private final boolean toServer;
private final StateChangeDetector changeDetector;
private final AtomicInteger packetCount;

public ChannelOutDebugHandler(
String implementationType,
boolean toServer,
StateChangeDetector changeDetector,
AtomicInteger packetCount,
FloodgateLogger logger) {
this.direction = implementationType + (toServer ? " -> Server" : " -> Player");
this.logger = logger;
this.toServer = toServer;
this.changeDetector = changeDetector;
this.packetCount = packetCount;
}

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) {
try {
int index = msg.readerIndex();

if (changeDetector.shouldPrintPacket(msg, toServer)) {
if (packetCount.getAndIncrement() < Constants.MAX_DEBUG_PACKET_COUNT) {
logger.info(
"{} {}:\n{}",
direction,
changeDetector.getCurrentState(),
packetCount.get(),
ByteBufUtil.prettyHexDump(msg)
);

// proxy acts as a client when it connects to a server
changeDetector.checkPacket(msg, toServer);
}

// reset index
msg.readerIndex(index);

out.writeBytes(msg);
} catch (Exception e) {
e.printStackTrace();
} catch (Exception exception) {
logger.error("Error in ChannelOutDebugHandler", exception);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public final class Constants {
public static final int CONFIG_VERSION = 3;

public static final int PROTOCOL_HEX_COLOR = 713; // added in 20w17a (1.16 snapshot)

public static final int MAX_DEBUG_PACKET_COUNT = 20;
public static final boolean DEBUG_MODE = false;
public static final boolean PRINT_ALL_PACKETS = false;

Expand Down
16 changes: 0 additions & 16 deletions core/src/main/java/org/geysermc/floodgate/core/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

package org.geysermc.floodgate.core.util;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelPipeline;
import java.io.IOException;
Expand Down Expand Up @@ -119,21 +118,6 @@ public static char generateCodeChar() {
return (char) ('A' + codeChar);
}

public static int readVarInt(ByteBuf buffer) {
int out = 0;
int count = 0;
byte current;
do {
current = buffer.readByte();
out |= (current & 0x7F) << (count++ * 7);

if (count > 5) {
throw new RuntimeException("VarInt is bigger then allowed");
}
} while ((current & 0x80) != 0);
return out;
}

public static String getStackTrace(Throwable throwable) {
StringWriter writer = new StringWriter();
throwable.printStackTrace(new PrintWriter(writer));
Expand Down

0 comments on commit c702a66

Please sign in to comment.