[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; |
---|
[1424] | 44 | this->bCommandChanged_ = false; |
---|
[1351] | 45 | this->originalCommand_ = command; |
---|
| 46 | this->command_ = command; |
---|
| 47 | this->commandTokens_.split(command, " ", SubString::WhiteSpaces, false, '\\', false, '"', false, '(', ')', false, '\0'); |
---|
| 48 | |
---|
[1341] | 49 | this->additionalParameter_ = ""; |
---|
| 50 | |
---|
[1351] | 51 | this->bEvaluatedParams_ = false; |
---|
| 52 | |
---|
[1390] | 53 | this->listOfPossibleIdentifiers_.clear(); |
---|
[1351] | 54 | this->listOfPossibleFunctions_.clear(); |
---|
[1416] | 55 | this->listOfPossibleArguments_.clear(); |
---|
[1351] | 56 | |
---|
[1341] | 57 | this->functionclass_ = 0; |
---|
| 58 | this->function_ = 0; |
---|
[1427] | 59 | this->possibleArgument_ = ""; |
---|
| 60 | this->argument_ = ""; |
---|
[1341] | 61 | |
---|
| 62 | this->errorMessage_ = ""; |
---|
[1351] | 63 | this->state_ = CS_Empty; |
---|
[1341] | 64 | } |
---|
| 65 | |
---|
[1351] | 66 | bool CommandEvaluation::isValid() const |
---|
[1341] | 67 | { |
---|
[1390] | 68 | return (this->function_); |
---|
[1351] | 69 | } |
---|
| 70 | |
---|
| 71 | bool CommandEvaluation::execute() const |
---|
| 72 | { |
---|
| 73 | if (!this->isValid()) |
---|
| 74 | return false; |
---|
| 75 | |
---|
| 76 | if (this->bEvaluatedParams_ && this->function_) |
---|
[1341] | 77 | { |
---|
[1351] | 78 | 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; |
---|
| 79 | (*this->function_)(this->param_[0], this->param_[1], this->param_[2], this->param_[3], this->param_[4]); |
---|
| 80 | return true; |
---|
[1341] | 81 | } |
---|
[1351] | 82 | |
---|
[1424] | 83 | if (!this->bCommandChanged_) |
---|
| 84 | { |
---|
| 85 | COUT(4) << "CE_execute: " << this->command_ << "\n"; |
---|
[1351] | 86 | |
---|
[1424] | 87 | unsigned int startindex = this->getStartindex(); |
---|
| 88 | if (this->commandTokens_.size() > startindex) |
---|
| 89 | return this->function_->parse(removeSlashes(this->commandTokens_.subSet(startindex).join() + this->getAdditionalParameter())); |
---|
| 90 | else |
---|
| 91 | return this->function_->parse(removeSlashes(this->additionalParameter_)); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | return false; |
---|
[1351] | 95 | } |
---|
| 96 | |
---|
[1424] | 97 | std::string CommandEvaluation::complete() |
---|
[1351] | 98 | { |
---|
[1424] | 99 | if (!this->bNewCommand_) |
---|
[1341] | 100 | { |
---|
[1427] | 101 | std::cout << "not new" << std::endl; |
---|
[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 | if (this->function_->getParamCount() > (maxIndex + 1 - this->getStartindex())) |
---|
| 139 | whitespace = " "; |
---|
| 140 | |
---|
| 141 | if (this->possibleArgument_ != "") |
---|
| 142 | { |
---|
| 143 | maxIndex -= 1; |
---|
| 144 | this->argument_ = this->possibleArgument_; |
---|
| 145 | } |
---|
| 146 | |
---|
[1430] | 147 | return (this->command_ = this->commandTokens_.subSet(0, maxIndex).join() + " " + this->argument_ + whitespace); |
---|
[1424] | 148 | break; |
---|
[1427] | 149 | } |
---|
[1424] | 150 | case CS_Finished: |
---|
| 151 | break; |
---|
| 152 | case CS_Error: |
---|
| 153 | break; |
---|
| 154 | } |
---|
[1341] | 155 | } |
---|
[1424] | 156 | this->bNewCommand_ = false; |
---|
| 157 | return this->command_; |
---|
[1341] | 158 | } |
---|
| 159 | |
---|
[1351] | 160 | std::string CommandEvaluation::hint() const |
---|
[1341] | 161 | { |
---|
[1351] | 162 | switch (this->state_) |
---|
[1341] | 163 | { |
---|
[1390] | 164 | case CS_Uninitialized: |
---|
| 165 | break; |
---|
[1351] | 166 | case CS_Empty: |
---|
[1390] | 167 | case CS_ShortcutOrIdentifier: |
---|
[1402] | 168 | if (this->listOfPossibleFunctions_.size() == 0) |
---|
| 169 | return CommandEvaluation::dump(this->listOfPossibleIdentifiers_); |
---|
| 170 | else if (this->listOfPossibleIdentifiers_.size() == 0) |
---|
| 171 | return CommandEvaluation::dump(this->listOfPossibleFunctions_); |
---|
| 172 | else |
---|
| 173 | return (CommandEvaluation::dump(this->listOfPossibleFunctions_) + "\n" + CommandEvaluation::dump(this->listOfPossibleIdentifiers_)); |
---|
[1351] | 174 | break; |
---|
| 175 | case CS_Function: |
---|
| 176 | return CommandEvaluation::dump(this->listOfPossibleFunctions_); |
---|
| 177 | break; |
---|
[1427] | 178 | case CS_ParamPreparation: |
---|
[1424] | 179 | case CS_Params: |
---|
[1402] | 180 | if (this->listOfPossibleArguments_.size() > 0) |
---|
| 181 | return CommandEvaluation::dump(this->listOfPossibleArguments_); |
---|
| 182 | else |
---|
| 183 | return CommandEvaluation::dump(this->function_); |
---|
[1424] | 184 | case CS_Finished: |
---|
[1351] | 185 | if (this->function_) |
---|
| 186 | return CommandEvaluation::dump(this->function_); |
---|
| 187 | break; |
---|
| 188 | case CS_Error: |
---|
| 189 | return this->errorMessage_; |
---|
| 190 | break; |
---|
[1341] | 191 | } |
---|
[1351] | 192 | |
---|
| 193 | return ""; |
---|
[1341] | 194 | } |
---|
| 195 | |
---|
| 196 | void CommandEvaluation::evaluateParams() |
---|
| 197 | { |
---|
| 198 | this->bEvaluatedParams_ = false; |
---|
| 199 | |
---|
| 200 | for (unsigned int i = 0; i < MAX_FUNCTOR_ARGUMENTS; i++) |
---|
| 201 | this->param_[i] = MT_null; |
---|
| 202 | |
---|
[1351] | 203 | if (!this->isValid()) |
---|
| 204 | return; |
---|
| 205 | |
---|
| 206 | unsigned int startindex = this->getStartindex(); |
---|
| 207 | |
---|
[1402] | 208 | if (this->commandTokens_.size() <= startindex) |
---|
[1341] | 209 | { |
---|
[1351] | 210 | if (this->function_->evaluate(this->getAdditionalParameter(), this->param_, " ")) |
---|
| 211 | this->bEvaluatedParams_ = true; |
---|
[1341] | 212 | } |
---|
[1402] | 213 | else if (this->commandTokens_.size() > startindex) |
---|
[1341] | 214 | { |
---|
[1402] | 215 | if (this->function_->evaluate(this->commandTokens_.subSet(startindex).join() + this->getAdditionalParameter(), this->param_, " ")) |
---|
[1351] | 216 | this->bEvaluatedParams_ = true; |
---|
[1341] | 217 | } |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | void CommandEvaluation::setEvaluatedParameter(unsigned int index, MultiTypeMath param) |
---|
| 221 | { |
---|
| 222 | if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS) |
---|
| 223 | this->param_[index] = param; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | MultiTypeMath CommandEvaluation::getEvaluatedParameter(unsigned int index) const |
---|
| 227 | { |
---|
| 228 | if (index >= 0 && index < MAX_FUNCTOR_ARGUMENTS) |
---|
| 229 | return this->param_[index]; |
---|
| 230 | |
---|
| 231 | return MT_null; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | bool CommandEvaluation::hasReturnvalue() const |
---|
| 235 | { |
---|
[1351] | 236 | if (this->function_) |
---|
| 237 | return this->function_->hasReturnvalue(); |
---|
[1341] | 238 | |
---|
| 239 | return MT_null; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | MultiTypeMath CommandEvaluation::getReturnvalue() const |
---|
| 243 | { |
---|
[1351] | 244 | if (this->function_) |
---|
| 245 | return this->function_->getReturnvalue(); |
---|
| 246 | |
---|
| 247 | return MultiTypeMath(); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | |
---|
| 251 | unsigned int CommandEvaluation::getStartindex() const |
---|
| 252 | { |
---|
[1424] | 253 | if (this->functionclass_ && this->function_) |
---|
| 254 | return 2; |
---|
| 255 | else if (this->function_) |
---|
[1351] | 256 | return 1; |
---|
| 257 | else |
---|
| 258 | return 0; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | std::string CommandEvaluation::dump(const std::list<std::pair<const std::string*, const std::string*> >& list) |
---|
| 262 | { |
---|
| 263 | std::string output = ""; |
---|
| 264 | for (std::list<std::pair<const std::string*, const std::string*> >::const_iterator it = list.begin(); it != list.end(); ++it) |
---|
| 265 | { |
---|
| 266 | if (it != list.begin()) |
---|
| 267 | output += " "; |
---|
| 268 | |
---|
| 269 | output += *(*it).second; |
---|
| 270 | } |
---|
| 271 | return output; |
---|
[1341] | 272 | } |
---|
[1351] | 273 | |
---|
| 274 | std::string CommandEvaluation::dump(const ConsoleCommand* command) |
---|
| 275 | { |
---|
[1427] | 276 | std::string output = command->getName(); |
---|
| 277 | if (command->getParamCount() > 0) |
---|
| 278 | output += ": "; |
---|
| 279 | |
---|
[1351] | 280 | for (unsigned int i = 0; i < command->getParamCount(); i++) |
---|
| 281 | { |
---|
| 282 | if (i != 0) |
---|
| 283 | output += " "; |
---|
| 284 | |
---|
| 285 | if (command->defaultValueSet(i)) |
---|
| 286 | output += "["; |
---|
| 287 | else |
---|
| 288 | output += "{"; |
---|
| 289 | |
---|
| 290 | output += command->getTypenameParam(i); |
---|
| 291 | |
---|
| 292 | if (command->defaultValueSet(i)) |
---|
| 293 | output += "=" + command->getDefaultValue(i).toString() + "]"; |
---|
| 294 | else |
---|
| 295 | output += "}"; |
---|
| 296 | } |
---|
| 297 | return output; |
---|
| 298 | } |
---|
[1341] | 299 | } |
---|