PacketOpcodesUtil.java 1.16 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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) {
20
21
22
23
24
25
			if(f.getType().equals(int.class)) {
				try {
					opcodeMap.put(f.getInt(null), f.getName());
				} catch (Exception e) {
					e.printStackTrace();
				}
Melledy's avatar
Melledy committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
			}
		}
	}

	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();
		}
	}
}