[1312] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[1313] | 29 | #include "Shell.h" |
---|
| 30 | #include "CommandExecutor.h" |
---|
| 31 | #include "CoreIncludes.h" |
---|
| 32 | #include "ConfigValueIncludes.h" |
---|
[1326] | 33 | #include "CoreSettings.h" |
---|
[1424] | 34 | #include "ConsoleCommand.h" |
---|
[1312] | 35 | |
---|
[1313] | 36 | #define SHELL_UPDATE_LISTENERS(function) \ |
---|
| 37 | for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) \ |
---|
| 38 | (*it)->function() |
---|
[1312] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
[1424] | 42 | SetConsoleCommand(Shell, clearShell, true); |
---|
| 43 | SetConsoleCommand(Shell, history, true); |
---|
| 44 | |
---|
[1312] | 45 | Shell::Shell() |
---|
| 46 | { |
---|
[1313] | 47 | RegisterRootObject(Shell); |
---|
| 48 | |
---|
| 49 | this->scrollPosition_ = 0; |
---|
| 50 | this->maxHistoryLength_ = 100; |
---|
| 51 | this->historyPosition_ = 0; |
---|
| 52 | this->historyOffset_ = 0; |
---|
[1322] | 53 | this->finishedLastLine_ = true; |
---|
[1334] | 54 | this->bAddOutputLevel_ = false; |
---|
[1313] | 55 | |
---|
| 56 | this->clearLines(); |
---|
[1322] | 57 | |
---|
[1312] | 58 | this->inputBuffer_.registerListener(this, &Shell::inputChanged, true); |
---|
| 59 | this->inputBuffer_.registerListener(this, &Shell::execute, '\r', false); |
---|
| 60 | this->inputBuffer_.registerListener(this, &Shell::hintandcomplete, '\t', true); |
---|
| 61 | this->inputBuffer_.registerListener(this, &Shell::backspace, '\b', true); |
---|
[1313] | 62 | this->inputBuffer_.registerListener(this, &Shell::deletechar, OIS::KC_DELETE); |
---|
[1312] | 63 | this->inputBuffer_.registerListener(this, &Shell::exit, (char)27, true); |
---|
| 64 | this->inputBuffer_.registerListener(this, &Shell::cursor_right, OIS::KC_RIGHT); |
---|
| 65 | this->inputBuffer_.registerListener(this, &Shell::cursor_left, OIS::KC_LEFT); |
---|
[1313] | 66 | this->inputBuffer_.registerListener(this, &Shell::cursor_end, OIS::KC_END); |
---|
| 67 | this->inputBuffer_.registerListener(this, &Shell::cursor_home, OIS::KC_HOME); |
---|
[1312] | 68 | this->inputBuffer_.registerListener(this, &Shell::history_up, OIS::KC_UP); |
---|
| 69 | this->inputBuffer_.registerListener(this, &Shell::history_down, OIS::KC_DOWN); |
---|
| 70 | this->inputBuffer_.registerListener(this, &Shell::scroll_up, OIS::KC_PGUP); |
---|
| 71 | this->inputBuffer_.registerListener(this, &Shell::scroll_down, OIS::KC_PGDOWN); |
---|
[1322] | 72 | |
---|
| 73 | this->outputBuffer_.registerListener(this); |
---|
| 74 | |
---|
[1313] | 75 | this->setConfigValues(); |
---|
[1312] | 76 | } |
---|
| 77 | |
---|
[1326] | 78 | Shell& Shell::createShell() |
---|
[1312] | 79 | { |
---|
[1326] | 80 | int level = CoreSettings::getSoftDebugLevel(OutputHandler::LD_Shell); |
---|
| 81 | CoreSettings::setSoftDebugLevel(OutputHandler::LD_Shell, -1); |
---|
[1313] | 82 | static Shell instance; |
---|
[1326] | 83 | CoreSettings::setSoftDebugLevel(OutputHandler::LD_Shell, level); |
---|
[1312] | 84 | return instance; |
---|
| 85 | } |
---|
| 86 | |
---|
[1326] | 87 | Shell& Shell::getInstance() |
---|
| 88 | { |
---|
| 89 | static Shell& instance = createShell(); |
---|
| 90 | return instance; |
---|
| 91 | } |
---|
| 92 | |
---|
[1312] | 93 | void Shell::setConfigValues() |
---|
[1313] | 94 | { |
---|
| 95 | SetConfigValue(maxHistoryLength_, 100); |
---|
| 96 | SetConfigValue(historyOffset_, 0); |
---|
[1325] | 97 | SetConfigValueVector(commandHistory_, std::vector<std::string>()); |
---|
[1312] | 98 | |
---|
[1313] | 99 | if (this->historyOffset_ >= this->maxHistoryLength_) |
---|
| 100 | this->historyOffset_ = 0; |
---|
| 101 | |
---|
| 102 | while (this->commandHistory_.size() > this->maxHistoryLength_) |
---|
| 103 | { |
---|
| 104 | unsigned int index = this->commandHistory_.size() - 1; |
---|
| 105 | this->commandHistory_.erase(this->commandHistory_.begin() + index); |
---|
| 106 | ModifyConfigValue(commandHistory_, remove, index); |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
[1424] | 110 | void Shell::clearShell() |
---|
| 111 | { |
---|
| 112 | Shell::getInstance().clearLines(); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | void Shell::history() |
---|
| 116 | { |
---|
| 117 | Shell& instance = Shell::getInstance(); |
---|
| 118 | |
---|
| 119 | for (int i = instance.historyOffset_; i < (int)instance.commandHistory_.size(); ++i) |
---|
| 120 | instance.addLine(instance.commandHistory_[i], -1); |
---|
| 121 | for (int i = 0; i < (int)instance.historyOffset_; ++i) |
---|
| 122 | instance.addLine(instance.commandHistory_[i], -1); |
---|
| 123 | } |
---|
| 124 | |
---|
[1313] | 125 | void Shell::registerListener(ShellListener* listener) |
---|
| 126 | { |
---|
| 127 | this->listeners_.insert(this->listeners_.end(), listener); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | void Shell::unregisterListener(ShellListener* listener) |
---|
| 131 | { |
---|
| 132 | for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ) |
---|
| 133 | { |
---|
| 134 | if ((*it) == listener) |
---|
| 135 | this->listeners_.erase(it++); |
---|
| 136 | else |
---|
| 137 | ++it; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | void Shell::setCursorPosition(unsigned int cursor) |
---|
| 142 | { |
---|
| 143 | this->inputBuffer_.setCursorPosition(cursor); |
---|
| 144 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | void Shell::setInput(const std::string& input) |
---|
| 148 | { |
---|
| 149 | this->inputBuffer_.set(input); |
---|
| 150 | this->inputChanged(); |
---|
| 151 | } |
---|
| 152 | |
---|
[1334] | 153 | void Shell::addLine(const std::string& line, int level) |
---|
[1313] | 154 | { |
---|
[1322] | 155 | int original_level = OutputHandler::getOutStream().getOutputLevel(); |
---|
| 156 | OutputHandler::getOutStream().setOutputLevel(level); |
---|
| 157 | |
---|
| 158 | if (!this->finishedLastLine_) |
---|
| 159 | this->outputBuffer_ << std::endl; |
---|
| 160 | |
---|
| 161 | this->outputBuffer_ << line << std::endl; |
---|
| 162 | OutputHandler::getOutStream().setOutputLevel(original_level); |
---|
[1313] | 163 | } |
---|
| 164 | |
---|
| 165 | void Shell::clearLines() |
---|
| 166 | { |
---|
| 167 | this->lines_.clear(); |
---|
| 168 | this->scrollIterator_ = this->lines_.begin(); |
---|
| 169 | |
---|
| 170 | this->scrollPosition_ = 0; |
---|
[1322] | 171 | this->finishedLastLine_ = true; |
---|
[1313] | 172 | |
---|
| 173 | SHELL_UPDATE_LISTENERS(linesChanged); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | std::list<std::string>::const_iterator Shell::getNewestLineIterator() const |
---|
| 177 | { |
---|
| 178 | if (this->scrollPosition_) |
---|
| 179 | return this->scrollIterator_; |
---|
| 180 | else |
---|
[1322] | 181 | return this->lines_.begin(); |
---|
[1313] | 182 | } |
---|
| 183 | |
---|
| 184 | std::list<std::string>::const_iterator Shell::getEndIterator() const |
---|
| 185 | { |
---|
| 186 | return this->lines_.end(); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | void Shell::addToHistory(const std::string& command) |
---|
| 190 | { |
---|
| 191 | ModifyConfigValue(commandHistory_, set, this->historyOffset_, command); |
---|
| 192 | this->historyPosition_ = 0; |
---|
[1322] | 193 | ModifyConfigValue(historyOffset_, set, (this->historyOffset_ + 1) % this->maxHistoryLength_); |
---|
[1313] | 194 | } |
---|
| 195 | |
---|
| 196 | std::string Shell::getFromHistory() const |
---|
| 197 | { |
---|
[1324] | 198 | unsigned int index = mod(((int)this->historyOffset_) - ((int)this->historyPosition_), this->maxHistoryLength_); |
---|
| 199 | if (index < this->commandHistory_.size() && this->historyPosition_ != 0) |
---|
| 200 | return this->commandHistory_[index]; |
---|
| 201 | else |
---|
| 202 | return ""; |
---|
[1313] | 203 | } |
---|
| 204 | |
---|
[1312] | 205 | void Shell::outputChanged() |
---|
[1313] | 206 | { |
---|
| 207 | std::string output; |
---|
[1322] | 208 | bool newline; |
---|
| 209 | do |
---|
[1313] | 210 | { |
---|
[1322] | 211 | newline = this->outputBuffer_.getLine(&output); |
---|
[1317] | 212 | |
---|
[1322] | 213 | if (!newline && output == "") |
---|
| 214 | break; |
---|
[1317] | 215 | |
---|
[1322] | 216 | if (this->finishedLastLine_) |
---|
| 217 | { |
---|
[1334] | 218 | if (this->bAddOutputLevel_) |
---|
| 219 | output.insert(0, 1, (char)OutputHandler::getOutStream().getOutputLevel()); |
---|
| 220 | |
---|
[1322] | 221 | this->lines_.insert(this->lines_.begin(), output); |
---|
[1317] | 222 | |
---|
[1322] | 223 | if (this->scrollPosition_) |
---|
| 224 | this->scrollPosition_++; |
---|
| 225 | else |
---|
| 226 | this->scrollIterator_ = this->lines_.begin(); |
---|
[1313] | 227 | |
---|
[1322] | 228 | this->finishedLastLine_ = newline; |
---|
[1327] | 229 | |
---|
| 230 | if (!this->scrollPosition_) |
---|
| 231 | { |
---|
| 232 | SHELL_UPDATE_LISTENERS(lineAdded); |
---|
| 233 | } |
---|
[1322] | 234 | } |
---|
[1313] | 235 | else |
---|
[1317] | 236 | { |
---|
[1322] | 237 | (*this->lines_.begin()) += output; |
---|
| 238 | this->finishedLastLine_ = newline; |
---|
| 239 | SHELL_UPDATE_LISTENERS(onlyLastLineChanged); |
---|
[1317] | 240 | } |
---|
[1313] | 241 | |
---|
[1322] | 242 | } while (newline); |
---|
[1313] | 243 | } |
---|
| 244 | |
---|
[1312] | 245 | void Shell::inputChanged() |
---|
[1313] | 246 | { |
---|
| 247 | SHELL_UPDATE_LISTENERS(inputChanged); |
---|
| 248 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 249 | } |
---|
| 250 | |
---|
[1312] | 251 | void Shell::execute() |
---|
[1313] | 252 | { |
---|
[1324] | 253 | this->addToHistory(this->inputBuffer_.get()); |
---|
[1322] | 254 | this->addLine(this->inputBuffer_.get(), 0); |
---|
| 255 | |
---|
| 256 | if (!CommandExecutor::execute(this->inputBuffer_.get())) |
---|
[1313] | 257 | this->addLine("Error: Can't execute \"" + this->inputBuffer_.get() + "\".", 1); |
---|
| 258 | |
---|
| 259 | this->clear(); |
---|
| 260 | } |
---|
| 261 | |
---|
[1312] | 262 | void Shell::hintandcomplete() |
---|
[1313] | 263 | { |
---|
[1424] | 264 | this->inputBuffer_.set(CommandExecutor::complete(this->inputBuffer_.get())); |
---|
[1334] | 265 | this->addLine(CommandExecutor::hint(this->inputBuffer_.get()), -1); |
---|
[1313] | 266 | |
---|
| 267 | this->inputChanged(); |
---|
| 268 | } |
---|
| 269 | |
---|
[1312] | 270 | void Shell::backspace() |
---|
[1313] | 271 | { |
---|
| 272 | this->inputBuffer_.removeBehindCursor(); |
---|
| 273 | SHELL_UPDATE_LISTENERS(inputChanged); |
---|
| 274 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | void Shell::deletechar() |
---|
| 278 | { |
---|
| 279 | this->inputBuffer_.removeAtCursor(); |
---|
| 280 | SHELL_UPDATE_LISTENERS(inputChanged); |
---|
| 281 | } |
---|
| 282 | |
---|
[1312] | 283 | void Shell::clear() |
---|
[1313] | 284 | { |
---|
| 285 | this->inputBuffer_.clear(); |
---|
[1324] | 286 | this->historyPosition_ = 0; |
---|
[1313] | 287 | SHELL_UPDATE_LISTENERS(inputChanged); |
---|
| 288 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 289 | } |
---|
| 290 | |
---|
[1312] | 291 | void Shell::cursor_right() |
---|
[1313] | 292 | { |
---|
| 293 | this->inputBuffer_.increaseCursor(); |
---|
| 294 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 295 | } |
---|
| 296 | |
---|
[1312] | 297 | void Shell::cursor_left() |
---|
[1313] | 298 | { |
---|
| 299 | this->inputBuffer_.decreaseCursor(); |
---|
| 300 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | void Shell::cursor_end() |
---|
| 304 | { |
---|
| 305 | this->inputBuffer_.setCursorToEnd(); |
---|
| 306 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | void Shell::cursor_home() |
---|
| 310 | { |
---|
| 311 | this->inputBuffer_.setCursorToBegin(); |
---|
| 312 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 313 | } |
---|
| 314 | |
---|
[1312] | 315 | void Shell::history_up() |
---|
[1313] | 316 | { |
---|
[1324] | 317 | if (this->historyPosition_ < this->commandHistory_.size()) |
---|
[1313] | 318 | { |
---|
| 319 | this->historyPosition_++; |
---|
| 320 | this->inputBuffer_.set(this->getFromHistory()); |
---|
| 321 | } |
---|
| 322 | } |
---|
| 323 | |
---|
[1312] | 324 | void Shell::history_down() |
---|
[1313] | 325 | { |
---|
| 326 | if (this->historyPosition_ > 0) |
---|
| 327 | { |
---|
[1322] | 328 | this->historyPosition_--; |
---|
[1313] | 329 | this->inputBuffer_.set(this->getFromHistory()); |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | |
---|
[1312] | 333 | void Shell::scroll_up() |
---|
[1313] | 334 | { |
---|
| 335 | if (this->scrollIterator_ != this->lines_.end()) |
---|
| 336 | { |
---|
| 337 | ++this->scrollIterator_; |
---|
| 338 | ++this->scrollPosition_; |
---|
| 339 | |
---|
| 340 | SHELL_UPDATE_LISTENERS(linesChanged); |
---|
| 341 | } |
---|
| 342 | } |
---|
| 343 | |
---|
[1312] | 344 | void Shell::scroll_down() |
---|
[1313] | 345 | { |
---|
| 346 | if (this->scrollIterator_ != this->lines_.begin()) |
---|
| 347 | { |
---|
| 348 | --this->scrollIterator_; |
---|
| 349 | --this->scrollPosition_; |
---|
| 350 | |
---|
| 351 | SHELL_UPDATE_LISTENERS(linesChanged); |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | |
---|
[1312] | 355 | void Shell::exit() |
---|
[1313] | 356 | { |
---|
| 357 | if (this->inputBuffer_.getSize() > 0) |
---|
| 358 | { |
---|
| 359 | this->clear(); |
---|
| 360 | return; |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | this->clear(); |
---|
[1326] | 364 | this->scrollPosition_ = 0; |
---|
| 365 | this->scrollIterator_ = this->lines_.begin(); |
---|
| 366 | |
---|
[1313] | 367 | SHELL_UPDATE_LISTENERS(exit); |
---|
| 368 | } |
---|
[1312] | 369 | } |
---|