mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-08 19:02:59 +00:00
Fixed wrong paths
This commit is contained in:
84
src/pocketmine/utils/Cache.php
Normal file
84
src/pocketmine/utils/Cache.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\utils;
|
||||
|
||||
|
||||
class Cache{
|
||||
public static $cached = array();
|
||||
|
||||
/**
|
||||
* Adds something to the cache
|
||||
*
|
||||
* @param string $identifier
|
||||
* @param mixed $blob
|
||||
* @param float|int $minTTL The data will remain cached for at least $minTTL seconds
|
||||
*/
|
||||
public static function add($identifier, $blob, $minTTL = 30){
|
||||
self::$cached[$identifier] = array($blob, microtime(true) + $minTTL, $minTTL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get something from the cache
|
||||
*
|
||||
* @param $identifier
|
||||
*
|
||||
* @return bool|mixed Returns false if not found, otherwise it returns the data
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $identifier
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function exists($identifier){
|
||||
return isset(self::$cached[$identifier]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $identifier
|
||||
*/
|
||||
public static function remove($identifier){
|
||||
unset(self::$cached[$identifier]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a cache cleanup
|
||||
*/
|
||||
public static function cleanup(){
|
||||
$time = microtime(true);
|
||||
foreach(self::$cached as $index => $data){
|
||||
if($data[1] < $time){
|
||||
unset(self::$cached[$index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
385
src/pocketmine/utils/Config.php
Normal file
385
src/pocketmine/utils/Config.php
Normal file
@ -0,0 +1,385 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\utils;
|
||||
|
||||
|
||||
/**
|
||||
* Class Config
|
||||
*
|
||||
* Config Class for simple config manipulation of multiple formats.
|
||||
*/
|
||||
class Config{
|
||||
const DETECT = -1; //Detect by file extension
|
||||
const PROPERTIES = 0; // .properties
|
||||
const CNF = Config::PROPERTIES; // .cnf
|
||||
const JSON = 1; // .js, .json
|
||||
const YAML = 2; // .yml, .yaml
|
||||
//const EXPORT = 3; // .export, .xport
|
||||
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;
|
||||
|
||||
public static $formats = array(
|
||||
"properties" => Config::PROPERTIES,
|
||||
"cnf" => Config::CNF,
|
||||
"conf" => Config::CNF,
|
||||
"config" => Config::CNF,
|
||||
"json" => Config::JSON,
|
||||
"js" => Config::JSON,
|
||||
"yml" => Config::YAML,
|
||||
"yaml" => Config::YAML,
|
||||
//"export" => Config::EXPORT,
|
||||
//"xport" => Config::EXPORT,
|
||||
"sl" => Config::SERIALIZED,
|
||||
"serialize" => Config::SERIALIZED,
|
||||
"txt" => Config::ENUM,
|
||||
"list" => Config::ENUM,
|
||||
"enum" => Config::ENUM,
|
||||
);
|
||||
|
||||
/**
|
||||
* @param string $file Path of the file to be loaded
|
||||
* @param int $type Config type to load, -1 by default (detect)
|
||||
* @param array $default Array with the default values, will be set if not existent
|
||||
* @param null &$correct Sets correct to true if everything has been loaded correctly
|
||||
*/
|
||||
public function __construct($file, $type = Config::DETECT, $default = array(), &$correct = null){
|
||||
$this->load($file, $type, $default);
|
||||
$correct = $this->correct;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all the changes in memory and loads the file again
|
||||
*/
|
||||
public function reload(){
|
||||
unset($this->config);
|
||||
unset($this->correct);
|
||||
unset($this->type);
|
||||
$this->load($this->file);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $str
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function fixYAMLIndexes($str){
|
||||
return preg_replace("#^([ ]*)([a-zA-Z_]{1}[^\:]*)\:#m", "$1\"$2\":", $str);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
* @param int $type
|
||||
* @param array $default
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function load($file, $type = Config::DETECT, $default = array()){
|
||||
$this->correct = true;
|
||||
$this->type = (int) $type;
|
||||
$this->file = $file;
|
||||
if(!is_array($default)){
|
||||
$default = array();
|
||||
}
|
||||
if(!file_exists($file)){
|
||||
$this->config = $default;
|
||||
$this->save();
|
||||
}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{
|
||||
$this->correct = false;
|
||||
}
|
||||
}
|
||||
if($this->correct === true){
|
||||
$content = @file_get_contents($this->file);
|
||||
switch($this->type){
|
||||
case Config::PROPERTIES:
|
||||
case Config::CNF:
|
||||
$this->parseProperties($content);
|
||||
break;
|
||||
case Config::JSON:
|
||||
$this->config = @json_decode($content, true);
|
||||
break;
|
||||
case Config::YAML:
|
||||
$content = self::fixYAMLIndexes($content);
|
||||
$this->config = yaml_parse($content);
|
||||
break;
|
||||
case Config::SERIALIZED:
|
||||
$this->config = @unserialize($content);
|
||||
break;
|
||||
case Config::ENUM:
|
||||
$this->parseList($content);
|
||||
break;
|
||||
default:
|
||||
$this->correct = false;
|
||||
|
||||
return false;
|
||||
}
|
||||
if(!is_array($this->config)){
|
||||
$this->config = $default;
|
||||
}
|
||||
if($this->fillDefaults($default, $this->config) > 0){
|
||||
$this->save();
|
||||
}
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function check(){
|
||||
return $this->correct === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function save(){
|
||||
if($this->correct === true){
|
||||
$content = null;
|
||||
switch($this->type){
|
||||
case Config::PROPERTIES:
|
||||
case Config::CNF:
|
||||
$content = $this->writeProperties();
|
||||
break;
|
||||
case Config::JSON:
|
||||
$content = json_encode($this->config, JSON_PRETTY_PRINT | JSON_BIGINT_AS_STRING);
|
||||
break;
|
||||
case Config::YAML:
|
||||
$content = yaml_emit($this->config, YAML_UTF8_ENCODING);
|
||||
break;
|
||||
case Config::SERIALIZED:
|
||||
$content = @serialize($this->config);
|
||||
break;
|
||||
case Config::ENUM:
|
||||
$content = implode("\r\n", array_keys($this->config));
|
||||
break;
|
||||
}
|
||||
@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 string $k key to be set
|
||||
* @param bool $v value to set key
|
||||
*/
|
||||
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 array $defaults
|
||||
*/
|
||||
public function setDefaults(array $defaults){
|
||||
$this->fillDefaults($defaults, $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";
|
||||
foreach($this->config as $k => $v){
|
||||
if(is_bool($v) === true){
|
||||
$v = $v === true ? "on" : "off";
|
||||
}elseif(is_array($v)){
|
||||
$v = implode(";", $v);
|
||||
}
|
||||
$content .= $k . "=" . $v . "\r\n";
|
||||
}
|
||||
|
||||
return $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]);
|
||||
switch(strtolower($v)){
|
||||
case "on":
|
||||
case "true":
|
||||
case "yes":
|
||||
$v = true;
|
||||
break;
|
||||
case "off":
|
||||
case "false":
|
||||
case "no":
|
||||
$v = false;
|
||||
break;
|
||||
}
|
||||
if(isset($this->config[$k])){
|
||||
console("[NOTICE] [Config] Repeated property " . $k . " on file " . $this->file, true, true, 2);
|
||||
}
|
||||
$this->config[$k] = $v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
122
src/pocketmine/utils/Random.php
Normal file
122
src/pocketmine/utils/Random.php
Normal file
@ -0,0 +1,122 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\utils;
|
||||
|
||||
|
||||
/**
|
||||
* Unsecure Random Number Generator, used for fast seeded values
|
||||
*/
|
||||
class Random{
|
||||
private $z, $w;
|
||||
|
||||
/**
|
||||
* @param int|bool $seed Integer to be used as seed. If false, generates a Random one
|
||||
*/
|
||||
public function __construct($seed = false){
|
||||
$this->setSeed($seed);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|bool $seed Integer to be used as seed. If false, generates a Random one
|
||||
*/
|
||||
public function setSeed($seed = false){
|
||||
$seed = $seed !== false ? (int) $seed : Utils::readInt(Utils::getRandomBytes(4, false));
|
||||
$this->z = $seed ^ 0xdeadbeef;
|
||||
$this->w = $seed ^ 0xc0de1337;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an 31-bit integer (not signed)
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function nextInt(){
|
||||
return Utils::readInt($this->nextBytes(4)) & 0x7FFFFFFF;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a 32-bit integer (signed)
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function nextSignedInt(){
|
||||
return Utils::readInt($this->nextBytes(4));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a float between 0.0 and 1.0 (inclusive)
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function nextFloat(){
|
||||
return $this->nextInt() / 0x7FFFFFFF;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a float between -1.0 and 1.0 (inclusive)
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function nextSignedFloat(){
|
||||
return $this->nextSignedInt() / 0x7FFFFFFF;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns $byteCount random bytes
|
||||
*
|
||||
* @param $byteCount
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function nextBytes($byteCount){
|
||||
$bytes = "";
|
||||
while(strlen($bytes) < $byteCount){
|
||||
$this->z = 36969 * ($this->z & 65535) + ($this->z >> 16);
|
||||
$this->w = 18000 * ($this->w & 65535) + ($this->w >> 16);
|
||||
$bytes .= pack("N", ($this->z << 16) + $this->w);
|
||||
}
|
||||
|
||||
return substr($bytes, 0, $byteCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random boolean
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function nextBoolean(){
|
||||
return ($this->nextSignedInt() & 0x01) === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random integer between $start and $end
|
||||
*
|
||||
* @param int $start default 0
|
||||
* @param int $end default PHP_INT_MAX
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function nextRange($start = 0, $end = PHP_INT_MAX){
|
||||
return $start + ($this->nextInt() % ($end + 1 - $start));
|
||||
}
|
||||
|
||||
}
|
281
src/pocketmine/utils/TextFormat.php
Normal file
281
src/pocketmine/utils/TextFormat.php
Normal file
@ -0,0 +1,281 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\utils;
|
||||
|
||||
|
||||
/**
|
||||
* Class used to handle Minecraft chat format, and convert it to other formats like ANSI or HTML
|
||||
*/
|
||||
class TextFormat{
|
||||
const BLACK = "§0";
|
||||
const DARK_BLUE = "§1";
|
||||
const DARK_GREEN = "§2";
|
||||
const DARK_AQUA = "§3";
|
||||
const DARK_RED = "§4";
|
||||
const DARK_PURPLE = "§5";
|
||||
const GOLD = "§6";
|
||||
const GRAY = "§7";
|
||||
const DARK_GRAY = "§8";
|
||||
const BLUE = "§9";
|
||||
const GREEN = "§a";
|
||||
const AQUA = "§b";
|
||||
const RED = "§c";
|
||||
const LIGHT_PURPLE = "§d";
|
||||
const YELLOW = "§e";
|
||||
const WHITE = "§f";
|
||||
|
||||
const OBFUSCATED = "§k";
|
||||
const BOLD = "§l";
|
||||
const STRIKETHROUGH = "§m";
|
||||
const UNDERLINE = "§n";
|
||||
const ITALIC = "§o";
|
||||
const RESET = "§r";
|
||||
|
||||
/**
|
||||
* Splits the string by Format tokens
|
||||
*
|
||||
* @param string $string
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function tokenize($string){
|
||||
return preg_split("/(§[0123456789abcdefklmnor])/", $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans the string from Minecraft codes and ANSI Escape Codes
|
||||
*
|
||||
* @param string $string
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function clean($string){
|
||||
return preg_replace(array("/§[0123456789abcdefklmnor]/", "/\\x1b*/"), "", $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an HTML-formatted string with colors/markup
|
||||
*
|
||||
* @param string|array $string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function toHTML($string){
|
||||
if(!is_array($string)){
|
||||
$string = self::tokenize($string);
|
||||
}
|
||||
$newString = "";
|
||||
$tokens = 0;
|
||||
foreach($string as $token){
|
||||
switch($token){
|
||||
case TextFormat::BOLD:
|
||||
$newString .= "<span style=font-weight:bold>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::OBFUSCATED:
|
||||
//$newString .= "<span style=text-decoration:line-through>";
|
||||
//++$tokens;
|
||||
break;
|
||||
case TextFormat::ITALIC:
|
||||
$newString .= "<span style=font-style:italic>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::UNDERLINE:
|
||||
$newString .= "<span style=text-decoration:underline>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::STRIKETHROUGH:
|
||||
$newString .= "<span style=text-decoration:line-through>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::RESET:
|
||||
$newString .= str_repeat("</span>", $tokens);
|
||||
$tokens = 0;
|
||||
break;
|
||||
|
||||
//Colors
|
||||
case TextFormat::BLACK:
|
||||
$newString .= "<span style=color:#000>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::DARK_BLUE:
|
||||
$newString .= "<span style=color:#00A>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::DARK_GREEN:
|
||||
$newString .= "<span style=color:#0A0>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::DARK_AQUA:
|
||||
$newString .= "<span style=color:#0AA>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::DARK_RED:
|
||||
$newString .= "<span style=color:#A00>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::DARK_PURPLE:
|
||||
$newString .= "<span style=color:#A0A>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::GOLD:
|
||||
$newString .= "<span style=color:#FA0>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::GRAY:
|
||||
$newString .= "<span style=color:#AAA>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::DARK_GRAY:
|
||||
$newString .= "<span style=color:#555>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::BLUE:
|
||||
$newString .= "<span style=color:#55F>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::GREEN:
|
||||
$newString .= "<span style=color:#5F5>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::AQUA:
|
||||
$newString .= "<span style=color:#5FF>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::RED:
|
||||
$newString .= "<span style=color:#F55>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::LIGHT_PURPLE:
|
||||
$newString .= "<span style=color:#F5F>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::YELLOW:
|
||||
$newString .= "<span style=color:#FF5>";
|
||||
++$tokens;
|
||||
break;
|
||||
case TextFormat::WHITE:
|
||||
$newString .= "<span style=color:#FFF>";
|
||||
++$tokens;
|
||||
break;
|
||||
default:
|
||||
$newString .= $token;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$newString .= str_repeat("</span>", $tokens);
|
||||
|
||||
return $newString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string with colorized ANSI Escape codes
|
||||
*
|
||||
* @param $string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function toANSI($string){
|
||||
if(!is_array($string)){
|
||||
$string = self::tokenize($string);
|
||||
}
|
||||
$newString = "";
|
||||
foreach($string as $token){
|
||||
switch($token){
|
||||
case TextFormat::BOLD:
|
||||
break;
|
||||
case TextFormat::OBFUSCATED:
|
||||
$newString .= "\x1b[8m";
|
||||
break;
|
||||
case TextFormat::ITALIC:
|
||||
$newString .= "\x1b[3m";
|
||||
break;
|
||||
case TextFormat::UNDERLINE:
|
||||
$newString .= "\x1b[4m";
|
||||
break;
|
||||
case TextFormat::STRIKETHROUGH:
|
||||
$newString .= "\x1b[9m";
|
||||
break;
|
||||
case TextFormat::RESET:
|
||||
$newString .= "\x1b[0m";
|
||||
break;
|
||||
|
||||
//Colors
|
||||
case TextFormat::BLACK:
|
||||
$newString .= "\x1b[30m";
|
||||
break;
|
||||
case TextFormat::DARK_BLUE:
|
||||
$newString .= "\x1b[34m";
|
||||
break;
|
||||
case TextFormat::DARK_GREEN:
|
||||
$newString .= "\x1b[32m";
|
||||
break;
|
||||
case TextFormat::DARK_AQUA:
|
||||
$newString .= "\x1b[36m";
|
||||
break;
|
||||
case TextFormat::DARK_RED:
|
||||
$newString .= "\x1b[31m";
|
||||
break;
|
||||
case TextFormat::DARK_PURPLE:
|
||||
$newString .= "\x1b[35m";
|
||||
break;
|
||||
case TextFormat::GOLD:
|
||||
$newString .= "\x1b[33m";
|
||||
break;
|
||||
case TextFormat::GRAY:
|
||||
$newString .= "\x1b[37m";
|
||||
break;
|
||||
case TextFormat::DARK_GRAY:
|
||||
$newString .= "\x1b[30;1m";
|
||||
break;
|
||||
case TextFormat::BLUE:
|
||||
$newString .= "\x1b[34;1m";
|
||||
break;
|
||||
case TextFormat::GREEN:
|
||||
$newString .= "\x1b[32;1m";
|
||||
break;
|
||||
case TextFormat::AQUA:
|
||||
$newString .= "\x1b[36;1m";
|
||||
break;
|
||||
case TextFormat::RED:
|
||||
$newString .= "\x1b[31;1m";
|
||||
break;
|
||||
case TextFormat::LIGHT_PURPLE:
|
||||
$newString .= "\x1b[35;1m";
|
||||
break;
|
||||
case TextFormat::YELLOW:
|
||||
$newString .= "\x1b[33;1m";
|
||||
break;
|
||||
case TextFormat::WHITE:
|
||||
$newString .= "\x1b[37;1m";
|
||||
break;
|
||||
default:
|
||||
$newString .= $token;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $newString;
|
||||
}
|
||||
|
||||
}
|
829
src/pocketmine/utils/Utils.php
Normal file
829
src/pocketmine/utils/Utils.php
Normal file
@ -0,0 +1,829 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Various Utilities used around the code
|
||||
*/
|
||||
namespace pocketmine\utils;
|
||||
|
||||
use pocketmine\item\Item;
|
||||
|
||||
/**
|
||||
* Class Utils
|
||||
* Big collection of functions
|
||||
*
|
||||
* @package PocketMine\Utils
|
||||
*/
|
||||
class Utils{
|
||||
const BIG_ENDIAN = 0x00;
|
||||
const LITTLE_ENDIAN = 0x01;
|
||||
|
||||
|
||||
public static $online = true;
|
||||
public static $ip = false;
|
||||
|
||||
/**
|
||||
* Generates an unique identifier to a callable
|
||||
*
|
||||
* @param callable $variable
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getCallableIdentifier(callable $variable){
|
||||
if(is_array($variable)){
|
||||
return sha1(strtolower(spl_object_hash($variable[0])) . "::" . strtolower($variable[1]));
|
||||
}else{
|
||||
return sha1(strtolower($variable));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets this machine / server instance unique ID
|
||||
* Returns a hash, the first 32 characters (or 16 if raw)
|
||||
* will be an identifier that won't change frequently.
|
||||
* The rest of the hash will change depending on other factors.
|
||||
*
|
||||
* @param bool $raw default false, if true, returns the raw identifier, not hexadecimal
|
||||
* @param string $extra optional, additional data to identify the machine
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getUniqueID($raw = false, $extra = ""){
|
||||
$machine = php_uname("a");
|
||||
$machine .= file_exists("/proc/cpuinfo") ? file_get_contents("/proc/cpuinfo") : "";
|
||||
$machine .= sys_get_temp_dir();
|
||||
$machine .= $extra;
|
||||
if(Utils::getOS() == "win"){
|
||||
exec("ipconfig /ALL", $mac);
|
||||
$mac = implode("\n", $mac);
|
||||
if(preg_match_all("#Physical Address[. ]{1,}: ([0-9A-F\-]{17})#", $mac, $matches)){
|
||||
foreach($matches[1] as $i => $v){
|
||||
if($v == "00-00-00-00-00-00"){
|
||||
unset($matches[1][$i]);
|
||||
}
|
||||
}
|
||||
$machine .= implode(" ", $matches[1]); //Mac Addresses
|
||||
}
|
||||
}
|
||||
$data = $machine . PHP_MAXPATHLEN;
|
||||
$data .= PHP_INT_MAX;
|
||||
$data .= PHP_INT_SIZE;
|
||||
$data .= get_current_user();
|
||||
foreach(get_loaded_extensions() as $ext){
|
||||
$data .= $ext . ":" . phpversion($ext);
|
||||
}
|
||||
|
||||
return hash("md5", $machine, $raw) . hash("sha512", $data, $raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the External IP using an external service, it is cached
|
||||
*
|
||||
* @param bool $force default false, force IP check even when cached
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public static function getIP($force = false){
|
||||
if(Utils::$online === false){
|
||||
return false;
|
||||
}elseif(Utils::$ip !== false and $force !== true){
|
||||
return Utils::$ip;
|
||||
}
|
||||
$ip = trim(strip_tags(Utils::getURL("http://checkip.dyndns.org/")));
|
||||
if(preg_match('#Current IP Address\: ([0-9a-fA-F\:\.]*)#', $ip, $matches) > 0){
|
||||
Utils::$ip = $matches[1];
|
||||
}else{
|
||||
$ip = Utils::getURL("http://www.checkip.org/");
|
||||
if(preg_match('#">([0-9a-fA-F\:\.]*)</span>#', $ip, $matches) > 0){
|
||||
Utils::$ip = $matches[1];
|
||||
}else{
|
||||
$ip = Utils::getURL("http://checkmyip.org/");
|
||||
if(preg_match('#Your IP address is ([0-9a-fA-F\:\.]*)#', $ip, $matches) > 0){
|
||||
Utils::$ip = $matches[1];
|
||||
}else{
|
||||
$ip = trim(Utils::getURL("http://ifconfig.me/ip"));
|
||||
if($ip != ""){
|
||||
Utils::$ip = $ip;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Utils::$ip;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current Operating System
|
||||
* Windows => win
|
||||
* MacOS => mac
|
||||
* iOS => ios
|
||||
* Android => android
|
||||
* Linux => Linux
|
||||
* BSD => bsd
|
||||
* Other => other
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a prettified hexdump
|
||||
*
|
||||
* @param string $bin
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function hexdump($bin){
|
||||
$output = "";
|
||||
$bin = str_split($bin, 16);
|
||||
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;
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a string that can be printed, replaces non-printable characters
|
||||
*
|
||||
* @param $str
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function printable($str){
|
||||
if(!is_string($str)){
|
||||
return gettype($str);
|
||||
}
|
||||
|
||||
return preg_replace('#([^\x20-\x7E])#', '.', $str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a 3-byte big-endian number
|
||||
*
|
||||
* @param $str
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function readTriad($str){
|
||||
list(, $unpacked) = @unpack("N", "\x00" . $str);
|
||||
|
||||
return $unpacked;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a 3-byte big-endian number
|
||||
*
|
||||
* @param $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function writeTriad($value){
|
||||
return substr(pack("N", $value), 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a coded metadata string
|
||||
* TODO: Replace and move this to entity
|
||||
*
|
||||
* @param $data
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function writeMetadata($data){
|
||||
$m = "";
|
||||
foreach($data as $bottom => $d){
|
||||
$m .= chr(($d["type"] << 5) | ($bottom & 0b00011111));
|
||||
switch($d["type"]){
|
||||
case 0:
|
||||
$m .= Utils::writeByte($d["value"]);
|
||||
break;
|
||||
case 1:
|
||||
$m .= Utils::writeLShort($d["value"]);
|
||||
break;
|
||||
case 2:
|
||||
$m .= Utils::writeLInt($d["value"]);
|
||||
break;
|
||||
case 3:
|
||||
$m .= Utils::writeLFloat($d["value"]);
|
||||
break;
|
||||
case 4:
|
||||
$m .= Utils::writeLShort(strlen($d["value"]));
|
||||
$m .= $data["value"];
|
||||
break;
|
||||
case 5:
|
||||
$m .= Utils::writeLShort($d["value"][0]);
|
||||
$m .= Utils::writeByte($d["value"][1]);
|
||||
$m .= Utils::writeLShort($d["value"][2]);
|
||||
break;
|
||||
case 6:
|
||||
for($i = 0; $i < 3; ++$i){
|
||||
$m .= Utils::writeLInt($d["value"][$i]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
$m .= "\x7f";
|
||||
|
||||
return $m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a Item to binary (short id, byte Count, short Damage)
|
||||
*
|
||||
* @param Item $item
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function writeSlot(Item $item){
|
||||
return Utils::writeShort($item->getID()) . chr($item->getCount()) . Utils::writeShort($item->getMetadata());
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a binary Item, returns an Item object
|
||||
*
|
||||
* @param $ob
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
|
||||
public static function readSlot($ob){
|
||||
$id = Utils::readShort($ob->get(2));
|
||||
$cnt = ord($ob->get(1));
|
||||
|
||||
return Item::get(
|
||||
$id,
|
||||
Utils::readShort($ob->get(2)),
|
||||
$cnt
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a metadata coded string
|
||||
* TODO: Change
|
||||
*
|
||||
* @param $value
|
||||
* @param bool $types
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function readMetadata($value, $types = false){
|
||||
$offset = 0;
|
||||
$m = array();
|
||||
$b = ord($value{$offset});
|
||||
++$offset;
|
||||
while($b !== 127 and isset($value{$offset})){
|
||||
$bottom = $b & 0x1F;
|
||||
$type = $b >> 5;
|
||||
switch($type){
|
||||
case 0:
|
||||
$r = Utils::readByte($value{$offset});
|
||||
++$offset;
|
||||
break;
|
||||
case 1:
|
||||
$r = Utils::readLShort(substr($value, $offset, 2));
|
||||
$offset += 2;
|
||||
break;
|
||||
case 2:
|
||||
$r = Utils::readLInt(substr($value, $offset, 4));
|
||||
$offset += 4;
|
||||
break;
|
||||
case 3:
|
||||
$r = Utils::readLFloat(substr($value, $offset, 4));
|
||||
$offset += 4;
|
||||
break;
|
||||
case 4:
|
||||
$len = Utils::readLShort(substr($value, $offset, 2));
|
||||
$offset += 2;
|
||||
$r = substr($value, $offset, $len);
|
||||
$offset += $len;
|
||||
break;
|
||||
case 5:
|
||||
$r = array();
|
||||
$r[] = Utils::readLShort(substr($value, $offset, 2));
|
||||
$offset += 2;
|
||||
$r[] = ord($value{$offset});
|
||||
++$offset;
|
||||
$r[] = Utils::readLShort(substr($value, $offset, 2));
|
||||
$offset += 2;
|
||||
break;
|
||||
case 6:
|
||||
$r = array();
|
||||
for($i = 0; $i < 3; ++$i){
|
||||
$r[] = Utils::readLInt(substr($value, $offset, 4));
|
||||
$offset += 4;
|
||||
}
|
||||
break;
|
||||
|
||||
}
|
||||
if($types === true){
|
||||
$m[$bottom] = array($r, $type);
|
||||
}else{
|
||||
$m[$bottom] = $r;
|
||||
}
|
||||
$b = ord($value{$offset});
|
||||
++$offset;
|
||||
}
|
||||
|
||||
return $m;
|
||||
}
|
||||
|
||||
public static function readDataArray($str, $len = 10, &$offset = null){
|
||||
$data = array();
|
||||
$offset = 0;
|
||||
for($i = 1; $i <= $len and isset($str{$offset}); ++$i){
|
||||
$l = Utils::readTriad(substr($str, $offset, 3));
|
||||
$offset += 3;
|
||||
$data[] = substr($str, $offset, $l);
|
||||
$offset += $l;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function writeDataArray($data){
|
||||
$raw = "";
|
||||
foreach($data as $v){
|
||||
$raw .= Utils::writeTriad(strlen($v));
|
||||
$raw .= $v;
|
||||
}
|
||||
|
||||
return $raw;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function tries to get all the entropy available in PHP, and distills it to get a good RNG.
|
||||
*
|
||||
*
|
||||
* @param int $length default 16, Number of bytes to generate
|
||||
* @param bool $secure default true, Generate secure distilled bytes, slower
|
||||
* @param bool $raw default true, returns a binary string if true, or an hexadecimal one
|
||||
* @param string $startEntropy default null, adds more initial entropy
|
||||
* @param int &$rounds Will be set to the number of rounds taken
|
||||
* @param int &$drop Will be set to the amount of dropped bytes
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getRandomBytes($length = 16, $secure = true, $raw = true, $startEntropy = "", &$rounds = 0, &$drop = 0){
|
||||
static $lastRandom = "";
|
||||
$output = "";
|
||||
$length = abs((int) $length);
|
||||
$secureValue = "";
|
||||
$rounds = 0;
|
||||
$drop = 0;
|
||||
while(!isset($output{$length - 1})){
|
||||
//some entropy, but works ^^
|
||||
$weakEntropy = array(
|
||||
is_array($startEntropy) ? implode($startEntropy) : $startEntropy,
|
||||
serialize(@stat(__FILE__)),
|
||||
__DIR__,
|
||||
PHP_OS,
|
||||
microtime(),
|
||||
(string) lcg_value(),
|
||||
(string) PHP_MAXPATHLEN,
|
||||
PHP_SAPI,
|
||||
(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(),
|
||||
php_uname(),
|
||||
phpversion(),
|
||||
extension_loaded("gmp") ? gmp_strval(gmp_random(4)) : microtime(),
|
||||
zend_version(),
|
||||
(string) getmypid(),
|
||||
(string) getmyuid(),
|
||||
(string) mt_rand(),
|
||||
(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(),
|
||||
serialize(get_loaded_extensions()),
|
||||
sys_get_temp_dir(),
|
||||
(string) disk_free_space("."),
|
||||
(string) disk_total_space("."),
|
||||
uniqid(microtime(), true),
|
||||
file_exists("/proc/cpuinfo") ? file_get_contents("/proc/cpuinfo") : microtime(),
|
||||
);
|
||||
|
||||
shuffle($weakEntropy);
|
||||
$value = hash("sha512", implode($weakEntropy), true);
|
||||
$lastRandom .= $value;
|
||||
foreach($weakEntropy as $k => $c){ //mixing entropy values with XOR and hash randomness extractor
|
||||
$value ^= hash("sha256", $c . microtime() . $k, true) . hash("sha256", mt_rand() . microtime() . $k . $c, true);
|
||||
$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
|
||||
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;
|
||||
}
|
||||
$value = "";
|
||||
//Von Neumann randomness extractor, increases entropy
|
||||
$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)){
|
||||
$secureValue |= $b << $bitcnt;
|
||||
if($bitcnt == 7){
|
||||
$value .= chr($secureValue);
|
||||
$secureValue = 0;
|
||||
$bitcnt = 0;
|
||||
}else{
|
||||
++$bitcnt;
|
||||
}
|
||||
++$drop;
|
||||
}else{
|
||||
$drop += 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$output .= substr($value, 0, min($length - strlen($output), $length));
|
||||
unset($value);
|
||||
++$rounds;
|
||||
}
|
||||
$lastRandom = hash("sha512", $lastRandom, true);
|
||||
|
||||
return $raw === false ? bin2hex($output) : $output;
|
||||
}
|
||||
|
||||
/*
|
||||
public static function angle3D($pos1, $pos2){
|
||||
$X = $pos1["x"] - $pos2["x"];
|
||||
$Z = $pos1["z"] - $pos2["z"];
|
||||
$dXZ = sqrt(pow($X, 2) + pow($Z, 2));
|
||||
$Y = $pos1["y"] - $pos2["y"];
|
||||
$hAngle = rad2deg(atan2($Z, $X) - M_PI_2);
|
||||
$vAngle = rad2deg(-atan2($Y, $dXZ));
|
||||
|
||||
return array("yaw" => $hAngle, "pitch" => $vAngle);
|
||||
}*/
|
||||
|
||||
/**
|
||||
* GETs an URL using cURL
|
||||
*
|
||||
* @param $page
|
||||
* @param int $timeout default 10
|
||||
*
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public static function getURL($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;
|
||||
}
|
||||
|
||||
/**
|
||||
* POSTs data to an URL
|
||||
*
|
||||
* @param $page
|
||||
* @param array|string $args
|
||||
* @param int $timeout
|
||||
*
|
||||
* @return bool|mixed
|
||||
*/
|
||||
public static function postURL($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_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_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int) $timeout);
|
||||
$ret = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a byte boolean
|
||||
*
|
||||
* @param $b
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function readBool($b){
|
||||
return Utils::readByte($b, false) === 0 ? false : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a byte boolean
|
||||
*
|
||||
* @param $b
|
||||
*
|
||||
* @return bool|string
|
||||
*/
|
||||
public static function writeBool($b){
|
||||
return Utils::writeByte($b === true ? 1 : 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads an unsigned/signed byte
|
||||
*
|
||||
* @param $c
|
||||
* @param bool $signed
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes an unsigned/signed byte
|
||||
*
|
||||
* @param $c
|
||||
*
|
||||
* @return bool|string
|
||||
*/
|
||||
public static function writeByte($c){
|
||||
if($c > 0xff){
|
||||
return false;
|
||||
}
|
||||
if($c < 0 and $c >= -0x80){
|
||||
$c = 0xff + $c + 1;
|
||||
}
|
||||
|
||||
return chr($c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a 16-bit signed/unsigned big-endian number
|
||||
*
|
||||
* @param $str
|
||||
* @param bool $signed
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a 16-bit signed/unsigned big-endian number
|
||||
*
|
||||
* @param $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function writeShort($value){
|
||||
if($value < 0){
|
||||
$value += 0x10000;
|
||||
}
|
||||
|
||||
return pack("n", $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a 16-bit signed/unsigned little-endian number
|
||||
*
|
||||
* @param $str
|
||||
* @param bool $signed
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a 16-bit signed/unsigned little-endian number
|
||||
*
|
||||
* @param $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function writeLShort($value){
|
||||
if($value < 0){
|
||||
$value += 0x10000;
|
||||
}
|
||||
|
||||
return pack("v", $value);
|
||||
}
|
||||
|
||||
public static function readInt($str){
|
||||
list(, $unpacked) = @unpack("N", $str);
|
||||
if($unpacked > 2147483647){
|
||||
$unpacked -= 4294967296;
|
||||
}
|
||||
|
||||
return (int) $unpacked;
|
||||
}
|
||||
|
||||
public static function writeInt($value){
|
||||
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){
|
||||
return pack("V", $value);
|
||||
}
|
||||
|
||||
public static function readFloat($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));
|
||||
}
|
||||
|
||||
public static function readLFloat($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);
|
||||
}
|
||||
|
||||
public static function printFloat($value){
|
||||
return preg_replace("/(\.\d+?)0+$/", "$1", sprintf("%F", $value));
|
||||
}
|
||||
|
||||
public static function readDouble($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));
|
||||
}
|
||||
|
||||
public static function readLDouble($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);
|
||||
}
|
||||
|
||||
public static function readLong($x, $signed = true){
|
||||
$value = "0";
|
||||
if($signed === true){
|
||||
$negative = ((ord($x{0}) & 0x80) === 0x80) ? true : false;
|
||||
if($negative){
|
||||
$x = ~$x;
|
||||
}
|
||||
}else{
|
||||
$negative = false;
|
||||
}
|
||||
|
||||
for($i = 0; $i < 8; $i += 4){
|
||||
$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);
|
||||
}
|
||||
|
||||
public static function writeLong($value){
|
||||
$x = "";
|
||||
if($value{0} === "-"){
|
||||
$negative = true;
|
||||
$value = bcadd($value, "1");
|
||||
if($value{0} === "-"){
|
||||
$value = substr($value, 1);
|
||||
}
|
||||
}else{
|
||||
$negative = false;
|
||||
}
|
||||
while(bccomp($value, "0", 0) > 0){
|
||||
$temp = bcmod($value, "16777216");
|
||||
$x = chr($temp >> 16) . chr($temp >> 8) . chr($temp) . $x;
|
||||
$value = bcdiv($value, "16777216", 0);
|
||||
}
|
||||
$x = str_pad(substr($x, 0, 8), 8, "\x00", STR_PAD_LEFT);
|
||||
if($negative === true){
|
||||
$x = ~$x;
|
||||
}
|
||||
|
||||
return $x;
|
||||
}
|
||||
|
||||
public static function readLLong($str){
|
||||
return Utils::readLong(strrev($str));
|
||||
}
|
||||
|
||||
public static function writeLLong($value){
|
||||
return strrev(Utils::writeLong($value));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
define("pocketmine\\utils\\ENDIANNESS", (pack("d", 1) === "\77\360\0\0\0\0\0\0" ? Utils::BIG_ENDIAN : Utils::LITTLE_ENDIAN));
|
113
src/pocketmine/utils/VersionString.php
Normal file
113
src/pocketmine/utils/VersionString.php
Normal file
@ -0,0 +1,113 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
namespace pocketmine\utils;
|
||||
|
||||
|
||||
/**
|
||||
* Manages PocketMine-MP version strings, and compares them
|
||||
*/
|
||||
class VersionString{
|
||||
public static $stageOrder = array(
|
||||
"alpha" => 0,
|
||||
"a" => 0,
|
||||
"beta" => 1,
|
||||
"b" => 1,
|
||||
"final" => 2,
|
||||
"f" => 2,
|
||||
);
|
||||
private $stage;
|
||||
private $major;
|
||||
private $release;
|
||||
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{
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
public function getNumber(){
|
||||
return (int) (VersionString::$stageOrder[$this->stage] << 13) + ($this->generation << 9) + ($this->major << 5) + $this->minor;
|
||||
}
|
||||
|
||||
public function getStage(){
|
||||
return $this->stage;
|
||||
}
|
||||
|
||||
public function getGeneration(){
|
||||
return $this->generation;
|
||||
}
|
||||
|
||||
public function getMajor(){
|
||||
return $this->major;
|
||||
}
|
||||
|
||||
public function getMinor(){
|
||||
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 compare($target, $diff = false){
|
||||
if(($target instanceof VersionString) === false){
|
||||
$target = new VersionString($target);
|
||||
}
|
||||
$number = $this->getNumber();
|
||||
$tNumber = $target->getNumber();
|
||||
if($diff === true){
|
||||
return $tNumber - $number;
|
||||
}
|
||||
if($number > $tNumber){
|
||||
return -1; //Target is older
|
||||
}elseif($number < $tNumber){
|
||||
return 1; //Target is newer
|
||||
}else{
|
||||
return 0; //Same version
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user