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