forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacket.cs
More file actions
502 lines (432 loc) · 15.4 KB
/
Copy pathPacket.cs
File metadata and controls
502 lines (432 loc) · 15.4 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
using System;
using System.Linq;
using L2dotNET.Utility;
namespace L2dotNET.Network
{
/// <summary>
/// Represents client packet structure.
/// </summary>
public struct Packet
{
/// <summary>
/// Default <see cref="Packet"/> overflow value.
/// </summary>
private const int DefaultOverflowValue = 128;
/// <summary>
/// <see cref="Packet"/> buffer.
/// </summary>
private byte[] _buffer;
/// <summary>
/// <see cref="Packet"/> reader / writer offset.
/// </summary>
private int _offset;
/// <summary>
/// Indicates if <see cref="Packet"/> was received or created to be sent.
/// </summary>
private readonly bool _receivedPacket;
/// <summary>
/// Initializes new instance of <see cref="Packet"/> (received packet).
/// </summary>
/// <param name="headerOffset"><see cref="Packet"/> header offset (for opcodes).</param>
/// <param name="buffer"><see cref="Packet"/> buffer.</param>
public Packet(int headerOffset, byte[] buffer)
{
_receivedPacket = true;
_buffer = buffer;
_offset = headerOffset;
}
/// <summary>
/// Initializes new instance of <see cref="Packet"/> (packet to send).
/// </summary>
/// <param name="opcodes">Packet opcodes.</param>
public Packet(params byte[] opcodes)
{
_receivedPacket = false;
_buffer = opcodes;
_offset = opcodes.Length;
}
/// <summary>
/// Writes <see cref="byte"/> value into packet buffer.
/// </summary>
/// <param name="v"><see cref="byte"/> value to write.</param>
public unsafe void WriteByte(byte v)
{
ValidateBufferSize(sizeof(byte));
fixed (byte* buf = _buffer)
*(buf + _offset++) = v;
}
/// <summary>
/// Writes array of <see cref="byte"/> values into packet buffer.
/// </summary>
/// <param name="v">Array of <see cref="byte"/> values.</param>
public void WriteByte(params byte[] v)
{
WriteByteArray(v);
}
/// <summary>
/// Writes array of <see cref="byte"/> into packet buffer.
/// </summary>
/// <param name="v">Array of <see cref="byte"/> values.</param>
public void WriteByteArray(byte[] v)
{
int length = v.Length;
ValidateBufferSize(length);
L2Buffer.Copy(v, 0, _buffer, _offset, length);
_offset += length;
}
/// <summary>
/// Writes <see cref="short"/> value into packet buffer.
/// </summary>
/// <param name="v"><see cref="short"/> value.</param>
public unsafe void WriteShort(short v)
{
ValidateBufferSize(sizeof(short));
fixed (byte* buf = _buffer)
*(short*)(buf + _offset) = v;
_offset += sizeof(short);
}
/// <summary>
/// Writes array of <see cref="short"/> values into packet buffer.
/// </summary>
/// <param name="v">Array of <see cref="short"/> values.</param>
public unsafe void WriteShort(params short[] v)
{
int length = v.Length * sizeof(short);
ValidateBufferSize(length);
fixed (byte* buf = _buffer)
{
fixed (short* w = v)
L2Buffer.UnsafeCopy(w, length, buf, ref _offset);
}
}
/// <summary>
/// Writes <see cref="int"/> value to packet buffer.
/// </summary>
/// <param name="v"><see cref="int"/> value.</param>
public unsafe void WriteInt(int v)
{
ValidateBufferSize(sizeof(int));
fixed (byte* buf = _buffer)
*(int*)(buf + _offset) = v;
_offset += sizeof(int);
}
/// <summary>
/// Writes array of <see cref="int"/> values into packet buffer.
/// </summary>
/// <param name="v">Array of <see cref="int"/> values.</param>
public unsafe void WriteInt(params int[] v)
{
int length = v.Length * sizeof(int);
ValidateBufferSize(Length);
fixed (byte* buf = _buffer)
{
fixed (int* w = v)
L2Buffer.UnsafeCopy(w, length, buf, ref _offset);
}
}
/// <summary>
/// Writes array of <see cref="int"/> values into packet buffer.
/// </summary>
/// <param name="v">Array of <see cref="int"/> values.</param>
public unsafe void WriteIntArray(int[] v)
{
int length = v.Length * sizeof(int);
ValidateBufferSize(Length);
fixed (byte* buf = _buffer)
{
fixed (int* w = v)
L2Buffer.UnsafeCopy(w, length, buf, ref _offset);
}
}
/// <summary>
/// Writes <see cref="double"/> value into packet buffer.
/// </summary>
/// <param name="v"><see cref="double"/> value.</param>
public unsafe void WriteDouble(double v)
{
ValidateBufferSize(sizeof(double));
fixed (byte* buf = _buffer)
*(double*)(buf + _offset) = v;
_offset += sizeof(double);
}
/// <summary>
/// Writes array of <see cref="double"/> values into packet buffer.
/// </summary>
/// <param name="v">Array of <see cref="double"/> values.</param>
public unsafe void WriteDouble(params double[] v)
{
int length = v.Length * sizeof(double);
ValidateBufferSize(length);
fixed (byte* buf = _buffer)
{
fixed (double* w = v)
L2Buffer.UnsafeCopy(w, length, buf, ref _offset);
}
}
/// <summary>
/// Writes <see cref="long"/> value into packet buffer.
/// </summary>
/// <param name="v"><see cref="long"/> value.</param>
public unsafe void WriteLong(long v)
{
ValidateBufferSize(sizeof(long));
fixed (byte* buf = _buffer)
*(long*)(buf + _offset) = v;
_offset += sizeof(long);
}
/// <summary>
/// Writes array of <see cref="long"/> values into packet buffer.
/// </summary>
/// <param name="v">Array of <see cref="long"/> values.</param>
public unsafe void WriteLong(params long[] v)
{
int length = v.Length * sizeof(long);
ValidateBufferSize(length);
fixed (byte* buf = _buffer)
{
fixed (long* w = v)
L2Buffer.UnsafeCopy(w, length, buf, ref _offset);
}
}
/// <summary>
/// Writes <see cref="string"/> object into packet buffer.
/// </summary>
/// <param name="s"><see cref="string"/> value.</param>
public unsafe void WriteString(string s)
{
s += '\0';
int length = s.Length * sizeof(char);
ValidateBufferSize(length);
fixed (byte* buf = _buffer)
{
fixed (char* w = s)
L2Buffer.UnsafeCopy(w, length, buf, ref _offset);
}
}
/// <summary>
/// Writes array of <see cref="string"/> values to packet buffer.
/// </summary>
/// <param name="s">Array of <see cref="string"/> values.</param>
public unsafe void WriteString(params string[] s)
{
string v = string.Join(string.Empty, s.Select(t => t + '\0').ToArray());
int length = v.Length * sizeof(char);
ValidateBufferSize(length);
fixed (byte* buf = _buffer)
{
fixed (char* w = v)
L2Buffer.UnsafeCopy(w, length, buf, ref _offset);
}
}
/// <summary>
/// Writes <see cref="bool"/> value to packet buffer. (Inner network only)
/// </summary>
/// <param name="v"><see cref="bool"/> value.</param>
public void InternalWriteBool(bool v)
{
WriteByte(v ? (byte)0x01 : (byte)0x00);
}
/// <summary>
/// Writes <see cref="DateTime"/> value to packet buffer. (Inner network only)
/// </summary>
/// <param name="v"><see cref="DateTime"/> value.</param>
public void InternalWriteDateTime(DateTime v)
{
WriteLong(v.Ticks);
}
/// <summary>
/// Reads <see cref="byte"/> value from packet buffer.
/// </summary>
/// <returns><see cref="byte"/> value.</returns>
public unsafe byte ReadByte()
{
fixed (byte* buf = _buffer)
return *(buf + _offset++);
}
/// <summary>
/// Reads array of <see cref="byte"/> values from packet buffer.
/// </summary>
/// <param name="length">length of array to read.</param>
/// <returns>Array of <see cref="byte"/> values.</returns>
public unsafe byte[] ReadBytesArray(int length)
{
byte[] dest = new byte[length];
fixed (byte* buf = _buffer, dst = dest)
L2Buffer.Copy(buf, length, dst, ref _offset);
return dest;
}
public byte[] ReadByteArrayAlt(int length)
{
byte[] result = new byte[length];
Array.Copy(GetBuffer(), _offset, result, 0, length);
_offset += length;
return result;
}
/// <summary>
/// Reads <see cref="short"/> value from packet buffer.
/// </summary>
/// <returns><see cref="short"/> value.</returns>
public unsafe short ReadShort()
{
fixed (byte* buf = _buffer)
{
short v = *(short*)(buf + _offset);
_offset += sizeof(short);
return v;
}
}
/// <summary>
/// Reads <see cref="int"/> value from packet buffer.
/// </summary>
/// <returns><see cref="int"/> value.</returns>
public unsafe int ReadInt()
{
fixed (byte* buf = _buffer)
{
int v = *(int*)(buf + _offset);
_offset += sizeof(int);
return v;
}
}
/// <summary>
/// Reads <see cref="double"/> value from packet buffer.
/// </summary>
/// <returns><see cref="double"/> value.</returns>
public unsafe double ReadDouble()
{
fixed (byte* buf = _buffer)
{
double v = *(double*)(buf + _offset);
_offset += sizeof(double);
return v;
}
}
/// <summary>
/// Reads <see cref="long"/> value from packet buffer.
/// </summary>
/// <returns><see cref="long"/> value.</returns>
public unsafe long ReadLong()
{
fixed (byte* buf = _buffer)
{
long v = *(long*)(buf + _offset);
_offset += sizeof(long);
return v;
}
}
/// <summary>
/// Reads <see cref="string"/> value from packet buffer.
/// </summary>
/// <returns><see cref="string"/> value.</returns>
public unsafe string ReadString()
{
fixed (byte* buf = _buffer)
return L2Buffer.GetTrimmedString(buf, ref _offset, _buffer.Length);
}
/// <summary>
/// Reads <see cref="bool"/> value from packet buffer. (Inner network only)
/// </summary>
/// <returns><see cref="bool"/> value.</returns>
public bool InternalReadBool()
{
return ReadByte() == 0x01;
}
/// <summary>
/// Reads <see cref="DateTime"/> value from packet buffer. (Inner network only)
/// </summary>
/// <returns><see cref="DateTime"/> value.</returns>
public DateTime InternalReadDateTime()
{
return new DateTime(ReadLong());
}
/// <summary>
/// Validates buffer capacity before writing into it.
/// </summary>
/// <param name="nextValueLength">length of next bytes sequence to write into buffer.</param>
private void ValidateBufferSize(int nextValueLength)
{
if ((_offset + nextValueLength) > _buffer.Length)
L2Buffer.Extend(ref _buffer, nextValueLength + DefaultOverflowValue);
}
/// <summary>
/// Resizes <see cref="Packet"/> buffer to it's actual capacity and appends buffer length to the beginning of <see cref="Packet"/> buffer.
/// </summary>
/// <param name="headerSize"><see cref="Packet"/> header (opcodes) capacity.</param>
public unsafe void Prepare(int headerSize)
{
_offset += headerSize;
L2Buffer.Extend(ref _buffer, headerSize, _offset);
fixed (byte* buf = _buffer)
{
if (headerSize == sizeof(short))
*(short*)buf = (short)_offset;
else
*(int*)buf = _offset;
}
}
/// <summary>
/// Returns packet buffer.
/// </summary>
/// <returns>Packet buffer.</returns>
public byte[] GetBuffer()
{
return _buffer;
}
/// <summary>
/// Returns packet buffer.
/// </summary>
/// <param name="skipFirstBytesCount">Amount of first bytes to skip.</param>
/// <returns>Buffer without provided amount of first bytes.</returns>
public byte[] GetBuffer(int skipFirstBytesCount)
{
return L2Buffer.Copy(_buffer, skipFirstBytesCount, new byte[_buffer.Length - skipFirstBytesCount], 0, _buffer.Length - skipFirstBytesCount);
}
/// <summary>
/// Moves <see cref="Packet"/> offset position.
/// </summary>
/// <param name="size">Additional offset length.</param>
public void MoveOffset(int size)
{
_offset += size;
}
/// <summary>
/// Gets first packet opcode.
/// </summary>
public unsafe byte FirstOpcode
{
get
{
fixed (byte* buf = _buffer)
return *buf;
}
}
/// <summary>
/// Gets second packet opcode.
/// </summary>
public unsafe int SecondOpcode
{
get
{
fixed (byte* buf = _buffer)
return *(buf + 1);
}
}
/// <summary>
/// Gets packet capacity.
/// </summary>
public int Length => _receivedPacket ? _buffer.Length : _offset;
/// <summary>
/// Returns string representation of current packet.
/// </summary>
/// <returns>String representation of current packet.</returns>
public override string ToString()
{
//System.Text.StringBuilder sb = new System.Text.StringBuilder();
//sb.AppendLine("Packet dump:");
//sb.AppendFormat("1s op: {0}{2}2d op: {1}{2}", FirstOpcode, SecondOpcode, Environment.NewLine);
//sb.Append(L2Buffer.ToString(m_Buffer));
//return sb.ToString();
return L2Buffer.ToString(_buffer);
}
}
}