RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
GamePacket.java
1package com.osroyale.net.packet;
2
3import com.osroyale.net.codec.ByteModification;
4import com.osroyale.net.codec.ByteOrder;
5import io.netty.buffer.ByteBuf;
6import io.netty.buffer.DefaultByteBufHolder;
7
49
50public class GamePacket extends DefaultByteBufHolder {
51
55 private final int opcode;
56
60 private final PacketType header;
61
65 private final ByteBuf payload;
66
70 private final int size;
71
82 public GamePacket(final int opcode, final PacketType header, final ByteBuf payload) {
83 super(payload);
84
85 this.opcode = opcode;
86 this.header = header;
87 this.payload = payload;
88 this.size = payload.readableBytes();
89 }
90
96 public int getSize() {
97 return size;
98 }
99
105 public int getOpcode() {
106 return opcode;
107 }
108
115 return header;
116 }
117
123 public ByteBuf getPayload() {
124 return payload;
125 }
126
132 public int readByte() {
133 return payload.readByte();
134 }
135
144 public int readByte(boolean signed) {
145 return readByte(signed, ByteModification.NONE);
146 }
147
156 public int readByte(ByteModification mod) {
157 return readByte(true, mod);
158 }
159
171 public int readByte(boolean signed, ByteModification mod) {
172 int value = payload.readByte();
173 switch (mod) {
174 case ADD:
175 value = value - 128;
176 break;
177
178 case SUB:
179 value = 128 - value;
180 break;
181
182 case NEG:
183 value = -value;
184 break;
185
186 case NONE:
187 break;
188
189 }
190 return signed ? value : value & 0xFF;
191 }
192
199 public int readShort() {
200 return payload.readShort();
201 }
202
212 public int readShort(boolean signed) {
213 return readShort(signed, ByteOrder.BE, ByteModification.NONE);
214 }
215
224 public int readShort(ByteOrder order) {
225 return readShort(true, order, ByteModification.NONE);
226 }
227
239 public int readShort(boolean signed, ByteOrder order) {
240 return readShort(signed, order, ByteModification.NONE);
241 }
242
254 public int readShort(ByteOrder order, ByteModification mod) {
255 return readShort(true, order, mod);
256 }
257
266 public int readShort(ByteModification mod) {
267 return readShort(true, ByteOrder.BE, mod);
268 }
269
281 public int readShort(boolean signed, ByteModification mod) {
282 return readShort(signed, ByteOrder.BE, mod);
283 }
284
299 public int readShort(boolean signed, ByteOrder order, ByteModification mod) {
300 int value = 0;
301 switch (order) {
302 case BE:
303 value |= readByte(false) << 8;
304 value |= readByte(false, mod);
305 break;
306
307 case IME:
308 throw new UnsupportedOperationException("Inverse-middle-endian short is impossible!");
309
310 case ME:
311 throw new UnsupportedOperationException("Middle-endian short " + "is impossible!");
312
313 case LE:
314 value |= readByte(false, mod);
315 value |= readByte(false) << 8;
316 break;
317 }
318 return signed ? value : value & 0xFFFF;
319 }
320
327 public int readInt() {
329 }
330
340 public int readInt(boolean signed) {
341 return readInt(signed, ByteOrder.BE, ByteModification.NONE);
342 }
343
353 public int readInt(ByteModification mod) {
354 return readInt(true, ByteOrder.BE, mod);
355 }
356
368 public int readInt(boolean signed, ByteModification mod) {
369 return readInt(signed, ByteOrder.BE, mod);
370 }
371
383 public int readInt(boolean signed, ByteOrder order) {
384 return readInt(signed, order, ByteModification.NONE);
385 }
386
401 public int readInt(boolean signed, ByteOrder order, ByteModification mod) {
402 long value = 0;
403 switch (order) {
404 case BE:
405 value |= readByte(false) << 24;
406 value |= readByte(false) << 16;
407 value |= readByte(false) << 8;
408 value |= readByte(false, mod);
409 break;
410
411 case ME:
412 value |= readByte(false) << 8;
413 value |= readByte(false, mod);
414 value |= readByte(false) << 24;
415 value |= readByte(false) << 16;
416 break;
417 case IME:
418 value |= readByte(false) << 16;
419 value |= readByte(false) << 24;
420 value |= readByte(false, mod);
421 value |= readByte(false) << 8;
422 break;
423
424 case LE:
425 value |= readByte(false, mod);
426 value |= readByte(false) << 8;
427 value |= readByte(false) << 16;
428 value |= readByte(false) << 24;
429 break;
430
431 }
432 return (int) (signed ? value : value & 0xFFFFFFFFL);
433 }
434
441 public long readLong() {
443 }
444
454 public long readLong(boolean signed) {
455 return readLong(signed, ByteOrder.BE, ByteModification.NONE);
456 }
457
466 public long readLong(ByteModification mod) {
467 return readLong(true, ByteOrder.BE, mod);
468 }
469
481 public long readLong(boolean signed, ByteModification mod) {
482 return readLong(signed, ByteOrder.BE, mod);
483 }
484
496 public long readLong(boolean signed, ByteOrder order) {
497 return readLong(signed, order, ByteModification.NONE);
498 }
499
514 public long readLong(boolean signed, ByteOrder order, ByteModification mod) {
515 long value = 0;
516 switch (order) {
517 case BE:
518 value |= (long) readByte(false) << 56L;
519 value |= (long) readByte(false) << 48L;
520 value |= (long) readByte(false) << 40L;
521 value |= (long) readByte(false) << 32L;
522 value |= (long) readByte(false) << 24L;
523 value |= (long) readByte(false) << 16L;
524 value |= (long) readByte(false) << 8L;
525 value |= readByte(false, mod);
526 break;
527
528 case ME:
529 case IME:
530 throw new UnsupportedOperationException("Middle and " + "inverse-middle value types not supported!");
531
532 case LE:
533 value |= readByte(false, mod);
534 value |= (long) readByte(false) << 8L;
535 value |= (long) readByte(false) << 16L;
536 value |= (long) readByte(false) << 24L;
537 value |= (long) readByte(false) << 32L;
538 value |= (long) readByte(false) << 40L;
539 value |= (long) readByte(false) << 48L;
540 value |= (long) readByte(false) << 56L;
541 break;
542
543 }
544 return signed ? value : value & 0xFFFFFFFFL;
545 }
546
555 public byte[] readBytes(int amount) {
556 return readBytes(amount, ByteModification.NONE);
557 }
558
570 public byte[] readBytes(int amount, ByteModification mod) {
571 byte[] data = new byte[amount];
572 for (int i = 0; i < amount; i++) {
573 data[i] = (byte) readByte(mod);
574 }
575 return data;
576 }
577
586 public byte[] readBytesReverse(int amount) {
587 return readBytesReverse(amount, ByteModification.NONE);
588 }
589
601 public byte[] readBytesReverse(int amount, ByteModification mod) {
602 byte[] data = new byte[amount];
603
604 int dataPosition = 0;
605
606 for (int index = payload.readerIndex() + amount - 1; index >= payload.readerIndex(); index--) {
607 int value = payload.getByte(index);
608
609 switch (mod) {
610
611 case ADD:
612 value -= 128;
613 break;
614
615 case NEG:
616 value = -value;
617 break;
618
619 case SUB:
620 value = 128 - value;
621 break;
622
623 case NONE:
624 break;
625
626 }
627 data[dataPosition++] = (byte) value;
628 }
629 return data;
630 }
631
637 public String getRS2String() {
638 final StringBuilder bldr = new StringBuilder();
639 byte b;
640 while (payload.isReadable() && (b = payload.readByte()) != 10) {
641 bldr.append((char) b);
642 }
643 return bldr.toString();
644 }
645
646 @Override
647 public String toString() {
648 return String.format("[opcode=%d], [type=%s], [size= %d]", opcode, header.name(), size);
649 }
650
651}
int readInt(boolean signed, ByteModification mod)
int readShort(boolean signed, ByteOrder order)
int readShort(boolean signed, ByteModification mod)
byte[] readBytes(int amount, ByteModification mod)
int readInt(boolean signed, ByteOrder order)
int readByte(ByteModification mod)
int readShort(ByteOrder order, ByteModification mod)
int readShort(ByteModification mod)
int readByte(boolean signed, ByteModification mod)
int readInt(boolean signed, ByteOrder order, ByteModification mod)
long readLong(boolean signed, ByteOrder order)
long readLong(boolean signed, ByteOrder order, ByteModification mod)
int readInt(ByteModification mod)
long readLong(boolean signed, ByteModification mod)
byte[] readBytesReverse(int amount, ByteModification mod)
int readShort(boolean signed, ByteOrder order, ByteModification mod)
GamePacket(final int opcode, final PacketType header, final ByteBuf payload)
long readLong(ByteModification mod)