Added new text formatting system for future updates [like vanilla PC]

This commit is contained in:
Shoghi Cervantes 2013-09-04 19:49:19 +02:00
parent 56d829eec6
commit 809076b7d7
9 changed files with 170 additions and 33 deletions

View File

@ -128,7 +128,7 @@ class ConsoleAPI{
$max = ceil(count($cmds) / 5);
$page = (int) (isset($params[0]) ? min($max, max(1, intval($params[0]))):1);
$output .= "- Showing help page $page of $max (/help <page>) -\n";
$output .= "\x1b[31;1m-\x1b[0m Showing help page $page of $max (/help <page>) \x1b[31;1m-\x1b[0m\n";
$current = 1;
foreach($cmds as $c => $h){
$curpage = (int) ceil($current / 5);
@ -175,9 +175,9 @@ class ConsoleAPI{
return $this->run($this->alias[$cmd] . ($params !== "" ? " " .$params:""), $issuer, $cmd);
}
if($issuer instanceof Player){
console("[DEBUG] \x1b[33m".$issuer->username."\x1b[0m issued server command: ".ltrim("$alias ")."/$cmd ".$params, true, true, 2);
console("[DEBUG] ".FORMAT_AQUA.$issuer->username.FORMAT_RESET." issued server command: ".ltrim("$alias ")."/$cmd ".$params, true, true, 2);
}else{
console("[DEBUG] \x1b[33m*".$issuer."\x1b[0m issued server command: ".ltrim("$alias ")."/$cmd ".$params, true, true, 2);
console("[DEBUG] ".FORMAT_YELLOW."*".$issuer.FORMAT_RESET." issued server command: ".ltrim("$alias ")."/$cmd ".$params, true, true, 2);
}
if(preg_match_all('#@([@a-z]{1,})#', $params, $matches, PREG_OFFSET_CAPTURE) > 0){

View File

@ -79,7 +79,7 @@ class PluginAPI extends stdClass{
console("[ERROR] Failed parsing of ".basename($file));
return false;
}
console("[INFO] Loading plugin \"\x1b[32m".$info["name"]."\x1b[0m\" \x1b[35m".$info["version"]." \x1b[0mby \x1b[36m".$info["author"]."\x1b[0m");
console("[INFO] Loading plugin \"".FORMAT_GREEN.$info["name"].FORMAT_RESET."\" ".FORMAT_AQUA.$info["version"].FORMAT_RESET." by ".FORMAT_AQUA.$info["author"].FORMAT_RESET);
if($info["class"] !== "none" and class_exists($info["class"])){
console("[ERROR] Failed loading plugin: class already exists");
return false;
@ -98,7 +98,7 @@ class PluginAPI extends stdClass{
if($info["class"] !== "none"){
$object = new $className($this->server->api, false);
if(!($object instanceof Plugin)){
console("[ERROR] Plugin \"\x1b[36m".$info["name"]."\x1b[0m\" doesn't use the Plugin Interface");
console("[ERROR] Plugin \"".$info["name"]."\" doesn't use the Plugin Interface");
if(method_exists($object, "__destruct")){
$object->__destruct();
}

View File

@ -48,7 +48,7 @@ class ServerAPI{
@mkdir(DATA_PATH."worlds/", 0755);
@mkdir(DATA_PATH."plugins/", 0755);
$version = new VersionString();
console("[INFO] Starting Minecraft PE server version \x1b[36;1m".CURRENT_MINECRAFT_VERSION);
console("[INFO] Starting Minecraft PE server version ".FORMAT_AQUA.CURRENT_MINECRAFT_VERSION);
console("[INFO] Loading properties...");
$this->config = new Config(DATA_PATH . "server.properties", CONFIG_PROPERTIES, array(
@ -92,7 +92,7 @@ class ServerAPI{
$this->server = new PocketMinecraftServer($this->getProperty("server-name"), $this->getProperty("gamemode"), ($seed = $this->getProperty("level-seed")) != "" ? (int) $seed:false, $this->getProperty("server-port"), ($ip = $this->getProperty("server-ip")) != "" ? $ip:"0.0.0.0");
$this->server->api = $this;
self::$serverRequest = $this->server;
console("[INFO] This server is running PocketMine-MP version ".($version->isDev() ? "\x1b[31;1m":"").MAJOR_VERSION."\x1b[0m (MCPE: ".CURRENT_MINECRAFT_VERSION.") (API ".CURRENT_API_VERSION.")", true, true, 0);
console("[INFO] This server is running PocketMine-MP version ".($version->isDev() ? FORMAT_YELLOW:"").MAJOR_VERSION.FORMAT_RESET." (MCPE: ".CURRENT_MINECRAFT_VERSION.") (API ".CURRENT_API_VERSION.")", true, true, 0);
console("[INFO] PocketMine-MP is distibuted under the LGPL License", true, true, 0);
if($this->getProperty("upnp-forwarding") === true){
@ -102,7 +102,7 @@ class ServerAPI{
if($this->getProperty("last-update") === false or ($this->getProperty("last-update") + 3600) < time()){
console("[INFO] Checking for new server version");
console("[INFO] Last check: \x1b[36m".date("Y-m-d H:i:s", $this->getProperty("last-update"))."\x1b[0m");
console("[INFO] Last check: ".FORMAT_AQUA.date("Y-m-d H:i:s", $this->getProperty("last-update"))."\x1b[0m");
$info = json_decode(Utils::curl_get("http://www.pocketmine.net/latest"), true);
if($this->server->version->isDev()){
if($info === false or !isset($info["development"])){
@ -110,13 +110,13 @@ class ServerAPI{
}else{
$last = $info["development"]["date"];
if($last >= $this->getProperty("last-update") and $this->getProperty("last-update") !== false and GIT_COMMIT != $info["development"]["commit"]){
console("[NOTICE] \x1b[33mA new DEVELOPMENT version of PocketMine-MP has been released");
console("[NOTICE] \x1b[33mVersion \"".$info["development"]["version"]."\" [".substr($info["development"]["commit"], 0, 10)."]");
console("[NOTICE] \x1b[36mGet it at PocketMine.net or ".$info["development"]["download"]);
console("[NOTICE] ".FORMAT_YELLOW."A new DEVELOPMENT version of PocketMine-MP has been released");
console("[NOTICE] ".FORMAT_YELLOW."Version \"".$info["development"]["version"]."\" [".substr($info["development"]["commit"], 0, 10)."]");
console("[NOTICE] ".FORMAT_YELLOW."Get it at PocketMine.net or ".$info["development"]["download"]);
console("[NOTICE] This message will dissapear after issuing the command \"/update-done\"");
}else{
$this->setProperty("last-update", time());
console("[INFO] \x1b[36mThis is the latest DEVELOPMENT version");
console("[INFO] ".FORMAT_AQUA."This is the latest DEVELOPMENT version");
}
}
}else{
@ -128,13 +128,13 @@ class ServerAPI{
$update = new VersionString($info["stable"]["version"]);
$updateN = $update->getNumber();
if($updateN > $newestN){
console("[NOTICE] \x1b[33mA new STABLE version of PocketMine-MP has been released");
console("[NOTICE] \x1b[36mVersion \"".$info["stable"]["version"]."\" #".$updateN);
console("[NOTICE] ".FORMAT_GREEN."A new STABLE version of PocketMine-MP has been released");
console("[NOTICE] ".FORMAT_GREEN."Version \"".$info["stable"]["version"]."\" #".$updateN);
console("[NOTICE] Get it at PocketMine.net or ".$info["stable"]["download"]);
console("[NOTICE] This message will dissapear as soon as you update");
}else{
$this->setProperty("last-update", time());
console("[INFO] \x1b[36mThis is the latest STABLE version");
console("[INFO] ".FORMAT_AQUA."This is the latest STABLE version");
}
}
}

View File

@ -280,7 +280,7 @@ class Player{
if($msg === true and $this->username != ""){
$this->server->api->chat->broadcast($this->username." left the game");
}
console("[INFO] \x1b[33m".$this->username."\x1b[0m[/".$this->ip.":".$this->port."] logged out due to ".$reason);
console("[INFO] ".FORMAT_AQUA.$this->username.FORMAT_RESET."[/".$this->ip.":".$this->port."] logged out due to ".$reason);
$this->windows = array();
$this->armor = array();
$this->inventory = array();
@ -1265,7 +1265,7 @@ class Player{
$this->evid[] = $this->server->event("tile.update", array($this, "eventHandler"));
$this->lastMeasure = microtime(true);
$this->server->schedule(50, array($this, "measureLag"), array(), true);
console("[INFO] \x1b[33m".$this->username."\x1b[0m[/".$this->ip.":".$this->port."] logged in with entity id ".$this->eid." at (".$this->entity->level->getName().", ".round($this->entity->x, 2).", ".round($this->entity->y, 2).", ".round($this->entity->z, 2).")");
console("[INFO] ".FORMAT_AQUA.$this->username.FORMAT_RESET."[/".$this->ip.":".$this->port."] logged in with entity id ".$this->eid." at (".$this->entity->level->getName().", ".round($this->entity->x, 2).", ".round($this->entity->y, 2).", ".round($this->entity->z, 2).")");
break;
case MC_READY:
if($this->loggedIn === false){
@ -1284,7 +1284,7 @@ class Player{
$this->server->api->entity->spawnToAll($this->entity);
$this->server->schedule(5, array($this->entity, "update"), array(), true);
$this->server->schedule(2, array($this->entity, "checkMovement"), array(), true);
$this->server->schedule(2, array($this->entity, "updateMovement"), array(), true);
$this->sendArmor();
$this->sendChat($this->server->motd."\n");

View File

@ -26,9 +26,9 @@ class PocketMinecraftServer{
private function load(){
$this->version = new VersionString();
if(defined("DEBUG") and DEBUG >= 0){
/*if(defined("DEBUG") and DEBUG >= 0){
@cli_set_process_title("PocketMine-MP ".MAJOR_VERSION);
}
}*/
console("[INFO] Starting Minecraft PE server on ".($this->serverip === "0.0.0.0" ? "*":$this->serverip).":".$this->port);
define("BOOTUP_RANDOM", Utils::getRandomBytes(16));
$this->serverID = $this->serverID === false ? Utils::readLong(Utils::getRandomBytes(8, false)):$this->serverID;
@ -89,7 +89,7 @@ class PocketMinecraftServer{
public function titleTick(){
$time = microtime(true);
if(defined("DEBUG") and DEBUG >= 0 and ENABLE_ANSI === true){
echo "\x1b]0;PocketMine-MP ".MAJOR_VERSION." | Online ". count($this->clients)."/".$this->maxClients." | RAM ".round((memory_get_usage() / 1024) / 1024, 2)."MB | U ".round(($this->interface->bandwidth[1] / max(1, $time - $this->interface->bandwidth[2])) / 1024, 2)." D ".round(($this->interface->bandwidth[0] / max(1, $time - $this->interface->bandwidth[2])) / 1024, 2)." kB/s | TPS ".$this->getTPS()."\x07";
cli_set_process_title("PocketMine-MP ".MAJOR_VERSION." | Online ". count($this->clients)."/".$this->maxClients." | RAM ".round((memory_get_usage() / 1024) / 1024, 2)."MB | U ".round(($this->interface->bandwidth[1] / max(1, $time - $this->interface->bandwidth[2])) / 1024, 2)." D ".round(($this->interface->bandwidth[0] / max(1, $time - $this->interface->bandwidth[2])) / 1024, 2)." kB/s | TPS ".$this->getTPS());
}
$this->interface->bandwidth = array(0, 0, $time);
}

View File

@ -21,6 +21,7 @@
/***REM_START***/
require_once(dirname(__FILE__)."/config.php");
require_once(FILE_PATH."/src/utils/TextFormat.php");
require_once(FILE_PATH."/src/functions.php");
/***REM_END***/
define(DATA_PATH, realpath(arg("data-path", FILE_PATH))."/");

View File

@ -182,7 +182,7 @@ function arguments ( $args ){
function console($message, $EOL = true, $log = true, $level = 1){
if(!defined("DEBUG") or DEBUG >= $level){
$message .= $EOL === true ? PHP_EOL:"";
$time = (ENABLE_ANSI === true ? "\x1b[36m".date("H:i:s")."\x1b[0m":date("H:i:s")) . " ";
$time = (ENABLE_ANSI === true ? FORMAT_AQUA . date("H:i:s") . FORMAT_RESET:date("H:i:s")) . " ";
$replaced = preg_replace('/\x1b\[[0-9;]*m/', "", $time . $message);
if($log === true and (!defined("LOG") or LOG === true)){
logg(date("Y-m-d")." ".$replaced, "console", false, $level);
@ -190,27 +190,26 @@ function console($message, $EOL = true, $log = true, $level = 1){
if(ENABLE_ANSI === true){
$add = "";
if(preg_match("/\[([a-zA-Z0-9]*)\]/", $message, $matches) > 0){
$add .= "\x1b";
switch($matches[1]){
case "ERROR":
$add .= "[31;1m";
$add .= FORMAT_RED;
break;
case "INTERNAL":
case "DEBUG":
$add .= "[30;1m";
$add .= FORMAT_GRAY;
break;
case "WARNING":
$add .= "[33;1m";
$add .= FORMAT_YELLOW;
break;
case "NOTICE":
$add .= "[37;1m";
$add .= FORMAT_AQUA;
break;
default:
$add = "";
break;
}
}
$message = $time . $add . $message . "\x1b[0m";
$message = TextFormat::toANSI($time . $add . $message . FORMAT_RESET);
}else{
$message = $replaced;
}

View File

@ -4,9 +4,9 @@
function testCase($name, $output, $expected){
global $testErrors;
if($output === $expected){
console("[TEST] $name: \x1b[32mOk.");
console("[TEST] $name: ".FORMAT_GREEN."Ok.");
}else{
console("[TEST] $name: \x1b[31mError.");
console("[TEST] $name: ".FORMAT_RED."Error.");
console("Expected ".print_r($expected, true).", got ".print_r($output, true));
++$testErrors;
}
@ -17,15 +17,15 @@
require_once(dirname(__FILE__)."/../dependencies.php");
require_once(FILE_PATH."/src/functions.php");
require_once(FILE_PATH."/src/dependencies.php");
console("\x1b[36m[TEST] Starting tests");
console(FORMAT_GREEN . "[TEST] Starting tests");
testCase("dummy", dummy(), null);
$t = new ServerSuiteTest;
echo PHP_EOL;
if($testErrors === 0){
console("\x1b[32m[TEST] No errors. Test complete.");
console(FORMAT_GREEN . "[TEST] No errors. Test complete.");
exit(0);
}else{
console("\x1b[31m[TEST] Errors found.");
console(FORMAT_RED . "[TEST] Errors found.");
exit(1);
}
}

137
src/utils/TextFormat.php Normal file
View File

@ -0,0 +1,137 @@
<?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");
define("FORMAT_DARK_AQUA", "§3");
define("FORMAT_DARK_RED", "§4");
define("FORMAT_DARK_PURPLE", "§5");
define("FORMAT_GOLD", "§6");
define("FORMAT_GRAY", "§7");
define("FORMAT_DARK_GRAY", "§8");
define("FORMAT_BLUE", "§9");
define("FORMAT_GREEN", "§a");
define("FORMAT_AQUA", "§b");
define("FORMAT_RED", "§c");
define("FORMAT_LIGHT_PURPLE", "§d");
define("FORMAT_YELLOW", "§e");
define("FORMAT_WHITE", "§f");
define("FORMAT_OBFUSCATED", "§k");
define("FORMAT_BOLD", "§l");
define("FORMAT_STRIKETHROUGH", "§m");
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);
}
public static function clean($string){
return preg_replace("/§[0123456789abcdefklmnor]/", "", $string);
}
public static function toANSI($string){
if(!is_array($string)){
$string = self::tokenize($string);
}
$newString = "";
foreach($string as $token){
switch($token){
case FORMAT_BOLD:
break;
case FORMAT_OBFUSCATED:
$newString .= "\x1b[8m";
break;
case FORMAT_ITALIC:
$newString .= "\x1b[3m";
break;
case FORMAT_UNDERLINE:
$newString .= "\x1b[4m";
break;
case FORMAT_STRIKETHROUGH:
$newString .= "\x1b[9m";
break;
case FORMAT_RESET:
$newString .= "\x1b[0m";
break;
//Colors
case FORMAT_BLACK:
$newString .= "\x1b[30m";
break;
case FORMAT_DARK_BLUE:
$newString .= "\x1b[34m";
break;
case FORMAT_DARK_GREEN:
$newString .= "\x1b[32m";
break;
case FORMAT_DARK_AQUA:
$newString .= "\x1b[36m";
break;
case FORMAT_DARK_RED:
$newString .= "\x1b[31m";
break;
case FORMAT_DARK_PURPLE:
$newString .= "\x1b[35m";
break;
case FORMAT_GOLD:
$newString .= "\x1b[33m";
break;
case FORMAT_GRAY:
$newString .= "\x1b[37m";
break;
case FORMAT_DARK_GRAY:
$newString .= "\x1b[30;1m";
break;
case FORMAT_BLUE:
$newString .= "\x1b[34;1m";
break;
case FORMAT_GREEN:
$newString .= "\x1b[32;1m";
break;
case FORMAT_AQUA:
$newString .= "\x1b[36;1m";
break;
case FORMAT_RED:
$newString .= "\x1b[31;1m";
break;
case FORMAT_LIGHT_PURPLE:
$newString .= "\x1b[35;1m";
break;
case FORMAT_YELLOW:
$newString .= "\x1b[33;1m";
break;
case FORMAT_WHITE:
$newString .= "\x1b[37;1m";
break;
default:
$newString .= $token;
break;
}
}
return $newString;
}
}