[1505] | 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 | |
---|
| 29 | #include "Shell.h" |
---|
[3196] | 30 | |
---|
| 31 | #include "util/OutputHandler.h" |
---|
[1505] | 32 | #include "CommandExecutor.h" |
---|
| 33 | #include "CoreIncludes.h" |
---|
| 34 | #include "ConfigValueIncludes.h" |
---|
[1535] | 35 | #include "Core.h" |
---|
[1505] | 36 | #include "ConsoleCommand.h" |
---|
| 37 | |
---|
| 38 | #define SHELL_UPDATE_LISTENERS(function) \ |
---|
[1540] | 39 | for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ) \ |
---|
| 40 | (*(it++))->function() |
---|
[1505] | 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
| 44 | SetConsoleCommand(Shell, clearShell, true); |
---|
| 45 | SetConsoleCommand(Shell, history, true); |
---|
| 46 | |
---|
[1747] | 47 | SetConsoleCommandShortcut(OutputHandler, log); |
---|
| 48 | SetConsoleCommandShortcut(OutputHandler, error); |
---|
| 49 | SetConsoleCommandShortcut(OutputHandler, warning); |
---|
| 50 | SetConsoleCommandShortcut(OutputHandler, info); |
---|
| 51 | SetConsoleCommandShortcut(OutputHandler, debug); |
---|
| 52 | |
---|
[3370] | 53 | Shell* Shell::singletonPtr_s = 0; |
---|
[1792] | 54 | |
---|
[1505] | 55 | Shell::Shell() |
---|
| 56 | { |
---|
[1792] | 57 | int level = Core::getSoftDebugLevel(OutputHandler::LD_Shell); |
---|
| 58 | Core::setSoftDebugLevel(OutputHandler::LD_Shell, -1); |
---|
| 59 | |
---|
[1505] | 60 | RegisterRootObject(Shell); |
---|
| 61 | |
---|
| 62 | this->scrollPosition_ = 0; |
---|
| 63 | this->maxHistoryLength_ = 100; |
---|
| 64 | this->historyPosition_ = 0; |
---|
| 65 | this->historyOffset_ = 0; |
---|
| 66 | this->finishedLastLine_ = true; |
---|
| 67 | this->bAddOutputLevel_ = false; |
---|
| 68 | |
---|
| 69 | this->clearLines(); |
---|
| 70 | |
---|
[1755] | 71 | this->inputBuffer_ = new InputBuffer(); |
---|
| 72 | this->configureInputBuffer(); |
---|
[1505] | 73 | |
---|
| 74 | this->outputBuffer_.registerListener(this); |
---|
[2662] | 75 | OutputHandler::getOutStream().setOutputBuffer(&this->outputBuffer_); |
---|
[1505] | 76 | |
---|
[3280] | 77 | // Get a config file for the command history |
---|
| 78 | this->commandHistoryConfigFileType_ = ConfigFileManager::getInstance().getNewConfigFileType(); |
---|
| 79 | ConfigFileManager::getInstance().setFilename(this->commandHistoryConfigFileType_, "commandHistory.ini"); |
---|
| 80 | |
---|
[1505] | 81 | this->setConfigValues(); |
---|
[1792] | 82 | |
---|
| 83 | Core::setSoftDebugLevel(OutputHandler::LD_Shell, level); |
---|
[1505] | 84 | } |
---|
| 85 | |
---|
[1755] | 86 | Shell::~Shell() |
---|
| 87 | { |
---|
[2662] | 88 | OutputHandler::getOutStream().setOutputBuffer(0); |
---|
[1755] | 89 | if (this->inputBuffer_) |
---|
[5929] | 90 | this->inputBuffer_->destroy(); |
---|
[1755] | 91 | } |
---|
| 92 | |
---|
[1505] | 93 | void Shell::setConfigValues() |
---|
| 94 | { |
---|
[3280] | 95 | SetConfigValueGeneric(commandHistoryConfigFileType_, maxHistoryLength_, 100) |
---|
| 96 | .callback(this, &Shell::commandHistoryLengthChanged); |
---|
| 97 | SetConfigValueGeneric(commandHistoryConfigFileType_, historyOffset_, 0) |
---|
| 98 | .callback(this, &Shell::commandHistoryOffsetChanged); |
---|
| 99 | SetConfigValueVectorGeneric(commandHistoryConfigFileType_, commandHistory_, std::vector<std::string>()); |
---|
[1747] | 100 | } |
---|
[1505] | 101 | |
---|
[1747] | 102 | void Shell::commandHistoryOffsetChanged() |
---|
| 103 | { |
---|
[1505] | 104 | if (this->historyOffset_ >= this->maxHistoryLength_) |
---|
| 105 | this->historyOffset_ = 0; |
---|
[1747] | 106 | } |
---|
[1505] | 107 | |
---|
[1747] | 108 | void Shell::commandHistoryLengthChanged() |
---|
| 109 | { |
---|
| 110 | this->commandHistoryOffsetChanged(); |
---|
| 111 | |
---|
[1505] | 112 | while (this->commandHistory_.size() > this->maxHistoryLength_) |
---|
| 113 | { |
---|
| 114 | unsigned int index = this->commandHistory_.size() - 1; |
---|
| 115 | this->commandHistory_.erase(this->commandHistory_.begin() + index); |
---|
| 116 | ModifyConfigValue(commandHistory_, remove, index); |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
[1755] | 120 | void Shell::configureInputBuffer() |
---|
[1505] | 121 | { |
---|
| 122 | this->inputBuffer_->registerListener(this, &Shell::inputChanged, true); |
---|
| 123 | this->inputBuffer_->registerListener(this, &Shell::execute, '\r', false); |
---|
| 124 | this->inputBuffer_->registerListener(this, &Shell::hintandcomplete, '\t', true); |
---|
| 125 | this->inputBuffer_->registerListener(this, &Shell::backspace, '\b', true); |
---|
| 126 | this->inputBuffer_->registerListener(this, &Shell::deletechar, KeyCode::Delete); |
---|
[3300] | 127 | this->inputBuffer_->registerListener(this, &Shell::exit, static_cast<char>(27), true); |
---|
[1505] | 128 | this->inputBuffer_->registerListener(this, &Shell::cursor_right, KeyCode::Right); |
---|
| 129 | this->inputBuffer_->registerListener(this, &Shell::cursor_left, KeyCode::Left); |
---|
| 130 | this->inputBuffer_->registerListener(this, &Shell::cursor_end, KeyCode::End); |
---|
| 131 | this->inputBuffer_->registerListener(this, &Shell::cursor_home, KeyCode::Home); |
---|
| 132 | this->inputBuffer_->registerListener(this, &Shell::history_up, KeyCode::Up); |
---|
| 133 | this->inputBuffer_->registerListener(this, &Shell::history_down, KeyCode::Down); |
---|
| 134 | this->inputBuffer_->registerListener(this, &Shell::scroll_up, KeyCode::PageUp); |
---|
| 135 | this->inputBuffer_->registerListener(this, &Shell::scroll_down, KeyCode::PageDown); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | void Shell::clearShell() |
---|
| 139 | { |
---|
| 140 | Shell::getInstance().clearLines(); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | void Shell::history() |
---|
| 144 | { |
---|
| 145 | Shell& instance = Shell::getInstance(); |
---|
| 146 | |
---|
[3300] | 147 | for (unsigned int i = instance.historyOffset_; i < instance.commandHistory_.size(); ++i) |
---|
[1505] | 148 | instance.addLine(instance.commandHistory_[i], -1); |
---|
[3300] | 149 | for (unsigned int i = 0; i < instance.historyOffset_; ++i) |
---|
[1505] | 150 | instance.addLine(instance.commandHistory_[i], -1); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | void Shell::registerListener(ShellListener* listener) |
---|
| 154 | { |
---|
| 155 | this->listeners_.insert(this->listeners_.end(), listener); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | void Shell::unregisterListener(ShellListener* listener) |
---|
| 159 | { |
---|
| 160 | for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ) |
---|
| 161 | { |
---|
| 162 | if ((*it) == listener) |
---|
| 163 | this->listeners_.erase(it++); |
---|
| 164 | else |
---|
| 165 | ++it; |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | void Shell::setCursorPosition(unsigned int cursor) |
---|
| 170 | { |
---|
| 171 | this->inputBuffer_->setCursorPosition(cursor); |
---|
| 172 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | void Shell::setInput(const std::string& input) |
---|
| 176 | { |
---|
| 177 | this->inputBuffer_->set(input); |
---|
| 178 | this->inputChanged(); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | void Shell::addLine(const std::string& line, int level) |
---|
| 182 | { |
---|
| 183 | int original_level = OutputHandler::getOutStream().getOutputLevel(); |
---|
| 184 | OutputHandler::getOutStream().setOutputLevel(level); |
---|
| 185 | |
---|
| 186 | if (!this->finishedLastLine_) |
---|
| 187 | this->outputBuffer_ << std::endl; |
---|
| 188 | |
---|
| 189 | this->outputBuffer_ << line << std::endl; |
---|
| 190 | OutputHandler::getOutStream().setOutputLevel(original_level); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | void Shell::clearLines() |
---|
| 194 | { |
---|
| 195 | this->lines_.clear(); |
---|
| 196 | this->scrollIterator_ = this->lines_.begin(); |
---|
| 197 | |
---|
| 198 | this->scrollPosition_ = 0; |
---|
| 199 | this->finishedLastLine_ = true; |
---|
| 200 | |
---|
| 201 | SHELL_UPDATE_LISTENERS(linesChanged); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | std::list<std::string>::const_iterator Shell::getNewestLineIterator() const |
---|
| 205 | { |
---|
| 206 | if (this->scrollPosition_) |
---|
| 207 | return this->scrollIterator_; |
---|
| 208 | else |
---|
| 209 | return this->lines_.begin(); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | std::list<std::string>::const_iterator Shell::getEndIterator() const |
---|
| 213 | { |
---|
| 214 | return this->lines_.end(); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | void Shell::addToHistory(const std::string& command) |
---|
| 218 | { |
---|
| 219 | ModifyConfigValue(commandHistory_, set, this->historyOffset_, command); |
---|
| 220 | this->historyPosition_ = 0; |
---|
| 221 | ModifyConfigValue(historyOffset_, set, (this->historyOffset_ + 1) % this->maxHistoryLength_); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | std::string Shell::getFromHistory() const |
---|
| 225 | { |
---|
[3301] | 226 | unsigned int index = mod(static_cast<int>(this->historyOffset_) - static_cast<int>(this->historyPosition_), this->maxHistoryLength_); |
---|
[1505] | 227 | if (index < this->commandHistory_.size() && this->historyPosition_ != 0) |
---|
| 228 | return this->commandHistory_[index]; |
---|
| 229 | else |
---|
| 230 | return ""; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | void Shell::outputChanged() |
---|
| 234 | { |
---|
| 235 | std::string output; |
---|
| 236 | bool newline; |
---|
| 237 | do |
---|
| 238 | { |
---|
| 239 | newline = this->outputBuffer_.getLine(&output); |
---|
| 240 | |
---|
| 241 | if (!newline && output == "") |
---|
| 242 | break; |
---|
| 243 | |
---|
| 244 | if (this->finishedLastLine_) |
---|
| 245 | { |
---|
| 246 | if (this->bAddOutputLevel_) |
---|
[3301] | 247 | output.insert(0, 1, static_cast<char>(OutputHandler::getOutStream().getOutputLevel())); |
---|
[1505] | 248 | |
---|
| 249 | this->lines_.insert(this->lines_.begin(), output); |
---|
| 250 | |
---|
| 251 | if (this->scrollPosition_) |
---|
| 252 | this->scrollPosition_++; |
---|
| 253 | else |
---|
| 254 | this->scrollIterator_ = this->lines_.begin(); |
---|
| 255 | |
---|
| 256 | this->finishedLastLine_ = newline; |
---|
| 257 | |
---|
| 258 | if (!this->scrollPosition_) |
---|
| 259 | { |
---|
| 260 | SHELL_UPDATE_LISTENERS(lineAdded); |
---|
| 261 | } |
---|
| 262 | } |
---|
| 263 | else |
---|
| 264 | { |
---|
| 265 | (*this->lines_.begin()) += output; |
---|
| 266 | this->finishedLastLine_ = newline; |
---|
| 267 | SHELL_UPDATE_LISTENERS(onlyLastLineChanged); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | } while (newline); |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | void Shell::inputChanged() |
---|
| 274 | { |
---|
| 275 | SHELL_UPDATE_LISTENERS(inputChanged); |
---|
| 276 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | void Shell::execute() |
---|
| 280 | { |
---|
| 281 | this->addToHistory(this->inputBuffer_->get()); |
---|
| 282 | this->addLine(this->inputBuffer_->get(), 0); |
---|
| 283 | |
---|
| 284 | if (!CommandExecutor::execute(this->inputBuffer_->get())) |
---|
| 285 | this->addLine("Error: Can't execute \"" + this->inputBuffer_->get() + "\".", 1); |
---|
| 286 | |
---|
| 287 | this->clear(); |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | void Shell::hintandcomplete() |
---|
| 291 | { |
---|
| 292 | this->inputBuffer_->set(CommandExecutor::complete(this->inputBuffer_->get())); |
---|
| 293 | this->addLine(CommandExecutor::hint(this->inputBuffer_->get()), -1); |
---|
| 294 | |
---|
| 295 | this->inputChanged(); |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | void Shell::backspace() |
---|
| 299 | { |
---|
| 300 | this->inputBuffer_->removeBehindCursor(); |
---|
| 301 | SHELL_UPDATE_LISTENERS(inputChanged); |
---|
| 302 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | void Shell::deletechar() |
---|
| 306 | { |
---|
| 307 | this->inputBuffer_->removeAtCursor(); |
---|
| 308 | SHELL_UPDATE_LISTENERS(inputChanged); |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | void Shell::clear() |
---|
| 312 | { |
---|
| 313 | this->inputBuffer_->clear(); |
---|
| 314 | this->historyPosition_ = 0; |
---|
| 315 | SHELL_UPDATE_LISTENERS(inputChanged); |
---|
| 316 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | void Shell::cursor_right() |
---|
| 320 | { |
---|
| 321 | this->inputBuffer_->increaseCursor(); |
---|
| 322 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | void Shell::cursor_left() |
---|
| 326 | { |
---|
| 327 | this->inputBuffer_->decreaseCursor(); |
---|
| 328 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | void Shell::cursor_end() |
---|
| 332 | { |
---|
| 333 | this->inputBuffer_->setCursorToEnd(); |
---|
| 334 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | void Shell::cursor_home() |
---|
| 338 | { |
---|
| 339 | this->inputBuffer_->setCursorToBegin(); |
---|
| 340 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | void Shell::history_up() |
---|
| 344 | { |
---|
| 345 | if (this->historyPosition_ < this->commandHistory_.size()) |
---|
| 346 | { |
---|
| 347 | this->historyPosition_++; |
---|
| 348 | this->inputBuffer_->set(this->getFromHistory()); |
---|
| 349 | } |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | void Shell::history_down() |
---|
| 353 | { |
---|
| 354 | if (this->historyPosition_ > 0) |
---|
| 355 | { |
---|
| 356 | this->historyPosition_--; |
---|
| 357 | this->inputBuffer_->set(this->getFromHistory()); |
---|
| 358 | } |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | void Shell::scroll_up() |
---|
| 362 | { |
---|
| 363 | if (this->scrollIterator_ != this->lines_.end()) |
---|
| 364 | { |
---|
| 365 | ++this->scrollIterator_; |
---|
| 366 | ++this->scrollPosition_; |
---|
| 367 | |
---|
| 368 | SHELL_UPDATE_LISTENERS(linesChanged); |
---|
| 369 | } |
---|
| 370 | } |
---|
| 371 | |
---|
| 372 | void Shell::scroll_down() |
---|
| 373 | { |
---|
| 374 | if (this->scrollIterator_ != this->lines_.begin()) |
---|
| 375 | { |
---|
| 376 | --this->scrollIterator_; |
---|
| 377 | --this->scrollPosition_; |
---|
| 378 | |
---|
| 379 | SHELL_UPDATE_LISTENERS(linesChanged); |
---|
| 380 | } |
---|
| 381 | } |
---|
| 382 | |
---|
| 383 | void Shell::exit() |
---|
| 384 | { |
---|
| 385 | if (this->inputBuffer_->getSize() > 0) |
---|
| 386 | { |
---|
| 387 | this->clear(); |
---|
| 388 | return; |
---|
| 389 | } |
---|
| 390 | |
---|
| 391 | this->clear(); |
---|
| 392 | this->scrollPosition_ = 0; |
---|
| 393 | this->scrollIterator_ = this->lines_.begin(); |
---|
| 394 | |
---|
| 395 | SHELL_UPDATE_LISTENERS(exit); |
---|
| 396 | } |
---|
| 397 | } |
---|