mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-17 09:25:06 +01:00
Fix whitespace [skip actions]
This commit is contained in:
@@ -8,134 +8,134 @@ import emu.grasscutter.net.proto.PacketHeadOuterClass.PacketHead;
|
||||
import emu.grasscutter.utils.Crypto;
|
||||
|
||||
public class BasePacket {
|
||||
private static final int const1 = 17767; // 0x4567
|
||||
private static final int const2 = -30293; // 0x89ab
|
||||
|
||||
private int opcode;
|
||||
private boolean shouldBuildHeader = false;
|
||||
private static final int const1 = 17767; // 0x4567
|
||||
private static final int const2 = -30293; // 0x89ab
|
||||
|
||||
private byte[] header;
|
||||
private byte[] data;
|
||||
|
||||
// Encryption
|
||||
private boolean useDispatchKey;
|
||||
public boolean shouldEncrypt = true;
|
||||
|
||||
public BasePacket(int opcode) {
|
||||
this.opcode = opcode;
|
||||
}
|
||||
|
||||
public BasePacket(int opcode, int clientSequence) {
|
||||
this.opcode = opcode;
|
||||
this.buildHeader(clientSequence);
|
||||
}
|
||||
|
||||
public BasePacket(int opcode, boolean buildHeader) {
|
||||
this.opcode = opcode;
|
||||
this.shouldBuildHeader = buildHeader;
|
||||
}
|
||||
private int opcode;
|
||||
private boolean shouldBuildHeader = false;
|
||||
|
||||
public int getOpcode() {
|
||||
return opcode;
|
||||
}
|
||||
private byte[] header;
|
||||
private byte[] data;
|
||||
|
||||
public void setOpcode(int opcode) {
|
||||
this.opcode = opcode;
|
||||
}
|
||||
// Encryption
|
||||
private boolean useDispatchKey;
|
||||
public boolean shouldEncrypt = true;
|
||||
|
||||
public boolean useDispatchKey() {
|
||||
return useDispatchKey;
|
||||
}
|
||||
public BasePacket(int opcode) {
|
||||
this.opcode = opcode;
|
||||
}
|
||||
|
||||
public void setUseDispatchKey(boolean useDispatchKey) {
|
||||
this.useDispatchKey = useDispatchKey;
|
||||
}
|
||||
public BasePacket(int opcode, int clientSequence) {
|
||||
this.opcode = opcode;
|
||||
this.buildHeader(clientSequence);
|
||||
}
|
||||
|
||||
public byte[] getHeader() {
|
||||
return header;
|
||||
}
|
||||
public BasePacket(int opcode, boolean buildHeader) {
|
||||
this.opcode = opcode;
|
||||
this.shouldBuildHeader = buildHeader;
|
||||
}
|
||||
|
||||
public void setHeader(byte[] header) {
|
||||
this.header = header;
|
||||
}
|
||||
public int getOpcode() {
|
||||
return opcode;
|
||||
}
|
||||
|
||||
public boolean shouldBuildHeader() {
|
||||
return shouldBuildHeader;
|
||||
}
|
||||
public void setOpcode(int opcode) {
|
||||
this.opcode = opcode;
|
||||
}
|
||||
|
||||
public byte[] getData() {
|
||||
return data;
|
||||
}
|
||||
public boolean useDispatchKey() {
|
||||
return useDispatchKey;
|
||||
}
|
||||
|
||||
public void setData(byte[] data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void setData(GeneratedMessageV3 proto) {
|
||||
this.data = proto.toByteArray();
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void setData(GeneratedMessageV3.Builder proto) {
|
||||
this.data = proto.build().toByteArray();
|
||||
}
|
||||
|
||||
public BasePacket buildHeader(int clientSequence) {
|
||||
if (this.getHeader() != null && clientSequence == 0) {
|
||||
return this;
|
||||
}
|
||||
setHeader(PacketHead.newBuilder().setClientSequenceId(clientSequence).setSentMs(System.currentTimeMillis()).build().toByteArray());
|
||||
return this;
|
||||
}
|
||||
|
||||
public byte[] build() {
|
||||
if (getHeader() == null) {
|
||||
this.header = new byte[0];
|
||||
}
|
||||
|
||||
if (getData() == null) {
|
||||
this.data = new byte[0];
|
||||
}
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(2 + 2 + 2 + 4 + getHeader().length + getData().length + 2);
|
||||
|
||||
this.writeUint16(baos, const1);
|
||||
this.writeUint16(baos, opcode);
|
||||
this.writeUint16(baos, header.length);
|
||||
this.writeUint32(baos, data.length);
|
||||
this.writeBytes(baos, header);
|
||||
this.writeBytes(baos, data);
|
||||
this.writeUint16(baos, const2);
|
||||
|
||||
byte[] packet = baos.toByteArray();
|
||||
|
||||
if (this.shouldEncrypt) {
|
||||
Crypto.xor(packet, this.useDispatchKey() ? Crypto.DISPATCH_KEY : Crypto.ENCRYPT_KEY);
|
||||
}
|
||||
public void setUseDispatchKey(boolean useDispatchKey) {
|
||||
this.useDispatchKey = useDispatchKey;
|
||||
}
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
public void writeUint16(ByteArrayOutputStream baos, int i) {
|
||||
// Unsigned short
|
||||
public byte[] getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
public void setHeader(byte[] header) {
|
||||
this.header = header;
|
||||
}
|
||||
|
||||
public boolean shouldBuildHeader() {
|
||||
return shouldBuildHeader;
|
||||
}
|
||||
|
||||
public byte[] getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(byte[] data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void setData(GeneratedMessageV3 proto) {
|
||||
this.data = proto.toByteArray();
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void setData(GeneratedMessageV3.Builder proto) {
|
||||
this.data = proto.build().toByteArray();
|
||||
}
|
||||
|
||||
public BasePacket buildHeader(int clientSequence) {
|
||||
if (this.getHeader() != null && clientSequence == 0) {
|
||||
return this;
|
||||
}
|
||||
setHeader(PacketHead.newBuilder().setClientSequenceId(clientSequence).setSentMs(System.currentTimeMillis()).build().toByteArray());
|
||||
return this;
|
||||
}
|
||||
|
||||
public byte[] build() {
|
||||
if (getHeader() == null) {
|
||||
this.header = new byte[0];
|
||||
}
|
||||
|
||||
if (getData() == null) {
|
||||
this.data = new byte[0];
|
||||
}
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(2 + 2 + 2 + 4 + getHeader().length + getData().length + 2);
|
||||
|
||||
this.writeUint16(baos, const1);
|
||||
this.writeUint16(baos, opcode);
|
||||
this.writeUint16(baos, header.length);
|
||||
this.writeUint32(baos, data.length);
|
||||
this.writeBytes(baos, header);
|
||||
this.writeBytes(baos, data);
|
||||
this.writeUint16(baos, const2);
|
||||
|
||||
byte[] packet = baos.toByteArray();
|
||||
|
||||
if (this.shouldEncrypt) {
|
||||
Crypto.xor(packet, this.useDispatchKey() ? Crypto.DISPATCH_KEY : Crypto.ENCRYPT_KEY);
|
||||
}
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
public void writeUint16(ByteArrayOutputStream baos, int i) {
|
||||
// Unsigned short
|
||||
baos.write((byte) ((i >>> 8) & 0xFF));
|
||||
baos.write((byte) (i & 0xFF));
|
||||
}
|
||||
|
||||
public void writeUint32(ByteArrayOutputStream baos, int i) {
|
||||
// Unsigned int (long)
|
||||
|
||||
public void writeUint32(ByteArrayOutputStream baos, int i) {
|
||||
// Unsigned int (long)
|
||||
baos.write((byte) ((i >>> 24) & 0xFF));
|
||||
baos.write((byte) ((i >>> 16) & 0xFF));
|
||||
baos.write((byte) ((i >>> 8) & 0xFF));
|
||||
baos.write((byte) (i & 0xFF));
|
||||
}
|
||||
|
||||
public void writeBytes(ByteArrayOutputStream baos, byte[] bytes) {
|
||||
try {
|
||||
baos.write(bytes);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
public void writeBytes(ByteArrayOutputStream baos, byte[] bytes) {
|
||||
try {
|
||||
baos.write(bytes);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1854,4 +1854,4 @@ public class PacketOpcodes {
|
||||
public static final int WorldRoutineTypeCloseNotify = 3502;
|
||||
public static final int WorldRoutineTypeRefreshNotify = 3525;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ public class PacketOpcodesUtils {
|
||||
PacketOpcodes.WindSeedClientNotify,
|
||||
PacketOpcodes.PlayerLuaShellNotify
|
||||
);
|
||||
|
||||
|
||||
public static final Set<Integer> LOOP_PACKETS = Set.of(
|
||||
PacketOpcodes.PingReq,
|
||||
PacketOpcodes.PingRsp,
|
||||
@@ -30,39 +30,39 @@ public class PacketOpcodesUtils {
|
||||
PacketOpcodes.UnionCmdNotify,
|
||||
PacketOpcodes.QueryPathReq
|
||||
);
|
||||
|
||||
static {
|
||||
opcodeMap = new Int2ObjectOpenHashMap<String>();
|
||||
|
||||
Field[] fields = PacketOpcodes.class.getFields();
|
||||
static {
|
||||
opcodeMap = new Int2ObjectOpenHashMap<String>();
|
||||
|
||||
for (Field f : fields) {
|
||||
if(f.getType().equals(int.class)) {
|
||||
try {
|
||||
opcodeMap.put(f.getInt(null), f.getName());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Field[] fields = PacketOpcodes.class.getFields();
|
||||
|
||||
public static String getOpcodeName(int opcode) {
|
||||
if (opcode <= 0) return "UNKNOWN";
|
||||
return opcodeMap.getOrDefault(opcode, "UNKNOWN");
|
||||
}
|
||||
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 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();
|
||||
}
|
||||
}
|
||||
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