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