mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-17 01:15:52 +01:00
Refactor PacketOpcodesUtil to be more... useful
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
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);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package emu.grasscutter.net.packet;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||
|
||||
public class PacketOpcodesUtil {
|
||||
private static Int2ObjectMap<String> opcodeMap;
|
||||
|
||||
static {
|
||||
opcodeMap = new Int2ObjectOpenHashMap<String>();
|
||||
|
||||
Field[] fields = PacketOpcodes.class.getFields();
|
||||
|
||||
for (Field f : fields) {
|
||||
if(f.getType().equals(int.class)) {
|
||||
try {
|
||||
opcodeMap.put(f.getInt(null), f.getName());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getOpcodeName(int opcode) {
|
||||
if (opcode <= 0) return "UNKNOWN";
|
||||
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();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package emu.grasscutter.net.packet;
|
||||
|
||||
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 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>();
|
||||
|
||||
Field[] fields = PacketOpcodes.class.getFields();
|
||||
|
||||
for (Field f : fields) {
|
||||
if(f.getType().equals(int.class)) {
|
||||
try {
|
||||
opcodeMap.put(f.getInt(null), f.getName());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getOpcodeName(int opcode) {
|
||||
if (opcode <= 0) return "UNKNOWN";
|
||||
return opcodeMap.getOrDefault(opcode, "UNKNOWN");
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user