[1341] | 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" |
---|
[1351] | 31 | #include "Debug.h" |
---|
[1434] | 32 | #include "util/String.h" |
---|
[1341] | 33 | |
---|
| 34 | namespace orxonox |
---|
| 35 | { |
---|
| 36 | CommandEvaluation::CommandEvaluation() |
---|
| 37 | { |
---|
[1351] | 38 | this->initialize(""); |
---|
| 39 | this->state_ = CS_Uninitialized; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | void CommandEvaluation::initialize(const std::string& command) |
---|
| 43 | { |
---|
| 44 | this->bNewCommand_ = true; |
---|
[1424] | 45 | this->bCommandChanged_ = false; |
---|
[1351] | 46 | this->originalCommand_ = command; |
---|
| 47 | this->command_ = command; |
---|
| 48 | this->commandTokens_.split(command, " ", SubString::WhiteSpaces, false, '\\', false, '"', false, '(', ')', false, '\0'); |
---|
| 49 | |
---|
[1341] | 50 | this->additionalParameter_ = ""; |
---|
| 51 | |
---|
[1351] | 52 | this->bEvaluatedParams_ = false; |
---|
| 53 | |
---|
[1390] | 54 | this->listOfPossibleIdentifiers_.clear(); |
---|
[1351] | 55 | this->listOfPossibleFunctions_.clear(); |
---|
[1416] | 56 | this->listOfPossibleArguments_.clear(); |
---|
[1351] | 57 | |
---|
[1341] | 58 | this->functionclass_ = 0; |
---|
| 59 | this->function_ = 0; |
---|
[1427] | 60 | this->possibleArgument_ = ""; |
---|
| 61 | this->argument_ = ""; |
---|
[1341] | 62 | |
---|
| 63 | this->errorMessage_ = ""; |
---|
[1351] | 64 | this->state_ = CS_Empty; |
---|
[1341] | 65 | } |
---|
| 66 | |
---|
[1351] | 67 | bool CommandEvaluation::isValid() const |
---|
[1341] | 68 | { |
---|
[1390] | 69 | return (this->function_); |
---|
[1351] | 70 | } |
---|
| 71 | |
---|
| 72 | bool CommandEvaluation::execute() const |
---|
| 73 | { |
---|
| 74 | if (!this->isValid()) |
---|
| 75 | return false; |
---|
| 76 | |
---|
| 77 | if (this->bEvaluatedParams_ && this->function_) |
---|
[1341] | 78 | { |
---|
[1351] | 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; |
---|
[1341] | 82 | } |
---|
[1351] | 83 | |
---|
[1434] | 84 | if (!this->bCommandChanged_ || removeTrailingWhitespaces(this->command_) == removeTrailingWhitespaces(this->originalCommand_)) |
---|
[1424] | 85 | { |
---|
| 86 | COUT(4) << "CE_execute: " << this->command_ << "\n"; |
---|
[1351] | 87 | |
---|
[1424] | 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; |
---|
[1351] | 96 | } |
---|
| 97 | |
---|
[1424] | 98 | std::string CommandEvaluation::complete() |
---|
[1351] | 99 | { |
---|
[1424] | 100 | if (!this->bNewCommand_) |
---|
[1341] | 101 | { |
---|
[1424] | 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_) |
---|
[1427] | 110 | { |
---|
| 111 | if (this->function_->getParamCount() == 0) |
---|
[1430] | 112 | return (this->command_ = this->function_->getName()); |
---|
[1427] | 113 | else |
---|
[1430] | 114 | return (this->command_ = this->function_->getName() + " "); |
---|
[1427] | 115 | } |
---|
[1424] | 116 | else if (this->functionclass_) |
---|
[1430] | 117 | return (this->command_ = this->functionclass_->getName() + " "); |
---|
[1424] | 118 | break; |
---|
| 119 | case CS_Function: |
---|
| 120 | if (this->function_) |
---|
[1427] | 121 | { |
---|
| 122 | if (this->function_->getParamCount() == 0) |
---|
[1430] | 123 | return (this->command_ = this->functionclass_->getName() + " " + this->function_->getName()); |
---|
[1427] | 124 | else |
---|
[1430] | 125 | return (this->command_ = this->functionclass_->getName() + " " + this->function_->getName() + " "); |
---|
[1427] | 126 | } |
---|
[1424] | 127 | break; |
---|
[1427] | 128 | case CS_ParamPreparation: |
---|
[1424] | 129 | case CS_Params: |
---|
[1427] | 130 | { |
---|
[1430] | 131 | if (this->argument_ == "" && this->possibleArgument_ == "") |
---|
| 132 | break; |
---|
| 133 | |
---|
[1427] | 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_; |
---|
[1434] | 142 | if (this->function_->getParamCount() > (maxIndex + 1 - this->getStartindex())) |
---|
| 143 | whitespace = " "; |
---|
[1427] | 144 | } |
---|
| 145 | |
---|
[1430] | 146 | return (this->command_ = this->commandTokens_.subSet(0, maxIndex).join() + " " + this->argument_ + whitespace); |
---|
[1424] | 147 | break; |
---|
[1427] | 148 | } |
---|
[1424] | 149 | case CS_Finished: |
---|
| 150 | break; |
---|
| 151 | case CS_Error: |
---|
| 152 | break; |
---|
| 153 | } |
---|
[1341] | 154 | } |
---|
[1424] | 155 | this->bNewCommand_ = false; |
---|
| 156 | return this->command_; |
---|
[1341] | 157 | } |
---|
| 158 | |
---|
[1351] | 159 | std::string CommandEvaluation::hint() const |
---|
[1341] | 160 | { |
---|
[1351] | 161 | switch (this->state_) |
---|
[1341] | 162 | { |
---|
[1390] | 163 | case CS_Uninitialized: |
---|
| 164 | break; |
---|
[1351] | 165 | case CS_Empty: |
---|
[1390] | 166 | case CS_ShortcutOrIdentifier: |
---|
[1402] | 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_)); |
---|
[1351] | 173 | break; |
---|
| 174 | case CS_Function: |
---|
| 175 | return CommandEvaluation::dump(this->listOfPossibleFunctions_); |
---|
| 176 | break; |
---|
[1427] | 177 | case CS_ParamPreparation: |
---|
[1424] | 178 | case CS_Params: |
---|
[1402] | 179 | if (this->listOfPossibleArguments_.size() > 0) |
---|
| 180 | return CommandEvaluation::dump(this->listOfPossibleArguments_); |
---|
| 181 | else |
---|
| 182 | return CommandEvaluation::dump(this->function_); |
---|
[1424] | 183 | case CS_Finished: |
---|
[1351] | 184 | if (this->function_) |
---|
| 185 | return CommandEvaluation::dump(this->function_); |
---|
| 186 | break; |
---|
| 187 | case CS_Error: |
---|
| 188 | return this->errorMessage_; |
---|
| 189 | break; |
---|
[1341] | 190 | } |
---|
[1351] | 191 | |
---|
| 192 | return ""; |
---|
[1341] | 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 | |
---|
[1351] | 202 | if (!this->isValid()) |
---|
| 203 | return; |
---|
| 204 | |
---|
| 205 | unsigned int startindex = this->getStartindex(); |
---|
| 206 | |
---|
[1402] | 207 | if (this->commandTokens_.size() <= startindex) |
---|
[1341] | 208 | { |
---|
[1351] | 209 | if (this->function_->evaluate(this->getAdditionalParameter(), this->param_, " ")) |
---|
| 210 | this->bEvaluatedParams_ = true; |
---|
[1341] | 211 | } |
---|
[1402] | 212 | else if (this->commandTokens_.size() > startindex) |
---|
[1341] | 213 | { |
---|
[1402] | 214 | if (this->function_->evaluate(this->commandTokens_.subSet(startindex).join() + this->getAdditionalParameter(), this->param_, " ")) |
---|
[1351] | 215 | this->bEvaluatedParams_ = true; |
---|
[1341] | 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 | { |
---|
[1351] | 235 | if (this->function_) |
---|
| 236 | return this->function_->hasReturnvalue(); |
---|
[1341] | 237 | |
---|
| 238 | return MT_null; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | MultiTypeMath CommandEvaluation::getReturnvalue() const |
---|
| 242 | { |
---|
[1351] | 243 | if (this->function_) |
---|
| 244 | return this->function_->getReturnvalue(); |
---|
| 245 | |
---|
| 246 | return MultiTypeMath(); |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | |
---|
| 250 | unsigned int CommandEvaluation::getStartindex() const |
---|
| 251 | { |
---|
[1424] | 252 | if (this->functionclass_ && this->function_) |
---|
| 253 | return 2; |
---|
| 254 | else if (this->function_) |
---|
[1351] | 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; |
---|
[1341] | 271 | } |
---|
[1351] | 272 | |
---|
[1434] | 273 | std::string CommandEvaluation::dump(const std::list<std::pair<std::string, std::string> >& list) |
---|
| 274 | { |
---|
| 275 | std::string output = ""; |
---|
| 276 | for (std::list<std::pair<std::string, std::string> >::const_iterator it = list.begin(); it != list.end(); ++it) |
---|
| 277 | { |
---|
| 278 | if (it != list.begin()) |
---|
| 279 | output += " "; |
---|
| 280 | |
---|
| 281 | output += (*it).second; |
---|
| 282 | } |
---|
| 283 | return output; |
---|
| 284 | } |
---|
| 285 | |
---|
[1351] | 286 | std::string CommandEvaluation::dump(const ConsoleCommand* command) |
---|
| 287 | { |
---|
[1427] | 288 | std::string output = command->getName(); |
---|
| 289 | if (command->getParamCount() > 0) |
---|
| 290 | output += ": "; |
---|
| 291 | |
---|
[1351] | 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 | } |
---|
[1341] | 311 | } |
---|