| 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" |
|---|
| 30 | #include "CommandExecutor.h" |
|---|
| 31 | #include "CoreIncludes.h" |
|---|
| 32 | #include "ConfigValueIncludes.h" |
|---|
| 33 | #include "CoreSettings.h" |
|---|
| 34 | #include "ConsoleCommand.h" |
|---|
| 35 | |
|---|
| 36 | #define SHELL_UPDATE_LISTENERS(function) \ |
|---|
| 37 | for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) \ |
|---|
| 38 | (*it)->function() |
|---|
| 39 | |
|---|
| 40 | namespace orxonox |
|---|
| 41 | { |
|---|
| 42 | SetConsoleCommand(Shell, clearShell, true); |
|---|
| 43 | SetConsoleCommand(Shell, history, true); |
|---|
| 44 | |
|---|
| 45 | Shell::Shell() |
|---|
| 46 | { |
|---|
| 47 | RegisterRootObject(Shell); |
|---|
| 48 | |
|---|
| 49 | this->scrollPosition_ = 0; |
|---|
| 50 | this->maxHistoryLength_ = 100; |
|---|
| 51 | this->historyPosition_ = 0; |
|---|
| 52 | this->historyOffset_ = 0; |
|---|
| 53 | this->finishedLastLine_ = true; |
|---|
| 54 | this->bAddOutputLevel_ = false; |
|---|
| 55 | |
|---|
| 56 | this->clearLines(); |
|---|
| 57 | |
|---|
| 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); |
|---|
| 62 | this->inputBuffer_.registerListener(this, &Shell::deletechar, OIS::KC_DELETE); |
|---|
| 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); |
|---|
| 66 | this->inputBuffer_.registerListener(this, &Shell::cursor_end, OIS::KC_END); |
|---|
| 67 | this->inputBuffer_.registerListener(this, &Shell::cursor_home, OIS::KC_HOME); |
|---|
| 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); |
|---|
| 72 | |
|---|
| 73 | this->outputBuffer_.registerListener(this); |
|---|
| 74 | |
|---|
| 75 | this->setConfigValues(); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | Shell& Shell::createShell() |
|---|
| 79 | { |
|---|
| 80 | int level = CoreSettings::getSoftDebugLevel(OutputHandler::LD_Shell); |
|---|
| 81 | CoreSettings::setSoftDebugLevel(OutputHandler::LD_Shell, -1); |
|---|
| 82 | static Shell instance; |
|---|
| 83 | CoreSettings::setSoftDebugLevel(OutputHandler::LD_Shell, level); |
|---|
| 84 | return instance; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | Shell& Shell::getInstance() |
|---|
| 88 | { |
|---|
| 89 | static Shell& instance = createShell(); |
|---|
| 90 | return instance; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | void Shell::setConfigValues() |
|---|
| 94 | { |
|---|
| 95 | SetConfigValue(maxHistoryLength_, 100); |
|---|
| 96 | SetConfigValue(historyOffset_, 0); |
|---|
| 97 | SetConfigValueVector(commandHistory_, std::vector<std::string>()); |
|---|
| 98 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 153 | void Shell::addLine(const std::string& line, int level) |
|---|
| 154 | { |
|---|
| 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); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | void Shell::clearLines() |
|---|
| 166 | { |
|---|
| 167 | this->lines_.clear(); |
|---|
| 168 | this->scrollIterator_ = this->lines_.begin(); |
|---|
| 169 | |
|---|
| 170 | this->scrollPosition_ = 0; |
|---|
| 171 | this->finishedLastLine_ = true; |
|---|
| 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 |
|---|
| 181 | return this->lines_.begin(); |
|---|
| 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; |
|---|
| 193 | ModifyConfigValue(historyOffset_, set, (this->historyOffset_ + 1) % this->maxHistoryLength_); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | std::string Shell::getFromHistory() const |
|---|
| 197 | { |
|---|
| 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 ""; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | void Shell::outputChanged() |
|---|
| 206 | { |
|---|
| 207 | std::string output; |
|---|
| 208 | bool newline; |
|---|
| 209 | do |
|---|
| 210 | { |
|---|
| 211 | newline = this->outputBuffer_.getLine(&output); |
|---|
| 212 | |
|---|
| 213 | if (!newline && output == "") |
|---|
| 214 | break; |
|---|
| 215 | |
|---|
| 216 | if (this->finishedLastLine_) |
|---|
| 217 | { |
|---|
| 218 | if (this->bAddOutputLevel_) |
|---|
| 219 | output.insert(0, 1, (char)OutputHandler::getOutStream().getOutputLevel()); |
|---|
| 220 | |
|---|
| 221 | this->lines_.insert(this->lines_.begin(), output); |
|---|
| 222 | |
|---|
| 223 | if (this->scrollPosition_) |
|---|
| 224 | this->scrollPosition_++; |
|---|
| 225 | else |
|---|
| 226 | this->scrollIterator_ = this->lines_.begin(); |
|---|
| 227 | |
|---|
| 228 | this->finishedLastLine_ = newline; |
|---|
| 229 | |
|---|
| 230 | if (!this->scrollPosition_) |
|---|
| 231 | { |
|---|
| 232 | SHELL_UPDATE_LISTENERS(lineAdded); |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | else |
|---|
| 236 | { |
|---|
| 237 | (*this->lines_.begin()) += output; |
|---|
| 238 | this->finishedLastLine_ = newline; |
|---|
| 239 | SHELL_UPDATE_LISTENERS(onlyLastLineChanged); |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | } while (newline); |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | void Shell::inputChanged() |
|---|
| 246 | { |
|---|
| 247 | SHELL_UPDATE_LISTENERS(inputChanged); |
|---|
| 248 | SHELL_UPDATE_LISTENERS(cursorChanged); |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | void Shell::execute() |
|---|
| 252 | { |
|---|
| 253 | this->addToHistory(this->inputBuffer_.get()); |
|---|
| 254 | this->addLine(this->inputBuffer_.get(), 0); |
|---|
| 255 | |
|---|
| 256 | if (!CommandExecutor::execute(this->inputBuffer_.get())) |
|---|
| 257 | this->addLine("Error: Can't execute \"" + this->inputBuffer_.get() + "\".", 1); |
|---|
| 258 | |
|---|
| 259 | this->clear(); |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | void Shell::hintandcomplete() |
|---|
| 263 | { |
|---|
| 264 | this->inputBuffer_.set(CommandExecutor::complete(this->inputBuffer_.get())); |
|---|
| 265 | this->addLine(CommandExecutor::hint(this->inputBuffer_.get()), -1); |
|---|
| 266 | |
|---|
| 267 | this->inputChanged(); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | void Shell::backspace() |
|---|
| 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 | |
|---|
| 283 | void Shell::clear() |
|---|
| 284 | { |
|---|
| 285 | this->inputBuffer_.clear(); |
|---|
| 286 | this->historyPosition_ = 0; |
|---|
| 287 | SHELL_UPDATE_LISTENERS(inputChanged); |
|---|
| 288 | SHELL_UPDATE_LISTENERS(cursorChanged); |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | void Shell::cursor_right() |
|---|
| 292 | { |
|---|
| 293 | this->inputBuffer_.increaseCursor(); |
|---|
| 294 | SHELL_UPDATE_LISTENERS(cursorChanged); |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | void Shell::cursor_left() |
|---|
| 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 | |
|---|
| 315 | void Shell::history_up() |
|---|
| 316 | { |
|---|
| 317 | if (this->historyPosition_ < this->commandHistory_.size()) |
|---|
| 318 | { |
|---|
| 319 | this->historyPosition_++; |
|---|
| 320 | this->inputBuffer_.set(this->getFromHistory()); |
|---|
| 321 | } |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | void Shell::history_down() |
|---|
| 325 | { |
|---|
| 326 | if (this->historyPosition_ > 0) |
|---|
| 327 | { |
|---|
| 328 | this->historyPosition_--; |
|---|
| 329 | this->inputBuffer_.set(this->getFromHistory()); |
|---|
| 330 | } |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | void Shell::scroll_up() |
|---|
| 334 | { |
|---|
| 335 | if (this->scrollIterator_ != this->lines_.end()) |
|---|
| 336 | { |
|---|
| 337 | ++this->scrollIterator_; |
|---|
| 338 | ++this->scrollPosition_; |
|---|
| 339 | |
|---|
| 340 | SHELL_UPDATE_LISTENERS(linesChanged); |
|---|
| 341 | } |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | void Shell::scroll_down() |
|---|
| 345 | { |
|---|
| 346 | if (this->scrollIterator_ != this->lines_.begin()) |
|---|
| 347 | { |
|---|
| 348 | --this->scrollIterator_; |
|---|
| 349 | --this->scrollPosition_; |
|---|
| 350 | |
|---|
| 351 | SHELL_UPDATE_LISTENERS(linesChanged); |
|---|
| 352 | } |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | void Shell::exit() |
|---|
| 356 | { |
|---|
| 357 | if (this->inputBuffer_.getSize() > 0) |
|---|
| 358 | { |
|---|
| 359 | this->clear(); |
|---|
| 360 | return; |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | this->clear(); |
|---|
| 364 | this->scrollPosition_ = 0; |
|---|
| 365 | this->scrollIterator_ = this->lines_.begin(); |
|---|
| 366 | |
|---|
| 367 | SHELL_UPDATE_LISTENERS(exit); |
|---|
| 368 | } |
|---|
| 369 | } |
|---|