Removed code remove comments

This commit is contained in:
Shoghi Cervantes
2014-03-06 13:16:44 +01:00
parent bbd66e6ad1
commit c08bf3ef86
350 changed files with 6317 additions and 5559 deletions

View File

@@ -20,31 +20,34 @@
*/
namespace PocketMine\Utils;
use PocketMine;
class Cache{
public static $cached = array();
public static function add($identifier, $blob, $minTTL = 30){
self::$cached[$identifier] = array($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){

View File

@@ -20,6 +20,7 @@
*/
namespace PocketMine\Utils;
use PocketMine;
/**
@@ -37,23 +38,23 @@ class Config{
const SERIALIZED = 4; // .sl
const ENUM = 5; // .txt, .list, .enum
const ENUMERATION = Config::ENUM;
/**
* @var array
*/
private $config;
/**
* @var string
*/
private $file;
/**
* @var boolean
*/
private $correct;
/**
* @var integer
*/
private $type = Config::DETECT;
/**
* @var array
*/
private $config;
/**
* @var string
*/
private $file;
/**
* @var boolean
*/
private $correct;
/**
* @var integer
*/
private $type = Config::DETECT;
public static $formats = array(
"properties" => Config::PROPERTIES,
@@ -73,36 +74,36 @@ class Config{
"enum" => Config::ENUM,
);
/**
* @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 = array(), &$correct = null){
$this->load($file, $type, $default);
$correct = $this->correct;
}
public function reload(){
public function reload(){
unset($this->config);
unset($this->correct);
unset($this->type);
$this->load($this->file);
}
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 = array()){
$this->correct = true;
$this->type = (int) $type;
$this->file = $file;
@@ -112,13 +113,13 @@ class Config{
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)));
if(isset(Config::$formats[$extension])){
$this->type = Config::$formats[$extension];
}else{
} else{
$this->correct = false;
}
}
@@ -144,6 +145,7 @@ class Config{
break;
default:
$this->correct = false;
return false;
}
if(!is_array($this->config)){
@@ -152,24 +154,25 @@ class Config{
if($this->fillDefaults($default, $this->config) > 0){
$this->save();
}
}else{
} else{
return false;
}
}
return true;
}
/**
* @return boolean
*/
public function check(){
/**
* @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:
@@ -188,114 +191,117 @@ class Config{
case Config::ENUM:
$content = implode("\r\n", array_keys($this->config));
break;
}
@file_put_contents($this->file, $content, LOCK_EX);
return true;
}else{
}
@file_put_contents($this->file, $content, LOCK_EX);
return true;
} else{
return false;
}
}
/**
* @param $k
*
* @return boolean|mixed
*/
public function &__get($k){
/**
* @param $k
*
* @return boolean|mixed
*/
public function &__get($k){
return $this->get($k);
}
/**
* @param $k
* @param $v
*/
public function __set($k, $v){
/**
* @param $k
* @param $v
*/
public function __set($k, $v){
$this->set($k, $v);
}
/**
* @param $k
*
* @return boolean
*/
public function __isset($k){
/**
* @param $k
*
* @return boolean
*/
public function __isset($k){
return $this->exists($k);
}
/**
* @param $k
*/
public function __unset($k){
/**
* @param $k
*/
public function __unset($k){
$this->remove($k);
}
/**
* @param $k
*
* @return boolean|mixed
*/
public function &get($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){
/**
* @param $k
* @param bool $v
*/
public function set($k, $v = true){
$this->config[$k] = $v;
}
/**
* @param array $v
*/
public function setAll($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
* @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){
/**
* @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 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){
/**
* @param $default
* @param $data
*
* @return integer
*/
private function fillDefaults($default, &$data){
$changed = 0;
foreach($default as $k => $v){
if(is_array($v)){
@@ -303,18 +309,19 @@ class Config{
$data[$k] = array();
}
$changed += $this->fillDefaults($v, $data[$k]);
}elseif(!isset($data[$k])){
} elseif(!isset($data[$k])){
$data[$k] = $v;
++$changed;
}
}
return $changed;
}
/**
* @param $content
*/
private function parseList($content){
/**
* @param $content
*/
private function parseList($content){
foreach(explode("\n", trim(str_replace("\r\n", "\n", $content))) as $v){
$v = trim($v);
if($v == ""){
@@ -324,26 +331,27 @@ class Config{
}
}
/**
* @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";
}elseif(is_array($v)){
$v = implode(";", $v);
$v = $v === true ? "on" : "off";
} elseif(is_array($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]);
@@ -360,7 +368,7 @@ 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;
}

View File

@@ -20,38 +20,40 @@
*/
namespace PocketMine\Utils;
use PocketMine;
use PocketMine\Utils\Utils as Utils;
//Unsecure, not used for "Real Randomness"
class Random{
private $z, $w;
public function __construct($seed = false){
$this->setSeed($seed);
}
public function setSeed($seed = false){
$seed = $seed !== false ? (int) $seed:Utils::readInt(Utils::getRandomBytes(4, false));
$seed = $seed !== false ? (int) $seed : Utils::readInt(Utils::getRandomBytes(4, false));
$this->z = $seed ^ 0xdeadbeef;
$this->w = $seed ^ 0xc0de1337;
}
public function nextInt(){
return Utils::readInt($this->nextBytes(4)) & 0x7FFFFFFF;
}
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 nextBytes($byteCount){
$bytes = "";
while(strlen($bytes) < $byteCount){
@@ -59,13 +61,14 @@ class Random{
$this->w = 18000 * ($this->w & 65535) + ($this->w >> 16);
$bytes .= pack("N", ($this->z << 16) + $this->w);
}
return substr($bytes, 0, $byteCount);
}
public function nextBoolean(){
return ($this->nextSignedInt() & 0x01) === 0;
}
public function nextRange($start = 0, $end = PHP_INT_MAX){
return $start + ($this->nextInt() % ($end + 1 - $start));
}

View File

@@ -20,6 +20,7 @@
*/
namespace PocketMine\Utils;
use PocketMine;
class TextFormat{
@@ -46,7 +47,7 @@ class TextFormat{
const UNDERLINE = "§n";
const ITALIC = "§o";
const RESET = "§r";
public static function tokenize($string){
return preg_split("/(§[0123456789abcdefklmnor])/", $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
}
@@ -54,7 +55,7 @@ class TextFormat{
public static function clean($string){
return preg_replace(array("/§[0123456789abcdefklmnor]/", "/\\x1b*/"), "", $string);
}
public static function toHTML($string){
if(!is_array($string)){
$string = self::tokenize($string);
@@ -158,11 +159,12 @@ class TextFormat{
break;
}
}
$newString .= str_repeat("</span>", $tokens);
return $newString;
}
public static function toANSI($string){
if(!is_array($string)){
$string = self::tokenize($string);
@@ -242,6 +244,7 @@ class TextFormat{
break;
}
}
return $newString;
}

View File

@@ -20,28 +20,29 @@
*/
namespace PocketMine\Utils;
use PocketMine;
use PocketMine\BlockAPI as BlockAPI;
class Utils{
const BIG_ENDIAN = 0x00;
const LITTLE_ENDIAN = 0x01;
public static $online = true;
public static $ip = false;
public static function getCallableIdentifier(callable $variable){
if(is_array($variable)){
return sha1(strtolower(get_class($variable[0]))."::".strtolower($variable[1]));
}else{
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,40 +62,42 @@ 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 getIP($force = false){
if(Utils::$online === false){
return false;
}elseif(Utils::$ip !== false and $force !== true){
} elseif(Utils::$ip !== false and $force !== true){
return Utils::$ip;
}
$ip = trim(strip_tags(Utils::curl_get("http://checkip.dyndns.org/")));
if(preg_match('#Current IP Address\: ([0-9a-fA-F\:\.]*)#', $ip, $matches) > 0){
Utils::$ip = $matches[1];
}else{
} else{
$ip = Utils::curl_get("http://www.checkip.org/");
if(preg_match('#">([0-9a-fA-F\:\.]*)</span>#', $ip, $matches) > 0){
Utils::$ip = $matches[1];
}else{
} else{
$ip = Utils::curl_get("http://checkmyip.org/");
if(preg_match('#Your IP address is ([0-9a-fA-F\:\.]*)#', $ip, $matches) > 0){
Utils::$ip = $matches[1];
}else{
} else{
$ip = trim(Utils::curl_get("http://ifconfig.me/ip"));
if($ip != ""){
Utils::$ip = $ip;
}else{
} else{
return false;
}
}
}
}
return Utils::$ip;
}
public static function getOS(){
@@ -102,20 +105,20 @@ class Utils{
if(stripos($uname, "Darwin") !== false){
if(strpos(php_uname("m"), "iP") === 0){
return "ios";
}else{
} else{
return "mac";
}
}elseif(stripos($uname, "Win") !== false or $uname === "Msys"){
} elseif(stripos($uname, "Win") !== false or $uname === "Msys"){
return "win";
}elseif(stripos($uname, "Linux") !== false){
} elseif(stripos($uname, "Linux") !== false){
if(@file_exists("/system/build.prop")){
return "android";
}else{
} else{
return "linux";
}
}elseif(stripos($uname, "BSD") !== false or $uname === "DragonFly"){
} elseif(stripos($uname, "BSD") !== false or $uname === "DragonFly"){
return "bsd";
}else{
} else{
return "other";
}
}
@@ -126,8 +129,9 @@ 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;
}
@@ -135,20 +139,22 @@ class Utils{
if(!is_string($str)){
return gettype($str);
}
return preg_replace('#([^\x20-\x7E])#', '.', $str);
}
public static function readTriad($str){
list(,$unpacked) = @unpack("N", "\x00".$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,23 +184,25 @@ 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;
}
}
$m .= "\x7f";
return $m;
}
public static function writeSlot(Item $item){
return Utils::writeShort($item->getID()).chr($item->getCount()).Utils::writeShort($item->getMetadata());
return Utils::writeShort($item->getID()) . chr($item->getCount()) . Utils::writeShort($item->getMetadata());
}
public static function readSlot($ob){
$id = Utils::readShort($ob->get(2));
$cnt = ord($ob->get(1));
return BlockAPI::getItem(
$id,
Utils::readShort($ob->get(2)),
@@ -244,7 +252,7 @@ class Utils{
break;
case 6:
$r = array();
for($i=0; $i < 3; ++$i){
for($i = 0; $i < 3; ++$i){
$r[] = Utils::readLInt(substr($value, $offset, 4));
$offset += 4;
}
@@ -253,12 +261,13 @@ class Utils{
}
if($types === true){
$m[$bottom] = array($r, $type);
}else{
} else{
$m[$bottom] = $r;
}
$b = ord($value{$offset});
++$offset;
}
return $m;
}
@@ -271,6 +280,7 @@ class Utils{
$data[] = substr($str, $offset, $l);
$offset += $l;
}
return $data;
}
@@ -280,6 +290,7 @@ class Utils{
$raw .= Utils::writeTriad(strlen($v));
$raw .= $v;
}
return $raw;
}
@@ -293,7 +304,7 @@ class Utils{
while(!isset($output{$length - 1})){
//some entropy, but works ^^
$weakEntropy = array(
is_array($startEntropy) ? implode($startEntropy):$startEntropy,
is_array($startEntropy) ? implode($startEntropy) : $startEntropy,
serialize(@stat(__FILE__)),
__DIR__,
PHP_OS,
@@ -301,15 +312,15 @@ 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(),
extension_loaded("gmp") ? gmp_strval(gmp_random(4)) : microtime(),
zend_version(),
(string) getmypid(),
(string) getmyuid(),
@@ -317,17 +328,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,10 +347,10 @@ 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
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),
@@ -354,19 +365,19 @@ 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);
$secureValue = 0;
$bitcnt = 0;
}else{
} else{
++$bitcnt;
}
++$drop;
}else{
} else{
$drop += 2;
}
}
@@ -377,7 +388,7 @@ class Utils{
++$rounds;
}
$lastRandom = hash("sha512", $lastRandom, true);
return $raw === false ? bin2hex($output):$output;
return $raw === false ? bin2hex($output) : $output;
}
public static function round($number){
@@ -395,6 +406,7 @@ 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);
}
@@ -408,13 +420,14 @@ class Utils{
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_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;
}
@@ -422,13 +435,13 @@ class Utils{
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);
@@ -437,6 +450,7 @@ class Utils{
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
@@ -449,11 +463,11 @@ 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);
return Utils::writeByte($b === true ? 1 : 0);
}
public static function readByte($c, $signed = true){
@@ -461,6 +475,7 @@ class Utils{
if($signed === true and ($b & 0x80) === 0x80){ //calculate Two's complement
$b = -0x80 + ($b & 0x7f);
}
return $b;
}
@@ -471,14 +486,16 @@ class Utils{
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);
list(, $unpacked) = @unpack("n", $str);
if($unpacked > 0x7fff and $signed === true){
$unpacked -= 0x10000; // Convert unsigned short to signed short
}
return $unpacked;
}
@@ -486,14 +503,16 @@ class Utils{
if($value < 0){
$value += 0x10000;
}
return pack("n", $value);
}
public static function readLShort($str, $signed = true){
list(,$unpacked) = @unpack("v", $str);
list(, $unpacked) = @unpack("v", $str);
if($unpacked > 0x7fff and $signed === true){
$unpacked -= 0x10000; // Convert unsigned short to signed short
}
return $unpacked;
}
@@ -501,14 +520,16 @@ class Utils{
if($value < 0){
$value += 0x10000;
}
return pack("v", $value);
}
public static function readInt($str){
list(,$unpacked) = @unpack("N", $str);
list(, $unpacked) = @unpack("N", $str);
if($unpacked >= 2147483648){
$unpacked -= 4294967296;
}
return (int) $unpacked;
}
@@ -516,14 +537,16 @@ class Utils{
if($value < 0){
$value += 0x100000000;
}
return pack("N", $value);
}
public static function readLInt($str){
list(,$unpacked) = @unpack("V", $str);
list(, $unpacked) = @unpack("V", $str);
if($unpacked >= 2147483648){
$unpacked -= 4294967296;
}
return (int) $unpacked;
}
@@ -531,25 +554,28 @@ class Utils{
if($value < 0){
$value += 0x100000000;
}
return pack("V", $value);
}
public static function readFloat($str){
list(,$value) = ENDIANNESS === Utils::BIG_ENDIAN ? @unpack("f", $str):@unpack("f", strrev($str));
list(, $value) = ENDIANNESS === Utils::BIG_ENDIAN ? @unpack("f", $str) : @unpack("f", strrev($str));
return $value;
}
public static function writeFloat($value){
return ENDIANNESS === Utils::BIG_ENDIAN ? pack("f", $value):strrev(pack("f", $value));
return ENDIANNESS === Utils::BIG_ENDIAN ? pack("f", $value) : strrev(pack("f", $value));
}
public static function readLFloat($str){
list(,$value) = ENDIANNESS === Utils::BIG_ENDIAN ? @unpack("f", strrev($str)):@unpack("f", $str);
list(, $value) = ENDIANNESS === Utils::BIG_ENDIAN ? @unpack("f", strrev($str)) : @unpack("f", $str);
return $value;
}
public static function writeLFloat($value){
return ENDIANNESS === Utils::BIG_ENDIAN ? strrev(pack("f", $value)):pack("f", $value);
return ENDIANNESS === Utils::BIG_ENDIAN ? strrev(pack("f", $value)) : pack("f", $value);
}
public static function printFloat($value){
@@ -557,31 +583,33 @@ class Utils{
}
public static function readDouble($str){
list(,$value) = ENDIANNESS === Utils::BIG_ENDIAN ? @unpack("d", $str):@unpack("d", strrev($str));
list(, $value) = ENDIANNESS === Utils::BIG_ENDIAN ? @unpack("d", $str) : @unpack("d", strrev($str));
return $value;
}
public static function writeDouble($value){
return ENDIANNESS === Utils::BIG_ENDIAN ? pack("d", $value):strrev(pack("d", $value));
return ENDIANNESS === Utils::BIG_ENDIAN ? pack("d", $value) : strrev(pack("d", $value));
}
public static function readLDouble($str){
list(,$value) = ENDIANNESS === Utils::BIG_ENDIAN ? @unpack("d", strrev($str)):@unpack("d", $str);
list(, $value) = ENDIANNESS === Utils::BIG_ENDIAN ? @unpack("d", strrev($str)) : @unpack("d", $str);
return $value;
}
public static function writeLDouble($value){
return ENDIANNESS === Utils::BIG_ENDIAN ? strrev(pack("d", $value)):pack("d", $value);
return ENDIANNESS === Utils::BIG_ENDIAN ? strrev(pack("d", $value)) : pack("d", $value);
}
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;
}
}else{
} else{
$negative = false;
}
@@ -589,7 +617,8 @@ 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 writeLong($value){
@@ -600,7 +629,7 @@ class Utils{
if($value{0} === "-"){
$value = substr($value, 1);
}
}else{
} else{
$negative = false;
}
while(bccomp($value, "0", 0) > 0){
@@ -610,8 +639,9 @@ class Utils{
}
$x = str_pad(substr($x, 0, 8), 8, "\x00", STR_PAD_LEFT);
if($negative === true){
$x = ~$x;
$x = ~$x;
}
return $x;
}

View File

@@ -20,6 +20,7 @@
*/
namespace PocketMine\Utils;
use PocketMine;
class VersionString{
@@ -37,19 +38,20 @@ class VersionString{
private $minor;
private $development = false;
private $generation;
public function __construct($version = PocketMine\VERSION){
if(is_int($version)){
$this->minor = $version & 0x1F;
$this->major = ($version >> 5) & 0x0F;
$this->generation = ($version >> 9) & 0x0F;
$this->stage = array_search(($version >> 13) & 0x0F, VersionString::$stageOrder, true);
}else{
} else{
$version = preg_split("/([A-Za-z]*)[ _\-]([0-9]*)\.([0-9]*)\.{0,1}([0-9]*)(dev|)/", $version, -1, PREG_SPLIT_DELIM_CAPTURE);
$this->stage = strtolower($version[1]); //0-15
$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;
}
}
@@ -76,13 +78,13 @@ class VersionString{
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":"");
return ucfirst($this->stage) . "_" . $this->getRelease() . ($this->development === true ? "dev" : "");
}
public function __toString(){
@@ -100,9 +102,9 @@ class VersionString{
}
if($number > $tNumber){
return -1; //Target is older
}elseif($number < $tNumber){
} elseif($number < $tNumber){
return 1; //Target is newer
}else{
} else{
return 0; //Same version
}
}

View File

@@ -29,28 +29,31 @@ class StackableArray extends \Stackable{
if(is_array($value)){
$this->{$n} = new StackableArray();
call_user_func_array(array($this->{$n}, "__construct"), $value);
}else{
} else{
$this->{$n} = $value;
}
}
}
public function __destruct(){}
public function run(){}
public function __destruct(){
}
public function run(){
}
}
class AsyncMultipleQueue extends Thread{
public $input;
public $output;
public $stop;
public function __construct(){
$this->input = "";
$this->output = "";
$this->stop = false;
$this->start();
}
private function get($len){
$str = "";
if($len <= 0){
@@ -64,9 +67,10 @@ 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
@@ -75,10 +79,10 @@ class AsyncMultipleQueue extends Thread{
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->output .= Utils::writeInt($rID) . Utils::writeShort(ASYNC_CURL_GET) . Utils::writeInt(strlen($res)) . $res;
$this->unlock();
break;
case ASYNC_CURL_POST:
@@ -92,14 +96,14 @@ class AsyncMultipleQueue extends Thread{
}
$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->output .= Utils::writeInt($rID) . Utils::writeShort(ASYNC_CURL_POST) . Utils::writeInt(strlen($res)) . $res;
$this->unlock();
break;
case ASYNC_FUNCTION:
$function = $this->get(Utils::readShort($this->get(2), false));
$params = unserialize($this->get(Utils::readInt($this->get(4))));
$res = serialize(@call_user_func_array($function, $params));
$this->output .= Utils::writeInt($rID).Utils::writeShort(ASYNC_FUNCTION).Utils::writeInt(strlen($res)).$res;
$this->output .= Utils::writeInt($rID) . Utils::writeShort(ASYNC_FUNCTION) . Utils::writeInt(strlen($res)) . $res;
break;
}
}
@@ -117,9 +121,9 @@ class Async extends Thread{
}
public function run(){
if(($this->result=call_user_func_array($this->method, $this->params))){
if(($this->result = call_user_func_array($this->method, $this->params))){
return true;
}else{
} else{
return false;
}
}