-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.php
246 lines (230 loc) · 5.94 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?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/
*/
/** @noinspection PhpMultipleClassesDeclarationsInOneFile */
class CommandReader extends Thread{
public static $os;
private $readline;
/** @var \Threaded */
protected $buffer;
public function __construct(){
$this->buffer = \ThreadedFactory::create();
$this->start();
}
private function readLine(){
if(!$this->readline){
$line = trim(fgets(fopen("php://stdin", "r")));
}else{
$line = trim(readline("> "));
if($line != ""){
readline_add_history($line);
}
}
return $line;
}
/**
* Reads a line from console, if available. Returns null if not available
*
* @return string|null
*/
public function getLine(){
if($this->buffer->count() !== 0){
return $this->buffer->shift();
}
return null;
}
public function run(){
$opts = getopt("", ["disable-readline"]);
if(extension_loaded("readline") and !isset($opts["disable-readline"])){
$this->readline = true;
}else{
$this->readline = false;
}
$lastLine = microtime(true);
while(true){
if(($line = $this->readLine()) !== ""){
$this->buffer[] = preg_replace("#\\x1b\\x5b([^\\x1b]*\\x7e|[\\x40-\\x50])#", "", $line);
}elseif((microtime(true) - $lastLine) <= 0.1){ //Non blocking! Sleep to save CPU
usleep(40000);
}
$lastLine = microtime(true);
}
}
public function getThreadName(){
return "Console";
}
public static function getOS($recalculate = false){
if(self::$os === null or $recalculate){
$uname = php_uname("s");
if(stripos($uname, "Darwin") !== false){
if(strpos(php_uname("m"), "iP") === 0){
self::$os = "ios";
}else{
self::$os = "mac";
}
}elseif(stripos($uname, "Win") !== false or $uname === "Msys"){
self::$os = "win";
}elseif(stripos($uname, "Linux") !== false){
if(@file_exists("/system/build.prop")){
self::$os = "android";
}else{
self::$os = "linux";
}
}elseif(stripos($uname, "BSD") !== false or $uname === "DragonFly"){
self::$os = "bsd";
}else{
self::$os = "other";
}
}
return self::$os;
}
}
/** @noinspection PhpMultipleClassesDeclarationsInOneFile */
class ThreadedFactory{
/** @var ThreadedFactory */
protected static $instance;
/** @var \Threaded[] */
protected $threadedList = [];
protected function __construct(){
global $threadedFactoryInstance;
$threadedFactoryInstance = $this;
self::$instance = $this;
}
/**
* @return ThreadedFactory
*/
public static function getInstance(){
if(self::$instance === null){
global $threadedFactoryInstance;
if($threadedFactoryInstance instanceof ThreadedFactory){
self::$instance = $threadedFactoryInstance;
}else{
new ThreadedFactory();
}
}
return self::$instance;
}
/**
* @param \Threaded $class
* @param ...$arguments
*
* @return \Threaded
*/
public static function create($class = \Threaded::class, ...$arguments){
/** @var \Threaded $threaded */
$threaded = new $class(...$arguments);
self::getInstance()->threadedList[spl_object_hash($threaded)] = $threaded;
return $threaded;
}
public static function destroy(\Threaded $threaded){
$instance = self::getInstance();
if(isset($instance->threadedList[$hash = spl_object_hash($threaded)])){
$threaded->synchronized(function(\Threaded $t){
$t->notify();
}, $threaded);
unset($instance->threadedList[$hash]);
return true;
}
return false;
}
/**
* @return \Threaded[]
*/
public static function all(){
return self::getInstance()->threadedList;
}
}
/** @noinspection PhpMultipleClassesDeclarationsInOneFile */
class StreamReader extends Thread{
public static $os;
/** @var \Threaded */
protected $buffer;
private $res;
public $running = true;
public function __construct($resource){
$this->buffer = \ThreadedFactory::create();
$this->res = $resource;
$this->start();
}
private function readLine(){
$line = rtrim(fgets($this->res), "\r\n");
return $line;
}
/**
* Reads a line from console, if available. Returns null if not available
*
* @return string|null
*/
public function getLine(){
if($this->buffer->count() !== 0){
return $this->buffer->shift();
}
return null;
}
public function run(){
$lastLine = microtime(true);
while($this->running){
if(($line = $this->readLine()) !== ""){
$this->buffer[] = preg_replace("#\\x1b\\x5b([^\\x1b]*\\x7e|[\\x40-\\x50])#", "", $line);
}elseif((microtime(true) - $lastLine) <= 0.1){ //Non blocking! Sleep to save CPU
usleep(40000);
}
$lastLine = microtime(true);
}
}
public function getThreadName(){
return "Console";
}
public static function getOS($recalculate = false){
if(self::$os === null or $recalculate){
$uname = php_uname("s");
if(stripos($uname, "Darwin") !== false){
if(strpos(php_uname("m"), "iP") === 0){
self::$os = "ios";
}else{
self::$os = "mac";
}
}elseif(stripos($uname, "Win") !== false or $uname === "Msys"){
self::$os = "win";
}elseif(stripos($uname, "Linux") !== false){
if(@file_exists("/system/build.prop")){
self::$os = "android";
}else{
self::$os = "linux";
}
}elseif(stripos($uname, "BSD") !== false or $uname === "DragonFly"){
self::$os = "bsd";
}else{
self::$os = "other";
}
}
return self::$os;
}
}
function kill($pid){
switch(CommandReader::getOS()){
case "win":
exec("taskkill.exe /F /PID " . ((int) $pid) . " > NUL");
break;
case "mac":
case "linux":
default:
exec("kill -9 " . ((int) $pid) . " > /dev/null 2>&1");
}
}
function console($msg, $ret = null, $eol = true){
echo date("[H:i:s] ") . $msg . ($eol ? PHP_EOL : "");
return $ret;
}
$cmdReader = new CommandReader();
function nonBlockReadLine(){
global $cmdReader;
return $cmdReader->getLine();
}