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