97 lines
2.6 KiB
Java
97 lines
2.6 KiB
Java
package net.krakatoaapi.protocol;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.Arrays;
|
|
import net.krakatoaapi.KrakatoaAPI;
|
|
|
|
public class KraProtocol {
|
|
|
|
public void SendMessage(KraProtocolMessage kraProtocolMessage) {
|
|
SendMessage(kraProtocolMessage.getStatus(), kraProtocolMessage.getCmdID(),
|
|
kraProtocolMessage.getDest(), kraProtocolMessage.getUuid(),
|
|
kraProtocolMessage.getCmdNumber(), kraProtocolMessage.getArgs());
|
|
}
|
|
|
|
public void SendMessage(int status, int cmdID, int dest, String uuid, int cmdNumber,
|
|
String args) {
|
|
System.out.println("sendMessage: " + status + " " + cmdID + " " + dest + " " + cmdNumber);
|
|
|
|
int argLen = args.length();
|
|
|
|
byte[] raw = new byte[40 + argLen];
|
|
|
|
raw[0] = (byte) status;
|
|
|
|
raw[1] = (byte) cmdID;
|
|
raw[2] = (byte) (cmdID >> 8);
|
|
raw[3] = (byte) (cmdID >> 16);
|
|
raw[4] = (byte) (cmdID >> 24);
|
|
|
|
raw[5] = (byte) dest;
|
|
|
|
byte[] uuidBytes = uuid.getBytes();
|
|
|
|
/* for (int i = 0; i < 32; i++) {
|
|
raw[6 + i] = uuidBytes[i];
|
|
}*/
|
|
|
|
System.arraycopy(uuidBytes, 0, raw, 6, 32);
|
|
|
|
raw[38] = (byte) cmdNumber;
|
|
raw[39] = (byte) (cmdNumber >> 8);
|
|
|
|
if (argLen > 0) {
|
|
for (int i = 0; i < argLen; i++) {
|
|
raw[40 + i] = (byte) args.charAt(i);
|
|
}
|
|
}
|
|
|
|
System.out.println("raw: " + Arrays.toString(raw));
|
|
|
|
KrakatoaAPI.getInstance().getSocketClient().SendMessage(raw);
|
|
}
|
|
|
|
public KraProtocolMessage DecodeMessage(byte[] data) {
|
|
System.out.println("start decode");
|
|
|
|
byte status = data[0];
|
|
|
|
int cmdID = Byte.toUnsignedInt(data[1]);
|
|
|
|
cmdID += Byte.toUnsignedInt(data[2]) * 256;
|
|
cmdID += Byte.toUnsignedInt(data[3]) * 256 * 256;
|
|
cmdID += Byte.toUnsignedInt(data[4]) * 256 * 256 * 256;
|
|
|
|
int dest = Byte.toUnsignedInt(data[5]);
|
|
|
|
byte[] playerUuidBytes = new byte[32];
|
|
|
|
System.arraycopy(data, 6, playerUuidBytes, 0, 32);
|
|
|
|
/* for (int i = 0; i < 32; i++) {
|
|
playerUuidBytes[i] = data[6 + i];
|
|
} */
|
|
|
|
String playerUuid = new String(playerUuidBytes, StandardCharsets.UTF_8);
|
|
|
|
short cmdNumber = (short) (Byte.toUnsignedInt(data[38]) + (Byte.toUnsignedInt(data[39]) * 256));
|
|
|
|
int argLen = data.length - 40;
|
|
byte[] argsBytes = new byte[argLen];
|
|
|
|
/* for (int i = 0; i < argLen; i++) {
|
|
argsBytes[i] = data[40 + i];
|
|
} */
|
|
|
|
System.arraycopy(data, 40, argsBytes, 0, argLen);
|
|
|
|
String args = new String(argsBytes, StandardCharsets.UTF_8);
|
|
|
|
System.out.println(
|
|
"decoded message " + status + " " + cmdID + " " + dest + " " + playerUuid + " "
|
|
+ cmdNumber + " " + args + " ");
|
|
|
|
return new KraProtocolMessage(status, cmdID, dest, playerUuid, cmdNumber, args);
|
|
}
|
|
}
|