-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrapper.php
115 lines (107 loc) · 3.39 KB
/
wrapper.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
<?php
if(!extension_loaded("pthreads") or !extension_loaded("yaml")){
die("Please run this script using PocketMine PHP binaries.");
}
require_once "functions.php";
$targets = [
"PocketMine-MP.phar",
"src/pocketmine/PocketMine.php"
];
foreach($targets as $target){
if(is_file($target)){
$ok = true;
break;
}
}
if(!isset($ok, $target)){
console("[FATAL] No PocketMine installation (source folder or .phar) found in " . getcwd() . "!");
exit(2);
}
$args = isset($argv) ? $argv :
console("[WARNING] Not passing any command line arguments to PocketMine server. Please enable 'register_argc_argv' in " .
php_ini_loaded_file() . " if you want this script to pass your arguments into PocketMine.", []);
if(is_file($args[0]) and realpath($args[0]) === realpath(__FILE__)){
array_shift($args);
}
array_unshift($args, $target);
request_enter_num_times_restart:
console("[?] Please enter the number of times to restart the server (including the first time starting).");
console("[?] Enter 1 if you don't want the server to restart.");
console("[?] Enter 0 if you want the server to restart for infinite times.");
while(($line = nonBlockReadLine()) === null){
;
}
if(!is_numeric($line)){
console("[ERROR] Invalid input!");
goto request_enter_num_times_restart;
}
$restarts = (int) $line;
$restarts = ($restarts === 0) ? PHP_INT_MAX : $restarts;
while($restarts--){
console("Initializing server...");
$server = proc_open(PHP_BINARY . " " . implode(" ", $args), [0 => ["pipe", "r"], 1 => ["pipe", "w"], 2 => fopen("php://stderr", "wb")], $pipes);
if(!is_resource($server)){
console("[ERROR] Failed to start server once :(");
continue;
}
console("[*] The server has been started. Type '-die' to stop the server and prevent this script from restarting. Type '-kill' to terminate the server and stop this script.");
$reader = new StreamReader($pipes[1]);
while(proc_get_status($server)["running"]){
while($line = $reader->getLine()){
echo $line . PHP_EOL;
}
while(($line = nonBlockReadLine()) !== null){
if(strtolower($line) === "-mypid"){
console("Wrapper PID: " . getmypid());
continue;
}elseif(strtolower($line) === "-pmpid"){
console("PocketMine server PID: " . proc_get_status($server)["pid"]);
}elseif(strtolower($line) === "-die"){
$restarts = 0;
$line = "stop";
}elseif(strtolower($line) === "-kill"){
fclose($pipes[0]);
fclose($pipes[1]);
if(isset($pipes[2])){
fclose($pipes[2]);
}
$pid = proc_get_status($server)["pid"];
kill($pid);
console("Server killed");
proc_terminate($server);
console("Process terminated.");
// switch(CommandReader::getOS()){
// case "win":
// $tasks = `umic process where (ParentProcessId=$pid) get ProcessId`;
// foreach(explode(PHP_EOL, $tasks) as $child){
// if(is_numeric($child)){
// kill($child);
// }
// }
// break;
// default:
// exec("kill -9 -\$(ps -o pgid= $pid | grep -o '[0-9]*')");
// break;
// }
proc_close($server);
console("Process closed.");
exit(0);
}
fwrite($pipes[0], $line . PHP_EOL);
}
}
echo stream_get_contents($pipes[1]);
$reader->running = false;
$reader->join();
fclose($pipes[1]);
fclose($pipes[0]);
if(isset($pipes[2])){ // I am not sure if it exists
fclose($pipes[2]);
}
if(is_resource($server)){
proc_close($server);
}
console("[*] The server has been stopped.");
}
console("Finished looping.");
exit(0);