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 "CommandExecutor.h" |
---|
30 | |
---|
31 | #include "ConsoleCommand.h" |
---|
32 | #include "TclBind.h" |
---|
33 | #include "Shell.h" |
---|
34 | |
---|
35 | namespace orxonox |
---|
36 | { |
---|
37 | static const std::string __CC_CommandExecutor_name = "CommandExecutor"; |
---|
38 | static const std::string __CC_autocomplete_name = "autocomplete"; |
---|
39 | |
---|
40 | SetConsoleCommand(__CC_CommandExecutor_name, __CC_autocomplete_name, &CommandExecutor::_autocomplete) |
---|
41 | .hide() |
---|
42 | .argumentCompleter(0, autocompletion::groupsandcommands()) |
---|
43 | .argumentCompleter(1, autocompletion::subcommands()); |
---|
44 | |
---|
45 | SetConsoleCommand("unhide", &CommandExecutor::unhide) |
---|
46 | .argumentCompleter(0, autocompletion::hiddencommand()); |
---|
47 | |
---|
48 | SetConsoleCommand("alias", &CommandExecutor::alias) |
---|
49 | .argumentCompleter(1, autocompletion::command()); |
---|
50 | |
---|
51 | /* static */ CommandExecutor& CommandExecutor::getInstance() |
---|
52 | { |
---|
53 | static CommandExecutor instance; |
---|
54 | return instance; |
---|
55 | } |
---|
56 | |
---|
57 | /* static */ int CommandExecutor::execute(const std::string& command, bool useTcl) |
---|
58 | { |
---|
59 | int error; |
---|
60 | CommandExecutor::queryMT(command, &error, useTcl); |
---|
61 | return error; |
---|
62 | } |
---|
63 | |
---|
64 | /* static */ MultiType CommandExecutor::queryMT(const std::string& command, int* error, bool useTcl) |
---|
65 | { |
---|
66 | if (useTcl) |
---|
67 | return TclBind::eval(command, error); |
---|
68 | else |
---|
69 | { |
---|
70 | CommandEvaluation evaluation; |
---|
71 | if (!CommandExecutor::getInstance().getCached(command, evaluation)) |
---|
72 | { |
---|
73 | evaluation = CommandExecutor::evaluate(command); |
---|
74 | evaluation.evaluateParams(); |
---|
75 | CommandExecutor::getInstance().cache(command, evaluation); |
---|
76 | } |
---|
77 | |
---|
78 | return evaluation.query(error); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | /* static */ std::string CommandExecutor::query(const std::string& command, int* error, bool useTcl) |
---|
83 | { |
---|
84 | return CommandExecutor::queryMT(command, error, useTcl).getString(); |
---|
85 | } |
---|
86 | |
---|
87 | /* static */ CommandEvaluation CommandExecutor::evaluate(const std::string& command) |
---|
88 | { |
---|
89 | CommandEvaluation evaluation; |
---|
90 | evaluation.initialize(command); |
---|
91 | |
---|
92 | evaluation.hintCommand_ = ConsoleCommand::getCommand(__CC_CommandExecutor_name, __CC_autocomplete_name); |
---|
93 | |
---|
94 | if (evaluation.getNumberOfArguments() >= 1) |
---|
95 | { |
---|
96 | evaluation.execCommand_ = ConsoleCommand::getCommandLC(evaluation.getToken(0)); |
---|
97 | if (evaluation.execCommand_) |
---|
98 | evaluation.execArgumentsOffset_ = 1; |
---|
99 | else if (evaluation.getNumberOfArguments() >= 2) |
---|
100 | { |
---|
101 | evaluation.execCommand_ = ConsoleCommand::getCommandLC(evaluation.getToken(0), evaluation.getToken(1)); |
---|
102 | if (evaluation.execCommand_) |
---|
103 | evaluation.execArgumentsOffset_ = 2; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | if (evaluation.execCommand_ && evaluation.getNumberOfArguments() > evaluation.execArgumentsOffset_) |
---|
108 | { |
---|
109 | evaluation.hintCommand_ = evaluation.execCommand_; |
---|
110 | evaluation.hintArgumentsOffset_ = evaluation.execArgumentsOffset_; |
---|
111 | } |
---|
112 | |
---|
113 | return evaluation; |
---|
114 | } |
---|
115 | |
---|
116 | bool CommandExecutor::getCached(const std::string& command, CommandEvaluation& evaluation) |
---|
117 | { |
---|
118 | if (Shell::getCacheSize() == 0) |
---|
119 | return false; |
---|
120 | |
---|
121 | std::map<std::string, CacheEntry>::iterator it = this->cache_.find(command); |
---|
122 | if (it != this->cache_.end()) |
---|
123 | { |
---|
124 | // update ordered list of cached commands (move it to the front) |
---|
125 | this->cachelist_.erase(it->second.iterator_); |
---|
126 | this->cachelist_.push_front(command); |
---|
127 | it->second.iterator_ = this->cachelist_.begin(); |
---|
128 | |
---|
129 | // assign the cached evaluation |
---|
130 | evaluation = it->second.evaluation_; |
---|
131 | return true; |
---|
132 | } |
---|
133 | return false; |
---|
134 | } |
---|
135 | |
---|
136 | void CommandExecutor::cache(const std::string& command, const CommandEvaluation& evaluation) |
---|
137 | { |
---|
138 | if (Shell::getCacheSize() == 0) |
---|
139 | return; |
---|
140 | |
---|
141 | // push command to the front of the ordered list |
---|
142 | this->cachelist_.push_front(command); |
---|
143 | |
---|
144 | // create a cache entry and store it in the cache |
---|
145 | CacheEntry entry; |
---|
146 | entry.evaluation_ = evaluation; |
---|
147 | entry.iterator_ = this->cachelist_.begin(); |
---|
148 | this->cache_[command] = entry; |
---|
149 | |
---|
150 | // remove the last command in the ordered list from the cache if it exceeds the maximum size of the cache |
---|
151 | if (this->cachelist_.size() > Shell::getCacheSize()) |
---|
152 | { |
---|
153 | this->cache_.erase(this->cachelist_.back()); |
---|
154 | this->cachelist_.pop_back(); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | /* static */ MultiType CommandExecutor::unhide(const std::string& command) |
---|
159 | { |
---|
160 | return CommandExecutor::queryMT(command); |
---|
161 | } |
---|
162 | |
---|
163 | /* static */ void CommandExecutor::alias(const std::string& alias, const std::string& command) |
---|
164 | { |
---|
165 | CommandEvaluation evaluation = CommandExecutor::evaluate(command); |
---|
166 | if (evaluation.isValid()) |
---|
167 | { |
---|
168 | ExecutorPtr executor = new Executor(*evaluation.getConsoleCommand()->getExecutor().get()); |
---|
169 | |
---|
170 | if (!evaluation.evaluateParams()) |
---|
171 | { |
---|
172 | for (size_t i = 0; i < MAX_FUNCTOR_ARGUMENTS; ++i) |
---|
173 | executor->setDefaultValue(i, evaluation.getEvaluatedParameter(i)); |
---|
174 | } |
---|
175 | |
---|
176 | SubString tokens(alias, " "); |
---|
177 | |
---|
178 | if ((tokens.size() == 1 && ConsoleCommand::getCommand(tokens[0])) || (tokens.size() == 2 && ConsoleCommand::getCommand(tokens[0], tokens[1]))) |
---|
179 | { |
---|
180 | COUT(1) << "Error: A command with name \"" << alias << "\" already exists." << std::endl; |
---|
181 | return; |
---|
182 | } |
---|
183 | |
---|
184 | if (tokens.size() == 1) |
---|
185 | createConsoleCommand(tokens[0], executor); |
---|
186 | else if (tokens.size() == 2) |
---|
187 | createConsoleCommand(tokens[0], tokens[1], executor); |
---|
188 | else |
---|
189 | COUT(1) << "Error: \"" << alias << "\" is not a valid alias name (must have one or two words)." << std::endl; |
---|
190 | } |
---|
191 | else |
---|
192 | COUT(1) << "Error: \"" << command << "\" is not a valid command (did you mean \"" << evaluation.getCommandSuggestion() << "\"?)." << std::endl; |
---|
193 | } |
---|
194 | } |
---|