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