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