-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConnection.php
1271 lines (1215 loc) · 31.4 KB
/
Connection.php
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Phpcraft;
use DomainException;
use GMP;
use hellsh\UUID;
use InvalidArgumentException;
use LengthException;
use Phpcraft\Exception\
{IOException, MissingMetadataException, NoConnectionException};
use Phpcraft\NBT\
{ByteArrayTag, ByteTag, CompoundTag, DoubleTag, EndTag, FloatTag, IntArrayTag, IntTag, ListTag, LongArrayTag, LongTag, NBT, ShortTag, StringTag};
use Phpcraft\Packet\PacketId;
/**
* A wrapper to read and write from streams.
* The Connection object can also be utilized without a stream, e.g.:
* <pre>$con = new \\Phpcraft\\Connection($protocol_version);
* $packet = new \\Phpcraft\\SpawnMobPacket();
* // $packet->...
* $packet->send($con);
* echo \\Phpcraft\\Phpcraft::binaryStringToHex($con->write_buffer)."\n";</pre>
*/
class Connection
{
const STATE_HANDSHAKE = 0;
const STATE_STATUS = 1;
const STATE_LOGIN = 2;
const STATE_PLAY = 3;
/**
* Allow packets that vanilla would reject.
*/
const LENIENCY_LENIENT = 1;
/**
* Reject packets that vanilla would reject.
*/
const LENIENCY_VANILLA = 0;
/**
* Don't allow malformatted packets, even if vanilla would accept them.
* Only recommended for people who want to perfect their client and server code.
*/
const LENIENCY_STRICT = -1;
static $zero;
static $pow2 = [];
/**
* The protocol version that is used for this connection.
*
* @var int $protocol_version
*/
public $protocol_version;
/**
* The stream of the connection or null.
*
* @var resource|null $stream
*/
public $stream;
/**
* The amount of bytes a packet needs for it to be compressed as an integer or -1 if disabled.
*
* @var int $compression_threshold
*/
public $compression_threshold = -1;
/**
* The state of the connection.
* 1 stands for status, 2 for logging in, and 3 for playing.
*
* @var int $state
*/
public $state;
/**
* The write buffer binary string.
*
* @var string $write_buffer
*/
public $write_buffer = "";
/**
* If true, $leniency will be set to LENIENCY_LENIENT.
*
* @var bool $lenient
* @since 0.5
* @deprecated Set $leniency to LENIENCY_LENIENT, instead.
*/
public $lenient = false;
/**
* Determines the leniency that will be used when sending and receiving packets.
*
* @var int $leniency
* @since 0.5.4
*/
public $leniency = self::LENIENCY_VANILLA;
/**
* The read buffer binary string.
*
* @var string $read_buffer
* @see Connection::setReadBuffer()
*/
public $read_buffer = "";
public $read_buffer_offset = 0;
/**
* @param int $protocol_version -1 will use the latest supported protocol version
* @param resource|null $stream
*/
function __construct(int $protocol_version = -1, $stream = null)
{
$this->protocol_version = $protocol_version == -1 ? Versions::protocol(false)[0] : $protocol_version;
if($stream !== null)
{
stream_set_blocking($stream, false);
$this->stream = $stream;
}
}
/**
* Returns true if this connection has an open stream.
*
* @return boolean
*/
function isOpen(): bool
{
return $this->stream !== null && @feof($this->stream) === false;
}
/**
* Updates the read buffer correctly.
*
* @param string $buffer The new read buffer binary string.
* @return void
*/
function setReadBuffer(string $buffer): void
{
$this->read_buffer = $buffer;
$this->read_buffer_offset = 0;
}
/**
* @since 0.5.22
* @return bool
*/
function hasRemainingData(): bool
{
return $this->read_buffer_offset < strlen($this->read_buffer);
}
/**
* Returns all the data in the read buffer that is yet to be read.
*
* @return string
*/
function getRemainingData(): string
{
return substr($this->read_buffer, $this->read_buffer_offset);
}
/**
* Adds a chat component to the write buffer.
*
* @param ChatComponent $value
* @return Connection $this
*/
function writeChat(ChatComponent $value): Connection
{
return $this->writeString(json_encode($value->toArray()));
}
/**
* Adds a string to the write buffer.
*
* @param string $value
* @return Connection $this
*/
function writeString(string $value): Connection
{
$this->write_buffer .= self::varInt(strlen($value)).$value;
return $this;
}
/**
* Converts a number to a VarInt binary string.
*
* @param GMP|int|string $value
* @return string
*/
static function varInt($value): string
{
if(is_float($value))
{
$value = intval($value);
}
if(gmp_cmp($value, 0) < 0)
{
$value = gmp_sub(self::$pow2[32], gmp_abs($value));
}
$bytes = "";
do
{
$temp = gmp_intval(gmp_and($value, 0b01111111));
$value = gmp_div($value, 128); // $value >> 7
if(gmp_cmp($value, 0) != 0)
{
$temp |= 0b10000000;
}
$bytes .= pack("C", $temp);
}
while($value != 0);
return $bytes;
}
/**
* Adds the byte string to the write buffer.
*
* @param string $value
* @return Connection $this
*/
function writeRaw(string $value): Connection
{
$this->write_buffer .= $value;
return $this;
}
/**
* Adds a float to the write buffer.
*
* @param float $value
* @return Connection $this
*/
function writeFloat(float $value): Connection
{
$this->write_buffer .= pack("G", $value);
return $this;
}
/**
* Adds a position encoded as an unsigned long to the write buffer.
*
* @param Point3D $pos
* @return Connection $this
*/
function writePosition(Point3D $pos): Connection
{
$long = gmp_mul(gmp_and(intval($pos->x), 0x3FFFFFF), self::$pow2[38]); // $long = ($pos->x & 0x3FFFFFF) << 38;
if($this->protocol_version < 472)
{
$long = gmp_or($long, gmp_mul(gmp_and(intval($pos->y), 0xFFF), self::$pow2[26])); // $long |= ($pos->y & 0xFFF) << 26;
$long = gmp_or($long, gmp_and(intval($pos->z), 0x3FFFFFF)); // $long |= ($pos->z & 0x3FFFFFF);
}
else
{
$long = gmp_or($long, gmp_mul(gmp_and(intval($pos->z), 0x3FFFFFF), self::$pow2[12])); // $long |= ($pos->z & 0x3FFFFFF) << 12;
$long = gmp_or($long, gmp_and(intval($pos->y), 0xFFF)); // $long |= ($pos->y & 0xFFF);
}
$this->writeGMP($long, 8, 64, false);
return $this;
}
function writeGMP($value, int $bytes, int $bits, bool $signed, int $gmp_export_options = GMP_MSW_FIRST | GMP_BIG_ENDIAN): void
{
if(is_float($value))
{
$value = intval($value);
}
if($signed)
{
$value = self::signNumber($value, $bits);
}
$cmp0 = gmp_cmp($value, 0);
if($cmp0 == 0)
{
$this->write_buffer .= str_repeat("\0", $bytes);
}
else
{
if($cmp0 < 0)
{
$value = gmp_sub(self::$pow2[$bits], gmp_abs($value));
}
$this->write_buffer .= gmp_export($value, $bytes, $gmp_export_options);
}
}
private static function signNumber($value, int $bits)
{
if(gmp_cmp($value, self::$pow2[$bits - 1]) >= 0)
{
$value = gmp_sub($value, self::$pow2[$bits]);
}
return $value;
}
/**
* Adds a signed long to the write buffer.
*
* @param GMP|int|string $value
* @return Connection $this
*/
function writeLong($value): Connection
{
$this->writeGMP($value, 8, 64, true);
return $this;
}
/**
* Adds a position encoded as three double to the write buffer.
*
* @param Point3D $pos
* @return Connection $this
*/
function writePrecisePosition(Point3D $pos): Connection
{
$this->writeDouble($pos->x);
$this->writeDouble($pos->y);
return $this->writeDouble($pos->z);
}
/**
* Adds a double to the write buffer.
*
* @param double $value
* @return Connection $this
*/
function writeDouble(float $value): Connection
{
$this->write_buffer .= pack("E", $value);
return $this;
}
/**
* Adds a position encoded as three ints to the write buffer.
*
* @param Point3D $pos
* @return Connection $this
*/
function writeFixedPointPosition(Point3D $pos): Connection
{
$this->writeInt(intval($pos->x * 32));
$this->writeInt(intval($pos->y * 32));
return $this->writeInt(intval($pos->z * 32));
}
/**
* Adds a signed integer to the write buffer.
*
* @param GMP|int|string $value
* @return Connection $this
*/
function writeInt($value): Connection
{
$this->writeGMP($value, 4, 32, true);
return $this;
}
/**
* Adds a slot to the write buffer.
*
* @param Slot $slot
* @return Connection $this
* @throws MissingMetadataException
*/
function writeSlot(Slot $slot): Connection
{
if(Slot::isEmpty($slot))
{
if($this->protocol_version >= 402)
{
$this->writeBoolean(false);
}
else
{
$this->writeShort(-1);
}
}
else
{
if($this->protocol_version >= 402)
{
$this->writeBoolean(true);
$this->writeVarInt($slot->item->getId($this->protocol_version));
$this->writeByte($slot->count);
}
else
{
$id = $slot->item->getId($this->protocol_version);
if($this->protocol_version < 346)
{
$this->writeShort($id >> 4);
$this->writeByte($slot->count);
switch($slot->item->name)
{
case "filled_map":
if(!($slot->nbt instanceof CompoundTag) || !$slot->nbt->hasChild("map"))
{
throw new MissingMetadataException("filled_map is missing ID");
}
$this->writeShort($slot->nbt->getChild("map")->value);
break;
default:
$this->writeShort($id & 0xF);
}
}
else
{
$this->writeShort($id);
$this->writeByte($slot->count);
}
}
if($this->protocol_version < 402 && $slot->nbt instanceof CompoundTag)
{
$display = $slot->nbt->getChild("display");
if($display instanceof CompoundTag)
{
$name = $display->getChild("Name");
if($name instanceof StringTag)
{
$name->value = ChatComponent::fromArray(json_decode($name->value, true))
->toString(ChatComponent::FORMAT_SILCROW);
}
}
}
$slot->nbt->write($this);
}
return $this;
}
/**
* Adds a boolean to the write buffer.
*
* @param boolean $value
* @return Connection this
*/
function writeBoolean(bool $value): Connection
{
$this->write_buffer .= pack("C", ($value ? 1 : 0));
return $this;
}
/**
* Adds a signed short to the write buffer.
*
* @param GMP|int|string $value
* @return Connection $this
*/
function writeShort($value): Connection
{
$this->writeGMP($value, 2, 16, true);
return $this;
}
/**
* Adds a VarInt to the write buffer.
*
* @param GMP|int|string $value
* @return Connection $this
*/
function writeVarInt($value): Connection
{
$this->write_buffer .= self::varInt($value);
return $this;
}
/**
* Adds a signed byte to the write buffer.
*
* @param int $value
* @return Connection $this
*/
function writeByte(int $value): Connection
{
$this->write_buffer .= pack("c", $value);
return $this;
}
/**
* Adds an unsigned short to the write buffer.
*
* @param GMP|int|string $value
* @return Connection $this
*/
function writeUnsignedShort($value): Connection
{
$this->writeGMP($value, 2, 16, false);
return $this;
}
/**
* Adds an unsigned byte to the write buffer.
*
* @param int $value
* @return Connection $this
*/
function writeUnsignedByte(int $value): Connection
{
$this->write_buffer .= pack("C", $value);
return $this;
}
/**
* Adds an angle to the write buffer.
*
* @param float $value
* @return Connection $this
*/
function writeAngle(float $value): Connection
{
return $this->writeByte($value / 360 * 256);
}
/**
* Adds a UUID to the write buffer.
*
* @param UUID $uuid
* @return Connection $this
*/
function writeUUID(UUID $uuid): Connection
{
$this->write_buffer .= $uuid->binary;
return $this;
}
/**
* Clears the write buffer and starts a new packet.
*
* @param string|integer|PacketId $packet The name or ID of the new packet.
* @return Connection $this
*/
function startPacket($packet): Connection
{
if($packet instanceof PacketId)
{
$packet = $packet->getId($this->protocol_version);
}
else if(gettype($packet) == "string")
{
$packetId = PacketId::get($packet);
if(!$packetId)
{
throw new DomainException("Unknown packet name: ".$packet);
}
$packet = $packetId->getId($this->protocol_version);
}
else if(gettype($packet) != "integer")
{
throw new InvalidArgumentException("\$packet has to be string, integer, or instance of PacketId");
}
$this->write_buffer = self::varInt($packet);
return $this;
}
/**
* Sends the contents of the write buffer over the stream and clears the write buffer. Does nothing if the connection has no stream.
*
* @param boolean $raw When true, the write buffer is sent as-is, without length prefix or compression, which you probably don't want.
* @throws NoConnectionException if the stream is closed
* @throws IOException if the packet is too big (LENIENCY_VANILLA)
* @return Connection $this
*/
function send(bool $raw = false): Connection
{
if($this->stream != null)
{
if(@feof($this->stream) !== false)
{
throw new NoConnectionException();
}
stream_set_blocking($this->stream, true);
$start = microtime(true);
if($raw)
{
$w = @fwrite($this->stream, $this->write_buffer);
}
else
{
$length = strlen($this->write_buffer);
if($this->lenient)
{
$this->leniency = self::LENIENCY_LENIENT;
}
if($length > 2097152 && $this->leniency != self::LENIENCY_LENIENT)
{
throw new IOException("Packet length exceeds 2097152 bytes");
}
if($this->compression_threshold > -1)
{
if($length >= $this->compression_threshold)
{
$compressed = zlib_encode($this->write_buffer, ZLIB_ENCODING_DEFLATE, 6);
$compressed_length = strlen($compressed);
$length_varint = self::varInt($length);
$w = @fwrite($this->stream, self::varInt($compressed_length + strlen($length_varint)).$length_varint.$compressed);
}
else
{
$w = @fwrite($this->stream, self::varInt($length + 1)."\x00".$this->write_buffer);
}
}
else
{
$w = @fwrite($this->stream, self::varInt($length).$this->write_buffer);
}
}
stream_set_blocking($this->stream, false);
$this->write_buffer = "";
if(!$w || (microtime(true) - $start) >= 3)
{
$this->close();
}
}
return $this;
}
/**
* Closes the connection's stream, if it has one.
*
* @return void
*/
function close(): void
{
if($this->stream != null)
{
stream_socket_shutdown($this->stream, STREAM_SHUT_RDWR);
fclose($this->stream);
$this->stream = null;
}
}
/**
* Puts raw bytes from the stream into the read buffer.
*
* @param float $timeout The amount of seconds to wait before the read is aborted.
* @param int $bytes The exact amount of bytes you would like to receive. 0 means read up to 8 KiB.
* @return boolean True on success.
* @see Connection::readPacket
*/
function readRawPacket(float $timeout = 3.000, int $bytes = 0): bool
{
$start = microtime(true);
if($bytes == 0)
{
$this->read_buffer = fread($this->stream, 8192);
while($this->read_buffer == "")
{
if((microtime(true) - $start) >= $timeout)
{
return false;
}
$this->read_buffer .= fread($this->stream, 8192);
}
}
else
{
$this->read_buffer = @fread($this->stream, $bytes);
if(strlen($this->read_buffer) > 0)
{
$timeout += 0.1;
}
while(strlen($this->read_buffer) < $bytes)
{
if((microtime(true) - $start) >= $timeout)
{
return false;
}
$this->read_buffer .= fread($this->stream, $bytes - strlen($this->read_buffer));
}
}
$this->read_buffer_offset = 0;
return strlen($this->read_buffer) > 0;
}
/**
* The address of the connection's peer or null if the connection has no stream.
*
* @return string|null
*/
function getRemoteAddress(): ?string
{
if($this->stream === null)
{
return null;
}
$name = stream_socket_get_name($this->stream, true);
return substr($name, 0, strpos($name, ":", -6));
}
/**
* Puts a new packet into the read buffer.
*
* @param float $timeout The amount of seconds to wait before read is aborted.
* @return int|boolean The packet's ID or false if no packet was received within the time limit.
* @throws IOException
* @see Connection::readRawPacket
* @see ClientboundPacketId::getById
* @see ServerboundPacketId::getById
*/
function readPacket(float $timeout = 3.000)
{
$start = microtime(true);
$length = 0;
$read = 0;
if($this->lenient)
{
$this->leniency = self::LENIENCY_LENIENT;
}
do
{
if($read > 3 && $this->leniency != self::LENIENCY_LENIENT)
{
throw new IOException("Packet length exceeds 2097152 bytes");
}
$byte = @fgetc($this->stream);
while($byte === false)
{
if((microtime(true) - $start) >= $timeout)
{
return false;
}
$byte = @fgetc($this->stream);
}
$byte = ord($byte);
$length |= (($byte & 0b01111111) << (7 * $read++));
}
while(($byte & 0b10000000) != 0);
// It's established that a packet is on the line, but it could take more than one read to get it into the read buffer, so some additional time is forcefully allocated.
$timeout += 0.1;
$this->read_buffer = fread($this->stream, $length);
while(strlen($this->read_buffer) < $length)
{
if((microtime(true) - $start) >= $timeout)
{
return false;
}
$this->read_buffer .= fread($this->stream, $length - strlen($this->read_buffer));
}
if($this->compression_threshold > -1)
{
$uncompressed_length = 0;
$offset = 0;
do
{
if($offset > 3)
{
throw new IOException("Uncompressed packet length exceeds 2097152 bytes");
}
$byte = unpack("Cbyte", substr($this->read_buffer, $offset, 1))["byte"];
$uncompressed_length |= (($byte & 0b01111111) << (7 * $offset++));
}
while(($byte & 0b10000000) != 0);
$this->read_buffer = substr($this->read_buffer, $offset);
if($uncompressed_length > 0)
{
$this->read_buffer = @zlib_decode($this->read_buffer, $uncompressed_length);
if($this->read_buffer === false)
{
throw new IOException("Failed to decompress packet data");
}
}
}
$this->read_buffer_offset = 0;
return gmp_intval($this->readVarInt());
}
/**
* Reads an integer encoded as a VarInt from the read buffer.
*
* @return GMP
* @throws IOException When the VarInt is too big or there are not enough bytes to read or continue reading a VarInt
*/
function readVarInt(): GMP
{
$value = self::$zero;
$read = 0;
do
{
if($this->read_buffer_offset >= strlen($this->read_buffer))
{
throw new IOException("There are not enough bytes to read a VarInt");
}
$byte = unpack("Cbyte", substr($this->read_buffer, $this->read_buffer_offset++, 1))["byte"];
$value = gmp_or($value, gmp_mul(gmp_and($byte, 0b01111111), pow(2, 7 * $read)));
// $value |= (($byte & 0b01111111) << (7 * $read));
if(++$read > 5)
{
throw new IOException("VarInt is too big");
}
}
while(($byte & 0b10000000) != 0);
return self::signNumber($value, 32);
}
/**
* Reads an unsigned byte from the read buffer.
*
* @return int
* @throws IOException When there are not enough bytes to read a byte.
*/
function readUnsignedByte(): int
{
if($this->read_buffer_offset >= strlen($this->read_buffer))
{
throw new IOException("There are not enough bytes to read a byte");
}
return unpack("Cbyte", substr($this->read_buffer, $this->read_buffer_offset++, 1))["byte"];
}
/**
* Reads an angle from the read buffer.
*
* @return float
* @throws IOException When there are not enough bytes to read an angle.
*/
function readAngle(): float
{
return $this->readByte() / 256 * 360;
}
/**
* Reads a signed byte from the read buffer.
*
* @return int
* @throws IOException When there are not enough bytes to read a byte.
*/
function readByte(): int
{
if($this->read_buffer_offset >= strlen($this->read_buffer))
{
throw new IOException("There are not enough bytes to read a byte");
}
return unpack("cbyte", substr($this->read_buffer, $this->read_buffer_offset++, 1))["byte"];
}
/**
* Reads a chat component from the read buffer.
*
* @return ChatComponent
* @throws IOException When there are not enough bytes to read the string.
*/
function readChat(): ChatComponent
{
return ChatComponent::fromArray(json_decode($this->readString(), true));
}
/**
* Reads a string from the read buffer.
*
* @param int $maxLength The maximum amount of bytes this string may use.
* @param int $minLength The minimum amount of bytes this string must use.
* @return string
* @throws LengthException When the string doesn't fit the length requirements.
* @throws IOException When there are not enough bytes to read a string.
*/
function readString(int $maxLength = 32767, int $minLength = -1): string
{
$length = gmp_intval($this->readVarInt());
if($length == 0)
{
return "";
}
if($length > $maxLength)
{
throw new IOException("The string on the wire apparently has {$length} bytes which exceeds {$maxLength}");
}
if($length < $minLength)
{
throw new LengthException("This string on the wire apparently has {$length} bytes but at least {$minLength} are required");
}
if($length > strlen($this->read_buffer))
{
throw new LengthException("String on the wire is apparently {$length} bytes long, but that exceeds the bytes in the read buffer");
}
$str = substr($this->read_buffer, $this->read_buffer_offset, $length);
$this->read_buffer_offset += $length;
return $str;
}
/**
* Reads a position encoded as an unsigned long from the read buffer.
*
* @return Point3D
* @throws IOException When there are not enough bytes to read a position.
*/
function readPosition(): Point3D
{
$long = $this->readGMP(8, 64, false);
$x = gmp_div($long, self::$pow2[38]); // $long >> 38
if($this->protocol_version < 472)
{
$y = gmp_and(gmp_div($long, self::$pow2[26]), 0xFFF); // ($long >> 26) & 0xFFF
$z = gmp_and($long, 0x3FFFFFF);
}
else
{
$y = gmp_and($long, 0xFFF); // $long & 0xFFF
$z = gmp_and(gmp_div($long, self::$pow2[12]), 0x3FFFFFF); // ($long >> 12) & 0x3FFFFFF
}
return new Point3D(gmp_intval(self::signNumber($x, 26)), gmp_intval(self::signNumber($y, 12)), gmp_intval(self::signNumber($z, 26)));
}
/**
* @param int $bytes
* @param int $bits
* @param bool $signed
* @param int $gmp_import_options
* @return GMP
* @throws IOException
* @since 0.5.5
*/
function readGMP(int $bytes, int $bits, bool $signed, int $gmp_import_options = GMP_MSW_FIRST | GMP_BIG_ENDIAN): GMP
{
if(strlen($this->read_buffer) - $this->read_buffer_offset < $bytes)
{
throw new IOException("There are not enough bytes to read a {$bytes}-byte number");
}
$value = gmp_import(substr($this->read_buffer, $this->read_buffer_offset, $bytes), $bytes, $gmp_import_options);
$this->read_buffer_offset += $bytes;
if($signed)
{
$value = self::signNumber($value, $bits);
}
return $value;
}
/**
* Reads a position encoded as three doubles from the read buffer.
*
* @return Point3D
* @throws IOException When there are not enough bytes to read a position.
*/
function readPrecisePosition(): Point3D
{
return new Point3D($this->readDouble(), $this->readDouble(), $this->readDouble());
}
/**
* Reads a double from the read buffer.
*
* @return float
* @throws IOException When there are not enough bytes to read a double.
*/
function readDouble(): float
{
if(strlen($this->read_buffer) - $this->read_buffer_offset < 8)
{
throw new IOException("There are not enough bytes to read a double");
}
$double = unpack("Edouble", substr($this->read_buffer, $this->read_buffer_offset, 8))["double"];
$this->read_buffer_offset += 8;
return $double;
}
/**
* Reads a position encoded as three ints from the read buffer.
*
* @return Point3D
* @throws IOException When there are not enough bytes to read a position.
*/
function readFixedPointPosition(): Point3D
{
return new Point3D(gmp_intval($this->readInt()) / 32, gmp_intval($this->readInt()) / 32, gmp_intval($this->readInt()) / 32);
}
/**
* Reads a signed integer from the read buffer.
*
* @return GMP
* @throws IOException When there are not enough bytes to read an integer.
*/
function readInt(): GMP
{
return $this->readGMP(4, 32, true);
}
/**
* Reads a UUID.
*
* @return UUID
* @throws IOException When there are not enough bytes to read a UUID.
*/
function readUUID(): UUID
{
if(strlen($this->read_buffer) - $this->read_buffer_offset < 16)
{
throw new IOException("There are not enough bytes to read a UUID");
}
$uuid = new UUID(substr($this->read_buffer, $this->read_buffer_offset, 16));
$this->read_buffer_offset += 16;
return $uuid;
}
/**
* Reads a Slot.
*
* @param boolean $additional_processing Whether additional processing should occur to properly receive pre-1.13 data. You should only set this to false if you want a lazy read, and don't even care about the slot.
* @return Slot
* @throws IOException
*/
function readSlot(bool $additional_processing = true): Slot
{
$slot = new Slot();
if($this->protocol_version >= 402)
{
if(!$this->readBoolean())
{
return $slot;
}