Cleaned up SetupWizard and fixed crash when pressing CTRL+C during setup

This commit is contained in:
Dylan K. Taylor 2017-06-13 17:33:52 +01:00
parent e96fa8b682
commit 5011198a4e

View File

@ -46,41 +46,40 @@ class SetupWizard{
} }
public function run(){ public function run(){
echo "[*] PocketMine-MP set-up wizard\n"; $this->message("PocketMine-MP set-up wizard");
$langs = BaseLang::getLanguageList(); $langs = BaseLang::getLanguageList();
if(empty($langs)){ if(empty($langs)){
echo "[!] No language files found, please use provided builds or clone the repository recursively." . PHP_EOL; $this->error("No language files found, please use provided builds or clone the repository recursively.");
return false; return false;
} }
echo "[*] Please select a language:\n"; $this->message("Please select a language");
foreach($langs as $short => $native){ foreach($langs as $short => $native){
echo " $native => $short\n"; $this->writeLine(" $native => $short");
} }
do{ do{
echo "[?] Language (eng): "; $lang = strtolower($this->getInput("Language", "eng"));
$lang = strtolower($this->getInput("eng"));
if(!isset($langs[$lang])){ if(!isset($langs[$lang])){
echo "[!] Couldn't find the language\n"; $this->error("Couldn't find the language");
$lang = null; $lang = null;
} }
}while($lang === null); }while($lang === null);
$this->lang = new BaseLang($lang); $this->lang = new BaseLang($lang);
$this->message($this->lang->get("language_has_been_selected"));
echo "[*] " . $this->lang->get("language_has_been_selected") . "\n";
if(!$this->showLicense()){ if(!$this->showLicense()){
return false; return false;
} }
echo "[?] " . $this->lang->get("skip_installer") . " (y/N): "; if(strtolower($this->getInput($this->lang->get("skip_installer"), "n", "y/N")) === "y"){
if(strtolower($this->getInput()) === "y"){
return true; return true;
} }
echo "\n";
$this->writeLine();
$this->welcome(); $this->welcome();
$this->generateBaseConfig(); $this->generateBaseConfig();
$this->generateUserFiles(); $this->generateUserFiles();
@ -88,11 +87,12 @@ class SetupWizard{
$this->networkFunctions(); $this->networkFunctions();
$this->endWizard(); $this->endWizard();
return true; return true;
} }
private function showLicense(){ private function showLicense(){
echo $this->lang->get("welcome_to_pocketmine") . "\n"; $this->message($this->lang->get("welcome_to_pocketmine"));
echo <<<LICENSE echo <<<LICENSE
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
@ -101,9 +101,9 @@ class SetupWizard{
(at your option) any later version. (at your option) any later version.
LICENSE; LICENSE;
echo "\n[?] " . $this->lang->get("accept_license") . " (y/N): "; $this->writeLine();
if(strtolower($this->getInput("n")) != "y"){ if(strtolower($this->getInput($this->lang->get("accept_license"), "n", "y/N")) !== "y"){
echo "[!] " . $this->lang->get("you_have_to_accept_the_license") . "\n"; $this->error($this->lang->get("you_have_to_accept_the_license"));
sleep(5); sleep(5);
return false; return false;
@ -113,61 +113,67 @@ LICENSE;
} }
private function welcome(){ private function welcome(){
echo "[*] " . $this->lang->get("setting_up_server_now") . "\n"; $this->message($this->lang->get("setting_up_server_now"));
echo "[*] " . $this->lang->get("default_values_info") . "\n"; $this->message($this->lang->get("default_values_info"));
echo "[*] " . $this->lang->get("server_properties") . "\n"; $this->message($this->lang->get("server_properties"));
} }
private function generateBaseConfig(){ private function generateBaseConfig(){
$config = new Config(\pocketmine\DATA . "server.properties", Config::PROPERTIES); $config = new Config(\pocketmine\DATA . "server.properties", Config::PROPERTIES);
echo "[?] " . $this->lang->get("name_your_server") . " (" . self::DEFAULT_NAME . "): ";
$config->set("motd", ($name = $this->getInput(self::DEFAULT_NAME))); $config->set("motd", ($name = $this->getInput($this->lang->get("name_your_server"), self::DEFAULT_NAME)));
$config->set("server-name", $name); $config->set("server-name", $name);
echo "[*] " . $this->lang->get("port_warning") . "\n";
$this->message($this->lang->get("port_warning"));
do{ do{
echo "[?] " . $this->lang->get("server_port") . " (" . self::DEFAULT_PORT . "): "; $port = (int) $this->getInput($this->lang->get("server_port"), (string) self::DEFAULT_PORT);
$port = (int) $this->getInput(self::DEFAULT_PORT);
if($port <= 0 or $port > 65535){ if($port <= 0 or $port > 65535){
echo "[!] " . $this->lang->get("invalid_port") . "\n"; $this->error($this->lang->get("invalid_port"));
continue;
} }
}while($port <= 0 or $port > 65535);
break;
}while(true);
$config->set("server-port", $port); $config->set("server-port", $port);
echo "[*] " . $this->lang->get("gamemode_info") . "\n"; $this->message($this->lang->get("gamemode_info"));
do{ do{
echo "[?] " . $this->lang->get("default_gamemode") . ": (" . self::DEFAULT_GAMEMODE . "): "; $gamemode = (int) $this->getInput($this->lang->get("default_gamemode"), (string) self::DEFAULT_GAMEMODE);
$gamemode = (int) $this->getInput(self::DEFAULT_GAMEMODE);
}while($gamemode < 0 or $gamemode > 3); }while($gamemode < 0 or $gamemode > 3);
$config->set("gamemode", $gamemode); $config->set("gamemode", $gamemode);
echo "[?] " . $this->lang->get("max_players") . " (" . self::DEFAULT_PLAYERS . "): ";
$config->set("max-players", (int) $this->getInput(self::DEFAULT_PLAYERS)); $config->set("max-players", (int) $this->getInput($this->lang->get("max_players"), (string) self::DEFAULT_PLAYERS));
echo "[*] " . $this->lang->get("spawn_protection_info") . "\n";
echo "[?] " . $this->lang->get("spawn_protection") . " (Y/n): "; $this->message($this->lang->get("spawn_protection_info"));
if(strtolower($this->getInput("y")) == "n"){
if(strtolower($this->getInput($this->lang->get("spawn_protection"), "y", "Y/n")) === "n"){
$config->set("spawn-protection", -1); $config->set("spawn-protection", -1);
}else{ }else{
$config->set("spawn-protection", 16); $config->set("spawn-protection", 16);
} }
$config->save(); $config->save();
} }
private function generateUserFiles(){ private function generateUserFiles(){
echo "[*] " . $this->lang->get("op_info") . "\n"; $this->message($this->lang->get("op_info"));
echo "[?] " . $this->lang->get("op_who") . ": ";
$op = strtolower($this->getInput("")); $op = strtolower($this->getInput($this->lang->get("op_who"), ""));
if($op === ""){ if($op === ""){
echo "[!] " . $this->lang->get("op_warning") . "\n"; $this->error($this->lang->get("op_warning"));
}else{ }else{
$ops = new Config(\pocketmine\DATA . "ops.txt", Config::ENUM); $ops = new Config(\pocketmine\DATA . "ops.txt", Config::ENUM);
$ops->set($op, true); $ops->set($op, true);
$ops->save(); $ops->save();
} }
echo "[*] " . $this->lang->get("whitelist_info") . "\n";
echo "[?] " . $this->lang->get("whitelist_enable") . " (y/N): "; $this->message($this->lang->get("whitelist_info"));
$config = new Config(\pocketmine\DATA . "server.properties", Config::PROPERTIES); $config = new Config(\pocketmine\DATA . "server.properties", Config::PROPERTIES);
if(strtolower($this->getInput("n")) === "y"){ if(strtolower($this->getInput($this->lang->get("whitelist_enable"), "n", "y/N")) === "y"){
echo "[!] " . $this->lang->get("whitelist_warning") . "\n"; $this->error($this->lang->get("whitelist_warning"));
$config->set("white-list", true); $config->set("white-list", true);
}else{ }else{
$config->set("white-list", false); $config->set("white-list", false);
@ -177,55 +183,78 @@ LICENSE;
private function networkFunctions(){ private function networkFunctions(){
$config = new Config(\pocketmine\DATA . "server.properties", Config::PROPERTIES); $config = new Config(\pocketmine\DATA . "server.properties", Config::PROPERTIES);
echo "[!] " . $this->lang->get("query_warning1") . "\n"; $this->error($this->lang->get("query_warning1"));
echo "[!] " . $this->lang->get("query_warning2") . "\n"; $this->error($this->lang->get("query_warning2"));
echo "[?] " . $this->lang->get("query_disable") . " (y/N): "; if(strtolower($this->getInput($this->lang->get("query_disable"), "n", "y/N")) === "y"){
if(strtolower($this->getInput("n")) === "y"){
$config->set("enable-query", false); $config->set("enable-query", false);
}else{ }else{
$config->set("enable-query", true); $config->set("enable-query", true);
} }
echo "[*] " . $this->lang->get("rcon_info") . "\n"; $this->message($this->lang->get("rcon_info"));
echo "[?] " . $this->lang->get("rcon_enable") . " (y/N): "; if(strtolower($this->getInput($this->lang->get("rcon_enable"), "n", "y/N")) === "y"){
if(strtolower($this->getInput("n")) === "y"){
$config->set("enable-rcon", true); $config->set("enable-rcon", true);
$password = substr(base64_encode(random_bytes(20)), 3, 10); $password = substr(base64_encode(random_bytes(20)), 3, 10);
$config->set("rcon.password", $password); $config->set("rcon.password", $password);
echo "[*] " . $this->lang->get("rcon_password") . ": " . $password . "\n"; $this->message($this->lang->get("rcon_password") . ": " . $password);
}else{ }else{
$config->set("enable-rcon", false); $config->set("enable-rcon", false);
} }
/*echo "[*] " . $this->lang->usage_info . "\n";
echo "[?] " . $this->lang->usage_disable . " (y/N): ";
if(strtolower($this->getInput("n")) === "y"){
$config->set("send-usage", false);
}else{
$config->set("send-usage", true);
}*/
$config->save(); $config->save();
echo "[*] " . $this->lang->get("ip_get") . "\n"; $this->message($this->lang->get("ip_get"));
$externalIP = Utils::getIP(); $externalIP = Utils::getIP();
if($externalIP === false){
$externalIP = "unknown (server offline)";
}
$internalIP = gethostbyname(trim(`hostname`)); $internalIP = gethostbyname(trim(`hostname`));
echo "[!] " . $this->lang->translateString("ip_warning", ["EXTERNAL_IP" => $externalIP, "INTERNAL_IP" => $internalIP]) . "\n"; $this->error($this->lang->translateString("ip_warning", ["EXTERNAL_IP" => $externalIP, "INTERNAL_IP" => $internalIP]));
echo "[!] " . $this->lang->get("ip_confirm"); $this->error($this->lang->get("ip_confirm"));
$this->getInput(); $this->readLine();
} }
private function endWizard(){ private function endWizard(){
echo "[*] " . $this->lang->get("you_have_finished") . "\n"; $this->message($this->lang->get("you_have_finished"));
echo "[*] " . $this->lang->get("pocketmine_plugins") . "\n"; $this->message($this->lang->get("pocketmine_plugins"));
echo "[*] " . $this->lang->get("pocketmine_will_start") . "\n\n\n"; $this->message($this->lang->get("pocketmine_will_start"));
$this->writeLine();
$this->writeLine();
sleep(4); sleep(4);
} }
private function getInput($default = ""){ private function writeLine(string $line = ""){
$input = trim(fgets(STDIN)); echo $line . PHP_EOL;
}
private function readLine() : string{
return trim((string) fgets(STDIN));
}
private function message(string $message){
$this->writeLine("[*] " . $message);
}
private function error(string $message){
$this->writeLine("[!] " . $message);
}
private function getInput(string $message, string $default = "", string $options = ""){
$message = "[?] " . $message;
if($options !== "" or $default !== ""){
$message .= " (" . ($options === "" ? $default : $options) . ")";
}
$message .= ": ";
echo $message;
$input = $this->readLine();
return $input === "" ? $default : $input; return $input === "" ? $default : $input;
} }