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 | #ifndef _ConsoleCommand_H__ |
---|
30 | #define _ConsoleCommand_H__ |
---|
31 | |
---|
32 | #include "core/CorePrereqs.h" |
---|
33 | |
---|
34 | #include <stack> |
---|
35 | #include <vector> |
---|
36 | #include <boost/preprocessor/cat.hpp> |
---|
37 | #include <boost/preprocessor/facilities/expand.hpp> |
---|
38 | |
---|
39 | #include "util/VA_NARGS.h" |
---|
40 | #include "ArgumentCompletionFunctions.h" |
---|
41 | #include "Executor.h" |
---|
42 | |
---|
43 | |
---|
44 | #define SetConsoleCommand(...) \ |
---|
45 | BOOST_PP_EXPAND(BOOST_PP_CAT(SetConsoleCommand, ORXONOX_VA_NARGS(__VA_ARGS__))(__VA_ARGS__)) |
---|
46 | #define SetConsoleCommand2(name, functionpointer) \ |
---|
47 | SetConsoleCommandGeneric("", name, orxonox::createFunctor(functionpointer)) |
---|
48 | #define SetConsoleCommand3(group, name, functionpointer) \ |
---|
49 | SetConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer)) |
---|
50 | #define SetConsoleCommand4(group, name, functionpointer, object) \ |
---|
51 | SetConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer, object)) |
---|
52 | |
---|
53 | #define SetConsoleCommandGeneric(group, name, functor) \ |
---|
54 | static orxonox::ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::createConsoleCommand(group, name, orxonox::createExecutor(functor))) |
---|
55 | |
---|
56 | |
---|
57 | #define DeclareConsoleCommand(...) \ |
---|
58 | BOOST_PP_EXPAND(BOOST_PP_CAT(DeclareConsoleCommand, ORXONOX_VA_NARGS(__VA_ARGS__))(__VA_ARGS__)) |
---|
59 | #define DeclareConsoleCommand2(name, functionpointer) \ |
---|
60 | DeclareConsoleCommandGeneric("", name, orxonox::createFunctor(functionpointer)) |
---|
61 | #define DeclareConsoleCommand3(group, name, functionpointer) \ |
---|
62 | DeclareConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer)) |
---|
63 | #define DeclareConsoleCommand4(group, name, functionpointer, object) \ |
---|
64 | DeclareConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer, object)) |
---|
65 | |
---|
66 | #define DeclareConsoleCommandGeneric(group, name, functor) \ |
---|
67 | static orxonox::ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::createConsoleCommand(group, name, orxonox::createExecutor(functor), false)) |
---|
68 | |
---|
69 | |
---|
70 | namespace orxonox |
---|
71 | { |
---|
72 | namespace prototype |
---|
73 | { |
---|
74 | inline void void__void(void) {} |
---|
75 | inline void void__string(const std::string&) {} |
---|
76 | } |
---|
77 | |
---|
78 | namespace AccessLevel |
---|
79 | { |
---|
80 | enum Enum |
---|
81 | { |
---|
82 | All, |
---|
83 | Standalone, |
---|
84 | Master, |
---|
85 | Server, |
---|
86 | Client, |
---|
87 | Online, |
---|
88 | Offline, |
---|
89 | None |
---|
90 | }; |
---|
91 | } |
---|
92 | |
---|
93 | class _CoreExport ConsoleCommand |
---|
94 | { |
---|
95 | friend struct ConsoleCommandManipulator; |
---|
96 | |
---|
97 | struct Command |
---|
98 | { |
---|
99 | ExecutorPtr executor_; |
---|
100 | FunctorPtr functor_; |
---|
101 | std::vector<void*> objectStack_; |
---|
102 | }; |
---|
103 | |
---|
104 | public: |
---|
105 | struct ConsoleCommandManipulator |
---|
106 | { |
---|
107 | public: |
---|
108 | ConsoleCommandManipulator(const ConsoleCommand* command) : command_(const_cast<ConsoleCommand*>(command)) {} |
---|
109 | |
---|
110 | template <class F> |
---|
111 | inline ConsoleCommandManipulator& setFunction(F function, bool bForce = false) |
---|
112 | { |
---|
113 | if (this->command_) |
---|
114 | { |
---|
115 | if (this->command_->getExecutor() && this->command_->getExecutor()->getFunctor() && this->command_->getExecutor()->getFunctor()->getFullIdentifier() == typeid(F)) |
---|
116 | { |
---|
117 | FunctorPointer<F>* functor = static_cast<FunctorPointer<F>*>(this->command_->getExecutor()->getFunctor().get()); |
---|
118 | functor->setFunction(function); |
---|
119 | return *this; |
---|
120 | } |
---|
121 | this->command_->setFunction(createFunctor(function), bForce); |
---|
122 | } |
---|
123 | return *this; |
---|
124 | } |
---|
125 | template <class F, class O> |
---|
126 | inline ConsoleCommandManipulator& setFunction(F function, O* object, bool bForce = false) |
---|
127 | { |
---|
128 | if (this->command_) |
---|
129 | { |
---|
130 | if (this->command_->getExecutor() && this->command_->getExecutor()->getFunctor() && this->command_->getExecutor()->getFunctor()->getFullIdentifier() == typeid(F)) |
---|
131 | { |
---|
132 | FunctorPointer<F, O>* functor = static_cast<FunctorPointer<F, O>*>(this->command_->getExecutor()->getFunctor().get()); |
---|
133 | functor->setFunction(function); |
---|
134 | functor->setObject(object); |
---|
135 | return *this; |
---|
136 | } |
---|
137 | this->command_->setFunction(createFunctor(function, object), bForce); |
---|
138 | } |
---|
139 | return *this; |
---|
140 | } |
---|
141 | inline ConsoleCommandManipulator& setFunction(const FunctorPtr& functor, bool bForce = false) |
---|
142 | { if (this->command_) { this->command_->setFunction(functor, bForce); } return *this; } |
---|
143 | inline ConsoleCommandManipulator& setFunction(const ExecutorPtr& executor, bool bForce = false) |
---|
144 | { if (this->command_) { this->command_->setFunction(executor, bForce); } return *this; } |
---|
145 | |
---|
146 | inline ConsoleCommandManipulator& pushFunction() |
---|
147 | { if (this->command_) { this->command_->pushFunction(); } return *this; } |
---|
148 | template <class F> |
---|
149 | inline ConsoleCommandManipulator& pushFunction(F function, bool bForce = false) |
---|
150 | { if (this->command_) { this->command_->pushFunction(createFunctor(function), bForce); } return *this; } |
---|
151 | template <class F, class O> |
---|
152 | inline ConsoleCommandManipulator& pushFunction(F function, O* object, bool bForce = false) |
---|
153 | { if (this->command_) { this->command_->pushFunction(createFunctor(function, object), bForce); } return *this; } |
---|
154 | inline ConsoleCommandManipulator& pushFunction(const FunctorPtr& functor, bool bForce = false) |
---|
155 | { if (this->command_) { this->command_->pushFunction(functor, bForce); } return *this; } |
---|
156 | inline ConsoleCommandManipulator& pushFunction(const ExecutorPtr& executor, bool bForce = false) |
---|
157 | { if (this->command_) { this->command_->pushFunction(executor, bForce); } return *this; } |
---|
158 | |
---|
159 | inline ConsoleCommandManipulator& popFunction() |
---|
160 | { if (this->command_) { this->command_->popFunction(); } return *this; } |
---|
161 | |
---|
162 | inline ConsoleCommandManipulator& resetFunction() |
---|
163 | { if (this->command_) { this->command_->resetFunction(); } return *this; } |
---|
164 | |
---|
165 | inline ConsoleCommandManipulator& setObject(void* object) |
---|
166 | { if (this->command_) { this->command_->setObject(object); } return *this; } |
---|
167 | inline ConsoleCommandManipulator& pushObject(void* object) |
---|
168 | { if (this->command_) { this->command_->pushObject(object); } return *this; } |
---|
169 | inline ConsoleCommandManipulator& popObject() |
---|
170 | { if (this->command_) { this->command_->popObject(); } return *this; } |
---|
171 | |
---|
172 | inline ConsoleCommandManipulator& setActive(bool bActive) |
---|
173 | { if (this->command_) { this->command_->setActive(bActive); } return *this; } |
---|
174 | inline ConsoleCommandManipulator& activate() |
---|
175 | { return this->setActive(true); } |
---|
176 | inline ConsoleCommandManipulator& deactivate() |
---|
177 | { return this->setActive(false); } |
---|
178 | |
---|
179 | inline ConsoleCommandManipulator& setHidden(bool bHidden) |
---|
180 | { if (this->command_) { this->command_->setHidden(bHidden); } return *this; } |
---|
181 | inline ConsoleCommandManipulator& hide() |
---|
182 | { return this->setHidden(true); } |
---|
183 | inline ConsoleCommandManipulator& show() |
---|
184 | { return this->setHidden(false); } |
---|
185 | |
---|
186 | inline ConsoleCommandManipulator& defaultValues(const MultiType& param1) |
---|
187 | { if (this->command_) { this->command_->defaultValues(param1); } return *this; } |
---|
188 | inline ConsoleCommandManipulator& defaultValues(const MultiType& param1, const MultiType& param2) |
---|
189 | { if (this->command_) { this->command_->defaultValues(param1, param2); } return *this; } |
---|
190 | inline ConsoleCommandManipulator& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3) |
---|
191 | { if (this->command_) { this->command_->defaultValues(param1, param2, param3); } return *this; } |
---|
192 | inline ConsoleCommandManipulator& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) |
---|
193 | { if (this->command_) { this->command_->defaultValues(param1, param2, param3, param4); } return *this; } |
---|
194 | inline ConsoleCommandManipulator& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) |
---|
195 | { if (this->command_) { this->command_->defaultValues(param1, param2, param3, param4, param5); } return *this; } |
---|
196 | inline ConsoleCommandManipulator& defaultValue(unsigned int index, const MultiType& param) |
---|
197 | { if (this->command_) { this->command_->defaultValue(index, param); } return *this; } |
---|
198 | |
---|
199 | inline ConsoleCommandManipulator& accessLevel(AccessLevel::Enum level) |
---|
200 | { if (this->command_) { this->command_->accessLevel(level); } return *this; } |
---|
201 | |
---|
202 | inline ConsoleCommandManipulator& argumentCompleter(unsigned int param, ArgumentCompleter* completer) |
---|
203 | { if (this->command_) { this->command_->argumentCompleter(param, completer); } return *this; } |
---|
204 | |
---|
205 | inline ConsoleCommandManipulator& setAsInputCommand() |
---|
206 | { if (this->command_) { this->command_->setAsInputCommand(); } return *this; } |
---|
207 | inline ConsoleCommandManipulator& keybindMode(KeybindMode::Value mode) |
---|
208 | { if (this->command_) { this->command_->keybindMode(mode); } return *this; } |
---|
209 | inline ConsoleCommandManipulator& inputConfiguredParam(int index) |
---|
210 | { if (this->command_) { this->command_->inputConfiguredParam(index); } return *this; } |
---|
211 | |
---|
212 | private: |
---|
213 | ConsoleCommand* command_; |
---|
214 | }; |
---|
215 | |
---|
216 | public: |
---|
217 | ConsoleCommand(const std::string& group, const std::string& name, const ExecutorPtr& executor, bool bInitialized = true); |
---|
218 | ~ConsoleCommand(); |
---|
219 | |
---|
220 | ConsoleCommand& addShortcut(); |
---|
221 | ConsoleCommand& addShortcut(const std::string& name); |
---|
222 | ConsoleCommand& addGroup(const std::string& group); |
---|
223 | ConsoleCommand& addGroup(const std::string& group, const std::string& name); |
---|
224 | |
---|
225 | inline const std::string& getName() const |
---|
226 | { return this->baseName_; } |
---|
227 | |
---|
228 | const ExecutorPtr& getExecutor() const; |
---|
229 | inline const FunctorPtr& getBaseFunctor() const |
---|
230 | { return this->baseFunctor_; } |
---|
231 | |
---|
232 | inline ConsoleCommand& setActive(bool bActive) |
---|
233 | { this->bActive_ = bActive; return *this; } |
---|
234 | inline ConsoleCommand& activate() |
---|
235 | { return this->setActive(true); } |
---|
236 | inline ConsoleCommand& deactivate() |
---|
237 | { return this->setActive(false); } |
---|
238 | |
---|
239 | inline ConsoleCommand& setHidden(bool bHidden) |
---|
240 | { this->bHidden_ = bHidden; return *this; } |
---|
241 | inline ConsoleCommand& hide() |
---|
242 | { return this->setHidden(true); } |
---|
243 | inline ConsoleCommand& show() |
---|
244 | { return this->setHidden(false); } |
---|
245 | |
---|
246 | bool isActive() const; |
---|
247 | bool hasAccess() const; |
---|
248 | inline bool isHidden() const |
---|
249 | { return this->bHidden_; } |
---|
250 | |
---|
251 | ConsoleCommand& description(const std::string& description); |
---|
252 | const std::string& getDescription() const; |
---|
253 | |
---|
254 | ConsoleCommand& descriptionParam(unsigned int param, const std::string& description); |
---|
255 | const std::string& getDescriptionParam(unsigned int param) const; |
---|
256 | |
---|
257 | ConsoleCommand& descriptionReturnvalue(const std::string& description); |
---|
258 | const std::string& getDescriptionReturnvalue(int param) const; |
---|
259 | |
---|
260 | ConsoleCommand& defaultValues(const MultiType& param1); |
---|
261 | ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2); |
---|
262 | ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3); |
---|
263 | ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4); |
---|
264 | ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5); |
---|
265 | ConsoleCommand& defaultValue(unsigned int index, const MultiType& param); |
---|
266 | |
---|
267 | inline ConsoleCommand& accessLevel(AccessLevel::Enum level) |
---|
268 | { this->accessLevel_ = level; return *this; } |
---|
269 | inline AccessLevel::Enum getAccessLevel() const |
---|
270 | { return this->accessLevel_; } |
---|
271 | |
---|
272 | ConsoleCommand& argumentCompleter(unsigned int param, ArgumentCompleter* completer); |
---|
273 | ArgumentCompleter* getArgumentCompleter(unsigned int param) const; |
---|
274 | |
---|
275 | inline ConsoleCommand& setAsInputCommand() |
---|
276 | { |
---|
277 | this->keybindMode(KeybindMode::OnHold); |
---|
278 | this->defaultValue(0, Vector2(0.0f, 0.0f)); |
---|
279 | this->inputConfiguredParam(0); |
---|
280 | return *this; |
---|
281 | } |
---|
282 | |
---|
283 | inline ConsoleCommand& keybindMode(KeybindMode::Value mode) |
---|
284 | { this->keybindMode_ = mode; return *this; } |
---|
285 | inline KeybindMode::Value getKeybindMode() const |
---|
286 | { return this->keybindMode_; } |
---|
287 | |
---|
288 | inline ConsoleCommand& inputConfiguredParam(int index) |
---|
289 | { this->inputConfiguredParam_ = index; return *this; } |
---|
290 | inline int getInputConfiguredParam_() const |
---|
291 | { return this->inputConfiguredParam_; } |
---|
292 | |
---|
293 | inline ConsoleCommandManipulator getManipulator() const |
---|
294 | { return this; } |
---|
295 | |
---|
296 | private: |
---|
297 | bool headersMatch(const FunctorPtr& functor); |
---|
298 | bool headersMatch(const ExecutorPtr& executor); |
---|
299 | |
---|
300 | bool setFunction(const ExecutorPtr& executor, bool bForce = false); |
---|
301 | bool setFunction(const FunctorPtr& functor, bool bForce = false); |
---|
302 | void pushFunction(const ExecutorPtr& executor, bool bForce = false); |
---|
303 | void pushFunction(const FunctorPtr& functor, bool bForce = false); |
---|
304 | void pushFunction(); |
---|
305 | void popFunction(); |
---|
306 | void resetFunction(); |
---|
307 | |
---|
308 | bool setObject(void* object); |
---|
309 | void pushObject(void* object); |
---|
310 | void popObject(); |
---|
311 | void* getObject() const; |
---|
312 | |
---|
313 | bool bActive_; |
---|
314 | bool bHidden_; |
---|
315 | AccessLevel::Enum accessLevel_; |
---|
316 | std::string baseName_; |
---|
317 | FunctorPtr baseFunctor_; |
---|
318 | |
---|
319 | ExecutorPtr executor_; |
---|
320 | std::stack<Command> commandStack_; |
---|
321 | std::vector<void*> objectStack_; |
---|
322 | |
---|
323 | ArgumentCompleter* argumentCompleter_[5]; |
---|
324 | |
---|
325 | KeybindMode::Value keybindMode_; |
---|
326 | int inputConfiguredParam_; |
---|
327 | |
---|
328 | LanguageEntryLabel description_; |
---|
329 | LanguageEntryLabel descriptionReturnvalue_; |
---|
330 | LanguageEntryLabel descriptionParam_[MAX_FUNCTOR_ARGUMENTS]; |
---|
331 | |
---|
332 | public: |
---|
333 | static inline const std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommands() |
---|
334 | { return ConsoleCommand::getCommandMap(); } |
---|
335 | static inline const std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommandsLC() |
---|
336 | { return ConsoleCommand::getCommandMapLC(); } |
---|
337 | |
---|
338 | static inline const ConsoleCommand* getCommand(const std::string& name, bool bPrintError = false) |
---|
339 | { return ConsoleCommand::getCommand("", name, bPrintError); } |
---|
340 | static inline const ConsoleCommand* getCommandLC(const std::string& name, bool bPrintError = false) |
---|
341 | { return ConsoleCommand::getCommandLC("", name, bPrintError); } |
---|
342 | |
---|
343 | static const ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false); |
---|
344 | static const ConsoleCommand* getCommandLC(const std::string& group, const std::string& name, bool bPrintError = false); |
---|
345 | |
---|
346 | static void destroyAll(); |
---|
347 | |
---|
348 | private: |
---|
349 | static std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommandMap(); |
---|
350 | static std::map<std::string, std::map<std::string, ConsoleCommand*> >& getCommandMapLC(); |
---|
351 | |
---|
352 | static void registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command); |
---|
353 | static void unregisterCommand(ConsoleCommand* command); |
---|
354 | }; |
---|
355 | |
---|
356 | inline ConsoleCommand* createConsoleCommand(const std::string& name, const ExecutorPtr& executor, bool bInitialized = true) |
---|
357 | { return new ConsoleCommand("", name, executor, bInitialized); } |
---|
358 | inline ConsoleCommand* createConsoleCommand(const std::string& group, const std::string& name, const ExecutorPtr& executor, bool bInitialized = true) |
---|
359 | { return new ConsoleCommand(group, name, executor, bInitialized); } |
---|
360 | |
---|
361 | inline ConsoleCommand::ConsoleCommandManipulator ModifyConsoleCommand(const std::string& name) |
---|
362 | { return ConsoleCommand::getCommand(name, true); } |
---|
363 | inline ConsoleCommand::ConsoleCommandManipulator ModifyConsoleCommand(const std::string& group, const std::string& name) |
---|
364 | { return ConsoleCommand::getCommand(group, name, true); } |
---|
365 | } |
---|
366 | |
---|
367 | #endif /* _ConsoleCommand_H__ */ |
---|