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