[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 "CommandExecutor.h" |
---|
[3196] | 30 | |
---|
| 31 | #include "util/Debug.h" |
---|
[3280] | 32 | #include "util/StringUtils.h" |
---|
[1505] | 33 | #include "ConsoleCommand.h" |
---|
| 34 | #include "Identifier.h" |
---|
| 35 | #include "Language.h" |
---|
| 36 | #include "TclBind.h" |
---|
| 37 | |
---|
| 38 | namespace orxonox |
---|
| 39 | { |
---|
| 40 | CommandExecutor& CommandExecutor::getInstance() |
---|
| 41 | { |
---|
| 42 | static CommandExecutor instance; |
---|
| 43 | return instance; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | CommandEvaluation& CommandExecutor::getEvaluation() |
---|
| 47 | { |
---|
| 48 | return CommandExecutor::getInstance().evaluation_; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | const CommandEvaluation& CommandExecutor::getLastEvaluation() |
---|
| 52 | { |
---|
| 53 | return CommandExecutor::getInstance().evaluation_; |
---|
| 54 | } |
---|
| 55 | |
---|
[2662] | 56 | ConsoleCommand& CommandExecutor::addConsoleCommandShortcut(ConsoleCommand* command, bool bDeleteAtExit) |
---|
[1505] | 57 | { |
---|
| 58 | std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_.find(command->getName()); |
---|
| 59 | if (it != CommandExecutor::getInstance().consoleCommandShortcuts_.end()) |
---|
| 60 | { |
---|
[6417] | 61 | COUT(2) << "Warning: Overwriting console-command shortcut with name " << command->getName() << '.' << std::endl; |
---|
[1505] | 62 | } |
---|
| 63 | |
---|
[2662] | 64 | // Make sure we can also delete the external ConsoleCommands that don't belong to an Identifier |
---|
| 65 | if (command && bDeleteAtExit) |
---|
| 66 | { |
---|
| 67 | CommandExecutor::getInstance().consoleCommandExternals_.insert(command); |
---|
| 68 | } |
---|
[1505] | 69 | |
---|
| 70 | CommandExecutor::getInstance().consoleCommandShortcuts_[command->getName()] = command; |
---|
| 71 | CommandExecutor::getInstance().consoleCommandShortcuts_LC_[getLowercase(command->getName())] = command; |
---|
| 72 | return (*command); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | @brief Returns the executor of a console command shortcut with given name. |
---|
| 77 | @brief name The name of the requested console command shortcut |
---|
| 78 | @return The executor of the requested console command shortcut |
---|
| 79 | */ |
---|
| 80 | ConsoleCommand* CommandExecutor::getConsoleCommandShortcut(const std::string& name) |
---|
| 81 | { |
---|
| 82 | std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_.find(name); |
---|
| 83 | if (it != CommandExecutor::getInstance().consoleCommandShortcuts_.end()) |
---|
[6417] | 84 | return it->second; |
---|
[1505] | 85 | else |
---|
| 86 | return 0; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | @brief Returns the executor of a console command shortcut with given name in lowercase. |
---|
| 91 | @brief name The name of the requested console command shortcut in lowercase |
---|
| 92 | @return The executor of the requested console command shortcut |
---|
| 93 | */ |
---|
| 94 | ConsoleCommand* CommandExecutor::getLowercaseConsoleCommandShortcut(const std::string& name) |
---|
| 95 | { |
---|
| 96 | std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_LC_.find(name); |
---|
| 97 | if (it != CommandExecutor::getInstance().consoleCommandShortcuts_LC_.end()) |
---|
[6417] | 98 | return it->second; |
---|
[1505] | 99 | else |
---|
| 100 | return 0; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | bool CommandExecutor::execute(const std::string& command, bool useTcl) |
---|
| 104 | { |
---|
| 105 | if (useTcl) |
---|
[7189] | 106 | { |
---|
| 107 | bool success; |
---|
| 108 | TclBind::eval(command, &success); |
---|
| 109 | return success; |
---|
| 110 | } |
---|
| 111 | else |
---|
| 112 | { |
---|
| 113 | CommandExecutor::parseIfNeeded(command); |
---|
| 114 | return CommandExecutor::getEvaluation().execute(); |
---|
| 115 | } |
---|
[1505] | 116 | } |
---|
| 117 | |
---|
[7189] | 118 | MultiType CommandExecutor::queryMT(const std::string& command, bool* success, bool useTcl) |
---|
[3035] | 119 | { |
---|
[7189] | 120 | if (useTcl) |
---|
| 121 | { |
---|
| 122 | return TclBind::eval(command, success); |
---|
| 123 | } |
---|
| 124 | else |
---|
| 125 | { |
---|
| 126 | CommandExecutor::parseIfNeeded(command); |
---|
| 127 | return CommandExecutor::getEvaluation().query(success); |
---|
| 128 | } |
---|
[3035] | 129 | } |
---|
| 130 | |
---|
[7189] | 131 | std::string CommandExecutor::query(const std::string& command, bool* success, bool useTcl) |
---|
[3035] | 132 | { |
---|
[7189] | 133 | if (useTcl) |
---|
| 134 | { |
---|
| 135 | return TclBind::eval(command, success); |
---|
| 136 | } |
---|
| 137 | else |
---|
| 138 | { |
---|
| 139 | CommandExecutor::parseIfNeeded(command); |
---|
| 140 | return CommandExecutor::getEvaluation().query(success).getString(); |
---|
| 141 | } |
---|
[3035] | 142 | } |
---|
| 143 | |
---|
[1505] | 144 | std::string CommandExecutor::complete(const std::string& command) |
---|
| 145 | { |
---|
| 146 | CommandExecutor::parseIfNeeded(command); |
---|
| 147 | return CommandExecutor::getEvaluation().complete(); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | std::string CommandExecutor::hint(const std::string& command) |
---|
| 151 | { |
---|
| 152 | CommandExecutor::parseIfNeeded(command); |
---|
| 153 | return CommandExecutor::getEvaluation().hint(); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | CommandEvaluation CommandExecutor::evaluate(const std::string& command) |
---|
| 157 | { |
---|
| 158 | CommandExecutor::parse(command); |
---|
| 159 | CommandExecutor::getEvaluation().evaluateParams(); |
---|
| 160 | return CommandExecutor::getEvaluation(); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | void CommandExecutor::parseIfNeeded(const std::string& command) |
---|
| 164 | { |
---|
[3280] | 165 | if (CommandExecutor::getEvaluation().state_ == CommandState::Uninitialized) |
---|
[1505] | 166 | { |
---|
| 167 | CommandExecutor::parse(command); |
---|
| 168 | } |
---|
| 169 | else if (CommandExecutor::getEvaluation().originalCommand_ != command) |
---|
| 170 | { |
---|
| 171 | if (CommandExecutor::getEvaluation().command_ == command) |
---|
| 172 | { |
---|
| 173 | CommandExecutor::parse(command); |
---|
| 174 | CommandExecutor::getEvaluation().bNewCommand_ = false; |
---|
| 175 | } |
---|
| 176 | else |
---|
| 177 | { |
---|
| 178 | CommandExecutor::parse(command); |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | void CommandExecutor::parse(const std::string& command, bool bInitialize) |
---|
| 184 | { |
---|
| 185 | if (bInitialize) |
---|
| 186 | CommandExecutor::getEvaluation().initialize(command); |
---|
| 187 | |
---|
| 188 | CommandExecutor::getEvaluation().commandTokens_.split(command, " ", SubString::WhiteSpaces, false, '\\', false, '"', false, '(', ')', false, '\0'); |
---|
| 189 | CommandExecutor::getEvaluation().command_ = command; |
---|
| 190 | |
---|
| 191 | switch (CommandExecutor::getEvaluation().state_) |
---|
| 192 | { |
---|
[3280] | 193 | case CommandState::Uninitialized: |
---|
[1505] | 194 | { |
---|
| 195 | // Impossible |
---|
| 196 | break; |
---|
| 197 | } |
---|
[3280] | 198 | case CommandState::Empty: |
---|
[1505] | 199 | { |
---|
| 200 | if (CommandExecutor::argumentsGiven() == 0) |
---|
| 201 | { |
---|
| 202 | CommandExecutor::createListOfPossibleFunctions(""); |
---|
| 203 | CommandExecutor::createListOfPossibleIdentifiers(""); |
---|
| 204 | break; |
---|
| 205 | } |
---|
| 206 | else |
---|
| 207 | { |
---|
[3280] | 208 | CommandExecutor::getEvaluation().state_ = CommandState::ShortcutOrIdentifier; |
---|
[1505] | 209 | // Move on to next case |
---|
| 210 | } |
---|
| 211 | } |
---|
[3280] | 212 | case CommandState::ShortcutOrIdentifier: |
---|
[1505] | 213 | { |
---|
| 214 | if (CommandExecutor::argumentsGiven() > 1) |
---|
| 215 | { |
---|
| 216 | // There's a finished first argument - check if it's a shortcut or a classname |
---|
| 217 | CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(0)); |
---|
| 218 | CommandExecutor::getEvaluation().functionclass_ = CommandExecutor::getPossibleIdentifier(CommandExecutor::getArgument(0)); |
---|
| 219 | |
---|
| 220 | if (CommandExecutor::getEvaluation().function_) |
---|
| 221 | { |
---|
| 222 | // It's a shortcut |
---|
[3280] | 223 | CommandExecutor::getEvaluation().state_ = CommandState::ParamPreparation; |
---|
[1505] | 224 | CommandExecutor::getEvaluation().functionclass_ = 0; |
---|
| 225 | // Move on to next case |
---|
| 226 | } |
---|
| 227 | else if (CommandExecutor::getEvaluation().functionclass_) |
---|
| 228 | { |
---|
| 229 | // It's a functionname |
---|
[3280] | 230 | CommandExecutor::getEvaluation().state_ = CommandState::Function; |
---|
[1505] | 231 | CommandExecutor::getEvaluation().function_ = 0; |
---|
| 232 | // Move on to next case |
---|
| 233 | } |
---|
| 234 | else |
---|
| 235 | { |
---|
| 236 | // The first argument is bad |
---|
[3280] | 237 | CommandExecutor::getEvaluation().state_ = CommandState::Error; |
---|
[1505] | 238 | AddLanguageEntry("commandexecutorunknownfirstargument", "is not a shortcut nor a classname"); |
---|
[6417] | 239 | CommandExecutor::getEvaluation().errorMessage_ = "Error: " + CommandExecutor::getArgument(0) + ' ' + GetLocalisation("commandexecutorunknownfirstargument") + '.'; |
---|
[1505] | 240 | return; |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | else |
---|
| 244 | { |
---|
| 245 | // There's no finished first argument - search possible shortcuts or classnames |
---|
| 246 | CommandExecutor::createListOfPossibleFunctions(CommandExecutor::getArgument(0)); |
---|
| 247 | CommandExecutor::createListOfPossibleIdentifiers(CommandExecutor::getArgument(0)); |
---|
| 248 | |
---|
| 249 | unsigned int num_functions = CommandExecutor::getEvaluation().listOfPossibleFunctions_.size(); |
---|
| 250 | unsigned int num_identifiers = CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.size(); |
---|
| 251 | |
---|
| 252 | if (num_functions == 1 && num_identifiers == 0) |
---|
| 253 | { |
---|
| 254 | // It's a shortcut |
---|
[6417] | 255 | const std::string& functionname = *CommandExecutor::getEvaluation().listOfPossibleFunctions_.begin()->first; |
---|
[1505] | 256 | CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(functionname); |
---|
| 257 | if (getLowercase(functionname) != getLowercase(CommandExecutor::getArgument(0))) |
---|
| 258 | { |
---|
| 259 | // Unfinished shortcut |
---|
| 260 | CommandExecutor::getEvaluation().bCommandChanged_ = true; |
---|
| 261 | } |
---|
[3280] | 262 | CommandExecutor::getEvaluation().state_ = CommandState::ParamPreparation; |
---|
[1505] | 263 | CommandExecutor::getEvaluation().functionclass_ = 0; |
---|
| 264 | CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().function_->getName(); |
---|
| 265 | if (CommandExecutor::getEvaluation().function_->getParamCount() > 0) |
---|
| 266 | { |
---|
[6417] | 267 | CommandExecutor::getEvaluation().command_ += ' '; |
---|
[1505] | 268 | CommandExecutor::getEvaluation().bCommandChanged_ = true; |
---|
| 269 | } |
---|
| 270 | // Move on to next case |
---|
| 271 | } |
---|
| 272 | else if (num_identifiers == 1 && num_functions == 0) |
---|
| 273 | { |
---|
| 274 | // It's a classname |
---|
[6417] | 275 | const std::string& classname = *CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.begin()->first; |
---|
[1505] | 276 | CommandExecutor::getEvaluation().functionclass_ = CommandExecutor::getPossibleIdentifier(classname); |
---|
| 277 | if (getLowercase(classname) != getLowercase(CommandExecutor::getArgument(0))) |
---|
| 278 | { |
---|
| 279 | // Unfinished classname |
---|
| 280 | CommandExecutor::getEvaluation().bCommandChanged_ = true; |
---|
| 281 | } |
---|
[3280] | 282 | CommandExecutor::getEvaluation().state_ = CommandState::Function; |
---|
[1505] | 283 | CommandExecutor::getEvaluation().function_ = 0; |
---|
[6417] | 284 | CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().functionclass_->getName() + ' '; |
---|
[1505] | 285 | // Move on to next case |
---|
| 286 | } |
---|
| 287 | else if (num_identifiers == 0 && num_functions == 0) |
---|
| 288 | { |
---|
| 289 | // No possibilities |
---|
[3280] | 290 | CommandExecutor::getEvaluation().state_ = CommandState::Error; |
---|
[1505] | 291 | AddLanguageEntry("commandexecutorunknownfirstargumentstart", "There is no command or classname starting with"); |
---|
[6417] | 292 | CommandExecutor::getEvaluation().errorMessage_ = "Error: " + GetLocalisation("commandexecutorunknownfirstargumentstart") + ' ' + CommandExecutor::getArgument(0) + '.'; |
---|
[1505] | 293 | return; |
---|
| 294 | } |
---|
| 295 | else |
---|
| 296 | { |
---|
| 297 | // There are several possiblilities |
---|
| 298 | std::list<std::pair<const std::string*, const std::string*> > temp; |
---|
| 299 | temp.insert(temp.end(), CommandExecutor::getEvaluation().listOfPossibleFunctions_.begin(), CommandExecutor::getEvaluation().listOfPossibleFunctions_.end()); |
---|
| 300 | temp.insert(temp.end(), CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.begin(), CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.end()); |
---|
| 301 | CommandExecutor::getEvaluation().command_ = CommandExecutor::getCommonBegin(temp); |
---|
| 302 | CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(0)); |
---|
| 303 | CommandExecutor::getEvaluation().functionclass_ = CommandExecutor::getPossibleIdentifier(CommandExecutor::getArgument(0)); |
---|
| 304 | CommandExecutor::getEvaluation().bCommandChanged_ = true; |
---|
| 305 | return; |
---|
| 306 | } |
---|
| 307 | } |
---|
| 308 | } |
---|
[3280] | 309 | case CommandState::Function: |
---|
[1505] | 310 | { |
---|
| 311 | if (CommandExecutor::getEvaluation().functionclass_) |
---|
| 312 | { |
---|
| 313 | // There is a classname - search for the commandname |
---|
| 314 | if (CommandExecutor::argumentsGiven() > 2) |
---|
| 315 | { |
---|
| 316 | // There is a finished second argument - check if it's a commandname |
---|
| 317 | CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(1), CommandExecutor::getEvaluation().functionclass_); |
---|
| 318 | |
---|
| 319 | if (CommandExecutor::getEvaluation().function_) |
---|
| 320 | { |
---|
| 321 | // It's a function |
---|
[3280] | 322 | CommandExecutor::getEvaluation().state_ = CommandState::ParamPreparation; |
---|
[1505] | 323 | // Move on to next case |
---|
| 324 | } |
---|
| 325 | else |
---|
| 326 | { |
---|
| 327 | // The second argument is bad |
---|
[3280] | 328 | CommandExecutor::getEvaluation().state_ = CommandState::Error; |
---|
[1505] | 329 | AddLanguageEntry("commandexecutorunknownsecondargument", "is not a function of"); |
---|
| 330 | CommandExecutor::getEvaluation().errorMessage_ = "Error: " + CommandExecutor::getArgument(1) + " " + GetLocalisation("commandexecutorunknownsecondargument") + " " + CommandExecutor::getEvaluation().functionclass_->getName() + "."; |
---|
| 331 | return; |
---|
| 332 | } |
---|
| 333 | } |
---|
| 334 | else |
---|
| 335 | { |
---|
| 336 | // There is no finished second argument - search for possibilities |
---|
| 337 | CommandExecutor::createListOfPossibleFunctions(CommandExecutor::getArgument(1), CommandExecutor::getEvaluation().functionclass_); |
---|
| 338 | unsigned int num_functions = CommandExecutor::getEvaluation().listOfPossibleFunctions_.size(); |
---|
| 339 | |
---|
| 340 | if (num_functions == 1) |
---|
| 341 | { |
---|
| 342 | // It's a function |
---|
[6417] | 343 | const std::string& functionname = *CommandExecutor::getEvaluation().listOfPossibleFunctions_.begin()->first; |
---|
[1505] | 344 | CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(functionname, CommandExecutor::getEvaluation().functionclass_); |
---|
| 345 | if (getLowercase(functionname) != getLowercase(CommandExecutor::getArgument(1))) |
---|
| 346 | { |
---|
| 347 | // Unfinished function |
---|
| 348 | CommandExecutor::getEvaluation().bCommandChanged_ = true; |
---|
| 349 | } |
---|
[3280] | 350 | CommandExecutor::getEvaluation().state_ = CommandState::ParamPreparation; |
---|
[6417] | 351 | CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().functionclass_->getName() + ' ' + CommandExecutor::getEvaluation().function_->getName(); |
---|
[1505] | 352 | if (CommandExecutor::getEvaluation().function_->getParamCount() > 0) |
---|
| 353 | { |
---|
[6417] | 354 | CommandExecutor::getEvaluation().command_ += ' '; |
---|
[1505] | 355 | CommandExecutor::getEvaluation().bCommandChanged_ = true; |
---|
| 356 | } |
---|
| 357 | // Move on to next case |
---|
| 358 | } |
---|
| 359 | else if (num_functions == 0) |
---|
| 360 | { |
---|
| 361 | // No possibilities |
---|
[3280] | 362 | CommandExecutor::getEvaluation().state_ = CommandState::Error; |
---|
[1505] | 363 | AddLanguageEntry("commandexecutorunknownsecondargumentstart", "has no function starting with"); |
---|
[6417] | 364 | CommandExecutor::getEvaluation().errorMessage_ = "Error: " + CommandExecutor::getEvaluation().functionclass_->getName() + ' ' + GetLocalisation("commandexecutorunknownsecondargumentstart") + ' ' + CommandExecutor::getArgument(1) + '.'; |
---|
[1505] | 365 | return; |
---|
| 366 | } |
---|
| 367 | else |
---|
| 368 | { |
---|
| 369 | // There are several possibilities |
---|
[6417] | 370 | CommandExecutor::getEvaluation().command_ = CommandExecutor::getEvaluation().functionclass_->getName() + ' ' + CommandExecutor::getCommonBegin(CommandExecutor::getEvaluation().listOfPossibleFunctions_); |
---|
[1505] | 371 | CommandExecutor::getEvaluation().function_ = CommandExecutor::getPossibleCommand(CommandExecutor::getArgument(1), CommandExecutor::getEvaluation().functionclass_); |
---|
| 372 | CommandExecutor::getEvaluation().bCommandChanged_ = true; |
---|
| 373 | return; |
---|
| 374 | } |
---|
| 375 | } |
---|
| 376 | } |
---|
| 377 | else |
---|
| 378 | { |
---|
[3280] | 379 | // There is no classname - move on to CommandState::ParamPreparation |
---|
[1505] | 380 | } |
---|
| 381 | } |
---|
[3280] | 382 | case CommandState::ParamPreparation: |
---|
[1505] | 383 | { |
---|
| 384 | if (CommandExecutor::getEvaluation().function_->getParamCount() == 0 || CommandExecutor::enoughArgumentsGiven(CommandExecutor::getEvaluation().function_)) |
---|
| 385 | { |
---|
[3280] | 386 | CommandExecutor::getEvaluation().state_ = CommandState::Finished; |
---|
[1505] | 387 | return; |
---|
| 388 | } |
---|
| 389 | else |
---|
| 390 | { |
---|
| 391 | unsigned int argumentNumber = CommandExecutor::argumentsGiven() - 2; |
---|
| 392 | if (CommandExecutor::getEvaluation().functionclass_) |
---|
| 393 | argumentNumber -= 1; |
---|
| 394 | |
---|
| 395 | CommandExecutor::createListOfPossibleArguments(CommandExecutor::getLastArgument(), CommandExecutor::getEvaluation().function_, argumentNumber); |
---|
[3280] | 396 | CommandExecutor::getEvaluation().state_ = CommandState::Params; |
---|
[1505] | 397 | |
---|
| 398 | if (CommandExecutor::getEvaluation().bCommandChanged_) |
---|
| 399 | { |
---|
| 400 | // Don't do more than one change |
---|
| 401 | return; |
---|
| 402 | } |
---|
| 403 | } |
---|
| 404 | } |
---|
[3280] | 405 | case CommandState::Params: |
---|
[1505] | 406 | { |
---|
| 407 | if (CommandExecutor::getEvaluation().listOfPossibleArguments_.size() == 1) |
---|
| 408 | { |
---|
| 409 | // There is exactly one possible argument |
---|
[6417] | 410 | CommandExecutor::getEvaluation().argument_ = CommandExecutor::getEvaluation().listOfPossibleArguments_.begin()->getString(); |
---|
| 411 | CommandExecutor::getEvaluation().possibleArgument_ = CommandExecutor::getEvaluation().listOfPossibleArguments_.begin()->getString(); |
---|
[3280] | 412 | CommandExecutor::getEvaluation().state_ = CommandState::ParamPreparation; |
---|
[1505] | 413 | return; |
---|
| 414 | } |
---|
| 415 | else if (CommandExecutor::getEvaluation().listOfPossibleArguments_.size() == 0) |
---|
| 416 | { |
---|
| 417 | // The user tries something new - we let him do |
---|
[3280] | 418 | CommandExecutor::getEvaluation().state_ = CommandState::ParamPreparation; |
---|
[1505] | 419 | CommandExecutor::getEvaluation().argument_ = CommandExecutor::getLastArgument(); |
---|
| 420 | return; |
---|
| 421 | } |
---|
| 422 | else |
---|
| 423 | { |
---|
| 424 | // There are several possibilities |
---|
| 425 | unsigned int argumentNumber = CommandExecutor::argumentsGiven(); |
---|
| 426 | if (argumentNumber > 0) |
---|
| 427 | --argumentNumber; |
---|
| 428 | if (CommandExecutor::getEvaluation().functionclass_ && argumentNumber > 0) |
---|
| 429 | --argumentNumber; |
---|
| 430 | |
---|
| 431 | CommandExecutor::getEvaluation().argument_ = CommandExecutor::getCommonBegin(CommandExecutor::getEvaluation().listOfPossibleArguments_); |
---|
| 432 | CommandExecutor::getEvaluation().possibleArgument_ = CommandExecutor::getPossibleArgument(CommandExecutor::getLastArgument(), CommandExecutor::getEvaluation().function_, argumentNumber); |
---|
[3280] | 433 | CommandExecutor::getEvaluation().state_ = CommandState::ParamPreparation; |
---|
[1505] | 434 | return; |
---|
| 435 | } |
---|
| 436 | } |
---|
[3280] | 437 | case CommandState::Finished: |
---|
[1505] | 438 | { |
---|
| 439 | // Nothing more to do |
---|
| 440 | break; |
---|
| 441 | } |
---|
[3280] | 442 | case CommandState::Error: |
---|
[1505] | 443 | { |
---|
| 444 | // Bad, very bad |
---|
| 445 | break; |
---|
| 446 | } |
---|
| 447 | } |
---|
| 448 | } |
---|
| 449 | |
---|
| 450 | unsigned int CommandExecutor::argumentsFinished() |
---|
| 451 | { |
---|
| 452 | unsigned int argumentsGiven = CommandExecutor::argumentsGiven(); |
---|
| 453 | if (argumentsGiven > 0) |
---|
| 454 | return argumentsGiven - 1; |
---|
| 455 | else |
---|
| 456 | return 0; |
---|
| 457 | } |
---|
| 458 | |
---|
| 459 | unsigned int CommandExecutor::argumentsGiven() |
---|
| 460 | { |
---|
| 461 | if (CommandExecutor::getEvaluation().command_.size() > 0 && CommandExecutor::getEvaluation().command_[CommandExecutor::getEvaluation().command_.size() - 1] == ' ') |
---|
| 462 | return CommandExecutor::getEvaluation().commandTokens_.size() + 1; |
---|
| 463 | else |
---|
| 464 | return CommandExecutor::getEvaluation().commandTokens_.size(); |
---|
| 465 | } |
---|
| 466 | |
---|
| 467 | bool CommandExecutor::enoughArgumentsGiven(ConsoleCommand* command) |
---|
| 468 | { |
---|
| 469 | if (CommandExecutor::getEvaluation().functionclass_) |
---|
| 470 | return (CommandExecutor::argumentsGiven() > (2 + command->getParamCount())); |
---|
| 471 | else |
---|
| 472 | return (CommandExecutor::argumentsGiven() > (1 + command->getParamCount())); |
---|
| 473 | } |
---|
| 474 | |
---|
[6417] | 475 | const std::string& CommandExecutor::getArgument(unsigned int index) |
---|
[1505] | 476 | { |
---|
| 477 | if (index < (CommandExecutor::getEvaluation().commandTokens_.size())) |
---|
| 478 | return CommandExecutor::getEvaluation().commandTokens_[index]; |
---|
| 479 | else |
---|
[6417] | 480 | return BLANKSTRING; |
---|
[1505] | 481 | } |
---|
| 482 | |
---|
[6417] | 483 | const std::string& CommandExecutor::getLastArgument() |
---|
[1505] | 484 | { |
---|
| 485 | return CommandExecutor::getArgument(CommandExecutor::argumentsGiven() - 1); |
---|
| 486 | } |
---|
| 487 | |
---|
| 488 | void CommandExecutor::createListOfPossibleIdentifiers(const std::string& fragment) |
---|
| 489 | { |
---|
| 490 | CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.clear(); |
---|
[6417] | 491 | const std::string& lowercase = getLowercase(fragment); |
---|
[5929] | 492 | for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getLowercaseStringIdentifierMapBegin(); it != Identifier::getLowercaseStringIdentifierMapEnd(); ++it) |
---|
[6417] | 493 | if (it->second->hasConsoleCommands()) |
---|
| 494 | if (it->first.find(lowercase) == 0 || fragment.empty()) |
---|
| 495 | CommandExecutor::getEvaluation().listOfPossibleIdentifiers_.push_back(std::pair<const std::string*, const std::string*>(&it->first, &it->second->getName())); |
---|
[1505] | 496 | } |
---|
| 497 | |
---|
| 498 | void CommandExecutor::createListOfPossibleFunctions(const std::string& fragment, Identifier* identifier) |
---|
| 499 | { |
---|
| 500 | CommandExecutor::getEvaluation().listOfPossibleFunctions_.clear(); |
---|
[6417] | 501 | const std::string& lowercase = getLowercase(fragment); |
---|
[1505] | 502 | if (!identifier) |
---|
| 503 | { |
---|
| 504 | for (std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getLowercaseConsoleCommandShortcutMapBegin(); it != CommandExecutor::getLowercaseConsoleCommandShortcutMapEnd(); ++it) |
---|
[6417] | 505 | if (it->first.find(lowercase) == 0 || fragment.empty()) |
---|
| 506 | CommandExecutor::getEvaluation().listOfPossibleFunctions_.push_back(std::pair<const std::string*, const std::string*>(&it->first, &it->second->getName())); |
---|
[1505] | 507 | } |
---|
| 508 | else |
---|
| 509 | { |
---|
| 510 | for (std::map<std::string, ConsoleCommand*>::const_iterator it = identifier->getLowercaseConsoleCommandMapBegin(); it != identifier->getLowercaseConsoleCommandMapEnd(); ++it) |
---|
[6417] | 511 | if (it->first.find(lowercase) == 0 || fragment.empty()) |
---|
| 512 | CommandExecutor::getEvaluation().listOfPossibleFunctions_.push_back(std::pair<const std::string*, const std::string*>(&it->first, &it->second->getName())); |
---|
[1505] | 513 | } |
---|
| 514 | } |
---|
| 515 | |
---|
| 516 | void CommandExecutor::createListOfPossibleArguments(const std::string& fragment, ConsoleCommand* command, unsigned int param) |
---|
| 517 | { |
---|
| 518 | CommandExecutor::createArgumentCompletionList(command, param); |
---|
| 519 | |
---|
| 520 | CommandExecutor::getEvaluation().listOfPossibleArguments_.clear(); |
---|
[6417] | 521 | const std::string& lowercase = getLowercase(fragment); |
---|
[1505] | 522 | for (ArgumentCompletionList::const_iterator it = command->getArgumentCompletionListBegin(); it != command->getArgumentCompletionListEnd(); ++it) |
---|
| 523 | { |
---|
[6417] | 524 | if (it->lowercaseComparison()) |
---|
[1505] | 525 | { |
---|
[6417] | 526 | if (it->getComparable().find(lowercase) == 0 || fragment.empty()) |
---|
[1505] | 527 | CommandExecutor::getEvaluation().listOfPossibleArguments_.push_back(*it); |
---|
| 528 | } |
---|
| 529 | else |
---|
| 530 | { |
---|
[6417] | 531 | if (it->getComparable().find(fragment) == 0 || fragment.empty()) |
---|
[1505] | 532 | CommandExecutor::getEvaluation().listOfPossibleArguments_.push_back(*it); |
---|
| 533 | } |
---|
| 534 | } |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | Identifier* CommandExecutor::getPossibleIdentifier(const std::string& name) |
---|
| 538 | { |
---|
[6417] | 539 | const std::string& lowercase = getLowercase(name); |
---|
[5929] | 540 | std::map<std::string, Identifier*>::const_iterator it = Identifier::getLowercaseStringIdentifierMap().find(lowercase); |
---|
[6417] | 541 | if ((it != Identifier::getLowercaseStringIdentifierMapEnd()) && it->second->hasConsoleCommands()) |
---|
| 542 | return it->second; |
---|
[1505] | 543 | |
---|
| 544 | return 0; |
---|
| 545 | } |
---|
| 546 | |
---|
| 547 | ConsoleCommand* CommandExecutor::getPossibleCommand(const std::string& name, Identifier* identifier) |
---|
| 548 | { |
---|
[6417] | 549 | const std::string& lowercase = getLowercase(name); |
---|
[1505] | 550 | if (!identifier) |
---|
| 551 | { |
---|
| 552 | std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getLowercaseConsoleCommandShortcutMap().find(lowercase); |
---|
| 553 | if (it != CommandExecutor::getLowercaseConsoleCommandShortcutMapEnd()) |
---|
[6417] | 554 | return it->second; |
---|
[1505] | 555 | } |
---|
| 556 | else |
---|
| 557 | { |
---|
| 558 | std::map<std::string, ConsoleCommand*>::const_iterator it = identifier->getLowercaseConsoleCommandMap().find(lowercase); |
---|
| 559 | if (it != identifier->getLowercaseConsoleCommandMapEnd()) |
---|
[6417] | 560 | return it->second; |
---|
[1505] | 561 | } |
---|
| 562 | return 0; |
---|
| 563 | } |
---|
| 564 | |
---|
[6417] | 565 | const std::string& CommandExecutor::getPossibleArgument(const std::string& name, ConsoleCommand* command, unsigned int param) |
---|
[1505] | 566 | { |
---|
| 567 | CommandExecutor::createArgumentCompletionList(command, param); |
---|
| 568 | |
---|
[6417] | 569 | const std::string& lowercase = getLowercase(name); |
---|
[1505] | 570 | for (ArgumentCompletionList::const_iterator it = command->getArgumentCompletionListBegin(); it != command->getArgumentCompletionListEnd(); ++it) |
---|
| 571 | { |
---|
[6417] | 572 | if (it->lowercaseComparison()) |
---|
[1505] | 573 | { |
---|
[6417] | 574 | if (it->getComparable() == lowercase) |
---|
| 575 | return it->getString(); |
---|
[1505] | 576 | } |
---|
| 577 | else |
---|
| 578 | { |
---|
[6417] | 579 | if (it->getComparable() == name) |
---|
| 580 | return it->getString(); |
---|
[1505] | 581 | } |
---|
| 582 | } |
---|
| 583 | |
---|
[6417] | 584 | return BLANKSTRING; |
---|
[1505] | 585 | } |
---|
| 586 | |
---|
| 587 | void CommandExecutor::createArgumentCompletionList(ConsoleCommand* command, unsigned int param) |
---|
| 588 | { |
---|
| 589 | std::string params[5]; |
---|
| 590 | |
---|
| 591 | unsigned int index = 0; |
---|
| 592 | unsigned int lowestIndex = 1 + (CommandExecutor::getEvaluation().functionclass_ != 0); |
---|
| 593 | |
---|
| 594 | for (unsigned int i = CommandExecutor::argumentsGiven() - 1; i >= lowestIndex; --i) |
---|
| 595 | { |
---|
| 596 | params[index] = CommandExecutor::getArgument(i); |
---|
| 597 | ++index; |
---|
| 598 | if (index >= 5) |
---|
| 599 | break; |
---|
| 600 | } |
---|
| 601 | |
---|
| 602 | command->createArgumentCompletionList(param, params[0], params[1], params[2], params[3], params[4]); |
---|
| 603 | } |
---|
| 604 | |
---|
| 605 | std::string CommandExecutor::getCommonBegin(const std::list<std::pair<const std::string*, const std::string*> >& list) |
---|
| 606 | { |
---|
| 607 | if (list.size() == 0) |
---|
| 608 | { |
---|
| 609 | return ""; |
---|
| 610 | } |
---|
| 611 | else if (list.size() == 1) |
---|
| 612 | { |
---|
[6417] | 613 | return ((*list.begin()->first) + ' '); |
---|
[1505] | 614 | } |
---|
| 615 | else |
---|
| 616 | { |
---|
[6417] | 617 | std::string output; |
---|
[1505] | 618 | for (unsigned int i = 0; true; i++) |
---|
| 619 | { |
---|
| 620 | char temp = 0; |
---|
| 621 | for (std::list<std::pair<const std::string*, const std::string*> >::const_iterator it = list.begin(); it != list.end(); ++it) |
---|
| 622 | { |
---|
[6417] | 623 | if (it->first->size() > i) |
---|
[1505] | 624 | { |
---|
| 625 | if (it == list.begin()) |
---|
| 626 | { |
---|
[6417] | 627 | temp = (*it->first)[i]; |
---|
[1505] | 628 | } |
---|
| 629 | else |
---|
| 630 | { |
---|
[6417] | 631 | if (temp != (*it->first)[i]) |
---|
[1505] | 632 | return output; |
---|
| 633 | } |
---|
| 634 | } |
---|
| 635 | else |
---|
| 636 | { |
---|
| 637 | return output; |
---|
| 638 | } |
---|
| 639 | } |
---|
| 640 | output += temp; |
---|
| 641 | } |
---|
| 642 | return output; |
---|
| 643 | } |
---|
| 644 | } |
---|
| 645 | |
---|
| 646 | std::string CommandExecutor::getCommonBegin(const ArgumentCompletionList& list) |
---|
| 647 | { |
---|
| 648 | if (list.size() == 0) |
---|
| 649 | { |
---|
| 650 | return ""; |
---|
| 651 | } |
---|
| 652 | else if (list.size() == 1) |
---|
| 653 | { |
---|
[6417] | 654 | return (list.begin()->getComparable() + ' '); |
---|
[1505] | 655 | } |
---|
| 656 | else |
---|
| 657 | { |
---|
[6417] | 658 | std::string output; |
---|
[1505] | 659 | for (unsigned int i = 0; true; i++) |
---|
| 660 | { |
---|
[6417] | 661 | char tempComparable = 0; |
---|
[1505] | 662 | char temp = 0; |
---|
| 663 | for (ArgumentCompletionList::const_iterator it = list.begin(); it != list.end(); ++it) |
---|
| 664 | { |
---|
[6417] | 665 | const std::string& argumentComparable = it->getComparable(); |
---|
| 666 | const std::string& argument = it->getString(); |
---|
[1505] | 667 | if (argument.size() > i) |
---|
| 668 | { |
---|
| 669 | if (it == list.begin()) |
---|
| 670 | { |
---|
[6417] | 671 | tempComparable = argumentComparable[i]; |
---|
[1505] | 672 | temp = argument[i]; |
---|
| 673 | } |
---|
| 674 | else |
---|
| 675 | { |
---|
[6417] | 676 | if (tempComparable != argumentComparable[i]) |
---|
[1505] | 677 | return output; |
---|
[6417] | 678 | else if (temp != argument[i]) |
---|
| 679 | temp = tempComparable; |
---|
[1505] | 680 | } |
---|
| 681 | } |
---|
| 682 | else |
---|
| 683 | { |
---|
| 684 | return output; |
---|
| 685 | } |
---|
| 686 | } |
---|
| 687 | output += temp; |
---|
| 688 | } |
---|
| 689 | return output; |
---|
| 690 | } |
---|
| 691 | } |
---|
[2662] | 692 | |
---|
| 693 | void CommandExecutor::destroyExternalCommands() |
---|
| 694 | { |
---|
| 695 | for (std::set<ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandExternals_.begin(); |
---|
| 696 | it != CommandExecutor::getInstance().consoleCommandExternals_.end(); ++it) |
---|
| 697 | delete *it; |
---|
| 698 | } |
---|
[1505] | 699 | } |
---|