RuneHive-Game
Loading...
Searching...
No Matches
GamePacket.java
Go to the documentation of this file.
1package com.runehive.net.packet;
2
3import com.runehive.net.codec.ByteModification;
4import com.runehive.net.codec.ByteOrder;
5import io.netty.buffer.ByteBuf;
6import io.netty.buffer.DefaultByteBufHolder;
7
8/**
9 * Represents a single game packet.
10 *
11 * @author nshusa
12 */
13public class GamePacket extends DefaultByteBufHolder {
14
15 /**
16 * The opcode for this packet.
17 */
18 private final int opcode;
19
20 /**
21 * The header for this packet.
22 */
23 private final PacketType header;
24
25 /**
26 * The buffer that contains the data for this packet.
27 */
28 private final ByteBuf payload;
29
30 /**
31 * The size of this packet.
32 */
33 private final int size;
34
35 /**
36 * Creates a {@code GamePacket}.
37 *
38 * @param opcode
39 * The opcode.
40 * @param header
41 * The header.
42 * @param payload
43 * The payload.
44 */
45 public GamePacket(final int opcode, final PacketType header, final ByteBuf payload) {
46 super(payload);
47
48 this.opcode = opcode;
49 this.header = header;
50 this.payload = payload;
51 this.size = payload.readableBytes();
52 }
53
54 /**
55 * Gets the size of this payload.
56 *
57 * @return The amount of readable bytes.
58 */
59 public int getSize() {
60 return size;
61 }
62
63 /**
64 * Gets the opcode.
65 *
66 * @return The opcode.
67 */
68 public int getOpcode() {
69 return opcode;
70 }
71
72 /**
73 * Gets the type.
74 *
75 * @return The type.
76 */
78 return header;
79 }
80
81 /**
82 * Gets the payload.
83 *
84 * @return The payload.
85 */
86 public ByteBuf getPayload() {
87 return payload;
88 }
89
90 /**
91 * Reads a {@code STANDARD} {@code signed} byte from the payload.
92 *
93 * @return The byte.
94 */
95 public int readByte() {
96 return payload.readByte();
97 }
98
99 /**
100 * Reads a {@code STANDARD} byte from the payload.
101 *
102 * @param signed
103 * The flag that denotes this value is signed.
104 *
105 * @return The byte.
106 */
107 public int readByte(boolean signed) {
108 return readByte(signed, ByteModification.NONE);
109 }
110
111 /**
112 * Reads a {@code signed} byte from the payload.
113 *
114 * @param mod
115 * The modification performed on this value.
116 *
117 * @return The signed byte.
118 */
119 public int readByte(ByteModification mod) {
120 return readByte(true, mod);
121 }
122
123 /**
124 * Reads a single byte from the payload.
125 *
126 * @param signed
127 * The flag that denotes this value is signed.
128 *
129 * @param mod
130 * The modification performed on this value.
131 *
132 * @return The byte.
133 */
134 public int readByte(boolean signed, ByteModification mod) {
135 int value = payload.readByte();
136 switch (mod) {
137 case ADD:
138 value = value - 128;
139 break;
140
141 case SUB:
142 value = 128 - value;
143 break;
144
145 case NEG:
146 value = -value;
147 break;
148
149 case NONE:
150 break;
151
152 }
153 return signed ? value : value & 0xFF;
154 }
155
156 /**
157 * Reads a {@code STANDARD} {@code signed} short value from the payload in
158 * {@code BIG} order.
159 *
160 * @return The standard signed short value.
161 */
162 public int readShort() {
163 return payload.readShort();
164 }
165
166 /**
167 * Reads a {@code STANDARD} short value from the payload in {@code BIG}
168 * order.
169 *
170 * @param signed
171 * The flag that denotes this value is signed.
172 *
173 * @return The standard short value.
174 */
175 public int readShort(boolean signed) {
176 return readShort(signed, ByteOrder.BE, ByteModification.NONE);
177 }
178
179 /**
180 * Reads a {@code STANDARD} {@code signed} short value from the payload.
181 *
182 * @param order
183 * The order in which this value is written.
184 *
185 * @return The signed standard short value.
186 */
187 public int readShort(ByteOrder order) {
188 return readShort(true, order, ByteModification.NONE);
189 }
190
191 /**
192 * Reads a {@code STANDARD} short value from the payload.
193 *
194 * @param signed
195 * The flag that denotes this value is signed.
196 *
197 * @param order
198 * The order in which this value is written.
199 *
200 * @return The standard short value.
201 */
202 public int readShort(boolean signed, ByteOrder order) {
203 return readShort(signed, order, ByteModification.NONE);
204 }
205
206 /**
207 * Reads a {@code signed} short value from the payload.
208 *
209 * @param order
210 * The order in which this value is written.
211 *
212 * @param mod
213 * The modifications performed on this value.
214 *
215 * @return The short value.
216 */
217 public int readShort(ByteOrder order, ByteModification mod) {
218 return readShort(true, order, mod);
219 }
220
221 /**
222 * Reads a {@code signed} short value from the payload {@code BIG} order.
223 *
224 * @param mod
225 * The modifications performed on this value.
226 *
227 * @return The short value.
228 */
229 public int readShort(ByteModification mod) {
230 return readShort(true, ByteOrder.BE, mod);
231 }
232
233 /**
234 * Reads a short value from the payload in {@code BIG} order.
235 *
236 * @param signed
237 * The flag that denotes this value is signed.
238 *
239 * @param mod
240 * The modifications performed on this value.
241 *
242 * @return The short value.
243 */
244 public int readShort(boolean signed, ByteModification mod) {
245 return readShort(signed, ByteOrder.BE, mod);
246 }
247
248 /**
249 * Reads a short value from the payload.
250 *
251 * @param signed
252 * The flag that denotes this value is signed.
253 *
254 * @param order
255 * The order in which the value is written.
256 *
257 * @param mod
258 * The modifications performed on this value.
259 *
260 * @return The short value.
261 */
262 public int readShort(boolean signed, ByteOrder order, ByteModification mod) {
263 int value = 0;
264 switch (order) {
265 case BE:
266 value |= readByte(false) << 8;
267 value |= readByte(false, mod);
268 break;
269
270 case IME:
271 throw new UnsupportedOperationException("Inverse-middle-endian short is impossible!");
272
273 case ME:
274 throw new UnsupportedOperationException("Middle-endian short " + "is impossible!");
275
276 case LE:
277 value |= readByte(false, mod);
278 value |= readByte(false) << 8;
279 break;
280 }
281 return signed ? value : value & 0xFFFF;
282 }
283
284 /**
285 * Reads a {@code STANDARD} {@code signed} integer value from the payload in
286 * {@code BIG} order.
287 *
288 * @return The integer value.
289 */
290 public int readInt() {
292 }
293
294 /**
295 * Reads a {@code STANDARD} integer value from the payload in {@code BIG}
296 * order.
297 *
298 * @param signed
299 * The flag that denotes this value is signed.
300 *
301 * @return The integer value.
302 */
303 public int readInt(boolean signed) {
304 return readInt(signed, ByteOrder.BE, ByteModification.NONE);
305 }
306
307 /**
308 * Reads a {@code signed} integer value from the payload in
309 * {@code ByteOrder} {@code BIG} order.
310 *
311 * @param mod
312 * The modifications performed on this value.
313 *
314 * @return The integer value.
315 */
316 public int readInt(ByteModification mod) {
317 return readInt(true, ByteOrder.BE, mod);
318 }
319
320 /**
321 * Reads an integer value from the payload in {@code BIG} order.
322 *
323 * @param signed
324 * The flag that denotes this value is signed.
325 *
326 * @param mod
327 * The modifications performed on this value.
328 *
329 * @return The integer value.
330 */
331 public int readInt(boolean signed, ByteModification mod) {
332 return readInt(signed, ByteOrder.BE, mod);
333 }
334
335 /**
336 * Reads a {@code STANDARD} integer value from the payload.
337 *
338 * @param signed
339 * The flag that denotes this value is signed.
340 *
341 * @param order
342 * The order in which the value is written.
343 *
344 * @return The integer value.
345 */
346 public int readInt(boolean signed, ByteOrder order) {
347 return readInt(signed, order, ByteModification.NONE);
348 }
349
350 /**
351 * Reads an integer value from the payload.
352 *
353 * @param signed
354 * The flag that denotes this value is signed.
355 *
356 * @param order
357 * The order in which the value is written.
358 *
359 * @param mod
360 * The modifications performed on this value.
361 *
362 * @return The integer value.
363 */
364 public int readInt(boolean signed, ByteOrder order, ByteModification mod) {
365 long value = 0;
366 switch (order) {
367 case BE:
368 value |= readByte(false) << 24;
369 value |= readByte(false) << 16;
370 value |= readByte(false) << 8;
371 value |= readByte(false, mod);
372 break;
373
374 case ME:
375 value |= readByte(false) << 8;
376 value |= readByte(false, mod);
377 value |= readByte(false) << 24;
378 value |= readByte(false) << 16;
379 break;
380 case IME:
381 value |= readByte(false) << 16;
382 value |= readByte(false) << 24;
383 value |= readByte(false, mod);
384 value |= readByte(false) << 8;
385 break;
386
387 case LE:
388 value |= readByte(false, mod);
389 value |= readByte(false) << 8;
390 value |= readByte(false) << 16;
391 value |= readByte(false) << 24;
392 break;
393
394 }
395 return (int) (signed ? value : value & 0xFFFFFFFFL);
396 }
397
398 /**
399 * Reads a {@code STANDARD} {@code signed} long value from the payload in
400 * {@code BIG} order.
401 *
402 * @return The long value.
403 */
404 public long readLong() {
406 }
407
408 /**
409 * Reads a {@code STANDARD} long value from the payload in {@code BIG}
410 * order.
411 *
412 * @param signed
413 * The flag that denotes this value is signed.
414 *
415 * @return The long value.
416 */
417 public long readLong(boolean signed) {
418 return readLong(signed, ByteOrder.BE, ByteModification.NONE);
419 }
420
421 /**
422 * Reads a {@code signed} long value from the payload in {@code BIG} order.
423 *
424 * @param mod
425 * The modifications performed on this value.
426 *
427 * @return The long value.
428 */
429 public long readLong(ByteModification mod) {
430 return readLong(true, ByteOrder.BE, mod);
431 }
432
433 /**
434 * Reads a long value from the payload in {@code BIG} order.
435 *
436 * @param signed
437 * The flag that denotes this value is signed.
438 *
439 * @param mod
440 * The modifications performed on this value.
441 *
442 * @return The long value.
443 */
444 public long readLong(boolean signed, ByteModification mod) {
445 return readLong(signed, ByteOrder.BE, mod);
446 }
447
448 /**
449 * Reads a {@code STANDARD} long value from the payload.
450 *
451 * @param signed
452 * The flag that denotes this value is signed.
453 *
454 * @param order
455 * The order in which the value is written.
456 *
457 * @return The long value.
458 */
459 public long readLong(boolean signed, ByteOrder order) {
460 return readLong(signed, order, ByteModification.NONE);
461 }
462
463 /**
464 * Reads a long value from the payload.
465 *
466 * @param signed
467 * The flag that denotes this value is signed.
468 *
469 * @param order
470 * The order in which the value is written.
471 *
472 * @param mod
473 * The modifications performed on this value.
474 *
475 * @return The long value.
476 */
477 public long readLong(boolean signed, ByteOrder order, ByteModification mod) {
478 long value = 0;
479 switch (order) {
480 case BE:
481 value |= (long) readByte(false) << 56L;
482 value |= (long) readByte(false) << 48L;
483 value |= (long) readByte(false) << 40L;
484 value |= (long) readByte(false) << 32L;
485 value |= (long) readByte(false) << 24L;
486 value |= (long) readByte(false) << 16L;
487 value |= (long) readByte(false) << 8L;
488 value |= readByte(false, mod);
489 break;
490
491 case ME:
492 case IME:
493 throw new UnsupportedOperationException("Middle and " + "inverse-middle value types not supported!");
494
495 case LE:
496 value |= readByte(false, mod);
497 value |= (long) readByte(false) << 8L;
498 value |= (long) readByte(false) << 16L;
499 value |= (long) readByte(false) << 24L;
500 value |= (long) readByte(false) << 32L;
501 value |= (long) readByte(false) << 40L;
502 value |= (long) readByte(false) << 48L;
503 value |= (long) readByte(false) << 56L;
504 break;
505
506 }
507 return signed ? value : value & 0xFFFFFFFFL;
508 }
509
510 /**
511 * Reads the amount of bytes into the array, starting at the current
512 * position.
513 *
514 * @param amount
515 * the amount to read.
516 * @return a buffer filled with the data.
517 */
518 public byte[] readBytes(int amount) {
519 return readBytes(amount, ByteModification.NONE);
520 }
521
522 /**
523 * Reads a series of bytes from a buffer.
524 *
525 * @param amount
526 * The amount of bytes to read.
527 *
528 * @param mod
529 * The modifications performed on the bytes values.
530 *
531 * @return The bytes that where read.
532 */
533 public byte[] readBytes(int amount, ByteModification mod) {
534 byte[] data = new byte[amount];
535 for (int i = 0; i < amount; i++) {
536 data[i] = (byte) readByte(mod);
537 }
538 return data;
539 }
540
541 /**
542 * Reads a series of bytes in reverse.
543 *
544 * @param amount
545 * The amount of bytes to read.
546 *
547 * @return The bytes in reverse.
548 */
549 public byte[] readBytesReverse(int amount) {
550 return readBytesReverse(amount, ByteModification.NONE);
551 }
552
553 /**
554 * Reads a series of bytes in reverse.
555 *
556 * @param amount
557 * The amount of bytes to read.
558 *
559 * @param mod
560 * The modification performed on these bytes.
561 *
562 * @return The bytes in reverse.
563 */
564 public byte[] readBytesReverse(int amount, ByteModification mod) {
565 byte[] data = new byte[amount];
566
567 int dataPosition = 0;
568
569 for (int index = payload.readerIndex() + amount - 1; index >= payload.readerIndex(); index--) {
570 int value = payload.getByte(index);
571
572 switch (mod) {
573
574 case ADD:
575 value -= 128;
576 break;
577
578 case NEG:
579 value = -value;
580 break;
581
582 case SUB:
583 value = 128 - value;
584 break;
585
586 case NONE:
587 break;
588
589 }
590 data[dataPosition++] = (byte) value;
591 }
592 return data;
593 }
594
595 /**
596 * Reads a RuneScape string.
597 *
598 * @return The string.
599 */
600 public String getRS2String() {
601 final StringBuilder bldr = new StringBuilder();
602 byte b;
603 while (payload.isReadable() && (b = payload.readByte()) != 10) {
604 bldr.append((char) b);
605 }
606 return bldr.toString();
607 }
608
609 @Override
610 public String toString() {
611 return String.format("[opcode=%d], [type=%s], [size= %d]", opcode, header.name(), size);
612 }
613
614}
byte[] readBytes(int amount)
Reads the amount of bytes into the array, starting at the current position.
int readShort(boolean signed, ByteModification mod)
Reads a short value from the payload in BIG order.
int readByte(boolean signed)
Reads a STANDARD byte from the payload.
int readShort(boolean signed, ByteOrder order)
Reads a STANDARD short value from the payload.
final PacketType header
The header for this packet.
int readShort(ByteOrder order, ByteModification mod)
Reads a signed short value from the payload.
int readShort(ByteOrder order)
Reads a STANDARD signed short value from the payload.
int getOpcode()
Gets the opcode.
int readShort(boolean signed, ByteOrder order, ByteModification mod)
Reads a short value from the payload.
int readInt(boolean signed, ByteOrder order, ByteModification mod)
Reads an integer value from the payload.
final ByteBuf payload
The buffer that contains the data for this packet.
long readLong()
Reads a STANDARD signed long value from the payload in BIG order.
int readByte(ByteModification mod)
Reads a signed byte from the payload.
int readShort(boolean signed)
Reads a STANDARD short value from the payload in BIG order.
ByteBuf getPayload()
Gets the payload.
PacketType getHeader()
Gets the type.
int readShort(ByteModification mod)
Reads a signed short value from the payload BIG order.
int readInt(boolean signed)
Reads a STANDARD integer value from the payload in BIG order.
long readLong(boolean signed, ByteOrder order, ByteModification mod)
Reads a long value from the payload.
byte[] readBytesReverse(int amount)
Reads a series of bytes in reverse.
String getRS2String()
Reads a RuneScape string.
int readByte()
Reads a STANDARD signed byte from the payload.
long readLong(boolean signed)
Reads a STANDARD long value from the payload in BIG order.
int readInt(boolean signed, ByteOrder order)
Reads a STANDARD integer value from the payload.
byte[] readBytesReverse(int amount, ByteModification mod)
Reads a series of bytes in reverse.
int getSize()
Gets the size of this payload.
final int size
The size of this packet.
int readInt()
Reads a STANDARD signed integer value from the payload in BIG order.
final int opcode
The opcode for this packet.
long readLong(boolean signed, ByteOrder order)
Reads a STANDARD long value from the payload.
long readLong(ByteModification mod)
Reads a signed long value from the payload in BIG order.
long readLong(boolean signed, ByteModification mod)
Reads a long value from the payload in BIG order.
int readShort()
Reads a STANDARD signed short value from the payload in BIG order.
int readByte(boolean signed, ByteModification mod)
Reads a single byte from the payload.
GamePacket(final int opcode, final PacketType header, final ByteBuf payload)
Creates a GamePacket.
int readInt(ByteModification mod)
Reads a signed integer value from the payload in ByteOrder BIG order.
byte[] readBytes(int amount, ByteModification mod)
Reads a series of bytes from a buffer.
int readInt(boolean signed, ByteModification mod)
Reads an integer value from the payload in BIG order.
Represents RuneScape's custom value types.
Represents the order in which bytes are written.
Definition ByteOrder.java:8
BE
Represents Big-endian.
Represents a type of packet.