BasePacket.java 3.26 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
4
5
6
7
8
9
package emu.grasscutter.net.packet;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.google.protobuf.GeneratedMessageV3;
import emu.grasscutter.net.proto.PacketHeadOuterClass.PacketHead;
import emu.grasscutter.utils.Crypto;

10
public class BasePacket {
Melledy's avatar
Melledy committed
11
12
13
14
15
16
17
18
19
20
21
22
23
	private static final int const1 = 17767; // 0x4567
	private static final int const2 = -30293; // 0x89ab
	
	private int opcode;
	private boolean shouldBuildHeader = false;

	private byte[] header;
	private byte[] data;
	
	// Encryption
	private boolean useDispatchKey;
	public boolean shouldEncrypt = true;
	
24
	public BasePacket(int opcode) {
Melledy's avatar
Melledy committed
25
26
27
		this.opcode = opcode;
	}
	
28
	public BasePacket(int opcode, int clientSequence) {
Melledy's avatar
Melledy committed
29
30
31
32
		this.opcode = opcode;
		this.buildHeader(clientSequence);
	}
	
33
	public BasePacket(int opcode, boolean buildHeader) {
Melledy's avatar
Melledy committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
		this.opcode = opcode;
		this.shouldBuildHeader = buildHeader;
	}

	public int getOpcode() {
		return opcode;
	}

	public void setOpcode(int opcode) {
		this.opcode = opcode;
	}

	public boolean useDispatchKey() {
		return useDispatchKey;
	}

	public void setUseDispatchKey(boolean useDispatchKey) {
		this.useDispatchKey = useDispatchKey;
	}

	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();
	}
	
83
	public BasePacket buildHeader(int clientSequence) {
Melledy's avatar
Melledy committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
		if (this.getHeader() != null && clientSequence == 0) {
			return this;
		}
		setHeader(PacketHead.newBuilder().setClientSequenceId(clientSequence).setTimestamp(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)
        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();
		}
    }
}