From 5fc6bc4002dc001898080ee5c9ba62b6daa16a0d Mon Sep 17 00:00:00 2001 From: Mike <76-Mike@users.noreply.git.krews.org> Date: Tue, 5 May 2020 01:31:50 +0200 Subject: [PATCH] Add packet names to logger. --- .../com/eu/habbo/messages/PacketManager.java | 7 +++ .../com/eu/habbo/messages/PacketNames.java | 58 +++++++++++++++++++ .../decoders/GameClientMessageLogger.java | 12 +++- .../encoders/GameServerMessageLogger.java | 12 +++- 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/eu/habbo/messages/PacketNames.java diff --git a/src/main/java/com/eu/habbo/messages/PacketManager.java b/src/main/java/com/eu/habbo/messages/PacketManager.java index 8360b2bb..54ad2f77 100644 --- a/src/main/java/com/eu/habbo/messages/PacketManager.java +++ b/src/main/java/com/eu/habbo/messages/PacketManager.java @@ -85,10 +85,13 @@ public class PacketManager { public static boolean MULTI_THREADED_PACKET_HANDLING = false; private final THashMap> incoming; private final THashMap> callables; + private final PacketNames names; public PacketManager() throws Exception { this.incoming = new THashMap<>(); this.callables = new THashMap<>(); + this.names = new PacketNames(); + this.names.initialize(); this.registerHandshake(); this.registerCatalog(); @@ -115,6 +118,10 @@ public class PacketManager { this.registerGameCenter(); } + public PacketNames getNames() { + return names; + } + @EventHandler public static void onConfigurationUpdated(EmulatorConfigUpdatedEvent event) { logList.clear(); diff --git a/src/main/java/com/eu/habbo/messages/PacketNames.java b/src/main/java/com/eu/habbo/messages/PacketNames.java new file mode 100644 index 00000000..4fa159ec --- /dev/null +++ b/src/main/java/com/eu/habbo/messages/PacketNames.java @@ -0,0 +1,58 @@ +package com.eu.habbo.messages; + +import com.eu.habbo.messages.incoming.Incoming; +import com.eu.habbo.messages.outgoing.Outgoing; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.HashMap; + +public class PacketNames { + + private static final Logger LOGGER = LoggerFactory.getLogger(PacketNames.class); + + private final HashMap incoming; + private final HashMap outgoing; + + public PacketNames() { + this.incoming = new HashMap<>(); + this.outgoing = new HashMap<>(); + } + + public void initialize() { + PacketNames.getNames(Incoming.class, this.incoming); + PacketNames.getNames(Outgoing.class, this.outgoing); + } + + public String getIncomingName(int key) { + return this.incoming.getOrDefault(key, "Unknown"); + } + + public String getOutgoingName(int key) { + return this.outgoing.getOrDefault(key, "Unknown"); + } + + private static void getNames(Class clazz, HashMap target) { + for (Field field : clazz.getFields()) { + int modifiers = field.getModifiers(); + if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers) && field.getType() == int.class) { + try { + int packetId = field.getInt(null); + if (packetId > 0) { + if (target.containsKey(packetId)) { + LOGGER.warn("Duplicate packet id found {} for {}.", packetId, clazz.getSimpleName()); + continue; + } + + target.put(packetId, field.getName()); + } + } catch (IllegalAccessException e) { + LOGGER.error("Failed to read field integer.", e); + } + } + } + } + +} diff --git a/src/main/java/com/eu/habbo/networking/gameserver/decoders/GameClientMessageLogger.java b/src/main/java/com/eu/habbo/networking/gameserver/decoders/GameClientMessageLogger.java index 8891e5a2..bf433250 100644 --- a/src/main/java/com/eu/habbo/networking/gameserver/decoders/GameClientMessageLogger.java +++ b/src/main/java/com/eu/habbo/networking/gameserver/decoders/GameClientMessageLogger.java @@ -1,6 +1,8 @@ package com.eu.habbo.networking.gameserver.decoders; +import com.eu.habbo.Emulator; import com.eu.habbo.messages.ClientMessage; +import com.eu.habbo.messages.PacketNames; import com.eu.habbo.util.ANSI; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageDecoder; @@ -12,10 +14,18 @@ import java.util.List; public class GameClientMessageLogger extends MessageToMessageDecoder { private static final Logger LOGGER = LoggerFactory.getLogger(GameClientMessageLogger.class); + private final PacketNames names; + + public GameClientMessageLogger() { + this.names = Emulator.getGameServer().getPacketManager().getNames(); + } @Override protected void decode(ChannelHandlerContext ctx, ClientMessage message, List out) { - LOGGER.debug(String.format("[" + ANSI.GREEN + "CLIENT" + ANSI.DEFAULT + "][%-4d] => %s", message.getMessageId(), message.getMessageBody())); + LOGGER.debug(String.format("[" + ANSI.GREEN + "CLIENT" + ANSI.DEFAULT + "][%-4d][%-41s] => %s", + message.getMessageId(), + this.names.getIncomingName(message.getMessageId()), + message.getMessageBody())); out.add(message); } diff --git a/src/main/java/com/eu/habbo/networking/gameserver/encoders/GameServerMessageLogger.java b/src/main/java/com/eu/habbo/networking/gameserver/encoders/GameServerMessageLogger.java index bcbe5b13..a21edde9 100644 --- a/src/main/java/com/eu/habbo/networking/gameserver/encoders/GameServerMessageLogger.java +++ b/src/main/java/com/eu/habbo/networking/gameserver/encoders/GameServerMessageLogger.java @@ -1,5 +1,7 @@ package com.eu.habbo.networking.gameserver.encoders; +import com.eu.habbo.Emulator; +import com.eu.habbo.messages.PacketNames; import com.eu.habbo.messages.ServerMessage; import com.eu.habbo.util.ANSI; import io.netty.channel.ChannelHandlerContext; @@ -12,10 +14,18 @@ import java.util.List; public class GameServerMessageLogger extends MessageToMessageEncoder { private static final Logger LOGGER = LoggerFactory.getLogger(GameServerMessageLogger.class); + private final PacketNames names; + + public GameServerMessageLogger() { + this.names = Emulator.getGameServer().getPacketManager().getNames(); + } @Override protected void encode(ChannelHandlerContext ctx, ServerMessage message, List out) { - LOGGER.debug(String.format("[" + ANSI.BLUE + "SERVER" + ANSI.DEFAULT + "][%-4d] => %s", message.getHeader(), message.getBodyString())); + LOGGER.debug(String.format("[" + ANSI.BLUE + "SERVER" + ANSI.DEFAULT + "][%-4d][%-41s] => %s", + message.getHeader(), + this.names.getOutgoingName(message.getHeader()), + message.getBodyString())); out.add(message.retain()); }