-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworking.PacketHandler.cs
More file actions
433 lines (408 loc) · 14.7 KB
/
Networking.PacketHandler.cs
File metadata and controls
433 lines (408 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
83
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
using Hexeract.Networking.ServerboundPackets;
using System;
namespace Hexeract.Networking.PacketHandler
{
/// <summary>
/// There is only a packet here which toggles states.
///
/// For further information about the protocol Handshake state: https://wiki.vg/Protocol#Serverbound
/// </summary>
public enum ServerPacketHandshakingIds
{
Handshake = 0x00
}
/// <summary>
/// The reason the IDs are set and are not automatically assigned is because
/// the packet protocol may change and the IDs may change as well.
///
/// For further information about the protocol Play state: https://wiki.vg/Protocol#Clientbound_2
/// </summary>
public enum ClientPacketPlayIds
{
SpawnObject = 0x00,
SpawnExperienceOrb = 0x01,
SpawnGlobalEntity = 0x02,
SpawnMob = 0x03,
SpawnPainting = 0x04,
SpawnPlayer = 0x05,
Animation = 0x06,
Statistics = 0x07,
BlockBreakAnimation = 0x08,
UpdateBlockEntity = 0x09,
BlockAction = 0x0A,
BlockChange = 0x0B,
BossBar = 0x0C,
ServerDifficulty = 0x0D,
ChatMessage = 0x0E,
MultiBlockChange = 0x0F,
TabComplete = 0x10,
DeclareCommands = 0x11,
ConfirmTransaction = 0x12,
CloseWindow = 0x13,
OpenWindow = 0x14,
WindowItems = 0x15,
WindowProperty = 0x16,
SetSlot = 0x17,
SetCooldown = 0x18,
PluginMessage = 0x19,
NamedSoundEffect = 0x1A,
Disconnect = 0x1B,
EntityStatus = 0x1C,
NBTQueryResponse = 0x1D,
Explosion = 0x1E,
UnloadChunk = 0x1F,
ChangeGameState = 0x20,
KeepAlive = 0x21,
ChunkData = 0x22,
Effect = 0x23,
Particle = 0x24,
JoinGame = 0x25,
MapData = 0x26,
Entity = 0x27,
EntityRelativeMove = 0x28,
EntityLookAndRelativeMove = 0x29,
EntityLook = 0x2A,
VehicleMove = 0x2B,
OpenSignEditor = 0x2C,
CraftRecipeResponse = 0x2D,
PlayerAbilities = 0x2E,
CombatEvent = 0x2F,
PlayerInfo = 0x30,
FacePlayer = 0x31,
PlayerPositionAndLook = 0x32,
UseBed = 0x33,
UnlockRecipes = 0x34,
DestroyEntities = 0x35,
RemoveEntityEffect = 0x36,
ResourcePackSend = 0x37,
Respawn = 0x38,
EntityHeadLook = 0x39,
SelectAdvancementTab = 0x3A,
WorldBorder = 0x3B,
Camera = 0x3C,
HeldItemChange = 0x3D,
DisplayScoreboard = 0x3E,
EntityMetadata = 0x3F,
AttatchEntity = 0x40,
EntityVelocity = 0x41,
EntityEquipment = 0x42,
SetExperience = 0x43,
UpdateHealth = 0x44,
ScoreboardObjective = 0x45,
SetPassengers = 0x46,
Teams = 0x47,
UpdateScore = 0x48,
SpawnPosition = 0x49,
TimeUpdate = 0x4A,
Title = 0x4B,
StopSound = 0x4C,
SoundEffect = 0x4D,
PlayerListHeaderAndFooter = 0x4E,
CollectItem = 0x4F,
EntityTeleport = 0x50,
Advancements = 0x51,
EntityProperties = 0x52,
EntityEffect = 0x53,
DeclareRecipes = 0x54,
Tags = 0x55
}
/// <summary>
/// The reason the IDs are set and are not automatically assigned is because
/// the packet protocol may change and the IDs may change as well.
///
/// For further information about the protocol Play state: https://wiki.vg/Protocol#Serverbound_2 https://wiki.vg/Protocol#Clientbound_3
/// </summary>
public enum ServerPacketPlayIds
{
TeleportConfirm = 0x00,
QueryBlockNBT = 0x01,
ChatMessage = 0x02,
ClientStatus = 0x03,
ClientSettings = 0x04,
TabComplete = 0x05,
ConfirmTransaction = 0x06,
EnchantItem = 0x07,
ClickWindow = 0x08,
CloseWindow = 0x09,
PluginMessage = 0x0A,
EditBook = 0x0B,
QueryEntityNBT = 0x0C,
UseEntity = 0x0D,
KeepAlive = 0x0E,
Player = 0x0F,
PlayerPosition = 0x10,
PlayerPositionAndLook = 0x11,
PlayerLook = 0x12,
VehicleMove = 0x13,
SteerBoat = 0x14,
PickItem = 0x15,
CraftRecipeRequest = 0x16,
PlayerAbilities = 0x17,
PlayerDigging = 0x18,
EntityAction = 0x19,
SteerVehicle = 0x1A,
RecipeBookData = 0x1B,
NameItem = 0x1C,
ResourcePackStatus = 0x1D,
AdvancementTab = 0x1E,
SelectTrade = 0x1F,
SetBeaconEffect = 0x20,
HeldItemChange = 0x21,
UpdateCommandBlock = 0x22,
UpdateCommandBlockMinecart = 0x23,
CreativeInventoryAction = 0x24,
UpdateStructureBlock = 0x25,
UpdateSign = 0x26,
Animation = 0x27,
Spectate = 0x28,
PlayerBlockPlacement = 0x29,
UseItem = 0x2A
}
/// <summary>
/// The reason the IDs are set and are not automatically assigned is because
/// the packet protocol may change and the IDs may change as well.
///
/// For further information about the protocol Status state: https://wiki.vg/Protocol#Clientbound_3
/// </summary>
public enum ClientPacketStatusIds
{
Response = 0x00,
Pong = 0x01
}
/// <summary>
/// The reason the IDs are set and are not automatically assigned is because
/// the packet protocol may change and the IDs may change as well.
///
/// For further information about the protocol Status state: https://wiki.vg/Protocol#Serverbound_3
/// </summary>
public enum ServerPacketStatusIds
{
Request = 0x00,
Ping = 0x01
}
/// <summary>
/// The reason the IDs are set and are not automatically assigned is because
/// the packet protocol may change and the IDs may change as well.
///
/// For further information about the protocol Login state: https://wiki.vg/Protocol#Clientbound_4
/// </summary>
public enum ClientPacketLoginIds
{
Disconnect = 0x00,
EncryptionRequest = 0x01,
LoginSuccess = 0x02,
SetCompression = 0x03,
LoginPluginRequest = 0x04
}
/// <summary>
/// The reason the IDs are set and are not automatically assigned is because
/// the packet protocol may change and the IDs may change as well.
///
/// For further information about the protocol Login state: https://wiki.vg/Protocol#Serverbound_4
/// </summary>
public enum ServerPacketLoginIds
{
LoginStart = 0x00,
EncryptionResponse = 0x01,
LoginPluginResponse = 0x02
}
/// <summary>
/// This interface was created purely with the objective
/// of getting a packet with a single method, saving time.
/// </summary>
public interface IPacket
{
}
public class PacketHandler
{
public int PacketLength { get; private set; }
public int PacketId { get; private set; }
public byte[] PacketData { get; private set; }
public PacketHandler(byte[] packet, bool compression)
{
byte[] receivedPacket = packet;
short packetCursor = 0;
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(receivedPacket);
}
PacketLength = DataTypeReader.ReadVarInt(ref packetCursor, receivedPacket);
PacketId = DataTypeReader.ReadVarInt(ref packetCursor, receivedPacket);
if (compression)
{
// TODO: Compression, not compression
}
}
public IPacket GetPacket(byte status)
{
switch (status)
{
case 0:
return GetHandshakePacket();
default:
throw new Exception("Error A02: Unsupported entrant packet");
}
}
private IPacket GetHandshakePacket()
{
return new Handshake(PacketData);
}
private IPacket
}
/// <summary>
/// This class takes for every method a pointer to know where to start
/// at the packet as a reference so the pointer can change outside scope, and
/// the packet itself to be processed.
/// </summary>
static public class DataTypeReader
{
/// <summary>
/// This method is used mostly by ReadVarInt and ReadVarLong, returns the
/// following byte of the packet.
/// </summary>
/// <param name="pointer">Index of the packet to be read</param>
/// <param name="packet">The packet to be processed</param>
/// <returns>Next byte of the packet</returns>
static public byte ReadByte(ref short pointer, byte[] packet)
{
if (pointer >= packet.Length)
{
throw new Exception("Error A01: Cursor is positioned after last byte of packet");
}
byte readByte = packet[pointer];
pointer++;
return readByte;
}
/// <summary>
/// This method reads the next boolean value of the packet.
/// </summary>
/// <param name="pointer">Index of the packet to be read</param>
/// <param name="packet">The packet to be processed</param>
/// <returns>Next boolean of the packet</returns>
static public bool ReadBool(ref short pointer, byte[] packet)
{
bool readBool = BitConverter.ToBoolean(packet, pointer);
pointer += 1;
return readBool;
}
/// <summary>
/// This method reads the next short value of the packet.
/// </summary>
/// <param name="pointer">Index of the packet to be read</param>
/// <param name="packet">The packet to be processed</param>
/// <returns>Next short of the packet</returns>
static public short ReadShort(ref short pointer, byte[] packet)
{
short readShort = BitConverter.ToInt16(packet, pointer);
pointer += 2;
return readShort;
}
/// <summary>
/// This method reads the next int (Int32) value of the packet.
/// </summary>
/// <param name="pointer">Index of the packet to be read</param>
/// <param name="packet">The packet to be processed</param>
/// <returns>Next int of the packet</returns>
static public int ReadInt(ref short pointer, byte[] packet)
{
int readInt = BitConverter.ToInt32(packet, pointer);
pointer += 4;
return readInt;
}
/// <summary>
/// This method reads the next long (Int64) value of the packet.
/// </summary>
/// <param name="pointer">Index of the packet to be read</param>
/// <param name="packet">The packet to be processed</param>
/// <returns>Next long of the packet</returns>
static public long ReadLong(ref short pointer, byte[] packet)
{
long readLong = BitConverter.ToInt64(packet, pointer);
pointer += 8;
return readLong;
}
/// <summary>
/// This method reads the next float value of the packet.
/// </summary>
/// <param name="pointer">Index of the packet to be read</param>
/// <param name="packet">The packet to be processed</param>
/// <returns>Next float of the packet</returns>
static public float ReadFloat(ref short pointer, byte[] packet)
{
float readFloat = BitConverter.ToSingle(packet, pointer);
pointer += 4;
return readFloat;
}
/// <summary>
/// This method reads the next double value of the packet.
/// </summary>
/// <param name="pointer">Index of the packet to be read</param>
/// <param name="packet">The packet to be processed</param>
/// <returns>Next double of the packet</returns>
static public double ReadDouble(ref short pointer, byte[] packet)
{
double readDouble = BitConverter.ToDouble(packet, pointer);
pointer += 8;
return readDouble;
}
/// <summary>
/// This method reads the next string value of the packet.
/// </summary>
/// <param name="pointer">Index of the packet to be read</param>
/// <param name="packet">The packet to be processed</param>
/// <param name="stringSize">The string size, must be given</param>
/// <returns>Next string of the packet</returns>
static public string ReadString(ref short pointer, byte[] packet, short stringSize)
{
string readString = BitConverter.ToString(packet, pointer, stringSize);
pointer += stringSize;
return readString;
}
/// <summary>
/// This method reads the next VarInt value of the packet.
/// For further information on how VarInt works check https://developers.google.com/protocol-buffers/docs/encoding.
/// </summary>
/// <param name="pointer">Index of the packet to be read</param>
/// <param name="packet">The packet to be processed</param>
/// <returns>Next VarInt of the packet</returns>
static public int ReadVarInt(ref short pointer, byte[] packet)
{
byte readByte;
byte count = 0;
int result = 0;
do
{
if (count > 5)
{
throw new Exception("Error A02: Packet has a VarInt with a length over 5 bytes");
}
readByte = ReadByte(ref pointer, packet);
result += readByte & 0x7F;
} while ((readByte & 0x80) > 0);
return result;
}
/// <summary>
/// This method reads the next VarLong value of the packet.
/// For further information on how VarInt works check https://developers.google.com/protocol-buffers/docs/encoding.
/// </summary>
/// <param name="pointer">Index of the packet to be read</param>
/// <param name="packet">The packet to be processed</param>
/// <returns>Next VarLong of the packet</returns>
static public long ReadVarLong(ref short pointer, byte[] packet)
{
byte readByte;
byte count = 0;
int result = 0;
do
{
if (count > 10)
{
throw new Exception("Error A02: Packet has a VarInt with a length over 5 bytes");
}
readByte = ReadByte(ref pointer, packet);
result += readByte & 0x7F;
} while ((readByte & 0x80) > 0);
return result;
}
}
}