예제 #1
0
 public function handle($address, $port, $packet)
 {
     $offset = 2;
     $packetType = ord($packet[$offset++]);
     $sessionID = Binary::readInt(substr($packet, $offset, 4));
     $offset += 4;
     $payload = substr($packet, $offset);
     switch ($packetType) {
         case self::HANDSHAKE:
             //Handshake
             $reply = chr(self::HANDSHAKE);
             $reply .= Binary::writeInt($sessionID);
             $reply .= self::getTokenString($this->token, $address) . "";
             $this->server->getNetwork()->sendPacket($address, $port, $reply);
             break;
         case self::STATISTICS:
             //Stat
             $token = Binary::readInt(substr($payload, 0, 4));
             if ($token !== self::getTokenString($this->token, $address) and $token !== self::getTokenString($this->lastToken, $address)) {
                 break;
             }
             $reply = chr(self::STATISTICS);
             $reply .= Binary::writeInt($sessionID);
             if ($this->timeout < microtime(true)) {
                 $this->regenerateInfo();
             }
             if (strlen($payload) === 8) {
                 $reply .= $this->longData;
             } else {
                 $reply .= $this->shortData;
             }
             $this->server->getNetwork()->sendPacket($address, $port, $reply);
             break;
     }
 }
 public function encode()
 {
     $this->reset();
     $this->putLong($this->eid);
     $meta = Binary::writeMetadata($this->metadata);
     $this->put($meta);
 }
예제 #3
0
 public function encode()
 {
     $this->reset();
     $this->putUUID($this->uuid);
     $this->putString($this->username);
     $this->putLong($this->eid);
     $this->putFloat($this->x);
     $this->putFloat($this->y);
     $this->putFloat($this->z);
     $this->putFloat($this->speedX);
     $this->putFloat($this->speedY);
     $this->putFloat($this->speedZ);
     $this->putFloat($this->yaw);
     $this->putFloat($this->yaw);
     //TODO headrot
     $this->putFloat($this->pitch);
     $this->putSlot($this->item);
     $meta = Binary::writeMetadata($this->metadata);
     $this->put($meta);
 }
예제 #4
0
 private function readPacket($client, &$size, &$requestID, &$packetType, &$payload)
 {
     socket_set_nonblock($client);
     $d = socket_read($client, 4);
     if ($this->stop === true) {
         return false;
     } elseif ($d === false) {
         return null;
     } elseif ($d === "" or strlen($d) < 4) {
         return false;
     }
     socket_set_block($client);
     $size = Binary::readLInt($d);
     if ($size < 0 or $size > 65535) {
         return false;
     }
     $requestID = Binary::readLInt(socket_read($client, 4));
     $packetType = Binary::readLInt(socket_read($client, 4));
     $payload = rtrim(socket_read($client, $size + 2));
     //Strip two null bytes
     return true;
 }
예제 #5
0
 public function encode()
 {
     $this->reset();
     $this->putLong($this->eid);
     $this->putInt($this->type);
     $this->putFloat($this->x);
     $this->putFloat($this->y);
     $this->putFloat($this->z);
     $this->putFloat($this->speedX);
     $this->putFloat($this->speedY);
     $this->putFloat($this->speedZ);
     $this->putFloat($this->yaw);
     $this->putFloat($this->pitch);
     $meta = Binary::writeMetadata($this->metadata);
     $this->put($meta);
     $this->putShort(count($this->links));
     foreach ($this->links as $link) {
         $this->putLong($link[0]);
         $this->putLong($link[1]);
         $this->putByte($link[2]);
     }
 }
예제 #6
0
 /**
  * @deprecated
  */
 public static function randomUUID()
 {
     return Utils::toUUID(Binary::writeInt(time()) . Binary::writeShort(getmypid()) . Binary::writeShort(getmyuid()) . Binary::writeInt(mt_rand(-0x7fffffff, 0x7fffffff)) . Binary::writeInt(mt_rand(-0x7fffffff, 0x7fffffff)), 2);
 }
예제 #7
0
 public function __construct($level, Compound $nbt = null)
 {
     if ($nbt === null) {
         $this->provider = $level;
         $this->nbt = new Compound("Level", []);
         return;
     }
     $this->nbt = $nbt;
     if (!isset($this->nbt->Entities) or !$this->nbt->Entities instanceof Enum) {
         $this->nbt->Entities = new Enum("Entities", []);
         $this->nbt->Entities->setTagType(NBT::TAG_Compound);
     }
     if (!isset($this->nbt->TileEntities) or !$this->nbt->TileEntities instanceof Enum) {
         $this->nbt->TileEntities = new Enum("TileEntities", []);
         $this->nbt->TileEntities->setTagType(NBT::TAG_Compound);
     }
     if (!isset($this->nbt->TileTicks) or !$this->nbt->TileTicks instanceof Enum) {
         $this->nbt->TileTicks = new Enum("TileTicks", []);
         $this->nbt->TileTicks->setTagType(NBT::TAG_Compound);
     }
     if (!isset($this->nbt->Sections) or !$this->nbt->Sections instanceof Enum) {
         $this->nbt->Sections = new Enum("Sections", []);
         $this->nbt->Sections->setTagType(NBT::TAG_Compound);
     }
     if (!isset($this->nbt->BiomeColors) or !$this->nbt->BiomeColors instanceof IntArray) {
         $this->nbt->BiomeColors = new IntArray("BiomeColors", array_fill(0, 256, 0));
     }
     if (!isset($this->nbt->HeightMap) or !$this->nbt->HeightMap instanceof IntArray) {
         $this->nbt->HeightMap = new IntArray("HeightMap", array_fill(0, 256, 0));
     }
     $sections = [];
     foreach ($this->nbt->Sections as $section) {
         if ($section instanceof Compound) {
             $y = (int) $section["Y"];
             if ($y < 8) {
                 $sections[$y] = new ChunkSection($section);
             }
         }
     }
     for ($y = 0; $y < 8; ++$y) {
         if (!isset($sections[$y])) {
             $sections[$y] = new EmptyChunkSection($y);
         }
     }
     $extraData = [];
     if (!isset($this->nbt->ExtraData) or !$this->nbt->ExtraData instanceof ByteArray) {
         $this->nbt->ExtraData = new ByteArray("ExtraData", Binary::writeInt(0));
     } else {
         $stream = new BinaryStream($this->nbt->ExtraData->getValue());
         $count = $stream->getInt();
         for ($i = 0; $i < $count; ++$i) {
             $key = $stream->getInt();
             $extraData[$key] = $stream->getShort(false);
         }
     }
     parent::__construct($level, (int) $this->nbt["xPos"], (int) $this->nbt["zPos"], $sections, $this->nbt->BiomeColors->getValue(), $this->nbt->HeightMap->getValue(), $this->nbt->Entities->getValue(), $this->nbt->TileEntities->getValue(), $extraData);
     if (isset($this->nbt->Biomes)) {
         $this->checkOldBiomes($this->nbt->Biomes->getValue());
         unset($this->nbt->Biomes);
     }
     unset($this->nbt->Sections, $this->nbt->ExtraData);
 }
예제 #8
0
 public static function chunkIndex($chunkX, $chunkZ)
 {
     return Binary::writeLInt($chunkX) . Binary::writeLInt($chunkZ);
 }
예제 #9
0
 protected function createBlank()
 {
     fseek($this->filePointer, 0);
     ftruncate($this->filePointer, 0);
     $this->lastSector = 1;
     $table = "";
     for ($i = 0; $i < 1024; ++$i) {
         $this->locationTable[$i] = [0, 0];
         $table .= Binary::writeInt(0);
     }
     $time = time();
     for ($i = 0; $i < 1024; ++$i) {
         $this->locationTable[$i][2] = $time;
         $table .= Binary::writeInt($time);
     }
     fwrite($this->filePointer, $table, 4096 * 2);
 }
예제 #10
0
 public function processBatch(BatchPacket $packet, Player $p)
 {
     $str = zlib_decode($packet->payload, 1024 * 1024 * 64);
     //Max 64MB
     $len = strlen($str);
     $offset = 0;
     try {
         while ($offset < $len) {
             $pkLen = Binary::readInt(substr($str, $offset, 4));
             $offset += 4;
             $buf = substr($str, $offset, $pkLen);
             $offset += $pkLen;
             if (($pk = $this->getPacket(ord($buf[0]))) !== null) {
                 if ($pk::NETWORK_ID === Info::BATCH_PACKET) {
                     throw new \InvalidStateException("Invalid BatchPacket inside BatchPacket");
                 }
                 $pk->setBuffer($buf, 1);
                 $pk->decode();
                 $p->handleDataPacket($pk);
                 if ($pk->getOffset() <= 0) {
                     return;
                 }
             }
         }
     } catch (\Exception $e) {
         if (\BukkitPE\DEBUG > 1) {
             $logger = $this->server->getLogger();
             if ($logger instanceof MainLogger) {
                 $logger->debug("BatchPacket " . " 0x" . bin2hex($packet->payload));
                 $logger->logException($e);
             }
         }
     }
 }
예제 #11
0
 public function putLTriad($v)
 {
     $this->buffer .= Binary::writeLTriad($v);
 }
예제 #12
0
파일: NBT.php 프로젝트: MunkySkunk/BukkitPE
 public function putDouble($v)
 {
     $this->buffer .= $this->endianness === self::BIG_ENDIAN ? Binary::writeDouble($v) : Binary::writeLDouble($v);
 }