mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-08 10:53:05 +00:00
Moved console-specific stuff to its own namespace
this stuff has different functionality than everything else in the command namespace (specifically console handling), so it doesn't belong in here. I know that this will probably break some plugins, but I don't care, because plugins shouldn't have been abusing ConsoleCommandSender in the first place.
This commit is contained in:
80
src/console/CommandReader.php
Normal file
80
src/console/CommandReader.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\console;
|
||||
|
||||
use function fclose;
|
||||
use function fgets;
|
||||
use function fopen;
|
||||
use function is_resource;
|
||||
use function stream_select;
|
||||
use function trim;
|
||||
use function usleep;
|
||||
|
||||
final class CommandReader{
|
||||
/** @var resource */
|
||||
private $stdin;
|
||||
|
||||
public function __construct(){
|
||||
$this->initStdin();
|
||||
}
|
||||
|
||||
private function initStdin() : void{
|
||||
if(is_resource($this->stdin)){
|
||||
fclose($this->stdin);
|
||||
}
|
||||
|
||||
$this->stdin = fopen("php://stdin", "r");
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a line from the console and adds it to the buffer. This method may block the thread.
|
||||
*/
|
||||
public function readLine() : ?string{
|
||||
if(!is_resource($this->stdin)){
|
||||
$this->initStdin();
|
||||
}
|
||||
|
||||
$r = [$this->stdin];
|
||||
$w = $e = null;
|
||||
if(($count = stream_select($r, $w, $e, 0, 200000)) === 0){ //nothing changed in 200000 microseconds
|
||||
return null;
|
||||
}elseif($count === false){ //stream error
|
||||
$this->initStdin();
|
||||
}
|
||||
|
||||
if(($raw = fgets($this->stdin)) === false){ //broken pipe or EOF
|
||||
$this->initStdin();
|
||||
usleep(200000); //prevent CPU waste if it's end of pipe
|
||||
return null; //loop back round
|
||||
}
|
||||
|
||||
$line = trim($raw);
|
||||
|
||||
return $line !== "" ? $line : null;
|
||||
}
|
||||
|
||||
public function __destruct(){
|
||||
fclose($this->stdin);
|
||||
}
|
||||
}
|
82
src/console/CommandReaderThread.php
Normal file
82
src/console/CommandReaderThread.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\console;
|
||||
|
||||
use pocketmine\snooze\SleeperNotifier;
|
||||
use pocketmine\thread\Thread;
|
||||
use pocketmine\thread\ThreadException;
|
||||
use function microtime;
|
||||
use function preg_replace;
|
||||
use function usleep;
|
||||
|
||||
final class CommandReaderThread extends Thread{
|
||||
private \Threaded $buffer;
|
||||
private ?SleeperNotifier $notifier;
|
||||
|
||||
public bool $shutdown = false;
|
||||
|
||||
public function __construct(\Threaded $buffer, ?SleeperNotifier $notifier = null){
|
||||
$this->buffer = $buffer;
|
||||
$this->notifier = $notifier;
|
||||
}
|
||||
|
||||
public function shutdown() : void{
|
||||
$this->shutdown = true;
|
||||
}
|
||||
|
||||
public function quit() : void{
|
||||
$wait = microtime(true) + 0.5;
|
||||
while(microtime(true) < $wait){
|
||||
if($this->isRunning()){
|
||||
usleep(100000);
|
||||
}else{
|
||||
parent::quit();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
throw new ThreadException("CommandReader is stuck in a blocking STDIN read");
|
||||
}
|
||||
|
||||
protected function onRun() : void{
|
||||
$buffer = $this->buffer;
|
||||
$notifier = $this->notifier;
|
||||
|
||||
$reader = new CommandReader();
|
||||
while(!$this->shutdown){
|
||||
$line = $reader->readLine();
|
||||
|
||||
if($line !== null){
|
||||
$buffer[] = preg_replace("#\\x1b\\x5b([^\\x1b]*\\x7e|[\\x40-\\x50])#", "", $line);
|
||||
if($notifier !== null){
|
||||
$notifier->wakeupSleeper();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getThreadName() : string{
|
||||
return "Console";
|
||||
}
|
||||
}
|
91
src/console/ConsoleCommandSender.php
Normal file
91
src/console/ConsoleCommandSender.php
Normal file
@ -0,0 +1,91 @@
|
||||
<?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/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\console;
|
||||
|
||||
use pocketmine\command\CommandSender;
|
||||
use pocketmine\lang\Language;
|
||||
use pocketmine\lang\TranslationContainer;
|
||||
use pocketmine\permission\DefaultPermissions;
|
||||
use pocketmine\permission\PermissibleBase;
|
||||
use pocketmine\permission\PermissibleDelegateTrait;
|
||||
use pocketmine\Server;
|
||||
use function explode;
|
||||
use function trim;
|
||||
use const PHP_INT_MAX;
|
||||
|
||||
class ConsoleCommandSender implements CommandSender{
|
||||
use PermissibleDelegateTrait;
|
||||
|
||||
/** @var Server */
|
||||
private $server;
|
||||
/** @var int|null */
|
||||
protected $lineHeight = null;
|
||||
/** @var Language */
|
||||
private $language;
|
||||
|
||||
public function __construct(Server $server, Language $language){
|
||||
$this->server = $server;
|
||||
$this->perm = new PermissibleBase([DefaultPermissions::ROOT_CONSOLE => true]);
|
||||
$this->language = $language;
|
||||
}
|
||||
|
||||
public function getServer() : Server{
|
||||
return $this->server;
|
||||
}
|
||||
|
||||
public function getLanguage() : Language{
|
||||
return $this->language;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TranslationContainer|string $message
|
||||
*/
|
||||
public function sendMessage($message) : void{
|
||||
$server = $this->getServer();
|
||||
if($message instanceof TranslationContainer){
|
||||
$message = $this->getLanguage()->translate($message);
|
||||
}else{
|
||||
$message = $this->getLanguage()->translateString($message);
|
||||
}
|
||||
|
||||
foreach(explode("\n", trim($message)) as $line){
|
||||
$server->getLogger()->info($line);
|
||||
}
|
||||
}
|
||||
|
||||
public function getName() : string{
|
||||
return "CONSOLE";
|
||||
}
|
||||
|
||||
public function getScreenLineHeight() : int{
|
||||
return $this->lineHeight ?? PHP_INT_MAX;
|
||||
}
|
||||
|
||||
public function setScreenLineHeight(?int $height) : void{
|
||||
if($height !== null and $height < 1){
|
||||
throw new \InvalidArgumentException("Line height must be at least 1");
|
||||
}
|
||||
$this->lineHeight = $height;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user