[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" |
---|
[1312] | 34 | |
---|
[1313] | 35 | #define SHELL_UPDATE_LISTENERS(function) \ |
---|
| 36 | for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) \ |
---|
| 37 | (*it)->function() |
---|
[1312] | 38 | |
---|
| 39 | namespace orxonox |
---|
| 40 | { |
---|
| 41 | Shell::Shell() |
---|
| 42 | { |
---|
[1313] | 43 | RegisterRootObject(Shell); |
---|
| 44 | |
---|
| 45 | this->scrollPosition_ = 0; |
---|
| 46 | this->maxHistoryLength_ = 100; |
---|
| 47 | this->historyPosition_ = 0; |
---|
| 48 | this->historyOffset_ = 0; |
---|
[1322] | 49 | this->finishedLastLine_ = true; |
---|
[1334] | 50 | this->bAddOutputLevel_ = false; |
---|
[1313] | 51 | |
---|
| 52 | this->clearLines(); |
---|
[1322] | 53 | |
---|
[1312] | 54 | this->inputBuffer_.registerListener(this, &Shell::inputChanged, true); |
---|
| 55 | this->inputBuffer_.registerListener(this, &Shell::execute, '\r', false); |
---|
| 56 | this->inputBuffer_.registerListener(this, &Shell::hintandcomplete, '\t', true); |
---|
| 57 | this->inputBuffer_.registerListener(this, &Shell::backspace, '\b', true); |
---|
[1313] | 58 | this->inputBuffer_.registerListener(this, &Shell::deletechar, OIS::KC_DELETE); |
---|
[1312] | 59 | this->inputBuffer_.registerListener(this, &Shell::exit, (char)27, true); |
---|
| 60 | this->inputBuffer_.registerListener(this, &Shell::cursor_right, OIS::KC_RIGHT); |
---|
| 61 | this->inputBuffer_.registerListener(this, &Shell::cursor_left, OIS::KC_LEFT); |
---|
[1313] | 62 | this->inputBuffer_.registerListener(this, &Shell::cursor_end, OIS::KC_END); |
---|
| 63 | this->inputBuffer_.registerListener(this, &Shell::cursor_home, OIS::KC_HOME); |
---|
[1312] | 64 | this->inputBuffer_.registerListener(this, &Shell::history_up, OIS::KC_UP); |
---|
| 65 | this->inputBuffer_.registerListener(this, &Shell::history_down, OIS::KC_DOWN); |
---|
| 66 | this->inputBuffer_.registerListener(this, &Shell::scroll_up, OIS::KC_PGUP); |
---|
| 67 | this->inputBuffer_.registerListener(this, &Shell::scroll_down, OIS::KC_PGDOWN); |
---|
[1322] | 68 | |
---|
| 69 | this->outputBuffer_.registerListener(this); |
---|
| 70 | |
---|
[1313] | 71 | this->setConfigValues(); |
---|
[1312] | 72 | } |
---|
| 73 | |
---|
[1326] | 74 | Shell& Shell::createShell() |
---|
[1312] | 75 | { |
---|
[1326] | 76 | int level = CoreSettings::getSoftDebugLevel(OutputHandler::LD_Shell); |
---|
| 77 | CoreSettings::setSoftDebugLevel(OutputHandler::LD_Shell, -1); |
---|
[1313] | 78 | static Shell instance; |
---|
[1326] | 79 | CoreSettings::setSoftDebugLevel(OutputHandler::LD_Shell, level); |
---|
[1312] | 80 | return instance; |
---|
| 81 | } |
---|
| 82 | |
---|
[1326] | 83 | Shell& Shell::getInstance() |
---|
| 84 | { |
---|
| 85 | static Shell& instance = createShell(); |
---|
| 86 | return instance; |
---|
| 87 | } |
---|
| 88 | |
---|
[1312] | 89 | void Shell::setConfigValues() |
---|
[1313] | 90 | { |
---|
| 91 | SetConfigValue(maxHistoryLength_, 100); |
---|
| 92 | SetConfigValue(historyOffset_, 0); |
---|
[1325] | 93 | SetConfigValueVector(commandHistory_, std::vector<std::string>()); |
---|
[1312] | 94 | |
---|
[1313] | 95 | if (this->historyOffset_ >= this->maxHistoryLength_) |
---|
| 96 | this->historyOffset_ = 0; |
---|
| 97 | |
---|
| 98 | while (this->commandHistory_.size() > this->maxHistoryLength_) |
---|
| 99 | { |
---|
| 100 | unsigned int index = this->commandHistory_.size() - 1; |
---|
| 101 | this->commandHistory_.erase(this->commandHistory_.begin() + index); |
---|
| 102 | ModifyConfigValue(commandHistory_, remove, index); |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | void Shell::registerListener(ShellListener* listener) |
---|
| 107 | { |
---|
| 108 | this->listeners_.insert(this->listeners_.end(), listener); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | void Shell::unregisterListener(ShellListener* listener) |
---|
| 112 | { |
---|
| 113 | for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ) |
---|
| 114 | { |
---|
| 115 | if ((*it) == listener) |
---|
| 116 | this->listeners_.erase(it++); |
---|
| 117 | else |
---|
| 118 | ++it; |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | void Shell::setCursorPosition(unsigned int cursor) |
---|
| 123 | { |
---|
| 124 | this->inputBuffer_.setCursorPosition(cursor); |
---|
| 125 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | void Shell::setInput(const std::string& input) |
---|
| 129 | { |
---|
| 130 | this->inputBuffer_.set(input); |
---|
| 131 | this->inputChanged(); |
---|
| 132 | } |
---|
| 133 | |
---|
[1334] | 134 | void Shell::addLine(const std::string& line, int level) |
---|
[1313] | 135 | { |
---|
[1322] | 136 | int original_level = OutputHandler::getOutStream().getOutputLevel(); |
---|
| 137 | OutputHandler::getOutStream().setOutputLevel(level); |
---|
| 138 | |
---|
| 139 | if (!this->finishedLastLine_) |
---|
| 140 | this->outputBuffer_ << std::endl; |
---|
| 141 | |
---|
| 142 | this->outputBuffer_ << line << std::endl; |
---|
| 143 | OutputHandler::getOutStream().setOutputLevel(original_level); |
---|
[1313] | 144 | } |
---|
| 145 | |
---|
| 146 | void Shell::clearLines() |
---|
| 147 | { |
---|
| 148 | this->lines_.clear(); |
---|
| 149 | this->scrollIterator_ = this->lines_.begin(); |
---|
| 150 | |
---|
| 151 | this->scrollPosition_ = 0; |
---|
[1322] | 152 | this->finishedLastLine_ = true; |
---|
[1313] | 153 | |
---|
| 154 | SHELL_UPDATE_LISTENERS(linesChanged); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | std::list<std::string>::const_iterator Shell::getNewestLineIterator() const |
---|
| 158 | { |
---|
| 159 | if (this->scrollPosition_) |
---|
| 160 | return this->scrollIterator_; |
---|
| 161 | else |
---|
[1322] | 162 | return this->lines_.begin(); |
---|
[1313] | 163 | } |
---|
| 164 | |
---|
| 165 | std::list<std::string>::const_iterator Shell::getEndIterator() const |
---|
| 166 | { |
---|
| 167 | return this->lines_.end(); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | void Shell::addToHistory(const std::string& command) |
---|
| 171 | { |
---|
| 172 | ModifyConfigValue(commandHistory_, set, this->historyOffset_, command); |
---|
| 173 | this->historyPosition_ = 0; |
---|
[1322] | 174 | ModifyConfigValue(historyOffset_, set, (this->historyOffset_ + 1) % this->maxHistoryLength_); |
---|
[1313] | 175 | } |
---|
| 176 | |
---|
| 177 | std::string Shell::getFromHistory() const |
---|
| 178 | { |
---|
[1324] | 179 | unsigned int index = mod(((int)this->historyOffset_) - ((int)this->historyPosition_), this->maxHistoryLength_); |
---|
| 180 | if (index < this->commandHistory_.size() && this->historyPosition_ != 0) |
---|
| 181 | return this->commandHistory_[index]; |
---|
| 182 | else |
---|
| 183 | return ""; |
---|
[1313] | 184 | } |
---|
| 185 | |
---|
[1312] | 186 | void Shell::outputChanged() |
---|
[1313] | 187 | { |
---|
| 188 | std::string output; |
---|
[1322] | 189 | bool newline; |
---|
| 190 | do |
---|
[1313] | 191 | { |
---|
[1322] | 192 | newline = this->outputBuffer_.getLine(&output); |
---|
[1317] | 193 | |
---|
[1322] | 194 | if (!newline && output == "") |
---|
| 195 | break; |
---|
[1317] | 196 | |
---|
[1322] | 197 | if (this->finishedLastLine_) |
---|
| 198 | { |
---|
[1334] | 199 | if (this->bAddOutputLevel_) |
---|
| 200 | output.insert(0, 1, (char)OutputHandler::getOutStream().getOutputLevel()); |
---|
| 201 | |
---|
[1322] | 202 | this->lines_.insert(this->lines_.begin(), output); |
---|
[1317] | 203 | |
---|
[1322] | 204 | if (this->scrollPosition_) |
---|
| 205 | this->scrollPosition_++; |
---|
| 206 | else |
---|
| 207 | this->scrollIterator_ = this->lines_.begin(); |
---|
[1313] | 208 | |
---|
[1322] | 209 | this->finishedLastLine_ = newline; |
---|
[1327] | 210 | |
---|
| 211 | if (!this->scrollPosition_) |
---|
| 212 | { |
---|
| 213 | SHELL_UPDATE_LISTENERS(lineAdded); |
---|
| 214 | } |
---|
[1322] | 215 | } |
---|
[1313] | 216 | else |
---|
[1317] | 217 | { |
---|
[1322] | 218 | (*this->lines_.begin()) += output; |
---|
| 219 | this->finishedLastLine_ = newline; |
---|
| 220 | SHELL_UPDATE_LISTENERS(onlyLastLineChanged); |
---|
[1317] | 221 | } |
---|
[1313] | 222 | |
---|
[1322] | 223 | } while (newline); |
---|
[1313] | 224 | } |
---|
| 225 | |
---|
[1312] | 226 | void Shell::inputChanged() |
---|
[1313] | 227 | { |
---|
| 228 | SHELL_UPDATE_LISTENERS(inputChanged); |
---|
| 229 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 230 | } |
---|
| 231 | |
---|
[1312] | 232 | void Shell::execute() |
---|
[1313] | 233 | { |
---|
[1324] | 234 | this->addToHistory(this->inputBuffer_.get()); |
---|
[1322] | 235 | this->addLine(this->inputBuffer_.get(), 0); |
---|
| 236 | |
---|
| 237 | if (!CommandExecutor::execute(this->inputBuffer_.get())) |
---|
[1313] | 238 | this->addLine("Error: Can't execute \"" + this->inputBuffer_.get() + "\".", 1); |
---|
| 239 | |
---|
| 240 | this->clear(); |
---|
| 241 | } |
---|
| 242 | |
---|
[1312] | 243 | void Shell::hintandcomplete() |
---|
[1313] | 244 | { |
---|
[1334] | 245 | this->addLine(CommandExecutor::hint(this->inputBuffer_.get()), -1); |
---|
[1313] | 246 | this->inputBuffer_.set(CommandExecutor::complete(this->inputBuffer_.get())); |
---|
| 247 | |
---|
| 248 | this->inputChanged(); |
---|
| 249 | } |
---|
| 250 | |
---|
[1312] | 251 | void Shell::backspace() |
---|
[1313] | 252 | { |
---|
| 253 | this->inputBuffer_.removeBehindCursor(); |
---|
| 254 | SHELL_UPDATE_LISTENERS(inputChanged); |
---|
| 255 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | void Shell::deletechar() |
---|
| 259 | { |
---|
| 260 | this->inputBuffer_.removeAtCursor(); |
---|
| 261 | SHELL_UPDATE_LISTENERS(inputChanged); |
---|
| 262 | } |
---|
| 263 | |
---|
[1312] | 264 | void Shell::clear() |
---|
[1313] | 265 | { |
---|
| 266 | this->inputBuffer_.clear(); |
---|
[1324] | 267 | this->historyPosition_ = 0; |
---|
[1313] | 268 | SHELL_UPDATE_LISTENERS(inputChanged); |
---|
| 269 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 270 | } |
---|
| 271 | |
---|
[1312] | 272 | void Shell::cursor_right() |
---|
[1313] | 273 | { |
---|
| 274 | this->inputBuffer_.increaseCursor(); |
---|
| 275 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 276 | } |
---|
| 277 | |
---|
[1312] | 278 | void Shell::cursor_left() |
---|
[1313] | 279 | { |
---|
| 280 | this->inputBuffer_.decreaseCursor(); |
---|
| 281 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | void Shell::cursor_end() |
---|
| 285 | { |
---|
| 286 | this->inputBuffer_.setCursorToEnd(); |
---|
| 287 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | void Shell::cursor_home() |
---|
| 291 | { |
---|
| 292 | this->inputBuffer_.setCursorToBegin(); |
---|
| 293 | SHELL_UPDATE_LISTENERS(cursorChanged); |
---|
| 294 | } |
---|
| 295 | |
---|
[1312] | 296 | void Shell::history_up() |
---|
[1313] | 297 | { |
---|
[1324] | 298 | if (this->historyPosition_ < this->commandHistory_.size()) |
---|
[1313] | 299 | { |
---|
| 300 | this->historyPosition_++; |
---|
| 301 | this->inputBuffer_.set(this->getFromHistory()); |
---|
| 302 | } |
---|
| 303 | } |
---|
| 304 | |
---|
[1312] | 305 | void Shell::history_down() |
---|
[1313] | 306 | { |
---|
| 307 | if (this->historyPosition_ > 0) |
---|
| 308 | { |
---|
[1322] | 309 | this->historyPosition_--; |
---|
[1313] | 310 | this->inputBuffer_.set(this->getFromHistory()); |
---|
| 311 | } |
---|
| 312 | } |
---|
| 313 | |
---|
[1312] | 314 | void Shell::scroll_up() |
---|
[1313] | 315 | { |
---|
| 316 | if (this->scrollIterator_ != this->lines_.end()) |
---|
| 317 | { |
---|
| 318 | ++this->scrollIterator_; |
---|
| 319 | ++this->scrollPosition_; |
---|
| 320 | |
---|
| 321 | SHELL_UPDATE_LISTENERS(linesChanged); |
---|
| 322 | } |
---|
| 323 | } |
---|
| 324 | |
---|
[1312] | 325 | void Shell::scroll_down() |
---|
[1313] | 326 | { |
---|
| 327 | if (this->scrollIterator_ != this->lines_.begin()) |
---|
| 328 | { |
---|
| 329 | --this->scrollIterator_; |
---|
| 330 | --this->scrollPosition_; |
---|
| 331 | |
---|
| 332 | SHELL_UPDATE_LISTENERS(linesChanged); |
---|
| 333 | } |
---|
| 334 | } |
---|
| 335 | |
---|
[1312] | 336 | void Shell::exit() |
---|
[1313] | 337 | { |
---|
| 338 | if (this->inputBuffer_.getSize() > 0) |
---|
| 339 | { |
---|
| 340 | this->clear(); |
---|
| 341 | return; |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | this->clear(); |
---|
[1326] | 345 | this->scrollPosition_ = 0; |
---|
| 346 | this->scrollIterator_ = this->lines_.begin(); |
---|
| 347 | |
---|
[1313] | 348 | SHELL_UPDATE_LISTENERS(exit); |
---|
| 349 | } |
---|
[1312] | 350 | } |
---|