From 00bd33abfb8c1c1f9f54091feb6e0e46c4e55e2a Mon Sep 17 00:00:00 2001 From: Sarjuuk Date: Mon, 7 Sep 2015 20:31:13 +0200 Subject: [PATCH] CLISetup/OSCompatibility - generally check for readline handler - only ignore \r for WIN --- includes/types/spell.class.php | 2 +- setup/tools/CLISetup.class.php | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/includes/types/spell.class.php b/includes/types/spell.class.php index e9c6120b..23c29b20 100644 --- a/includes/types/spell.class.php +++ b/includes/types/spell.class.php @@ -176,7 +176,7 @@ class SpellList extends BaseType if ($mv < 0) // all stats { for ($iMod = ITEM_MOD_AGILITY; $iMod <= ITEM_MOD_STAMINA; $iMod++) - Util::arraySumByKey($stats, [iMod => $pts]); + Util::arraySumByKey($stats, [$iMod => $pts]); } else if ($mv == STAT_STRENGTH) // one stat Util::arraySumByKey($stats, [ITEM_MOD_STRENGTH => $pts]); diff --git a/setup/tools/CLISetup.class.php b/setup/tools/CLISetup.class.php index 618b0bed..3705d3a6 100644 --- a/setup/tools/CLISetup.class.php +++ b/setup/tools/CLISetup.class.php @@ -23,6 +23,7 @@ class CLISetup const LOG_INFO = 3; private static $win = true; + private static $hasReadline = false; private static $logFile = ''; private static $logHandle = null; @@ -44,7 +45,8 @@ class CLISetup public static function init() { - self::$win = substr(PHP_OS, 0, 3) == 'WIN'; + self::$win = substr(PHP_OS, 0, 3) == 'WIN'; + self::$hasReadline = function_exists('readline_callback_handler_install'); if ($_ = getopt('d', ['log::', 'locales::', 'mpqDataDir::', 'delete'])) { @@ -342,8 +344,8 @@ class CLISetup public static function readInput(&$fields, $singleChar = false) { - // prevent default output on *nix (readline doen't exist for WIN) - if (!self::$win) + // prevent default output if able + if (self::$hasReadline) readline_callback_handler_install('', function() { }); foreach ($fields as $name => $data) @@ -366,13 +368,16 @@ class CLISetup $char = stream_get_contents(STDIN, 1); $keyId = ord($char); - if ($keyId == self::CHR_TAB) // ignore this one + // ignore this one + if ($keyId == self::CHR_TAB) continue; - if ($keyId == self::CHR_CR) // also ignore this bastard! + // WIN sends \r\n as sequence, ignore one + if ($keyId == self::CHR_CR && self::$win) continue; - if ($keyId == self::CHR_ESC) // will not be send on WIN .. other ways of returning from setup? (besides ctrl + c) + // will not be send on WIN .. other ways of returning from setup? (besides ctrl + c) + if ($keyId == self::CHR_ESC) { echo chr(self::CHR_BELL); return false; @@ -393,10 +398,10 @@ class CLISetup else if (!$validPattern || preg_match($validPattern, $char)) { $charBuff .= $char; - if (!$isHidden && !self::$win) // see note above + if (!$isHidden && self::$hasReadline) echo $char; - if ($singleChar && !self::$win) // see note above + if ($singleChar && self::$hasReadline) { $fields[$name] = $charBuff; break;