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 | |
---|
31 | #include "util/StringUtils.h" |
---|
32 | #include "CommandExecutor.h" |
---|
33 | #include "ConsoleCommand.h" |
---|
34 | |
---|
35 | namespace orxonox |
---|
36 | { |
---|
37 | CommandEvaluation::CommandEvaluation() |
---|
38 | { |
---|
39 | this->initialize(""); |
---|
40 | } |
---|
41 | |
---|
42 | void CommandEvaluation::initialize(const std::string& command) |
---|
43 | { |
---|
44 | this->execCommand_ = 0; |
---|
45 | this->hintCommand_ = 0; |
---|
46 | this->string_ = command; |
---|
47 | this->execArgumentsOffset_ = 0; |
---|
48 | this->hintArgumentsOffset_ = 0; |
---|
49 | this->bPossibleArgumentsRetrieved_ = false; |
---|
50 | this->possibleArguments_.clear(); |
---|
51 | this->bEvaluatedParams_ = false; |
---|
52 | this->bTriedToEvaluatedParams_ = false; |
---|
53 | this->numberOfEvaluatedParams_ = 0; |
---|
54 | |
---|
55 | this->tokens_.split(command, " ", SubString::WhiteSpaces, false, '\\', true, '"', true, '(', ')', true, '\0'); |
---|
56 | } |
---|
57 | |
---|
58 | unsigned int CommandEvaluation::getNumberOfArguments() const |
---|
59 | { |
---|
60 | unsigned int count = this->tokens_.size(); |
---|
61 | if (count > 0 && this->string_[this->string_.size() - 1] != ' ') |
---|
62 | return count; |
---|
63 | else |
---|
64 | return count + 1; |
---|
65 | } |
---|
66 | |
---|
67 | const std::string& CommandEvaluation::getLastArgument() const |
---|
68 | { |
---|
69 | if (this->tokens_.size() > 0 && this->string_[this->string_.size() - 1] != ' ') |
---|
70 | return this->tokens_.back(); |
---|
71 | else |
---|
72 | return BLANKSTRING; |
---|
73 | } |
---|
74 | |
---|
75 | const std::string& CommandEvaluation::getToken(unsigned int i) const |
---|
76 | { |
---|
77 | if (i < this->tokens_.size()) |
---|
78 | return this->tokens_[i]; |
---|
79 | else |
---|
80 | return BLANKSTRING; |
---|
81 | } |
---|
82 | |
---|
83 | int CommandEvaluation::execute() |
---|
84 | { |
---|
85 | int error; |
---|
86 | this->query(&error); |
---|
87 | return error; |
---|
88 | } |
---|
89 | |
---|
90 | MultiType CommandEvaluation::query(int* error) |
---|
91 | { |
---|
92 | if (error) |
---|
93 | { |
---|
94 | *error = CommandExecutor::Success; |
---|
95 | |
---|
96 | if (!this->execCommand_) |
---|
97 | *error = CommandExecutor::Error; |
---|
98 | else if (!this->execCommand_->isActive()) |
---|
99 | *error = CommandExecutor::Deactivated; |
---|
100 | else if (!this->execCommand_->hasAccess()) |
---|
101 | *error = CommandExecutor::Denied; |
---|
102 | |
---|
103 | if (*error != CommandExecutor::Success) |
---|
104 | return MT_Type::Null; |
---|
105 | } |
---|
106 | |
---|
107 | if (this->execCommand_ && this->execCommand_->isActive() && this->execCommand_->hasAccess()) |
---|
108 | { |
---|
109 | if (!this->bTriedToEvaluatedParams_) |
---|
110 | this->evaluateParams(false); |
---|
111 | |
---|
112 | if (this->bEvaluatedParams_) |
---|
113 | { |
---|
114 | COUT(6) << "CE_execute (evaluation): " << this->execCommand_->getName() << " with " << this->numberOfEvaluatedParams_ << " params: " << this->param_[0] << ' ' << this->param_[1] << ' ' << this->param_[2] << ' ' << this->param_[3] << ' ' << this->param_[4] << std::endl; |
---|
115 | switch (this->numberOfEvaluatedParams_) |
---|
116 | { |
---|
117 | case 0: return (*this->execCommand_->getExecutor())(); |
---|
118 | case 1: return (*this->execCommand_->getExecutor())(this->param_[0]); |
---|
119 | case 2: return (*this->execCommand_->getExecutor())(this->param_[0], this->param_[1]); |
---|
120 | case 3: return (*this->execCommand_->getExecutor())(this->param_[0], this->param_[1], this->param_[2]); |
---|
121 | case 4: return (*this->execCommand_->getExecutor())(this->param_[0], this->param_[1], this->param_[2], this->param_[3]); |
---|
122 | case 5: |
---|
123 | default: return (*this->execCommand_->getExecutor())(this->param_[0], this->param_[1], this->param_[2], this->param_[3], this->param_[4]); |
---|
124 | } |
---|
125 | } |
---|
126 | else |
---|
127 | { |
---|
128 | COUT(5) << "CE_execute: " << this->string_ << "\n"; |
---|
129 | return this->execCommand_->getExecutor()->parse(this->tokens_.subSet(this->execArgumentsOffset_), error, " "); |
---|
130 | } |
---|
131 | } |
---|
132 | else |
---|
133 | return MT_Type::Null; |
---|
134 | } |
---|
135 | |
---|
136 | int CommandEvaluation::evaluateParams(bool bPrintError) |
---|
137 | { |
---|
138 | this->bTriedToEvaluatedParams_ = true; |
---|
139 | |
---|
140 | if (!this->execCommand_) |
---|
141 | { |
---|
142 | if (bPrintError) |
---|
143 | COUT(1) << "Error: Can't evaluate params, no console command assigned." << std::endl; |
---|
144 | return CommandExecutor::Error; |
---|
145 | } |
---|
146 | |
---|
147 | int error; |
---|
148 | this->numberOfEvaluatedParams_ = this->execCommand_->getExecutor()->evaluateParams(this->tokens_.subSet(this->execArgumentsOffset_), this->param_, &error, " "); |
---|
149 | if (!error) |
---|
150 | this->bEvaluatedParams_ = true; |
---|
151 | else if (bPrintError) |
---|
152 | COUT(1) << "Error: Can't evaluate params, not enough arguments given." << std::endl; |
---|
153 | |
---|
154 | return error; |
---|
155 | } |
---|
156 | |
---|
157 | void CommandEvaluation::setEvaluatedParameter(unsigned int index, const MultiType& param) |
---|
158 | { |
---|
159 | if (index < MAX_FUNCTOR_ARGUMENTS) |
---|
160 | this->param_[index] = param; |
---|
161 | } |
---|
162 | |
---|
163 | MultiType CommandEvaluation::getEvaluatedParameter(unsigned int index) const |
---|
164 | { |
---|
165 | if (index < MAX_FUNCTOR_ARGUMENTS) |
---|
166 | return this->param_[index]; |
---|
167 | |
---|
168 | return MT_Type::Null; |
---|
169 | } |
---|
170 | |
---|
171 | std::string CommandEvaluation::complete() |
---|
172 | { |
---|
173 | if (!this->hintCommand_ || !this->hintCommand_->isActive()) |
---|
174 | return this->string_; |
---|
175 | |
---|
176 | if (!this->bPossibleArgumentsRetrieved_) |
---|
177 | this->retrievePossibleArguments(); |
---|
178 | |
---|
179 | if (this->possibleArguments_.empty()) |
---|
180 | { |
---|
181 | return this->string_; |
---|
182 | } |
---|
183 | else |
---|
184 | { |
---|
185 | std::string output = this->string_.substr(0, this->string_.find_last_of(' ') + 1); |
---|
186 | |
---|
187 | output += CommandEvaluation::getCommonBegin(this->possibleArguments_); |
---|
188 | return output; |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | std::string CommandEvaluation::hint() |
---|
193 | { |
---|
194 | if (!this->hintCommand_ || !this->hintCommand_->isActive()) |
---|
195 | return ""; |
---|
196 | |
---|
197 | if (!this->bPossibleArgumentsRetrieved_) |
---|
198 | this->retrievePossibleArguments(); |
---|
199 | |
---|
200 | if (!this->possibleArguments_.empty()) |
---|
201 | return CommandEvaluation::dump(this->possibleArguments_); |
---|
202 | |
---|
203 | if (this->isValid()) |
---|
204 | { |
---|
205 | return CommandEvaluation::dump(this->hintCommand_); |
---|
206 | } |
---|
207 | else |
---|
208 | { |
---|
209 | if (this->getNumberOfArguments() > 2) |
---|
210 | { |
---|
211 | return std::string("Error: There is no command with name \"") + this->getToken(0) + " " + this->getToken(1) + "\"."; |
---|
212 | } |
---|
213 | else |
---|
214 | { |
---|
215 | std::string groupLC = getLowercase(this->getToken(0)); |
---|
216 | std::map<std::string, std::map<std::string, _ConsoleCommand*> >::const_iterator it_group = _ConsoleCommand::getCommands().begin(); |
---|
217 | for ( ; it_group != _ConsoleCommand::getCommands().end(); ++it_group) |
---|
218 | if (getLowercase(it_group->first) == groupLC) |
---|
219 | return std::string("Error: There is no command in group \"") + this->getToken(0) + "\" starting with \"" + this->getToken(1) + "\"."; |
---|
220 | |
---|
221 | return std::string("Error: There is no command starting with \"") + this->getToken(0) + "\"."; |
---|
222 | } |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | void CommandEvaluation::retrievePossibleArguments() |
---|
227 | { |
---|
228 | this->bPossibleArgumentsRetrieved_ = true; |
---|
229 | unsigned int argumentID = std::min(this->getNumberOfArguments() - this->hintArgumentsOffset_, this->hintCommand_->getExecutor()->getParamCount()); |
---|
230 | ArgumentCompleter* ac = this->hintCommand_->getArgumentCompleter(argumentID - 1); |
---|
231 | |
---|
232 | if (ac) |
---|
233 | { |
---|
234 | MultiType param[MAX_FUNCTOR_ARGUMENTS]; |
---|
235 | |
---|
236 | for (size_t i = 0; i < argumentID; ++i) |
---|
237 | param[i] = this->getToken(this->getNumberOfArguments() - i - 1); |
---|
238 | |
---|
239 | this->possibleArguments_ = (*ac)(param[0], param[1], param[2], param[3], param[4]); |
---|
240 | |
---|
241 | CommandEvaluation::strip(this->possibleArguments_, param[0]); |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | /* static */ void CommandEvaluation::strip(ArgumentCompletionList& list, const std::string& fragment) |
---|
246 | { |
---|
247 | std::string fragmentLC = getLowercase(fragment); |
---|
248 | |
---|
249 | for (ArgumentCompletionList::iterator it = list.begin(); it != list.end(); ) |
---|
250 | { |
---|
251 | const std::string& entry = it->getComparable(); |
---|
252 | |
---|
253 | if (entry.size() < fragmentLC.size()) |
---|
254 | { |
---|
255 | list.erase(it++); |
---|
256 | } |
---|
257 | else |
---|
258 | { |
---|
259 | bool bErase = false; |
---|
260 | for (size_t i = 0; i < fragmentLC.size(); ++i) |
---|
261 | { |
---|
262 | if (fragmentLC[i] != entry[i]) |
---|
263 | { |
---|
264 | bErase = true; |
---|
265 | break; |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | if (bErase) |
---|
270 | list.erase(it++); |
---|
271 | else |
---|
272 | ++it; |
---|
273 | } |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | /* static */ std::string CommandEvaluation::dump(const ArgumentCompletionList& list) |
---|
278 | { |
---|
279 | std::string output; |
---|
280 | for (ArgumentCompletionList::const_iterator it = list.begin(); it != list.end(); ++it) |
---|
281 | { |
---|
282 | if (it != list.begin()) |
---|
283 | output += ' '; |
---|
284 | |
---|
285 | output += it->getDisplay(); |
---|
286 | } |
---|
287 | return output; |
---|
288 | } |
---|
289 | |
---|
290 | /* static */ std::string CommandEvaluation::dump(const _ConsoleCommand* command) |
---|
291 | { |
---|
292 | std::string output = command->getName(); |
---|
293 | if (command->getExecutor()->getParamCount() > 0) |
---|
294 | output += ": "; |
---|
295 | |
---|
296 | for (unsigned int i = 0; i < command->getExecutor()->getParamCount(); i++) |
---|
297 | { |
---|
298 | if (i != 0) |
---|
299 | output += ' '; |
---|
300 | |
---|
301 | if (command->getExecutor()->defaultValueSet(i)) |
---|
302 | output += '['; |
---|
303 | else |
---|
304 | output += '{'; |
---|
305 | |
---|
306 | output += command->getExecutor()->getTypenameParam(i); |
---|
307 | |
---|
308 | if (command->getExecutor()->defaultValueSet(i)) |
---|
309 | output += '=' + command->getExecutor()->getDefaultValue(i).getString() + ']'; |
---|
310 | else |
---|
311 | output += '}'; |
---|
312 | } |
---|
313 | return output; |
---|
314 | } |
---|
315 | |
---|
316 | /* static */ std::string CommandEvaluation::getCommonBegin(const ArgumentCompletionList& list) |
---|
317 | { |
---|
318 | if (list.size() == 0) |
---|
319 | { |
---|
320 | return ""; |
---|
321 | } |
---|
322 | else if (list.size() == 1) |
---|
323 | { |
---|
324 | if (list.begin()->hasDisplay()) |
---|
325 | return (list.begin()->getString()); |
---|
326 | else |
---|
327 | return (list.begin()->getString() + ' '); |
---|
328 | } |
---|
329 | else |
---|
330 | { |
---|
331 | std::string output; |
---|
332 | for (unsigned int i = 0; true; i++) |
---|
333 | { |
---|
334 | char tempComparable = 0; |
---|
335 | char temp = 0; |
---|
336 | for (ArgumentCompletionList::const_iterator it = list.begin(); it != list.end(); ++it) |
---|
337 | { |
---|
338 | const std::string& argumentComparable = it->getComparable(); |
---|
339 | const std::string& argument = it->getString(); |
---|
340 | if (argument.size() > i) |
---|
341 | { |
---|
342 | if (it == list.begin()) |
---|
343 | { |
---|
344 | tempComparable = argumentComparable[i]; |
---|
345 | temp = argument[i]; |
---|
346 | } |
---|
347 | else |
---|
348 | { |
---|
349 | if (tempComparable != argumentComparable[i]) |
---|
350 | return output; |
---|
351 | else if (temp != argument[i]) |
---|
352 | temp = tempComparable; |
---|
353 | } |
---|
354 | } |
---|
355 | else |
---|
356 | { |
---|
357 | return output; |
---|
358 | } |
---|
359 | } |
---|
360 | output += temp; |
---|
361 | } |
---|
362 | return output; |
---|
363 | } |
---|
364 | } |
---|
365 | } |
---|