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;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import emu.grasscutter.Grasscutter.ServerDebugMode;
import emu.grasscutter.auth.AuthenticationSystem;
import emu.grasscutter.auth.DefaultAuthentication;
import emu.grasscutter.command.CommandMap;
......@@ -14,6 +13,7 @@ import emu.grasscutter.command.PermissionHandler;
import emu.grasscutter.config.ConfigContainer;
import emu.grasscutter.data.ResourceLoader;
import emu.grasscutter.database.DatabaseManager;
import emu.grasscutter.net.packet.PacketOpcodesUtils;
import emu.grasscutter.plugin.PluginManager;
import emu.grasscutter.plugin.api.ServerHook;
import emu.grasscutter.scripts.ScriptLoader;
......@@ -98,6 +98,10 @@ public final class Grasscutter {
Tools.createGmHandbook();
exitEarly = true;
}
case "-dumppacketids" -> {
PacketOpcodesUtils.dumpPacketIds();
exitEarly = true;
}
case "-gachamap" -> {
Tools.createGachaMapping(DATA("gacha_mappings.js"));
exitEarly = true;
......
package emu.grasscutter.net.packet;
import java.util.HashSet;
public class PacketOpcodes {
// Empty
public static final int NONE = 0;
......@@ -1856,12 +1854,4 @@ public class PacketOpcodes {
public static final int WorldRoutineTypeCloseNotify = 3502;
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,12 +4,32 @@ import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
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.Int2ObjectOpenHashMap;
public class PacketOpcodesUtil {
private static Int2ObjectMap<String> opcodeMap;
public class PacketOpcodesUtils {
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 {
opcodeMap = new Int2ObjectOpenHashMap<String>();
......@@ -32,13 +52,15 @@ public class PacketOpcodesUtil {
return opcodeMap.getOrDefault(opcode, "UNKNOWN");
}
public static void dumpOpcodes() {
try {
BufferedWriter out = new BufferedWriter(new FileWriter("opcodes.ini"));
for (Int2ObjectMap.Entry<String> entry : opcodeMap.int2ObjectEntrySet()) {
out.write(String.format("%04X=%s%s", entry.getIntKey(), entry.getValue(), System.lineSeparator()));
}
out.close();
public static void dumpPacketIds() {
try (FileWriter writer = new FileWriter("./PacketIds_" + GameConstants.VERSION + ".json")) {
// Create sorted tree map
Map<Integer, String> packetIds = opcodeMap.int2ObjectEntrySet().stream()
.filter(e -> e.getIntKey() > 0)
.collect(Collectors.toMap(Int2ObjectMap.Entry::getIntKey, Int2ObjectMap.Entry::getValue, (k, v) -> v, TreeMap::new));
// Write to file
writer.write(Grasscutter.getGsonFactory().toJson(packetIds));
Grasscutter.getLogger().info("Dumped packet ids.");
} catch (IOException e) {
e.printStackTrace();
}
......
......@@ -95,7 +95,7 @@ public class GameServerPacketHandler {
// Log unhandled packets
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;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.net.packet.BasePacket;
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.utils.Crypto;
import emu.grasscutter.utils.FileUtils;
......@@ -126,7 +126,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
}
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));
}
public void send(BasePacket packet) {
......@@ -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)
// Stop WindSeedClientNotify from being sent for security purposes.
if(PacketOpcodes.BANNED_PACKETS.contains(packet.getOpcode())) {
if(PacketOpcodesUtils.BANNED_PACKETS.contains(packet.getOpcode())) {
return;
}
......@@ -150,7 +150,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
// Log
switch (GAME_INFO.logPackets) {
case ALL -> {
if (!loopPacket.contains(packet.getOpcode())) {
if (!PacketOpcodesUtils.LOOP_PACKETS.contains(packet.getOpcode())) {
logPacket("SEND", packet.getOpcode(), packet.getData());
}
}
......@@ -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
public void onConnected(GameSessionManager.KcpTunnel tunnel) {
this.tunnel = tunnel;
......@@ -234,7 +226,7 @@ public class GameSession implements GameSessionManager.KcpChannel {
// Log packet
switch (GAME_INFO.logPackets) {
case ALL -> {
if (!loopPacket.contains(opcode)) {
if (!PacketOpcodesUtils.LOOP_PACKETS.contains(opcode)) {
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