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