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