Added debug levels. Code style

This commit is contained in:
Nanit 2020-11-26 20:35:12 +02:00
parent bc05228da3
commit 7cb3846848
10 changed files with 69 additions and 48 deletions

View File

@ -9,11 +9,11 @@ public final class LimboConfig {
private static String host; private static String host;
private static int port; private static int port;
private static boolean onlineMode;
private static int maxPlayers; private static int maxPlayers;
private static IpForwardingType ipForwardingType; private static IpForwardingType ipForwardingType;
private static long readTimeout; private static long readTimeout;
private static PingData pingData; private static PingData pingData;
private static int debugLevel = 3;
public static void load(Path file) throws IOException { public static void load(Path file) throws IOException {
if (!Files.exists(file)){ if (!Files.exists(file)){
@ -25,14 +25,16 @@ public final class LimboConfig {
host = properties.getProperty("host"); host = properties.getProperty("host");
port = Integer.parseInt(properties.getProperty("port")); port = Integer.parseInt(properties.getProperty("port"));
onlineMode = Boolean.parseBoolean(properties.getProperty("online-mode"));
maxPlayers = Integer.parseInt(properties.getProperty("max-players")); maxPlayers = Integer.parseInt(properties.getProperty("max-players"));
ipForwardingType = IpForwardingType.valueOf(properties.getProperty("ip-forwarding").toUpperCase()); ipForwardingType = IpForwardingType.valueOf(properties.getProperty("ip-forwarding").toUpperCase());
readTimeout = Long.parseLong(properties.getProperty("read-timeout")); readTimeout = Long.parseLong(properties.getProperty("read-timeout"));
pingData = new PingData();
pingData = new PingData();
pingData.setVersion(properties.getProperty("ping-version")); pingData.setVersion(properties.getProperty("ping-version"));
pingData.setDescription(properties.getProperty("ping-description")); pingData.setDescription(properties.getProperty("ping-description"));
debugLevel = Integer.parseInt(properties.getProperty("debug-level"));
} }
public static String getHost() { public static String getHost() {
@ -43,10 +45,6 @@ public final class LimboConfig {
return port; return port;
} }
public static boolean isOnlineMode() {
return onlineMode;
}
public static int getMaxPlayers() { public static int getMaxPlayers() {
return maxPlayers; return maxPlayers;
} }
@ -63,6 +61,10 @@ public final class LimboConfig {
return pingData; return pingData;
} }
public static int getDebugLevel() {
return debugLevel;
}
public enum IpForwardingType { public enum IpForwardingType {
NONE, NONE,
LEGACY, LEGACY,

View File

@ -4,7 +4,6 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInboundHandlerAdapter;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import ru.nanit.limbo.LimboConfig; import ru.nanit.limbo.LimboConfig;
import ru.nanit.limbo.protocol.packets.login.*; import ru.nanit.limbo.protocol.packets.login.*;
import ru.nanit.limbo.protocol.packets.play.*; import ru.nanit.limbo.protocol.packets.play.*;
@ -19,7 +18,7 @@ import ru.nanit.limbo.protocol.registry.State;
import ru.nanit.limbo.server.LimboServer; import ru.nanit.limbo.server.LimboServer;
import ru.nanit.limbo.util.Logger; import ru.nanit.limbo.util.Logger;
import ru.nanit.limbo.util.UuidUtil; import ru.nanit.limbo.util.UuidUtil;
import ru.nanit.limbo.world.DefaultDimension; import ru.nanit.limbo.world.DimensionRegistry;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
@ -38,10 +37,6 @@ public class ClientConnection extends ChannelInboundHandlerAdapter {
return uuid; return uuid;
} }
public String getUsername() {
return username;
}
public ClientConnection(Channel channel, LimboServer server){ public ClientConnection(Channel channel, LimboServer server){
this.server = server; this.server = server;
this.channel = channel; this.channel = channel;
@ -105,10 +100,6 @@ public class ClientConnection extends ChannelInboundHandlerAdapter {
startJoinProcess(); startJoinProcess();
} }
if (packet instanceof PacketKeepAlive){
System.out.println("Get KeepAlive " + ((PacketKeepAlive)packet).getId());
}
} }
private void startJoinProcess(){ private void startJoinProcess(){
@ -127,8 +118,8 @@ public class ClientConnection extends ChannelInboundHandlerAdapter {
joinGame.setWorldName("minecraft:world"); joinGame.setWorldName("minecraft:world");
joinGame.setWorldNames("minecraft:world"); joinGame.setWorldNames("minecraft:world");
joinGame.setHashedSeed(0); joinGame.setHashedSeed(0);
joinGame.setDimensionCodec(DefaultDimension.getCodec()); joinGame.setDimensionCodec(DimensionRegistry.getCodec());
joinGame.setDimension(DefaultDimension.getDimension()); joinGame.setDimension(DimensionRegistry.getDimension());
PacketPlayerPositionAndLook positionAndLook = new PacketPlayerPositionAndLook(); PacketPlayerPositionAndLook positionAndLook = new PacketPlayerPositionAndLook();

View File

@ -22,7 +22,7 @@ public class VarIntByteDecoder implements ByteProcessor {
return true; return true;
} }
public int getReadVarint() { public int getReadVarInt() {
return readVarInt; return readVarInt;
} }

View File

@ -16,31 +16,27 @@ public class VarIntFrameDecoder extends ByteToMessageDecoder {
return; return;
} }
final VarIntByteDecoder reader = new VarIntByteDecoder(); VarIntByteDecoder reader = new VarIntByteDecoder();
int varIntEnd = in.forEachByte(reader);
int varintEnd = in.forEachByte(reader); if (varIntEnd == -1) return;
if (varintEnd == -1) {
return;
}
if (reader.getResult() == VarIntByteDecoder.DecodeResult.SUCCESS) { if (reader.getResult() == VarIntByteDecoder.DecodeResult.SUCCESS) {
int readVarint = reader.getReadVarint(); int readVarInt = reader.getReadVarInt();
int bytesRead = reader.getBytesRead(); int bytesRead = reader.getBytesRead();
if (readVarint < 0) { if (readVarInt < 0) {
Logger.error("BAD_LENGTH_CACHED"); Logger.error("[VarIntFrameDecoder] Bad data length");
} else if (readVarint == 0) { } else if (readVarInt == 0) {
// skip over the empty packet and ignore it in.readerIndex(varIntEnd + 1);
in.readerIndex(varintEnd + 1);
} else { } else {
int minimumRead = bytesRead + readVarint; int minimumRead = bytesRead + readVarInt;
if (in.isReadable(minimumRead)) { if (in.isReadable(minimumRead)) {
out.add(in.retainedSlice(varintEnd + 1, readVarint)); out.add(in.retainedSlice(varIntEnd + 1, readVarInt));
in.skipBytes(minimumRead); in.skipBytes(minimumRead);
} }
} }
} else if (reader.getResult() == VarIntByteDecoder.DecodeResult.TOO_BIG) { } else if (reader.getResult() == VarIntByteDecoder.DecodeResult.TOO_BIG) {
Logger.error("Too big"); Logger.error("[VarIntFrameDecoder] Too big data");
} }
} }
} }

View File

@ -10,14 +10,14 @@ import ru.nanit.limbo.protocol.ByteMessage;
public class VarIntLengthEncoder extends MessageToByteEncoder<ByteBuf> { public class VarIntLengthEncoder extends MessageToByteEncoder<ByteBuf> {
@Override @Override
protected void encode(ChannelHandlerContext ctx, ByteBuf buf, ByteBuf out) throws Exception { protected void encode(ChannelHandlerContext ctx, ByteBuf buf, ByteBuf out) {
ByteMessage msg = new ByteMessage(out); ByteMessage msg = new ByteMessage(out);
msg.writeVarInt(buf.readableBytes()); msg.writeVarInt(buf.readableBytes());
msg.writeBytes(buf); msg.writeBytes(buf);
} }
@Override @Override
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect) throws Exception { protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect) {
int anticipatedRequiredCapacity = 5 + msg.readableBytes(); int anticipatedRequiredCapacity = 5 + msg.readableBytes();
return ctx.alloc().heapBuffer(anticipatedRequiredCapacity); return ctx.alloc().heapBuffer(anticipatedRequiredCapacity);
} }

View File

@ -7,7 +7,7 @@ import ru.nanit.limbo.LimboConfig;
import ru.nanit.limbo.connection.ClientChannelInitializer; import ru.nanit.limbo.connection.ClientChannelInitializer;
import ru.nanit.limbo.connection.ClientConnection; import ru.nanit.limbo.connection.ClientConnection;
import ru.nanit.limbo.util.Logger; import ru.nanit.limbo.util.Logger;
import ru.nanit.limbo.world.DefaultDimension; import ru.nanit.limbo.world.DimensionRegistry;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Map; import java.util.Map;
@ -38,7 +38,7 @@ public final class LimboServer {
Logger.info("Starting server..."); Logger.info("Starting server...");
LimboConfig.load(Paths.get("./settings.properties")); LimboConfig.load(Paths.get("./settings.properties"));
DefaultDimension.init(); DimensionRegistry.init();
executor.scheduleAtFixedRate(this::broadcastKeepAlive, 0L, 5L, TimeUnit.SECONDS); executor.scheduleAtFixedRate(this::broadcastKeepAlive, 0L, 5L, TimeUnit.SECONDS);

View File

@ -1,5 +1,7 @@
package ru.nanit.limbo.util; package ru.nanit.limbo.util;
import ru.nanit.limbo.LimboConfig;
import java.time.LocalTime; import java.time.LocalTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
@ -7,6 +9,8 @@ public final class Logger {
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("hh:mm:ss"); private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("hh:mm:ss");
private Logger(){}
public static void info(Object msg, Object... args){ public static void info(Object msg, Object... args){
print(Level.INFO, msg, null, args); print(Level.INFO, msg, null, args);
} }
@ -32,9 +36,11 @@ public final class Logger {
} }
public static void print(Level level, Object msg, Throwable t, Object... args){ public static void print(Level level, Object msg, Throwable t, Object... args){
if (LimboConfig.getDebugLevel() >= level.getIndex()){
System.out.println(String.format("%s: %s", getPrefix(level), String.format(msg.toString(), args))); System.out.println(String.format("%s: %s", getPrefix(level), String.format(msg.toString(), args)));
if (t != null) t.printStackTrace(); if (t != null) t.printStackTrace();
} }
}
private static String getPrefix(Level level){ private static String getPrefix(Level level){
return String.format("[%s] [%s]", getTime(), level.getDisplay()); return String.format("[%s] [%s]", getTime(), level.getDisplay());
@ -46,18 +52,24 @@ public final class Logger {
public enum Level { public enum Level {
INFO ("INFO"), INFO ("INFO", 1),
WARNING("WARNING"), WARNING("WARNING", 2),
ERROR("ERROR"); ERROR("ERROR", 3);
private final String display; private final String display;
private final int index;
Level(String display){ Level(String display, int index){
this.display = display; this.display = display;
this.index = index;
} }
public String getDisplay() { public String getDisplay() {
return display; return display;
} }
public int getIndex() {
return index;
}
} }
} }

View File

@ -3,7 +3,9 @@ package ru.nanit.limbo.util;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.UUID; import java.util.UUID;
public class UuidUtil { public final class UuidUtil {
private UuidUtil(){}
public static UUID getOfflineModeUuid(String username){ public static UUID getOfflineModeUuid(String username){
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + username) return UUID.nameUUIDFromBytes(("OfflinePlayer:" + username)

View File

@ -3,7 +3,7 @@ package ru.nanit.limbo.world;
import net.kyori.adventure.nbt.CompoundBinaryTag; import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.kyori.adventure.nbt.ListBinaryTag; import net.kyori.adventure.nbt.ListBinaryTag;
public final class DefaultDimension { public final class DimensionRegistry {
private static CompoundBinaryTag CODEC; private static CompoundBinaryTag CODEC;
private static CompoundBinaryTag DIMENSION; private static CompoundBinaryTag DIMENSION;

View File

@ -2,10 +2,19 @@
# NanoLimbo configuration # NanoLimbo configuration
# #
# Server's host address
host=localhost host=localhost
# Server's port
port=65535 port=65535
# Max amount of players can join to server
max-players=100 max-players=100
# Version string when client version is not compatible with server one
ping-version=NanoLimbo ping-version=NanoLimbo
# Server's description component
ping-description={"text": "NanoLimbo"} ping-description={"text": "NanoLimbo"}
# Player info forwarding support. Available types: NONE, LEGACY, MODERN # Player info forwarding support. Available types: NONE, LEGACY, MODERN
@ -16,3 +25,12 @@ ip-forwarding-secret=<YOUR_SECRET_HERE>
# Read timeout for connections in milliseconds # Read timeout for connections in milliseconds
read-timeout=30000 read-timeout=30000
# Define debug level. On release, i recommend to use 1 level, since
# there are many useless for release warnings about undefined packets and other.
# Levels:
# 0 - Display nothing
# 1 - Display only useful info
# 2 - Display info and warnings
# 3 - Display info, warnings, errors
debug-level=3