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