Commit 8b4212ff authored by Melledy's avatar Melledy
Browse files

Refactor `PacketOpcodesUtil` to be more... useful

parent 52ee229e
...@@ -5,7 +5,6 @@ import ch.qos.logback.classic.Logger; ...@@ -5,7 +5,6 @@ import ch.qos.logback.classic.Logger;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import emu.grasscutter.Grasscutter.ServerDebugMode;
import emu.grasscutter.auth.AuthenticationSystem; import emu.grasscutter.auth.AuthenticationSystem;
import emu.grasscutter.auth.DefaultAuthentication; import emu.grasscutter.auth.DefaultAuthentication;
import emu.grasscutter.command.CommandMap; import emu.grasscutter.command.CommandMap;
...@@ -14,6 +13,7 @@ import emu.grasscutter.command.PermissionHandler; ...@@ -14,6 +13,7 @@ import emu.grasscutter.command.PermissionHandler;
import emu.grasscutter.config.ConfigContainer; import emu.grasscutter.config.ConfigContainer;
import emu.grasscutter.data.ResourceLoader; import emu.grasscutter.data.ResourceLoader;
import emu.grasscutter.database.DatabaseManager; import emu.grasscutter.database.DatabaseManager;
import emu.grasscutter.net.packet.PacketOpcodesUtils;
import emu.grasscutter.plugin.PluginManager; import emu.grasscutter.plugin.PluginManager;
import emu.grasscutter.plugin.api.ServerHook; import emu.grasscutter.plugin.api.ServerHook;
import emu.grasscutter.scripts.ScriptLoader; import emu.grasscutter.scripts.ScriptLoader;
...@@ -98,6 +98,10 @@ public final class Grasscutter { ...@@ -98,6 +98,10 @@ public final class Grasscutter {
Tools.createGmHandbook(); Tools.createGmHandbook();
exitEarly = true; exitEarly = true;
} }
case "-dumppacketids" -> {
PacketOpcodesUtils.dumpPacketIds();
exitEarly = true;
}
case "-gachamap" -> { case "-gachamap" -> {
Tools.createGachaMapping(DATA("gacha_mappings.js")); Tools.createGachaMapping(DATA("gacha_mappings.js"));
exitEarly = true; exitEarly = true;
......
package emu.grasscutter.net.packet; package emu.grasscutter.net.packet;
import java.util.HashSet;
public class PacketOpcodes { public class PacketOpcodes {
// Empty // Empty
public static final int NONE = 0; public static final int NONE = 0;
...@@ -1856,12 +1854,4 @@ public class PacketOpcodes { ...@@ -1856,12 +1854,4 @@ public class PacketOpcodes {
public static final int WorldRoutineTypeCloseNotify = 3502; public static final int WorldRoutineTypeCloseNotify = 3502;
public static final int WorldRoutineTypeRefreshNotify = 3525; public static final int WorldRoutineTypeRefreshNotify = 3525;
// Unknown
public static final HashSet<Integer> BANNED_PACKETS = new HashSet<>() {
{
this.add(PacketOpcodes.WindSeedClientNotify);
this.add(PacketOpcodes.PlayerLuaShellNotify);
}
};
} }
\ No newline at end of file
...@@ -4,13 +4,33 @@ import java.io.BufferedWriter; ...@@ -4,13 +4,33 @@ import java.io.BufferedWriter;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
import emu.grasscutter.GameConstants;
import emu.grasscutter.Grasscutter;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
public class PacketOpcodesUtil { public class PacketOpcodesUtils {
private static Int2ObjectMap<String> opcodeMap; private static Int2ObjectMap<String> opcodeMap;
public static final Set<Integer> BANNED_PACKETS = Set.of(
PacketOpcodes.WindSeedClientNotify,
PacketOpcodes.PlayerLuaShellNotify
);
public static final Set<Integer> LOOP_PACKETS = Set.of(
PacketOpcodes.PingReq,
PacketOpcodes.PingRsp,
PacketOpcodes.WorldPlayerRTTNotify,
PacketOpcodes.UnionCmdNotify,
PacketOpcodes.QueryPathReq
);
static { static {
opcodeMap = new Int2ObjectOpenHashMap<String>(); opcodeMap = new Int2ObjectOpenHashMap<String>();
...@@ -32,13 +52,15 @@ public class PacketOpcodesUtil { ...@@ -32,13 +52,15 @@ public class PacketOpcodesUtil {
return opcodeMap.getOrDefault(opcode, "UNKNOWN"); return opcodeMap.getOrDefault(opcode, "UNKNOWN");
} }
public static void dumpOpcodes() { public static void dumpPacketIds() {
try { try (FileWriter writer = new FileWriter("./PacketIds_" + GameConstants.VERSION + ".json")) {
BufferedWriter out = new BufferedWriter(new FileWriter("opcodes.ini")); // Create sorted tree map
for (Int2ObjectMap.Entry<String> entry : opcodeMap.int2ObjectEntrySet()) { Map<Integer, String> packetIds = opcodeMap.int2ObjectEntrySet().stream()
out.write(String.format("%04X=%s%s", entry.getIntKey(), entry.getValue(), System.lineSeparator())); .filter(e -> e.getIntKey() > 0)
} .collect(Collectors.toMap(Int2ObjectMap.Entry::getIntKey, Int2ObjectMap.Entry::getValue, (k, v) -> v, TreeMap::new));
out.close(); // Write to file
writer.write(Grasscutter.getGsonFactory().toJson(packetIds));
Grasscutter.getLogger().info("Dumped packet ids.");
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -95,7 +95,7 @@ public class GameServerPacketHandler { ...@@ -95,7 +95,7 @@ public class GameServerPacketHandler {
// Log unhandled packets // Log unhandled packets
if (GAME_INFO.logPackets == ServerDebugMode.MISSING) { if (GAME_INFO.logPackets == ServerDebugMode.MISSING) {
Grasscutter.getLogger().info("Unhandled packet (" + opcode + "): " + emu.grasscutter.net.packet.PacketOpcodesUtil.getOpcodeName(opcode)); Grasscutter.getLogger().info("Unhandled packet (" + opcode + "): " + emu.grasscutter.net.packet.PacketOpcodesUtils.getOpcodeName(opcode));
} }
} }
} }
...@@ -9,7 +9,7 @@ import emu.grasscutter.game.Account; ...@@ -9,7 +9,7 @@ import emu.grasscutter.game.Account;
import emu.grasscutter.game.player.Player; import emu.grasscutter.game.player.Player;
import emu.grasscutter.net.packet.BasePacket; import emu.grasscutter.net.packet.BasePacket;
import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.packet.PacketOpcodesUtil; import emu.grasscutter.net.packet.PacketOpcodesUtils;
import emu.grasscutter.server.event.game.SendPacketEvent; import emu.grasscutter.server.event.game.SendPacketEvent;
import emu.grasscutter.utils.Crypto; import emu.grasscutter.utils.Crypto;
import emu.grasscutter.utils.FileUtils; import emu.grasscutter.utils.FileUtils;
...@@ -126,7 +126,7 @@ public class GameSession implements GameSessionManager.KcpChannel { ...@@ -126,7 +126,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
} }
public void logPacket( String sendOrRecv, int opcode, byte[] payload) { public void logPacket( String sendOrRecv, int opcode, byte[] payload) {
Grasscutter.getLogger().info(sendOrRecv + ": " + PacketOpcodesUtil.getOpcodeName(opcode) + " (" + opcode + ")"); Grasscutter.getLogger().info(sendOrRecv + ": " + PacketOpcodesUtils.getOpcodeName(opcode) + " (" + opcode + ")");
System.out.println(Utils.bytesToHex(payload)); System.out.println(Utils.bytesToHex(payload));
} }
public void send(BasePacket packet) { public void send(BasePacket packet) {
...@@ -138,7 +138,7 @@ public class GameSession implements GameSessionManager.KcpChannel { ...@@ -138,7 +138,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
// DO NOT REMOVE (unless we find a way to validate code before sending to client which I don't think we can) // DO NOT REMOVE (unless we find a way to validate code before sending to client which I don't think we can)
// Stop WindSeedClientNotify from being sent for security purposes. // Stop WindSeedClientNotify from being sent for security purposes.
if(PacketOpcodes.BANNED_PACKETS.contains(packet.getOpcode())) { if(PacketOpcodesUtils.BANNED_PACKETS.contains(packet.getOpcode())) {
return; return;
} }
...@@ -150,7 +150,7 @@ public class GameSession implements GameSessionManager.KcpChannel { ...@@ -150,7 +150,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
// Log // Log
switch (GAME_INFO.logPackets) { switch (GAME_INFO.logPackets) {
case ALL -> { case ALL -> {
if (!loopPacket.contains(packet.getOpcode())) { if (!PacketOpcodesUtils.LOOP_PACKETS.contains(packet.getOpcode())) {
logPacket("SEND", packet.getOpcode(), packet.getData()); logPacket("SEND", packet.getOpcode(), packet.getData());
} }
} }
...@@ -174,14 +174,6 @@ public class GameSession implements GameSessionManager.KcpChannel { ...@@ -174,14 +174,6 @@ public class GameSession implements GameSessionManager.KcpChannel {
} }
} }
private static final Set<Integer> loopPacket = Set.of(
PacketOpcodes.PingReq,
PacketOpcodes.PingRsp,
PacketOpcodes.WorldPlayerRTTNotify,
PacketOpcodes.UnionCmdNotify,
PacketOpcodes.QueryPathReq
);
@Override @Override
public void onConnected(GameSessionManager.KcpTunnel tunnel) { public void onConnected(GameSessionManager.KcpTunnel tunnel) {
this.tunnel = tunnel; this.tunnel = tunnel;
...@@ -234,7 +226,7 @@ public class GameSession implements GameSessionManager.KcpChannel { ...@@ -234,7 +226,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
// Log packet // Log packet
switch (GAME_INFO.logPackets) { switch (GAME_INFO.logPackets) {
case ALL -> { case ALL -> {
if (!loopPacket.contains(opcode)) { if (!PacketOpcodesUtils.LOOP_PACKETS.contains(opcode)) {
logPacket("RECV",opcode, payload); logPacket("RECV",opcode, payload);
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment