Grasscutter/src/main/java/emu/grasscutter/net/packet/PacketOpcodesUtils.java

68 lines
2.2 KiB
Java
Raw Normal View History

package emu.grasscutter.net.packet;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
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 emu.grasscutter.utils.JsonUtils;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
public class PacketOpcodesUtils {
private static Int2ObjectMap<String> opcodeMap;
public static final Set<Integer> BANNED_PACKETS = Set.of(
PacketOpcodes.WindSeedClientNotify,
PacketOpcodes.PlayerLuaShellNotify
);
2022-07-21 15:21:22 +08:00
public static final Set<Integer> LOOP_PACKETS = Set.of(
PacketOpcodes.PingReq,
PacketOpcodes.PingRsp,
PacketOpcodes.WorldPlayerRTTNotify,
PacketOpcodes.UnionCmdNotify,
PacketOpcodes.QueryPathReq
);
2022-07-21 15:21:22 +08:00
static {
opcodeMap = new Int2ObjectOpenHashMap<String>();
Field[] fields = PacketOpcodes.class.getFields();
2022-07-21 15:21:22 +08:00
for (Field f : fields) {
if (f.getType().equals(int.class)) {
try {
opcodeMap.put(f.getInt(null), f.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
2022-07-21 15:21:22 +08:00
public static String getOpcodeName(int opcode) {
if (opcode <= 0) return "UNKNOWN";
return opcodeMap.getOrDefault(opcode, "UNKNOWN");
}
2022-07-21 15:21:22 +08:00
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(JsonUtils.encode(packetIds));
2022-07-21 15:21:22 +08:00
Grasscutter.getLogger().info("Dumped packet ids.");
} catch (IOException e) {
e.printStackTrace();
}
}
}