Initial spawn on 0.16

This commit is contained in:
Dylan K. Taylor
2016-10-04 12:29:26 +01:00
parent bb9ab525b6
commit dd0c5efb56
10 changed files with 239 additions and 117 deletions

View File

@ -428,8 +428,45 @@ class Binary{
return self::readLong(strrev($str));
}
//TODO: varlong, length checks
public static function writeLLong($value){
return strrev(self::writeLong($value));
}
public static function readVarInt($stream){
$raw = self::readUnsignedVarInt($stream);
$temp = ((($raw << 31) >> 31) ^ $raw) >> 1;
return $temp ^ ($raw & (1 << 31));
}
public static function readUnsignedVarInt($stream){
$value = 0;
$i = 0;
do{
$value |= ((($b = $stream->getByte()) & 0x7f) << $i);
$i += 7;
}while($b & 0x80);
return $value;
}
public static function writeVarInt($v){
return self::writeUnsignedVarInt(($v << 1) ^ ($v >> 31));
}
public static function writeUnsignedVarInt($v){
$buf = "";
do{
$w = $v & 0x7f;
if(($v >> 7) !== 0){
$w = $v | 0x80;
}
$buf .= self::writeByte($w);
$v >>= 7;
}while($v);
return $buf;
}
}

View File

@ -233,14 +233,76 @@ class BinaryStream extends \stdClass{
}
public function getString(){
return $this->get($this->getShort());
return $this->get($this->getUnsignedVarInt());
}
public function putString($v){
$this->putShort(strlen($v));
$this->putUnsignedVarInt(strlen($v));
$this->put($v);
}
//TODO: varint64
/**
* Reads an unsigned varint32 from the stream.
*/
public function getUnsignedVarInt(){
return Binary::readUnsignedVarInt($this);
}
/**
* Writes an unsigned varint32 to the stream.
*/
public function putUnsignedVarInt($v){
$this->put(Binary::writeUnsignedVarInt($v));
}
/**
* Reads a signed varint32 from the stream.
*/
public function getVarInt(){
return Binary::readVarInt($this);
}
/**
* Writes a signed varint32 to the stream.
*/
public function putVarInt($v){
$this->put(Binary::writeVarInt($v));
}
public function getEntityId(){
return $this->getVarInt();
}
public function putEntityId($v){
$this->putVarInt($v);
}
public function getBlockCoords(&$x, &$y, &$z){
$x = $this->getVarInt();
$y = $this->getByte();
$z = $this->getVarInt();
}
public function putBlockCoords(int $x, int $y, int $z){
$this->putVarInt($x);
$this->putByte($y);
$this->putVarInt($z);
}
public function getVector3f(&$x, &$y, &$z){
$x = $this->getLFloat();
$y = $this->getLFloat();
$z = $this->getLFloat();
}
public function putVector3f(float $x, float $y, float $z){
$this->putLFloat($x);
$this->putLFloat($y);
$this->putLFloat($z);
}
public function feof(){
return !isset($this->buffer{$this->offset});
}