support php8019 and some cleanup (by @D-aHTuCT)

This commit is contained in:
kotyara 2022-07-26 16:06:34 +05:00
parent cf0f9df267
commit b60f0877e1
376 changed files with 6528 additions and 13472 deletions

View file

@ -1,47 +1,30 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class Cache{
public static $cached = array();
public static $cached = [];
public static function add($identifier, $blob, $minTTL = 30){
self::$cached[$identifier] = array($blob, microtime(true) + $minTTL, $minTTL);
self::$cached[$identifier] = [$blob, microtime(true) + $minTTL, $minTTL];
}
public static function get($identifier){
if(isset(self::$cached[$identifier])){
self::$cached[$identifier][1] = microtime(true) + self::$cached[$identifier][2];
return self::$cached[$identifier][0];
}
return false;
}
public static function exists($identifier){
return isset(self::$cached[$identifier]);
}
public static function remove($identifier){
unset(self::$cached[$identifier]);
}
public static function cleanup(){
$time = microtime(true);
foreach(self::$cached as $index => $data){
@ -50,5 +33,4 @@ class Cache{
}
}
}
}

View file

@ -1,24 +1,5 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
define("CONFIG_DETECT", -1); //Detect by file extension
define("CONFIG_PROPERTIES", 0); // .properties
define("CONFIG_CNF", CONFIG_PROPERTIES); // .cnf
@ -34,24 +15,8 @@ define("CONFIG_LIST", 5); // .txt, .list
* Config Class for simple config manipulation of multiple formats.
*/
class Config{
/**
* @var array
*/
private $config;
/**
* @var string
*/
private $file;
/**
* @var boolean
*/
private $correct;
/**
* @var integer
*/
private $type = CONFIG_DETECT;
public static $formats = array(
public static $formats = [
"properties" => CONFIG_PROPERTIES,
"cnf" => CONFIG_CNF,
"conf" => CONFIG_CNF,
@ -65,50 +30,54 @@ class Config{
"sl" => CONFIG_SERIALIZED,
"serialize" => CONFIG_SERIALIZED,
"txt" => CONFIG_LIST,
"list" => CONFIG_LIST,
);
"list" => CONFIG_LIST,
];
/**
* @var array
*/
private $config;
/**
* @var string
*/
private $file;
/**
* @var boolean
*/
private $correct;
/**
* @var integer
*/
private $type = CONFIG_DETECT;
/**
* @param string $file
* @param int $type
* @param array $default
* @param null|boolean $correct
*/
public function __construct($file, $type = CONFIG_DETECT, $default = array(), &$correct = null){
/**
* @param string $file
* @param int $type
* @param array $default
* @param null|boolean $correct
*/
public function __construct($file, $type = CONFIG_DETECT, $default = [], &$correct = null){
$this->load($file, $type, $default);
$correct = $this->check();
}
public function reload(){
unset($this->config);
unset($this->correct);
unset($this->type);
$this->load($this->file);
$correct = $this->check();
}
public function fixYAMLIndexes($str){
return preg_replace("#^([ ]*)([a-zA-Z_]{1}[^\:]*)\:#m", "$1\"$2\":", $str);
}
/**
* @param string $file
* @param int $type
* @param array $default
*
* @return boolean
*/
public function load($file, $type = CONFIG_DETECT, $default = array()){
/**
* @param string $file
* @param int $type
* @param array $default
*
* @return boolean
*/
public function load($file, $type = CONFIG_DETECT, $default = []){
$this->correct = true;
$this->type = (int) $type;
$this->file = $file;
if(!is_array($default)){
$default = array();
$default = [];
}
if(!file_exists($file)){
$this->config = $default;
$this->save();
}else{
}else{
if($this->type === CONFIG_DETECT){
$extension = explode(".", basename($this->file));
$extension = strtolower(trim(array_pop($extension)));
@ -156,17 +125,10 @@ class Config{
return true;
}
/**
* @return boolean
*/
public function check(){
return $this->correct === true;
}
/**
* @return boolean
*/
public function save(){
/**
* @return boolean
*/
public function save(){
if($this->correct === true){
switch($this->type){
case CONFIG_PROPERTIES:
@ -185,162 +147,34 @@ class Config{
case CONFIG_LIST:
$content = implode("\r\n", array_keys($this->config));
break;
}
@file_put_contents($this->file, $content, LOCK_EX);
return true;
}
@file_put_contents($this->file, $content, LOCK_EX);
return true;
}else{
return false;
}
}
/**
* @param $k
*
* @return boolean|mixed
*/
public function &__get($k){
return $this->get($k);
}
/**
* @param $k
* @param $v
*/
public function __set($k, $v){
$this->set($k, $v);
}
/**
* @param $k
*
* @return boolean
*/
public function __isset($k){
return $this->exists($k);
}
/**
* @param $k
*/
public function __unset($k){
$this->remove($k);
}
/**
* @param $k
*
* @return boolean|mixed
*/
public function &get($k){
if(isset($this->correct) and ($this->correct === false or !isset($this->config[$k]))){
$false = false;
return $false;
}
return $this->config[$k];
}
/**
* @param $k
* @param bool $v
*/
public function set($k, $v = true){
$this->config[$k] = $v;
}
/**
* @param array $v
*/
public function setAll($v){
$this->config = $v;
}
/**
* @param $k
* @param bool $lowercase If set, searches Config in single-case / lowercase.
*
* @return boolean
*/
public function exists($k, $lowercase = false){
if($lowercase === true){
$k = strtolower($k);//Convert requested key to lower
$array = array_change_key_case($this->config, CASE_LOWER);//Change all keys in array to lower
return isset($array[$k]);//Find $k in modified array
}else{
return isset($this->config[$k]);
}
}
/**
* @param $k
*/
public function remove($k){
unset($this->config[$k]);
}
/**
* @param bool $keys
*
* @return array
*/
public function getAll($keys = false){
return ($keys === true ? array_keys($this->config):$this->config);
}
/**
* @param $default
* @param $data
*
* @return integer
*/
private function fillDefaults($default, &$data){
$changed = 0;
foreach($default as $k => $v){
if(is_array($v)){
if(!isset($data[$k]) or !is_array($data[$k])){
$data[$k] = array();
}
$changed += $this->fillDefaults($v, $data[$k]);
}elseif(!isset($data[$k])){
$data[$k] = $v;
++$changed;
}
}
return $changed;
}
/**
* @param $content
*/
private function parseList($content){
foreach(explode("\n", trim(str_replace("\r\n", "\n", $content))) as $v){
$v = trim($v);
if($v == ""){
continue;
}
$this->config[$v] = true;
}
}
/**
* @return string
*/
private function writeProperties(){
$content = "#Properties Config file\r\n#".date("D M j H:i:s T Y")."\r\n";
/**
* @return string
*/
private function writeProperties(){
$content = "#Properties Config file\r\n#" . date("D M j H:i:s T Y") . "\r\n";
foreach($this->config as $k => $v){
if(is_bool($v) === true){
$v = $v === true ? "on":"off";
$v = $v === true ? "on" : "off";
}elseif(is_array($v)){
$v = implode(";", $v);
$v = implode(";", $v);
}
$content .= $k."=".$v."\r\n";
$content .= $k . "=" . $v . "\r\n";
}
return $content;
}
/**
* @param $content
*/
private function parseProperties($content){
/**
* @param $content
*/
private function parseProperties($content){
if(preg_match_all('/([a-zA-Z0-9\-_\.]*)=([^\r\n]*)/u', $content, $matches) > 0){ //false or 0 matches
foreach($matches[1] as $i => $k){
$v = trim($matches[2][$i]);
@ -357,11 +191,158 @@ class Config{
break;
}
if(isset($this->config[$k])){
console("[NOTICE] [Config] Repeated property ".$k." on file ".$this->file, true, true, 2);
console("[NOTICE] [Config] Repeated property " . $k . " on file " . $this->file, true, true, 2);
}
$this->config[$k] = $v;
}
}
}
public function fixYAMLIndexes($str){
return preg_replace("#^([ ]*)([a-zA-Z_]{1}[^\:]*)\:#m", "$1\"$2\":", $str);
}
/**
* @param $content
*/
private function parseList($content){
foreach(explode("\n", trim(str_replace("\r\n", "\n", $content))) as $v){
$v = trim($v);
if($v == ""){
continue;
}
$this->config[$v] = true;
}
}
/**
* @param $default
* @param $data
*
* @return integer
*/
private function fillDefaults($default, &$data){
$changed = 0;
foreach($default as $k => $v){
if(is_array($v)){
if(!isset($data[$k]) or !is_array($data[$k])){
$data[$k] = [];
}
$changed += $this->fillDefaults($v, $data[$k]);
}elseif(!isset($data[$k])){
$data[$k] = $v;
++$changed;
}
}
return $changed;
}
/**
* @return boolean
*/
public function check(){
return $this->correct === true;
}
public function reload(){
unset($this->config);
unset($this->correct);
unset($this->type);
$this->load($this->file);
$correct = $this->check();
}
/**
* @param $k
*
* @return boolean|mixed
*/
public function &__get($k){
return $this->get($k);
}
/**
* @param $k
* @param $v
*/
public function __set($k, $v){
$this->set($k, $v);
}
/**
* @param $k
*
* @return boolean|mixed
*/
public function &get($k){
if(isset($this->correct) and ($this->correct === false or !isset($this->config[$k]))){
$false = false;
return $false;
}
return $this->config[$k];
}
/**
* @param $k
* @param bool $v
*/
public function set($k, $v = true){
$this->config[$k] = $v;
}
/**
* @param $k
*
* @return boolean
*/
public function __isset($k){
return $this->exists($k);
}
/**
* @param $k
* @param bool $lowercase If set, searches Config in single-case / lowercase.
*
* @return boolean
*/
public function exists($k, $lowercase = false){
if($lowercase === true){
$k = strtolower($k);//Convert requested key to lower
$array = array_change_key_case($this->config, CASE_LOWER);//Change all keys in array to lower
return isset($array[$k]);//Find $k in modified array
}else{
return isset($this->config[$k]);
}
}
/**
* @param $k
*/
public function __unset($k){
$this->remove($k);
}
/**
* @param $k
*/
public function remove($k){
unset($this->config[$k]);
}
/**
* @param array $v
*/
public function setAll($v){
$this->config = $v;
}
/**
* @param bool $keys
*
* @return array
*/
public function getAll($keys = false){
return ($keys === true ? array_keys($this->config) : $this->config);
}
}

View file

@ -1,26 +1,9 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class Container{
private $payload = "", $whitelist = false, $blacklist = false;
public function __construct($payload = "", $whitelist = false, $blacklist = false){
$this->payload = $payload;
if(is_array($whitelist)){
@ -30,11 +13,11 @@ class Container{
$this->blacklist = $blacklist;
}
}
public function get(){
return $this->payload;
}
public function check($target){
$w = true;
$b = false;
@ -59,8 +42,8 @@ class Container{
}
return true;
}
public function __toString(){
return $this->payload;
}

View file

@ -1,26 +1,8 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class Java_String{
private $value = "", $count = 0, $hash = 0;
public function __construct($string = false){
@ -42,15 +24,6 @@ class Java_String{
return $this->count === 0;
}
public function charAt($index){
$index = (int) $index;
if($index < 0 or $index >= $this->count){
trigger_error("Undefined offset $index", E_USER_WARNING);
return false;
}
return $this->value[$index];
}
public function hashCode(){
$h = $this->hash;
if($h === 0 and $this->count > 0){
@ -63,4 +36,13 @@ class Java_String{
}
return $h;
}
public function charAt($index){
$index = (int) $index;
if($index < 0 or $index >= $this->count){
trigger_error("Undefined offset $index", E_USER_WARNING);
return false;
}
return $this->value[$index];
}
}

View file

@ -1,6 +1,7 @@
<?php
class LightUtils {
class LightUtils{
/*
args: $source: <Block> instanceof LightingBlock, $block: Block
*/
@ -8,16 +9,13 @@ class LightUtils {
if($source === null || $block === null){
return 0;
}
$distance = sqrt(LightUtils::sqr($source->x - $block->x)+LightUtils::sqr($source->y - $block->y)+LightUtils::sqr($source->z - $block->z)); /*Distance between 2 3d objects*/
$distance = sqrt(LightUtils::sqr($source->x - $block->x) + LightUtils::sqr($source->y - $block->y) + LightUtils::sqr($source->z - $block->z)); /*Distance between 2 3d objects*/
$result = floor($source->getMaxLightValue() - $distance); //i dont need something like 0.2192032434454...
return $result <= 0 ? 0 : $result; //if result is smaller or equals 0 then return 0 else return result
}
/* Used only internally for calculating 2^(2) */
private static function sqr($val){
return pow($val,2);
return pow($val, 2);
}
}
}

View file

@ -1,4 +1,5 @@
<?php
/*
Mersenne Twister, 1.1.0
-----------------------
@ -20,351 +21,365 @@
http://kingfisher.nfshost.com/sw/twister/
*/
const N = 624;
const M = 397;
const MATRIX_A = 0x9908b0df;
const UPPER_MASK = 0x80000000;
const LOWER_MASK = 0x7fffffff;
foreach (array(10, 11, 12, 14, 20, 21, 22, 26, 27, 31) as $n) {
$val = ~((~0) << $n);
foreach([10, 11, 12, 14, 20, 21, 22, 26, 27, 31] as $n){
$val = ~((~0) << $n);
define("MASK$n", $val);
}
foreach (array(16, 31, 32) as $n) {
$val = pow(2, $n);
define("TWO_TO_THE_$n",$val);
}
$val = MASK31 | (MASK31 << 1);
define("MASK32", $val);
class twister {
const N = N;
# the class constant is not used anywhere in this namespace,
# but it makes the API cleaner.
function __construct() {
$this->bits32 = PHP_INT_MAX == 2147483647;
if(func_num_args() == 1) {
$this->init_with_integer(func_get_arg(0));
}
}
function init_with_integer($integer_seed) {
$integer_seed = force_32_bit_int($integer_seed);
$mt = &$this->mt;
$mti = &$this->mti;
$mt = array_fill(0, N, 0);
$mt[0] = $integer_seed;
for($mti = 1; $mti < N; $mti++) {
$mt[$mti] = add_2(mul(1812433253,
($mt[$mti - 1] ^ (($mt[$mti - 1] >> 30) & 3))), $mti);
/*
mt[mti] =
(1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
*/
}
}
function init_with_array(array $integer_array) {
$integer_array =
array_map("force_32_bit_int", $integer_array);
$mt = &$this->mt;
$mti = &$this->mti;
$key_length = count($integer_array);
$this->init_with_integer(19650218);
$i=1; $j=0;
$k = (N>$key_length ? N : $key_length);
for (; $k; $k--) {
$mt[$i] = add_3($mt[$i] ^
mul_by_1664525($mt[$i-1] ^ (($mt[$i-1] >> 30) & 3)),
$integer_array[$j], $j);
/*
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
+ init_key[j] + j;
*/
$i++; $j++;
if ($i>=N) { $mt[0] = $mt[N-1]; $i=1; }
if ($j>=$key_length){ $j=0; }
}
for ($k=N-1; $k; $k--) {
$mt[$i] = sub($mt[$i] ^
mul($mt[$i-1] ^ (($mt[$i-1] >> 30) & 3), 1566083941), $i);
/*
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
- i;
*/
$i++;
if ($i>=N) { $mt[0] = $mt[N-1]; $i=1; }
}
$mt[0] = (1 << 31); /* MSB is 1; assuring non-zero initial array */
}
function init_with_string($string) {
$remainder = strlen($string) % 4;
if($remainder > 0){
$string .= str_repeat("\0", 4 - $remainder);
}
$integer_array = array_values(unpack("N*", $string));
$this->init_with_array($integer_array);
}
function init_with_files(array $filenames, $max_ints = -1) {
$limit_applies = $max_ints !== -1;
if($limit_applies){
$limit = $max_ints * 4; # 32 bits is 4 characters
}
$data = "";
foreach ($filenames as $filename) {
$contents =
file_get_contents($filename, FALSE, NULL, 0,
$limit_applies? $limit - strlen($data): -1);
if($contents === FALSE) {
throw new Exception("problem reading from $filename");
} else {
$data .= $contents;
if($limit_applies && strlen($data) == $limit) {
break;
}
}
}
$this->init_with_string($data);
}
function init_with_file($filename, $max_ints = -1) {
$this->init_with_files(array($filename), $max_ints);
}
function int32() {
static $mag01 = array(0, MATRIX_A);
$mt = &$this->mt;
$mti = &$this->mti;
if ($mti >= N) { /* generate N words all at once */
for ($kk=0;$kk<N-M;$kk++) {
$y = ($mt[$kk]&UPPER_MASK)|($mt[$kk+1]&LOWER_MASK);
$mt[$kk] = $mt[$kk+M] ^ (($y >> 1) & MASK31) ^ $mag01[$y & 1];
}
for (;$kk<N-1;$kk++) {
$y = ($mt[$kk]&UPPER_MASK)|($mt[$kk+1]&LOWER_MASK);
$mt[$kk] =
$mt[$kk+(M-N)] ^ (($y >> 1) & MASK31) ^ $mag01[$y & 1];
}
$y = ($mt[N-1]&UPPER_MASK)|($mt[0]&LOWER_MASK);
$mt[N-1] = $mt[M-1] ^ (($y >> 1) & MASK31) ^ $mag01[$y & 1];
$mti = 0;
}
$y = $mt[$mti++];
/* Tempering */
$y ^= ($y >> 11) & MASK21;
$y ^= ($y << 7) & ((0x9d2c << 16) | 0x5680);
$y ^= ($y << 15) & (0xefc6 << 16);
$y ^= ($y >> 18) & MASK14;
return $y;
}
function int31() {
return $this->int32() & MASK31;
}
/* generates a random number on [0,1]-real-interval */
function real_closed() {
return signed2unsigned($this->int32()) * (1.0 / 4294967295.0);
}
/* generates a random number on [0,1)-real-interval */
function real_halfopen() {
return signed2unsigned($this->int32()) * (1.0 / 4294967296.0);
}
/* generates a random number on (0,1)-real-interval */
function real_open() {
return (signed2unsigned($this->int32()) + .5) * (1.0 / 4294967296.0);
}
/* generates a random number on [0,1) with 53-bit resolution */
function real_halfopen2() {
return ((($this->int32() & MASK27) * 67108864.0) +
($this->int32() & MASK26)) *
(1.0 / 9007199254740992.0);
}
function rangeint($lower_bound, $upper_bound) {
$lower_bound = intval($lower_bound);
$upper_bound = intval($upper_bound);
if($this->bits32) {
$pow_2_32 = pow(2, 32);
$size_of_range = $upper_bound - $lower_bound + 1;
$remainder = fmod($pow_2_32, $size_of_range);
if($remainder == 0) {
return $lower_bound +
($this->int32() & unsigned2signed($size_of_range - 1));
} else {
$start_of_partial_range = $pow_2_32 - $remainder;
$start_as_int = unsigned2signed($start_of_partial_range);
do {
$rand = $this->int32();
} while($rand >= $start_as_int && $rand < 0);
$result = $lower_bound +
fmod(signed2unsigned($rand), $size_of_range);
return intval($result);
}
} else {
if($lower_bound == -PHP_INT_MAX - 1 && $upper_bound == PHP_INT_MAX) {
return ($this->int32() << 32) & $this->int32();
} else {
$pow_2_32 = 1 << 32;
$size_of_range = $upper_bound - $lower_bound + 1;
if($size_of_range > $pow_2_32) {
$size_of_range >>= 32;
$shift = 32;
$low_bits = $this->int32();
} else {
$shift = 0;
$low_bits = 0;
}
$remainder = $pow_2_32 % $size_of_range;
if($remainder == 0) {
$high_bits = $this->int32() & ($size_of_range - 1);
} else {
$start_of_partial_range = $pow_2_32 - $remainder;
do {
$rand = $this->int32();
} while($rand >= $start_of_partial_range);
$high_bits = $rand % $size_of_range;
}
return $lower_bound + (($high_bits << $shift) | $low_bits);
}
}
}
/*
in each of the next 3 functions, we loop until we have a number that
meets the function's post-condition. this may be more work than
is really necessary, but i am concerned about rounding errors.
why no rangereal_closed? because, due to the aforementioned
rounding errors, i am unable to guarantee that $upper_bound
would be a possible return value of such a function.
*/
function rangereal_open($lower_bound, $upper_bound) {
do {
$rand = $lower_bound +
$this->real_open() * ($upper_bound - $lower_bound);
} while($rand <= $lower_bound || $rand >= $upper_bound);
return $rand;
}
function rangereal_halfopen($lower_bound, $upper_bound) {
do {
$rand = $lower_bound +
$this->real_halfopen() * ($upper_bound - $lower_bound);
} while($rand >= $upper_bound);
/*
$rand cannot go any lower than $lower_bound, because
$this->real_halfopen() cannot go any lower than 0
*/
return $rand;
}
function rangereal_halfopen2($lower_bound, $upper_bound) {
do {
$rand = $lower_bound +
$this->real_halfopen2() * ($upper_bound - $lower_bound);
} while($rand >= $upper_bound);
return $rand;
}
}
function signed2unsigned($signed_integer) {
## assert(is_integer($signed_integer));
## assert(($signed_integer & ~MASK32) === 0);
return $signed_integer >= 0? $signed_integer:
TWO_TO_THE_32 + $signed_integer;
foreach([16, 31, 32] as $n){
$val = pow(2, $n);
define("TWO_TO_THE_$n", $val);
}
function unsigned2signed($unsigned_integer) {
## assert($unsigned_integer >= 0);
## assert($unsigned_integer < pow(2, 32));
## assert(floor($unsigned_integer) === floatval($unsigned_integer));
$val = MASK31 | (MASK31 << 1);
define("MASK32", $val);
return intval($unsigned_integer < TWO_TO_THE_31? $unsigned_integer:
$unsigned_integer - TWO_TO_THE_32);
}
function force_32_bit_int($x) {
/*
it would be un-PHP-like to require is_integer($x),
so we have to handle cases like this:
class twister{
$x === pow(2, 31)
$x === strval(pow(2, 31))
const N = N;
# the class constant is not used anywhere in this namespace,
# but it makes the API cleaner.
we are also opting to do something sensible (rather than dying)
if the seed is outside the range of a 32-bit unsigned integer.
*/
function __construct(){
$this->bits32 = PHP_INT_MAX == 2147483647;
if(is_integer($x)) {
/*
we mask in case we are on a 64-bit machine and at least one
bit is set between position 32 and position 63.
*/
return $x & MASK32;
} else {
$x = floatval($x);
$x = $x < 0? ceil($x): floor($x);
$x = fmod($x, TWO_TO_THE_32);
if($x < 0){
$x += TWO_TO_THE_32;
if(func_num_args() == 1){
$this->init_with_integer(func_get_arg(0));
}
}
return unsigned2signed($x);
}
function init_with_integer($integer_seed){
$integer_seed = force_32_bit_int($integer_seed);
$mt = &$this->mt;
$mti = &$this->mti;
$mt = array_fill(0, N, 0);
$mt[0] = $integer_seed;
for($mti = 1; $mti < N; $mti++){
$mt[$mti] = add_2(mul(1812433253,
($mt[$mti - 1] ^ (($mt[$mti - 1] >> 30) & 3))), $mti);
/*
mt[mti] =
(1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
*/
}
}
function init_with_file($filename, $max_ints = -1){
$this->init_with_files([$filename], $max_ints);
}
function init_with_files(array $filenames, $max_ints = -1){
$limit_applies = $max_ints !== -1;
if($limit_applies){
$limit = $max_ints * 4; # 32 bits is 4 characters
}
$data = "";
foreach($filenames as $filename){
$contents =
file_get_contents($filename, false, null, 0,
$limit_applies ? $limit - strlen($data) : -1);
if($contents === false){
throw new Exception("problem reading from $filename");
}else{
$data .= $contents;
if($limit_applies && strlen($data) == $limit){
break;
}
}
}
$this->init_with_string($data);
}
function init_with_string($string){
$remainder = strlen($string) % 4;
if($remainder > 0){
$string .= str_repeat("\0", 4 - $remainder);
}
$integer_array = array_values(unpack("N*", $string));
$this->init_with_array($integer_array);
}
function init_with_array(array $integer_array){
$integer_array =
array_map("force_32_bit_int", $integer_array);
$mt = &$this->mt;
$mti = &$this->mti;
$key_length = count($integer_array);
$this->init_with_integer(19650218);
$i = 1;
$j = 0;
$k = (N > $key_length ? N : $key_length);
for(; $k; $k--){
$mt[$i] = add_3($mt[$i] ^
mul_by_1664525($mt[$i - 1] ^ (($mt[$i - 1] >> 30) & 3)),
$integer_array[$j], $j);
/*
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
+ init_key[j] + j;
*/
$i++;
$j++;
if($i >= N){
$mt[0] = $mt[N - 1];
$i = 1;
}
if($j >= $key_length){
$j = 0;
}
}
for($k = N - 1; $k; $k--){
$mt[$i] = sub($mt[$i] ^
mul($mt[$i - 1] ^ (($mt[$i - 1] >> 30) & 3), 1566083941), $i);
/*
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
- i;
*/
$i++;
if($i >= N){
$mt[0] = $mt[N - 1];
$i = 1;
}
}
$mt[0] = (1 << 31); /* MSB is 1; assuring non-zero initial array */
}
function int31(){
return $this->int32() & MASK31;
}
function int32(){
static $mag01 = [0, MATRIX_A];
$mt = &$this->mt;
$mti = &$this->mti;
if($mti >= N){ /* generate N words all at once */
for($kk = 0; $kk < N - M; $kk++){
$y = ($mt[$kk] & UPPER_MASK) | ($mt[$kk + 1] & LOWER_MASK);
$mt[$kk] = $mt[$kk + M] ^ (($y >> 1) & MASK31) ^ $mag01[$y & 1];
}
for(; $kk < N - 1; $kk++){
$y = ($mt[$kk] & UPPER_MASK) | ($mt[$kk + 1] & LOWER_MASK);
$mt[$kk] =
$mt[$kk + (M - N)] ^ (($y >> 1) & MASK31) ^ $mag01[$y & 1];
}
$y = ($mt[N - 1] & UPPER_MASK) | ($mt[0] & LOWER_MASK);
$mt[N - 1] = $mt[M - 1] ^ (($y >> 1) & MASK31) ^ $mag01[$y & 1];
$mti = 0;
}
$y = $mt[$mti++];
/* Tempering */
$y ^= ($y >> 11) & MASK21;
$y ^= ($y << 7) & ((0x9d2c << 16) | 0x5680);
$y ^= ($y << 15) & (0xefc6 << 16);
$y ^= ($y >> 18) & MASK14;
return $y;
}
/* generates a random number on [0,1]-real-interval */
function real_closed(){
return signed2unsigned($this->int32()) * (1.0 / 4294967295.0);
}
/* generates a random number on [0,1)-real-interval */
function rangeint($lower_bound, $upper_bound){
$lower_bound = intval($lower_bound);
$upper_bound = intval($upper_bound);
if($this->bits32){
$pow_2_32 = pow(2, 32);
$size_of_range = $upper_bound - $lower_bound + 1;
$remainder = fmod($pow_2_32, $size_of_range);
if($remainder == 0){
return $lower_bound +
($this->int32() & unsigned2signed($size_of_range - 1));
}else{
$start_of_partial_range = $pow_2_32 - $remainder;
$start_as_int = unsigned2signed($start_of_partial_range);
do{
$rand = $this->int32();
}while($rand >= $start_as_int && $rand < 0);
$result = $lower_bound +
fmod(signed2unsigned($rand), $size_of_range);
return intval($result);
}
}else{
if($lower_bound == -PHP_INT_MAX - 1 && $upper_bound == PHP_INT_MAX){
return ($this->int32() << 32) & $this->int32();
}else{
$pow_2_32 = 1 << 32;
$size_of_range = $upper_bound - $lower_bound + 1;
if($size_of_range > $pow_2_32){
$size_of_range >>= 32;
$shift = 32;
$low_bits = $this->int32();
}else{
$shift = 0;
$low_bits = 0;
}
$remainder = $pow_2_32 % $size_of_range;
if($remainder == 0){
$high_bits = $this->int32() & ($size_of_range - 1);
}else{
$start_of_partial_range = $pow_2_32 - $remainder;
do{
$rand = $this->int32();
}while($rand >= $start_of_partial_range);
$high_bits = $rand % $size_of_range;
}
return $lower_bound + (($high_bits << $shift) | $low_bits);
}
}
}
/* generates a random number on (0,1)-real-interval */
function rangereal_open($lower_bound, $upper_bound){
do{
$rand = $lower_bound +
$this->real_open() * ($upper_bound - $lower_bound);
}while($rand <= $lower_bound || $rand >= $upper_bound);
return $rand;
}
/* generates a random number on [0,1) with 53-bit resolution */
function real_open(){
return (signed2unsigned($this->int32()) + .5) * (1.0 / 4294967296.0);
}
function rangereal_halfopen($lower_bound, $upper_bound){
do{
$rand = $lower_bound +
$this->real_halfopen() * ($upper_bound - $lower_bound);
}while($rand >= $upper_bound);
/*
$rand cannot go any lower than $lower_bound, because
$this->real_halfopen() cannot go any lower than 0
*/
return $rand;
}
/*
in each of the next 3 functions, we loop until we have a number that
meets the function's post-condition. this may be more work than
is really necessary, but i am concerned about rounding errors.
why no rangereal_closed? because, due to the aforementioned
rounding errors, i am unable to guarantee that $upper_bound
would be a possible return value of such a function.
*/
function real_halfopen(){
return signed2unsigned($this->int32()) * (1.0 / 4294967296.0);
}
function rangereal_halfopen2($lower_bound, $upper_bound){
do{
$rand = $lower_bound +
$this->real_halfopen2() * ($upper_bound - $lower_bound);
}while($rand >= $upper_bound);
return $rand;
}
function real_halfopen2(){
return ((($this->int32() & MASK27) * 67108864.0) +
($this->int32() & MASK26)) *
(1.0 / 9007199254740992.0);
}
}
function signed2unsigned($signed_integer){
## assert(is_integer($signed_integer));
## assert(($signed_integer & ~MASK32) === 0);
return $signed_integer >= 0 ? $signed_integer :
TWO_TO_THE_32 + $signed_integer;
}
function unsigned2signed($unsigned_integer){
## assert($unsigned_integer >= 0);
## assert($unsigned_integer < pow(2, 32));
## assert(floor($unsigned_integer) === floatval($unsigned_integer));
return intval($unsigned_integer < TWO_TO_THE_31 ? $unsigned_integer :
$unsigned_integer - TWO_TO_THE_32);
}
function force_32_bit_int($x){
/*
it would be un-PHP-like to require is_integer($x),
so we have to handle cases like this:
$x === pow(2, 31)
$x === strval(pow(2, 31))
we are also opting to do something sensible (rather than dying)
if the seed is outside the range of a 32-bit unsigned integer.
*/
if(is_integer($x)){
/*
we mask in case we are on a 64-bit machine and at least one
bit is set between position 32 and position 63.
*/
return $x & MASK32;
}else{
$x = floatval($x);
$x = $x < 0 ? ceil($x) : floor($x);
$x = fmod($x, TWO_TO_THE_32);
if($x < 0){
$x += TWO_TO_THE_32;
}
return unsigned2signed($x);
}
}
/*
@ -378,13 +393,13 @@ function force_32_bit_int($x) {
a slight complication is that the sum of the low half-integers
may not fit into 16 bits; any "overspill" is added to the sum
of the high half-integers.
*/
function add_2($n1, $n2) {
$x = ($n1 & 0xffff) + ($n2 & 0xffff);
*/
function add_2($n1, $n2){
$x = ($n1 & 0xffff) + ($n2 & 0xffff);
return (((($n1 >> 16) & 0xffff) +
(($n2 >> 16) & 0xffff) +
($x >> 16)) << 16) | ($x & 0xffff);
return (((($n1 >> 16) & 0xffff) +
(($n2 >> 16) & 0xffff) +
($x >> 16)) << 16) | ($x & 0xffff);
}
# takes 2 integers, treats them as unsigned 32-bit integers,
@ -392,13 +407,13 @@ function add_2($n1, $n2) {
#
# for how it works, see the comment for add_2.
#
function add_3($n1, $n2, $n3) {
$x = ($n1 & 0xffff) + ($n2 & 0xffff) + ($n3 & 0xffff);
function add_3($n1, $n2, $n3){
$x = ($n1 & 0xffff) + ($n2 & 0xffff) + ($n3 & 0xffff);
return (((($n1 >> 16) & 0xffff) +
(($n2 >> 16) & 0xffff) +
(($n3 >> 16) & 0xffff) +
($x >> 16)) << 16) | ($x & 0xffff);
return (((($n1 >> 16) & 0xffff) +
(($n2 >> 16) & 0xffff) +
(($n3 >> 16) & 0xffff) +
($x >> 16)) << 16) | ($x & 0xffff);
}
# takes 2 integers, treats them as unsigned 32-bit integers,
@ -407,72 +422,72 @@ function add_3($n1, $n2, $n3) {
# the explanation of why this works is too long to be
# included here, so it has been moved into the file why-sub-works.txt.
#
function sub($a, $b) {
return (($a & MASK31) - ($b & MASK31)) ^
(($a ^ $b) & 0x80000000);
function sub($a, $b){
return (($a & MASK31) - ($b & MASK31)) ^
(($a ^ $b) & 0x80000000);
}
function mul($a, $b) {
/*
a and b, considered as unsigned integers, can be expressed as follows:
function mul($a, $b){
/*
a and b, considered as unsigned integers, can be expressed as follows:
a = 2**16 * a1 + a2,
a = 2**16 * a1 + a2,
b = 2**16 * b1 + b2,
b = 2**16 * b1 + b2,
where
where
0 <= a2 < 2**16,
0 <= b2 < 2**16.
0 <= a2 < 2**16,
0 <= b2 < 2**16.
given those 2 equations, what this function essentially does is to
use the following identity:
given those 2 equations, what this function essentially does is to
use the following identity:
a * b = 2**32 * a1 * b1 + 2**16 * a1 * b2 + 2**16 * b1 * a2 + a2 * b2
a * b = 2**32 * a1 * b1 + 2**16 * a1 * b2 + 2**16 * b1 * a2 + a2 * b2
note that the first term, i.e. 2**32 * a1 * b1, is unnecessary here,
so we don't compute it.
note that the first term, i.e. 2**32 * a1 * b1, is unnecessary here,
so we don't compute it.
we could make the following code clearer by using intermediate
variables, but that would probably hurt performance.
*/
we could make the following code clearer by using intermediate
variables, but that would probably hurt performance.
*/
return unsigned2signed(
fmod(
TWO_TO_THE_16 *
/*
the next line of code calculates a1 * b2,
the line after that calculates b1 * a2,
and the line after that calculates a2 * b2.
*/
((($a >> 16) & 0xffff) * ($b & 0xffff) +
(($b >> 16) & 0xffff) * ($a & 0xffff)) +
($a & 0xffff) * ($b & 0xffff),
return unsigned2signed(
fmod(
TWO_TO_THE_16 *
/*
the next line of code calculates a1 * b2,
the line after that calculates b1 * a2,
and the line after that calculates a2 * b2.
*/
((($a >> 16) & 0xffff) * ($b & 0xffff) +
(($b >> 16) & 0xffff) * ($a & 0xffff)) +
($a & 0xffff) * ($b & 0xffff),
TWO_TO_THE_32));
TWO_TO_THE_32));
}
/*
mul_by_1664525($x) should be more efficient than mul(1664525, $x).
*/
function mul_by_1664525($n) {
return unsigned2signed(fmod(1664525 * ($n >= 0?
$n: (TWO_TO_THE_32 + $n)), TWO_TO_THE_32));
function mul_by_1664525($n){
return unsigned2signed(fmod(1664525 * ($n >= 0 ?
$n : (TWO_TO_THE_32 + $n)), TWO_TO_THE_32));
}
function do_bc_op($bc_op, array $numbers) {
$modulus = pow(2, 32);
function do_bc_op($bc_op, array $numbers){
$modulus = pow(2, 32);
$result =
call_user_func_array($bc_op,
array_map("signed2unsigned", $numbers));
$result =
call_user_func_array($bc_op,
array_map("signed2unsigned", $numbers));
$result = bcmod($result, $modulus);
$result = bcmod($result, $modulus);
return unsigned2signed(bccomp($result, 0) < 0?
bcadd($result, $modulus): $result);
return unsigned2signed(bccomp($result, 0) < 0 ?
bcadd($result, $modulus) : $result);
}
function get_random_32bit_int() {
return rand(0, 0xffff) | (rand(0, 0xffff) << 16);
function get_random_32bit_int(){
return rand(0, 0xffff) | (rand(0, 0xffff) << 16);
}

View file

@ -1,28 +1,6 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class NBT{
public $tree = array();
public $binary = b"";
private $offset = 0;
const TAG_END = 0;
const TAG_BYTE = 1;
@ -35,142 +13,26 @@ class NBT{
const TAG_STRING = 8;
const TAG_LIST = 9;
const TAG_COMPOUND = 10;
public function read($n){
if($n <= 0){
return "";
}
$this->offset += $n;
return substr($this->binary, $this->offset - $n, $n);
}
private function feof(){
return !isset($this->binary[$this->offset]);
}
public $tree = [];
public $binary = b"";
private $offset = 0;
public function write($bin){
$this->binary .= $bin;
}
public function load($str){
$this->offset = 0;
$this->binary = (string) $str;
$this->parseTree($this->tree);
}
public function readTAG_BYTE(){
return Utils::readByte($this->read(1));
}
public function readTAG_SHORT(){
return Utils::readLShort($this->read(2));
}
public function readTAG_INT(){
return Utils::readLInt($this->read(4));
}
public function readTAG_LONG(){
return Utils::readLLong($this->read(8));
}
public function readTAG_FLOAT(){
return Utils::readLFloat($this->read(4));
}
public function readTAG_DOUBLE(){
return Utils::readLDouble($this->read(8));
}
public function readTAG_BYTE_ARRAY(){
return $this->read($this->readTAG_INT());
}
public function readTAG_STRING(){
return $this->read(Utils::readLShort($this->read(2), false));
}
public function writeTAG_BYTE($v){
$this->binary .= chr($v);
}
public function writeTAG_SHORT($v){
$this->binary .= Utils::writeLShort($v);
}
public function writeTAG_INT($v){
$this->binary .= Utils::writeLInt($v);
}
public function writeTAG_LONG($v){
$this->binary .= Utils::writeLLong($v);
}
public function writeTAG_FLOAT($v){
$this->binary .= Utils::writeLFloar($v);
}
public function writeTAG_DOUBLE($v){
$this->binary .= Utils::writeLDouble($v);
}
public function writeTAG_BYTE_ARRAY($v){
$this->binary .= $this->writeTAG_INT(strlen($v)).$v;
}
public function writeTAG_STRING($v){
$this->binary .= $this->writeTAG_SHORT(strlen($v)).$v;
}
private function parseList(&$node, $tag, $cnt){
for($i = 0; $i < $cnt and !$this->feof(); ++$i){
switch($tag){
case self::TAG_BYTE:
$value = $this->readTAG_BYTE();
break;
case self::TAG_SHORT:
$value = $this->readTAG_SHORT();
break;
case self::TAG_INT:
$value = $this->readTAG_INT();
break;
case self::TAG_LONG:
$value = $this->readTAG_LONG();
break;
case self::TAG_FLOAT:
$value = $this->readTAG_FLOAT();
break;
case self::TAG_DOUBLE:
$value = $this->readTAG_DOUBLE();
break;
case self::TAG_BYTE_ARRAY:
$value = $this->readTAG_BYTE_ARRAY();
break;
case self::TAG_STRING:
$value = $this->readTAG_STRING();
break;
case self::TAG_LIST:
$value = array();
$this->parseList($value, ord($this->read(1)), Utils::readLInt($this->read(4)));
break;
case self::TAG_COMPOUND:
$value = array();
$this->parseTree($value);
break;
default:
echo bin2hex(substr($this->binary, $this->offset - 1)).PHP_EOL.PHP_EOL;
die("Invalid NBT Tag $tag");
break;
}
$node[] = $value;
}
}
private function parseTree(&$node){
while(($tag = ord($this->read(1))) !== self::TAG_END and !$this->feof()){
$name = $this->readTAG_STRING();
switch($tag){
case self::TAG_BYTE:
case self::TAG_BYTE:
$value = $this->readTAG_BYTE();
break;
case self::TAG_SHORT:
@ -195,19 +57,139 @@ class NBT{
$value = $this->readTAG_STRING();
break;
case self::TAG_LIST:
$value = array();
$value = [];
$this->parseList($value, ord($this->read(1)), Utils::readLInt($this->read(4)));
break;
case self::TAG_COMPOUND:
$value = array();
$value = [];
$this->parseTree($value);
break;
default:
echo bin2hex(substr($this->binary, $this->offset - 1)).PHP_EOL.PHP_EOL;
echo bin2hex(substr($this->binary, $this->offset - 1)) . PHP_EOL . PHP_EOL;
die("Invalid NBT Tag $tag");
break;
}
$node[$name] = $value;
}
}
public function read($n){
if($n <= 0){
return "";
}
$this->offset += $n;
return substr($this->binary, $this->offset - $n, $n);
}
private function feof(){
return !isset($this->binary[$this->offset]);
}
public function readTAG_STRING(){
return $this->read(Utils::readLShort($this->read(2), false));
}
public function readTAG_BYTE(){
return Utils::readByte($this->read(1));
}
public function readTAG_SHORT(){
return Utils::readLShort($this->read(2));
}
public function readTAG_INT(){
return Utils::readLInt($this->read(4));
}
public function readTAG_LONG(){
return Utils::readLLong($this->read(8));
}
public function readTAG_FLOAT(){
return Utils::readLFloat($this->read(4));
}
public function readTAG_DOUBLE(){
return Utils::readLDouble($this->read(8));
}
public function readTAG_BYTE_ARRAY(){
return $this->read($this->readTAG_INT());
}
private function parseList(&$node, $tag, $cnt){
for($i = 0; $i < $cnt and !$this->feof(); ++$i){
switch($tag){
case self::TAG_BYTE:
$value = $this->readTAG_BYTE();
break;
case self::TAG_SHORT:
$value = $this->readTAG_SHORT();
break;
case self::TAG_INT:
$value = $this->readTAG_INT();
break;
case self::TAG_LONG:
$value = $this->readTAG_LONG();
break;
case self::TAG_FLOAT:
$value = $this->readTAG_FLOAT();
break;
case self::TAG_DOUBLE:
$value = $this->readTAG_DOUBLE();
break;
case self::TAG_BYTE_ARRAY:
$value = $this->readTAG_BYTE_ARRAY();
break;
case self::TAG_STRING:
$value = $this->readTAG_STRING();
break;
case self::TAG_LIST:
$value = [];
$this->parseList($value, ord($this->read(1)), Utils::readLInt($this->read(4)));
break;
case self::TAG_COMPOUND:
$value = [];
$this->parseTree($value);
break;
default:
echo bin2hex(substr($this->binary, $this->offset - 1)) . PHP_EOL . PHP_EOL;
die("Invalid NBT Tag $tag");
break;
}
$node[] = $value;
}
}
public function writeTAG_BYTE($v){
$this->binary .= chr($v);
}
public function writeTAG_LONG($v){
$this->binary .= Utils::writeLLong($v);
}
public function writeTAG_FLOAT($v){
$this->binary .= Utils::writeLFloar($v);
}
public function writeTAG_DOUBLE($v){
$this->binary .= Utils::writeLDouble($v);
}
public function writeTAG_BYTE_ARRAY($v){
$this->binary .= $this->writeTAG_INT(strlen($v)) . $v;
}
public function writeTAG_INT($v){
$this->binary .= Utils::writeLInt($v);
}
public function writeTAG_STRING($v){
$this->binary .= $this->writeTAG_SHORT(strlen($v)) . $v;
}
public function writeTAG_SHORT($v){
$this->binary .= Utils::writeLShort($v);
}
}

View file

@ -1,37 +1,19 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
//Unsecure, not used for "Real Randomness"
class Random{
private $x, $y, $z, $w;
private $haveNextNextGaussian = false;
private $nextNextGaussian = 0;
public function __construct($seed = false){
$this->setSeed($seed);
}
public function setSeed($seed = false){
$seed = $seed !== false ? Utils::writeInt((int) $seed):Utils::getRandomBytes(4, false);
$state = array();
$seed = $seed !== false ? Utils::writeInt((int) $seed) : Utils::getRandomBytes(4, false);
$state = [];
for($i = 0; $i < 256; ++$i){
$state[] = $i;
}
@ -44,39 +26,33 @@ class Random{
$this->state = $state;
$this->i = $this->j = 0;
}
public function nextGaussian(){
if ($this->haveNextNextGaussian) {
$this->haveNextNextGaussian = false;
if($this->haveNextNextGaussian){
$this->haveNextNextGaussian = false;
return $this->nextNextGaussian;
} else {
$v1 = $v2 = $s = null;
do {
$v1 = 2 * $this->nextFloat() - 1; // between -1.0 and 1.0
$v2 = 2 * $this->nextFloat() - 1; // between -1.0 and 1.0
$s = $v1 * $v1 + $v2 * $v2;
} while ($s >= 1 || $s == 0);
$multiplier = sqrt(-2 * log($s)/$s);
$nextNextGaussian = $v2 * $multiplier;
$haveNextNextGaussian = true;
return $v1 * $multiplier;
}
}
public function nextInt(){
return Utils::readInt($this->nextBytes(4)) & 0x7FFFFFFF;
}else{
$v1 = $v2 = $s = null;
do{
$v1 = 2 * $this->nextFloat() - 1; // between -1.0 and 1.0
$v2 = 2 * $this->nextFloat() - 1; // between -1.0 and 1.0
$s = $v1 * $v1 + $v2 * $v2;
}while($s >= 1 || $s == 0);
$multiplier = sqrt(-2 * log($s) / $s);
$nextNextGaussian = $v2 * $multiplier;
$haveNextNextGaussian = true;
return $v1 * $multiplier;
}
}
public function nextSignedInt(){
return Utils::readInt($this->nextBytes(4));
}
public function nextFloat(){
return $this->nextInt() / 0x7FFFFFFF;
}
public function nextSignedFloat(){
return $this->nextSignedInt() / 0x7FFFFFFF;
public function nextInt(){
return Utils::readInt($this->nextBytes(4)) & 0x7FFFFFFF;
}
public function nextBytes($byteCount){
$bytes = "";
for($i = 0; $i < $byteCount; ++$i){
@ -89,11 +65,19 @@ class Random{
}
return $bytes;
}
public function nextSignedFloat(){
return $this->nextSignedInt() / 0x7FFFFFFF;
}
public function nextSignedInt(){
return Utils::readInt($this->nextBytes(4));
}
public function nextBoolean(){
return ($this->nextBytes(1) & 0x01) == 0;
}
public function nextRange($start = 0, $end = PHP_INT_MAX){
return $start + ($this->nextInt() % ($end + 1 - $start));
}

View file

@ -1,24 +1,5 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
define("FORMAT_BLACK", "§0");
define("FORMAT_DARK_BLUE", "§1");
define("FORMAT_DARK_GREEN", "§2");
@ -43,7 +24,6 @@ define("FORMAT_UNDERLINE", "§n");
define("FORMAT_ITALIC", "§o");
define("FORMAT_RESET", "§r");
class TextFormat{
public static function tokenize($string){
return preg_split("/(§[0123456789abcdefklmnor])/", $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
@ -160,7 +140,7 @@ class TextFormat{
$newString .= str_repeat("</span>", $tokens);
return $newString;
}
public static function toANSI($string){
if(!is_array($string)){
$string = self::tokenize($string);

View file

@ -1,24 +1,5 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
function UPnP_PortForward($port){
if(Utils::$online === false){
return false;

View file

@ -1,47 +1,29 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
define("BIG_ENDIAN", 0x00);
define("LITTLE_ENDIAN", 0x01);
define("ENDIANNESS", (pack("d", 1) === "\77\360\0\0\0\0\0\0" ? BIG_ENDIAN:LITTLE_ENDIAN));
define("ENDIANNESS", (pack("d", 1) === "\77\360\0\0\0\0\0\0" ? BIG_ENDIAN : LITTLE_ENDIAN));
class Utils{
public static $online = true;
public static $ip = false;
/*public static function isOnline(){
return ((@fsockopen("8.8.8.8", 80, $e = null, $n = null, 2) !== false or @fsockopen("www.linux.org", 80, $e = null, $n = null, 2) !== false or @fsockopen("www.php.net", 80, $e = null, $n = null, 2) !== false) ? true:false);
}*/
public static function getCallableIdentifier(callable $variable){
if(is_array($variable)){
return sha1(strtolower(get_class($variable[0]))."::".strtolower($variable[1]));
return sha1(strtolower(get_class($variable[0])) . "::" . strtolower($variable[1]));
}else{
return sha1(strtolower($variable));
}
}
public static function getUniqueID($raw = false, $extra = ""){
$machine = php_uname("a");
$machine .= file_exists("/proc/cpuinfo") ? file_get_contents("/proc/cpuinfo") : "";
$machine .= file_exists("/proc/cpuinfo") ? file_get_contents("/proc/cpuinfo") : "";
$machine .= sys_get_temp_dir();
$machine .= $extra;
if(Utils::getOS() == "win"){
@ -61,11 +43,34 @@ class Utils{
$data .= PHP_INT_SIZE;
$data .= get_current_user();
foreach(get_loaded_extensions() as $ext){
$data .= $ext.":".phpversion($ext);
$data .= $ext . ":" . phpversion($ext);
}
return hash("md5", $machine, $raw).hash("sha512", $data, $raw);
return hash("md5", $machine, $raw) . hash("sha512", $data, $raw);
}
public static function getOS(){
$uname = php_uname("s");
if(stripos($uname, "Darwin") !== false){
if(strpos(php_uname("m"), "iP") === 0){
return "ios";
}else{
return "mac";
}
}elseif(stripos($uname, "Win") !== false or $uname === "Msys"){
return "win";
}elseif(stripos($uname, "Linux") !== false){
if(@file_exists("/system/build.prop")){
return "android";
}else{
return "linux";
}
}elseif(stripos($uname, "BSD") !== false or $uname === "DragonFly"){
return "bsd";
}else{
return "other";
}
}
public static function getIP($force = false){
if(Utils::$online === false){
return false;
@ -94,30 +99,27 @@ class Utils{
}
}
return Utils::$ip;
}
public static function getOS(){
$uname = php_uname("s");
if(stripos($uname, "Darwin") !== false){
if(strpos(php_uname("m"), "iP") === 0){
return "ios";
}else{
return "mac";
}
}elseif(stripos($uname, "Win") !== false or $uname === "Msys"){
return "win";
}elseif(stripos($uname, "Linux") !== false){
if(@file_exists("/system/build.prop")){
return "android";
}else{
return "linux";
}
}elseif(stripos($uname, "BSD") !== false or $uname === "DragonFly"){
return "bsd";
}else{
return "other";
public static function curl_get($page, $timeout = 10){
if(Utils::$online === false){
return false;
}
$ch = curl_init($page);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 PocketMine-MP"]);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
public static function hexdump($bin){
@ -126,7 +128,7 @@ class Utils{
foreach($bin as $counter => $line){
$hex = chunk_split(chunk_split(str_pad(bin2hex($line), 32, " ", STR_PAD_RIGHT), 2, " "), 24, " ");
$ascii = preg_replace('#([^\x20-\x7E])#', ".", $line);
$output .= str_pad(dechex($counter << 4), 4, "0", STR_PAD_LEFT). " " . $hex . " " . $ascii . PHP_EOL;
$output .= str_pad(dechex($counter << 4), 4, "0", STR_PAD_LEFT) . " " . $hex . " " . $ascii . PHP_EOL;
}
return $output;
}
@ -138,17 +140,8 @@ class Utils{
return preg_replace('#([^\x20-\x7E])#', '.', $str);
}
public static function readTriad($str){
list(,$unpacked) = @unpack("N", "\x00".$str);
return $unpacked;
}
public static function writeTriad($value){
return substr(pack("N", $value), 1);
}
public static function getRandomUpdateTicks(){
return -log(lcg_value())*1365.4; //Poisson distribution (1/(68.27 * 20))
return -log(lcg_value()) * 1365.4; //Poisson distribution (1/(68.27 * 20))
}
public static function writeMetadata($data){
@ -178,7 +171,7 @@ class Utils{
$m .= Utils::writeLShort($d["value"][2]);
break;
case 6:
for($i=0; $i < 3; ++$i){
for($i = 0; $i < 3; ++$i){
$m .= Utils::writeLInt($d["value"][$i]);
}
break;
@ -187,11 +180,46 @@ class Utils{
$m .= "\x7f";
return $m;
}
public static function writeSlot(Item $item){
return Utils::writeShort($item->getID()).chr($item->count).Utils::writeShort($item->getMetadata());
public static function writeByte($c){
if($c > 0xff){
return false;
}
if($c < 0 and $c >= -0x80){
$c = 0xff + $c + 1;
}
return chr($c);
}
public static function writeLShort($value){
if($value < 0){
$value += 0x10000;
}
return pack("v", $value);
}
public static function writeLInt($value){
if($value < 0){
$value += 0x100000000;
}
return pack("V", $value);
}
public static function writeLFloat($value){
return ENDIANNESS === BIG_ENDIAN ? strrev(pack("f", $value)) : pack("f", $value);
}
public static function writeSlot(Item $item){
return Utils::writeShort($item->getID()) . chr($item->count) . Utils::writeShort($item->getMetadata());
}
public static function writeShort($value){
if($value < 0){
$value += 0x10000;
}
return pack("n", $value);
}
public static function readSlot($ob){
$id = Utils::readShort($ob->get(2));
$cnt = ord($ob->get(1));
@ -202,9 +230,22 @@ class Utils{
);
}
public static function readShort($str, $signed = true){
if(strlen($str) === 0){
return;
}
[, $unpacked] = unpack("n", $str);
if($unpacked > 0x7fff and $signed === true){
$unpacked -= 0x10000; // Convert unsigned short to signed short
}
return $unpacked;
}
public static function readMetadata($value, $types = false){
$offset = 0;
$m = array();
$m = [];
$b = ord($value[$offset]);
++$offset;
while($b !== 127 and isset($value[$offset])){
@ -234,7 +275,7 @@ class Utils{
$offset += $len;
break;
case 5:
$r = array();
$r = [];
$r[] = Utils::readLShort(substr($value, $offset, 2));
$offset += 2;
$r[] = ord($value[$offset]);
@ -243,8 +284,8 @@ class Utils{
$offset += 2;
break;
case 6:
$r = array();
for($i=0; $i < 3; ++$i){
$r = [];
for($i = 0; $i < 3; ++$i){
$r[] = Utils::readLInt(substr($value, $offset, 4));
$offset += 4;
}
@ -252,7 +293,7 @@ class Utils{
}
if($types === true){
$m[$bottom] = array($r, $type);
$m[$bottom] = [$r, $type];
}else{
$m[$bottom] = $r;
}
@ -262,8 +303,37 @@ class Utils{
return $m;
}
public static function readByte($c, $signed = true){
$b = ord($c[0]);
if($signed === true and ($b & 0x80) === 0x80){ //calculate Two's complement
$b = -0x80 + ($b & 0x7f);
}
return $b;
}
public static function readLShort($str, $signed = true){
[, $unpacked] = @unpack("v", $str);
if($unpacked > 0x7fff and $signed === true){
$unpacked -= 0x10000; // Convert unsigned short to signed short
}
return $unpacked;
}
public static function readLInt($str){
[, $unpacked] = @unpack("V", $str);
if($unpacked >= 2147483648){
$unpacked -= 4294967296;
}
return (int) $unpacked;
}
public static function readLFloat($str){
[, $value] = ENDIANNESS === BIG_ENDIAN ? @unpack("f", strrev($str)) : @unpack("f", $str);
return $value;
}
public static function readDataArray($str, $len = 10, &$offset = null){
$data = array();
$data = [];
$offset = 0;
for($i = 1; $i <= $len and isset($str[$offset]); ++$i){
$l = Utils::readTriad(substr($str, $offset, 3));
@ -274,6 +344,11 @@ class Utils{
return $data;
}
public static function readTriad($str){
[, $unpacked] = @unpack("N", "\x00" . $str);
return $unpacked;
}
public static function writeDataArray($data){
$raw = "";
foreach($data as $v){
@ -283,6 +358,10 @@ class Utils{
return $raw;
}
public static function writeTriad($value){
return substr(pack("N", $value), 1);
}
public static function getRandomBytes($length = 16, $secure = true, $raw = true, $startEntropy = "", &$rounds = 0, &$drop = 0){
static $lastRandom = "";
$output = b"";
@ -292,8 +371,8 @@ class Utils{
$drop = 0;
while(!isset($output[$length - 1])){
//some entropy, but works ^^
$weakEntropy = array(
is_array($startEntropy) ? implode($startEntropy):$startEntropy,
$weakEntropy = [
is_array($startEntropy) ? implode($startEntropy) : $startEntropy,
serialize(@stat(__FILE__)),
__DIR__,
PHP_OS,
@ -301,12 +380,12 @@ class Utils{
(string) lcg_value(),
(string) PHP_MAXPATHLEN,
PHP_SAPI,
(string) PHP_INT_MAX.".".PHP_INT_SIZE,
(string) PHP_INT_MAX . "." . PHP_INT_SIZE,
serialize($_SERVER),
serialize(get_defined_constants()),
get_current_user(),
serialize(ini_get_all()),
(string) memory_get_usage().".".memory_get_peak_usage(),
(string) memory_get_usage() . "." . memory_get_peak_usage(),
php_uname(),
phpversion(),
//extension_loaded("gmp") ? gmp_strval(gmp_random(4)):microtime(),
@ -317,17 +396,17 @@ class Utils{
(string) getmyinode(),
(string) getmygid(),
(string) rand(),
function_exists("zend_thread_id") ? ((string) zend_thread_id()):microtime(),
function_exists("getrusage") ? @implode(getrusage()):microtime(),
function_exists("sys_getloadavg") ? @implode(sys_getloadavg()):microtime(),
function_exists("zend_thread_id") ? ((string) zend_thread_id()) : microtime(),
function_exists("getrusage") ? @implode(getrusage()) : microtime(),
function_exists("sys_getloadavg") ? @implode(sys_getloadavg()) : microtime(),
serialize(get_loaded_extensions()),
sys_get_temp_dir(),
(string) disk_free_space("."),
(string) disk_total_space("."),
uniqid(microtime(),true),
uniqid(microtime(), true),
file_exists("/proc/cpuinfo") ? file_get_contents("/proc/cpuinfo") : microtime(),
);
];
shuffle($weakEntropy);
$value = hash("sha512", implode($weakEntropy), true);
$lastRandom .= $value;
@ -336,15 +415,15 @@ class Utils{
$value ^= hash("sha512", ((string) lcg_value()) . $c . microtime() . $k, true);
}
unset($weakEntropy);
if($secure === true){
$strongEntropyValues = array(
is_array($startEntropy) ? hash("sha512", $startEntropy[($rounds + $drop) % count($startEntropy)], true):hash("sha512", $startEntropy, true), //Get a random index of the startEntropy, or just read it
$strongEntropyValues = [
is_array($startEntropy) ? hash("sha512", $startEntropy[($rounds + $drop) % count($startEntropy)], true) : hash("sha512", $startEntropy, true), //Get a random index of the startEntropy, or just read it
file_exists("/dev/urandom") ? fread(fopen("/dev/urandom", "rb"), 64) : str_repeat("\x00", 64),
(function_exists("openssl_random_pseudo_bytes") and version_compare(PHP_VERSION, "5.3.4", ">=")) ? openssl_random_pseudo_bytes(64) : str_repeat("\x00", 64),
function_exists("mcrypt_create_iv") ? mcrypt_create_iv(64, MCRYPT_DEV_URANDOM) : str_repeat("\x00", 64),
$value,
);
];
$strongEntropy = array_pop($strongEntropyValues);
foreach($strongEntropyValues as $value){
$strongEntropy = $strongEntropy ^ $value;
@ -354,9 +433,9 @@ class Utils{
$bitcnt = 0;
for($j = 0; $j < 64; ++$j){
$a = ord($strongEntropy[$j]);
for($i = 0; $i < 8; $i += 2){
$b = ($a & (1 << $i)) > 0 ? 1:0;
if($b != (($a & (1 << ($i + 1))) > 0 ? 1:0)){
for($i = 0; $i < 8; $i += 2){
$b = ($a & (1 << $i)) > 0 ? 1 : 0;
if($b != (($a & (1 << ($i + 1))) > 0 ? 1 : 0)){
$secureValue = $b << $bitcnt;
if($bitcnt == 7){
$value .= chr($secureValue);
@ -377,17 +456,17 @@ class Utils{
++$rounds;
}
$lastRandom = hash("sha512", $lastRandom, true);
return $raw === false ? bin2hex($output):$output;
return $raw === false ? bin2hex($output) : $output;
}
public static function randomFloat(){//GameHerobrine's code
return rand()/getrandmax();
}
public static function chance($i){//GameHerobrine's code
return self::randomFloat() <= $i / 100;
}
public static function randomFloat(){//GameHerobrine's code
return rand() / getrandmax();
}
public static function round($number){
return round($number, 0, PHP_ROUND_HALF_DOWN);
}
@ -403,44 +482,24 @@ class Utils{
$Y = $pos1["y"] - $pos2["y"];
$hAngle = rad2deg(atan2($Z, $X) - M_PI_2);
$vAngle = rad2deg(-atan2($Y, $dXZ));
return array("yaw" => $hAngle, "pitch" => $vAngle);
}
public static function curl_get($page, $timeout = 10){
if(Utils::$online === false){
return false;
}
$ch = curl_init($page);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 PocketMine-MP"));
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
return ["yaw" => $hAngle, "pitch" => $vAngle];
}
public static function curl_post($page, $args, $timeout = 10){
if(Utils::$online === false){
return false;
}
$ch = curl_init($page);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 PocketMine-MP"));
curl_setopt($ch, CURLOPT_HTTPHEADER, ["User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 PocketMine-MP"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
$ret = curl_exec($ch);
@ -457,66 +516,23 @@ class Utils{
}
public static function readBool($b){
return Utils::readByte($b, false) === 0 ? false:true;
return Utils::readByte($b, false) === 0 ? false : true;
}
public static function writeBool($b){
return Utils::writeByte($b === true ? 1:0);
}
public static function readByte($c, $signed = true){
$b = ord($c[0]);
if($signed === true and ($b & 0x80) === 0x80){ //calculate Two's complement
$b = -0x80 + ($b & 0x7f);
}
return $b;
}
public static function writeByte($c){
if($c > 0xff){
return false;
}
if($c < 0 and $c >= -0x80){
$c = 0xff + $c + 1;
}
return chr($c);
}
public static function readShort($str, $signed = true){
list(,$unpacked) = @unpack("n", $str);
if($unpacked > 0x7fff and $signed === true){
$unpacked -= 0x10000; // Convert unsigned short to signed short
}
return $unpacked;
}
public static function writeShort($value){
if($value < 0){
$value += 0x10000;
}
return pack("n", $value);
}
public static function readLShort($str, $signed = true){
list(,$unpacked) = @unpack("v", $str);
if($unpacked > 0x7fff and $signed === true){
$unpacked -= 0x10000; // Convert unsigned short to signed short
}
return $unpacked;
}
public static function writeLShort($value){
if($value < 0){
$value += 0x10000;
}
return pack("v", $value);
return Utils::writeByte($b === true ? 1 : 0);
}
public static function readInt($str){
list(,$unpacked) = @unpack("N", $str);
if(strlen($str) === 0){
return;
}
[, $unpacked] = unpack("N", $str);
if($unpacked >= 2147483648){
$unpacked -= 4294967296;
}
return (int) $unpacked;
}
@ -527,37 +543,13 @@ class Utils{
return pack("N", $value);
}
public static function readLInt($str){
list(,$unpacked) = @unpack("V", $str);
if($unpacked >= 2147483648){
$unpacked -= 4294967296;
}
return (int) $unpacked;
}
public static function writeLInt($value){
if($value < 0){
$value += 0x100000000;
}
return pack("V", $value);
}
public static function readFloat($str){
list(,$value) = ENDIANNESS === BIG_ENDIAN ? @unpack("f", $str):@unpack("f", strrev($str));
[, $value] = ENDIANNESS === BIG_ENDIAN ? @unpack("f", $str) : @unpack("f", strrev($str));
return $value;
}
public static function writeFloat($value){
return ENDIANNESS === BIG_ENDIAN ? pack("f", $value):strrev(pack("f", $value));
}
public static function readLFloat($str){
list(,$value) = ENDIANNESS === BIG_ENDIAN ? @unpack("f", strrev($str)):@unpack("f", $str);
return $value;
}
public static function writeLFloat($value){
return ENDIANNESS === BIG_ENDIAN ? strrev(pack("f", $value)):pack("f", $value);
return ENDIANNESS === BIG_ENDIAN ? pack("f", $value) : strrev(pack("f", $value));
}
public static function printFloat($value){
@ -565,27 +557,31 @@ class Utils{
}
public static function readDouble($str){
list(,$value) = ENDIANNESS === BIG_ENDIAN ? @unpack("d", $str):@unpack("d", strrev($str));
[, $value] = ENDIANNESS === BIG_ENDIAN ? @unpack("d", $str) : @unpack("d", strrev($str));
return $value;
}
public static function writeDouble($value){
return ENDIANNESS === BIG_ENDIAN ? pack("d", $value):strrev(pack("d", $value));
return ENDIANNESS === BIG_ENDIAN ? pack("d", $value) : strrev(pack("d", $value));
}
public static function readLDouble($str){
list(,$value) = ENDIANNESS === BIG_ENDIAN ? @unpack("d", strrev($str)):@unpack("d", $str);
[, $value] = ENDIANNESS === BIG_ENDIAN ? @unpack("d", strrev($str)) : @unpack("d", $str);
return $value;
}
public static function writeLDouble($value){
return ENDIANNESS === BIG_ENDIAN ? strrev(pack("d", $value)):pack("d", $value);
return ENDIANNESS === BIG_ENDIAN ? strrev(pack("d", $value)) : pack("d", $value);
}
public static function readLLong($str){
return Utils::readLong(strrev($str));
}
public static function readLong($x, $signed = true){
$value = "0";
if($signed === true){
$negative = ((ord($x[0]) & 0x80) === 0x80) ? true:false;
$negative = ((ord($x[0]) & 0x80) === 0x80) ? true : false;
if($negative){
$x = ~$x;
}
@ -597,12 +593,16 @@ class Utils{
$value = bcmul($value, "4294967296", 0); //4294967296 == 2^32
$value = bcadd($value, 0x1000000 * ord(@$x[$i]) + ((ord(@$x[$i + 1]) << 16) | (ord(@$x[$i + 2]) << 8) | ord(@$x[$i + 3])), 0);
}
return ($negative === true ? "-".$value:$value);
return ($negative === true ? "-" . $value : $value);
}
public static function writeLLong($value){
return strrev(Utils::writeLong($value));
}
public static function writeLong($value){
$x = "";
if(strval($value[0]) === "-"){
if(strval($value[0] ?? '') === "-"){
$negative = true;
$value = bcadd($value, "1");
if(strval($value[0]) === "-"){
@ -618,22 +618,12 @@ class Utils{
}
$x = str_pad(substr($x, 0, 8), 8, "\x00", STR_PAD_LEFT);
if($negative === true){
$x = ~$x;
$x = ~$x;
}
return $x;
}
public static function readLLong($str){
return Utils::readLong(strrev($str));
}
public static function writeLLong($value){
return strrev(Utils::writeLong($str));
}
}
//if(Utils::isOnline() === false){
//Utils::$online = false;
//Utils::$online = false;
//}

View file

@ -1,38 +1,21 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
class VersionString{
public static $stageOrder = array(
public static $stageOrder = [
"alpha" => 0,
"a" => 0,
"beta" => 1,
"b" => 1,
"final" => 2,
"f" => 2,
);
];
private $stage;
private $major;
private $release;
private $minor;
private $development = false;
public function __construct($version = MAJOR_VERSION){
if(is_int($version)){
$this->minor = $version & 0x1F;
@ -45,14 +28,10 @@ class VersionString{
$this->generation = (int) $version[2]; //0-15
$this->major = (int) $version[3]; //0-15
$this->minor = (int) $version[4]; //0-31
$this->development = $version[5] === "dev" ? true:false;
$this->development = $version[5] === "dev" ? true : false;
}
}
public function getNumber(){
return (int) (VersionString::$stageOrder[$this->stage] << 13) + ($this->generation << 9) + ($this->major << 5) + $this->minor;
}
public function getStage(){
return $this->stage;
}
@ -69,22 +48,22 @@ class VersionString{
return $this->minor;
}
public function getRelease(){
return $this->generation . "." . $this->major . "." . $this->minor;
}
public function isDev(){
return $this->development === true;
}
public function get(){
return ucfirst($this->stage) . "_" . $this->getRelease() . ($this->development === true ? "dev":"");
}
public function __toString(){
return $this->get();
}
public function get(){
return ucfirst($this->stage) . "_" . $this->getRelease() . ($this->development === true ? "dev" : "");
}
public function getRelease(){
return $this->generation . "." . $this->major . "." . $this->minor;
}
public function compare($target, $diff = false){
if(($target instanceof VersionString) === false){
$target = new VersionString($target);
@ -102,4 +81,8 @@ class VersionString{
return 0; //Same version
}
}
public function getNumber(){
return (int) (VersionString::$stageOrder[$this->stage] << 13) + ($this->generation << 9) + ($this->major << 5) + $this->minor;
}
}

View file

@ -1,38 +1,55 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
define("ASYNC_CURL_GET", 1);
define("ASYNC_CURL_POST", 2);
class AsyncMultipleQueue extends Thread{
public $input;
public $output;
public $stop;
public function __construct(){
$this->input = "";
$this->output = "";
$this->stop = false;
$this->start();
}
public function run(){
while($this->stop === false){
if(isset($this->input[5])){ //len 6 min
$rID = Utils::readInt($this->get(4));
switch(Utils::readShort($this->get(2), false)){
case ASYNC_CURL_GET:
$url = $this->get(Utils::readShort($this->get(2), false));
$timeout = Utils::readShort($this->get(2));
$res = (string) Utils::curl_get($url, $timeout);
//$this->lock();
$this->output .= Utils::writeInt($rID) . Utils::writeShort(ASYNC_CURL_GET) . Utils::writeInt(strlen($res)) . $res;
//$this->unlock();
break;
case ASYNC_CURL_POST:
$url = $this->get(Utils::readShort($this->get(2), false));
$timeout = Utils::readShort($this->get(2));
$cnt = Utils::readShort($this->get(2), false);
$d = [];
for($c = 0; $c < $cnt; ++$c){
$key = $this->get(Utils::readShort($this->get(2), false));
$d[$key] = $this->get(Utils::readInt($this->get(4), false));
}
$res = (string) Utils::curl_post($url, $d, $timeout);
//$this->lock();
$this->output .= Utils::writeInt($rID) . Utils::writeShort(ASYNC_CURL_POST) . Utils::writeInt(strlen($res)) . $res;
//$this->unlock();
break;
}
}
usleep(10000);
}
}
private function get($len){
$str = "";
if($len <= 0){
@ -48,65 +65,32 @@ class AsyncMultipleQueue extends Thread{
$this->input = (string) substr($this->input, $offset);
return $str;
}
public function run(){
while($this->stop === false){
if(isset($this->input[5])){ //len 6 min
$rID = Utils::readInt($this->get(4));
switch(Utils::readShort($this->get(2), false)){
case ASYNC_CURL_GET:
$url = $this->get(Utils::readShort($this->get(2), false));
$timeout = Utils::readShort($this->get(2));
$res = (string) Utils::curl_get($url, $timeout);
//$this->lock();
$this->output .= Utils::writeInt($rID).Utils::writeShort(ASYNC_CURL_GET).Utils::writeInt(strlen($res)).$res;
//$this->unlock();
break;
case ASYNC_CURL_POST:
$url = $this->get(Utils::readShort($this->get(2), false));
$timeout = Utils::readShort($this->get(2));
$cnt = Utils::readShort($this->get(2), false);
$d = array();
for($c = 0; $c < $cnt; ++$c){
$key = $this->get(Utils::readShort($this->get(2), false));
$d[$key] = $this->get(Utils::readInt($this->get(4), false));
}
$res = (string) Utils::curl_post($url, $d, $timeout);
//$this->lock();
$this->output .= Utils::writeInt($rID).Utils::writeShort(ASYNC_CURL_POST).Utils::writeInt(strlen($res)).$res;
//$this->unlock();
break;
}
}
usleep(10000);
}
}
}
class Async extends Thread{
public function __construct($method, $params = array()){
public function __construct($method, $params = []){
$this->method = $method;
$this->params = $params;
$this->result = null;
$this->joined = false;
}
public function run(){
if(($this->result=call_user_func_array($this->method, $this->params))){
return true;
}else{
return false;
}
}
public static function call($method, $params = array()){
public static function call($method, $params = []){
$thread = new Async($method, $params);
if($thread->start()){
return $thread;
}
}
public function run(){
if(($this->result = call_user_func_array($this->method, $this->params))){
return true;
}else{
return false;
}
}
public function __toString(){
if(!$this->joined){
$this->joined = true;