| 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 "CommandEvaluation.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include "util/Debug.h" | 
|---|
| 32 | #include "util/StringUtils.h" | 
|---|
| 33 | #include "ConsoleCommand.h" | 
|---|
| 34 | #include "Identifier.h" | 
|---|
| 35 |  | 
|---|
| 36 | namespace orxonox | 
|---|
| 37 | { | 
|---|
| 38 | CommandEvaluation::CommandEvaluation() | 
|---|
| 39 | { | 
|---|
| 40 | this->initialize(""); | 
|---|
| 41 | this->state_ = CommandState::Uninitialized; | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | void CommandEvaluation::initialize(const std::string& command) | 
|---|
| 45 | { | 
|---|
| 46 | this->bNewCommand_ = true; | 
|---|
| 47 | this->bCommandChanged_ = false; | 
|---|
| 48 | this->originalCommand_ = command; | 
|---|
| 49 | this->command_ = command; | 
|---|
| 50 | this->commandTokens_.split(command, " ", SubString::WhiteSpaces, false, '\\', false, '"', false, '(', ')', false, '\0'); | 
|---|
| 51 |  | 
|---|
| 52 | this->additionalParameter_ = ""; | 
|---|
| 53 |  | 
|---|
| 54 | this->bEvaluatedParams_ = false; | 
|---|
| 55 |  | 
|---|
| 56 | this->listOfPossibleIdentifiers_.clear(); | 
|---|
| 57 | this->listOfPossibleFunctions_.clear(); | 
|---|
| 58 | this->listOfPossibleArguments_.clear(); | 
|---|
| 59 |  | 
|---|
| 60 | this->functionclass_ = 0; | 
|---|
| 61 | this->function_ = 0; | 
|---|
| 62 | this->possibleArgument_ = ""; | 
|---|
| 63 | this->argument_ = ""; | 
|---|
| 64 |  | 
|---|
| 65 | this->errorMessage_ = ""; | 
|---|
| 66 | this->state_ = CommandState::Empty; | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | bool CommandEvaluation::isValid() const | 
|---|
| 70 | { | 
|---|
| 71 | return (this->function_); | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | bool CommandEvaluation::execute() const | 
|---|
| 75 | { | 
|---|
| 76 | if (!this->isValid()) | 
|---|
| 77 | return false; | 
|---|
| 78 |  | 
|---|
| 79 | if (this->bEvaluatedParams_ && this->function_) | 
|---|
| 80 | { | 
|---|
| 81 | COUT(6) << "CE_execute (evaluation): " << this->function_->getName() << " " << this->param_[0] << " " << this->param_[1] << " " << this->param_[2] << " " << this->param_[3] << " " << this->param_[4] << std::endl; | 
|---|
| 82 | (*this->function_)(this->param_[0], this->param_[1], this->param_[2], this->param_[3], this->param_[4]); | 
|---|
| 83 | return true; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | if (!this->bCommandChanged_ || nocaseCmp(removeTrailingWhitespaces(this->command_), removeTrailingWhitespaces(this->originalCommand_)) == 0) | 
|---|
| 87 | { | 
|---|
| 88 | COUT(4) << "CE_execute: " << this->command_ << "\n"; | 
|---|
| 89 |  | 
|---|
| 90 | unsigned int startindex = this->getStartindex(); | 
|---|
| 91 | if (this->commandTokens_.size() > startindex) | 
|---|
| 92 | return this->function_->parse(removeSlashes(this->commandTokens_.subSet(startindex).join() + this->getAdditionalParameter())); | 
|---|
| 93 | else | 
|---|
| 94 | return this->function_->parse(removeSlashes(this->additionalParameter_)); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | return false; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | std::string CommandEvaluation::complete() | 
|---|
| 101 | { | 
|---|
| 102 | if (!this->bNewCommand_) | 
|---|
| 103 | { | 
|---|
| 104 | switch (this->state_) | 
|---|
| 105 | { | 
|---|
| 106 | case CommandState::Uninitialized: | 
|---|
| 107 | break; | 
|---|
| 108 | case CommandState::Empty: | 
|---|
| 109 | break; | 
|---|
| 110 | case CommandState::ShortcutOrIdentifier: | 
|---|
| 111 | if (this->function_) | 
|---|
| 112 | { | 
|---|
| 113 | if (this->function_->getParamCount() == 0) | 
|---|
| 114 | return (this->command_ = this->function_->getName()); | 
|---|
| 115 | else | 
|---|
| 116 | return (this->command_ = this->function_->getName() + " "); | 
|---|
| 117 | } | 
|---|
| 118 | else if (this->functionclass_) | 
|---|
| 119 | return (this->command_ = this->functionclass_->getName() + " "); | 
|---|
| 120 | break; | 
|---|
| 121 | case CommandState::Function: | 
|---|
| 122 | if (this->function_) | 
|---|
| 123 | { | 
|---|
| 124 | if (this->function_->getParamCount() == 0) | 
|---|
| 125 | return (this->command_ = this->functionclass_->getName() + " " + this->function_->getName()); | 
|---|
| 126 | else | 
|---|
| 127 | return (this->command_ = this->functionclass_->getName() + " " + this->function_->getName() + " "); | 
|---|
| 128 | } | 
|---|
| 129 | break; | 
|---|
| 130 | case CommandState::ParamPreparation: | 
|---|
| 131 | case CommandState::Params: | 
|---|
| 132 | { | 
|---|
| 133 | if (this->argument_ == "" && this->possibleArgument_ == "") | 
|---|
| 134 | break; | 
|---|
| 135 |  | 
|---|
| 136 | unsigned int maxIndex = this->commandTokens_.size(); | 
|---|
| 137 | if (this->command_[this->command_.size() - 1] != ' ') | 
|---|
| 138 | maxIndex -= 1; | 
|---|
| 139 | std::string whitespace = ""; | 
|---|
| 140 |  | 
|---|
| 141 | if (this->possibleArgument_ != "") | 
|---|
| 142 | { | 
|---|
| 143 | this->argument_ = this->possibleArgument_; | 
|---|
| 144 | if (this->function_->getParamCount() > (maxIndex + 1 - this->getStartindex())) | 
|---|
| 145 | whitespace = " "; | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | return (this->command_ = this->commandTokens_.subSet(0, maxIndex).join() + " " + this->argument_ + whitespace); | 
|---|
| 149 | break; | 
|---|
| 150 | } | 
|---|
| 151 | case CommandState::Finished: | 
|---|
| 152 | break; | 
|---|
| 153 | case CommandState::Error: | 
|---|
| 154 | break; | 
|---|
| 155 | } | 
|---|
| 156 | } | 
|---|
| 157 | this->bNewCommand_ = false; | 
|---|
| 158 | return this->command_; | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | std::string CommandEvaluation::hint() const | 
|---|
| 162 | { | 
|---|
| 163 | switch (this->state_) | 
|---|
| 164 | { | 
|---|
| 165 | case CommandState::Uninitialized: | 
|---|
| 166 | break; | 
|---|
| 167 | case CommandState::Empty: | 
|---|
| 168 | case CommandState::ShortcutOrIdentifier: | 
|---|
| 169 | if (this->listOfPossibleFunctions_.size() == 0) | 
|---|
| 170 | return CommandEvaluation::dump(this->listOfPossibleIdentifiers_); | 
|---|
| 171 | else if (this->listOfPossibleIdentifiers_.size() == 0) | 
|---|
| 172 | return CommandEvaluation::dump(this->listOfPossibleFunctions_); | 
|---|
| 173 | else | 
|---|
| 174 | return (CommandEvaluation::dump(this->listOfPossibleFunctions_) + "\n" + CommandEvaluation::dump(this->listOfPossibleIdentifiers_)); | 
|---|
| 175 | break; | 
|---|
| 176 | case CommandState::Function: | 
|---|
| 177 | return CommandEvaluation::dump(this->listOfPossibleFunctions_); | 
|---|
| 178 | break; | 
|---|
| 179 | case CommandState::ParamPreparation: | 
|---|
| 180 | case CommandState::Params: | 
|---|
| 181 | if (this->listOfPossibleArguments_.size() > 0) | 
|---|
| 182 | return CommandEvaluation::dump(this->listOfPossibleArguments_); | 
|---|
| 183 | else | 
|---|
| 184 | return CommandEvaluation::dump(this->function_); | 
|---|
| 185 | case CommandState::Finished: | 
|---|
| 186 | if (this->function_) | 
|---|
| 187 | return CommandEvaluation::dump(this->function_); | 
|---|
| 188 | break; | 
|---|
| 189 | case CommandState::Error: | 
|---|
| 190 | return this->errorMessage_; | 
|---|
| 191 | break; | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | return ""; | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | void CommandEvaluation::evaluateParams() | 
|---|
| 198 | { | 
|---|
| 199 | this->bEvaluatedParams_ = false; | 
|---|
| 200 |  | 
|---|
| 201 | for (unsigned int i = 0; i < MAX_FUNCTOR_ARGUMENTS; i++) | 
|---|
| 202 | this->param_[i] = MT_Type::Null; | 
|---|
| 203 |  | 
|---|
| 204 | if (!this->isValid()) | 
|---|
| 205 | return; | 
|---|
| 206 |  | 
|---|
| 207 | unsigned int startindex = this->getStartindex(); | 
|---|
| 208 |  | 
|---|
| 209 | if (this->commandTokens_.size() <= startindex) | 
|---|
| 210 | { | 
|---|
| 211 | if (this->function_->evaluate(this->getAdditionalParameter(), this->param_, " ")) | 
|---|
| 212 | this->bEvaluatedParams_ = true; | 
|---|
| 213 | } | 
|---|
| 214 | else if (this->commandTokens_.size() > startindex) | 
|---|
| 215 | { | 
|---|
| 216 | if (this->function_->evaluate(this->commandTokens_.subSet(startindex).join() + this->getAdditionalParameter(), this->param_, " ")) | 
|---|
| 217 | this->bEvaluatedParams_ = true; | 
|---|
| 218 | } | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | void CommandEvaluation::setEvaluatedParameter(unsigned int index, MultiType param) | 
|---|
| 222 | { | 
|---|
| 223 | if (index < MAX_FUNCTOR_ARGUMENTS) | 
|---|
| 224 | this->param_[index] = param; | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | MultiType CommandEvaluation::getEvaluatedParameter(unsigned int index) const | 
|---|
| 228 | { | 
|---|
| 229 | if (index < MAX_FUNCTOR_ARGUMENTS) | 
|---|
| 230 | return this->param_[index]; | 
|---|
| 231 |  | 
|---|
| 232 | return MT_Type::Null; | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 | bool CommandEvaluation::hasReturnvalue() const | 
|---|
| 236 | { | 
|---|
| 237 | if (this->function_) | 
|---|
| 238 | return this->function_->hasReturnvalue(); | 
|---|
| 239 |  | 
|---|
| 240 | return MT_Type::Null; | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | MultiType CommandEvaluation::getReturnvalue() const | 
|---|
| 244 | { | 
|---|
| 245 | if (this->function_) | 
|---|
| 246 | return this->function_->getReturnvalue(); | 
|---|
| 247 |  | 
|---|
| 248 | return MultiType(); | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 |  | 
|---|
| 252 | unsigned int CommandEvaluation::getStartindex() const | 
|---|
| 253 | { | 
|---|
| 254 | if (this->functionclass_ && this->function_) | 
|---|
| 255 | return 2; | 
|---|
| 256 | else if (this->function_) | 
|---|
| 257 | return 1; | 
|---|
| 258 | else | 
|---|
| 259 | return 0; | 
|---|
| 260 | } | 
|---|
| 261 |  | 
|---|
| 262 | std::string CommandEvaluation::dump(const std::list<std::pair<const std::string*, const std::string*> >& list) | 
|---|
| 263 | { | 
|---|
| 264 | std::string output = ""; | 
|---|
| 265 | for (std::list<std::pair<const std::string*, const std::string*> >::const_iterator it = list.begin(); it != list.end(); ++it) | 
|---|
| 266 | { | 
|---|
| 267 | if (it != list.begin()) | 
|---|
| 268 | output += " "; | 
|---|
| 269 |  | 
|---|
| 270 | output += *(*it).second; | 
|---|
| 271 | } | 
|---|
| 272 | return output; | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | std::string CommandEvaluation::dump(const ArgumentCompletionList& list) | 
|---|
| 276 | { | 
|---|
| 277 | std::string output = ""; | 
|---|
| 278 | for (ArgumentCompletionList::const_iterator it = list.begin(); it != list.end(); ++it) | 
|---|
| 279 | { | 
|---|
| 280 | if (it != list.begin()) | 
|---|
| 281 | output += " "; | 
|---|
| 282 |  | 
|---|
| 283 | output += (*it).getDisplay(); | 
|---|
| 284 | } | 
|---|
| 285 | return output; | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 | std::string CommandEvaluation::dump(const ConsoleCommand* command) | 
|---|
| 289 | { | 
|---|
| 290 | std::string output = command->getName(); | 
|---|
| 291 | if (command->getParamCount() > 0) | 
|---|
| 292 | output += ": "; | 
|---|
| 293 |  | 
|---|
| 294 | for (unsigned int i = 0; i < command->getParamCount(); i++) | 
|---|
| 295 | { | 
|---|
| 296 | if (i != 0) | 
|---|
| 297 | output += " "; | 
|---|
| 298 |  | 
|---|
| 299 | if (command->defaultValueSet(i)) | 
|---|
| 300 | output += "["; | 
|---|
| 301 | else | 
|---|
| 302 | output += "{"; | 
|---|
| 303 |  | 
|---|
| 304 | output += command->getTypenameParam(i); | 
|---|
| 305 |  | 
|---|
| 306 | if (command->defaultValueSet(i)) | 
|---|
| 307 | output += "=" + command->getDefaultValue(i).getString() + "]"; | 
|---|
| 308 | else | 
|---|
| 309 | output += "}"; | 
|---|
| 310 | } | 
|---|
| 311 | return output; | 
|---|
| 312 | } | 
|---|
| 313 | } | 
|---|