[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 | |
---|
| 32 | #include "CorePrereqs.h" |
---|
| 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" |
---|
| 40 | #include "CommandExecutor.h" |
---|
[1505] | 41 | #include "Executor.h" |
---|
[1549] | 42 | #include "Identifier.h" |
---|
[1505] | 43 | |
---|
| 44 | |
---|
[1747] | 45 | #define SetConsoleCommand(classname, function, bCreateShortcut) \ |
---|
[3304] | 46 | SetConsoleCommandGeneric(classname, function, #function, bCreateShortcut) |
---|
[1747] | 47 | #define SetConsoleCommandAlias(classname, function, name, bCreateShortcut) \ |
---|
[3304] | 48 | SetConsoleCommandGeneric(classname, function, name, bCreateShortcut) |
---|
[1505] | 49 | |
---|
[3304] | 50 | #define SetConsoleCommandGeneric(classname, function, name, bCreateShortcut) \ |
---|
| 51 | orxonox::ConsoleCommand& BOOST_PP_CAT(classname##function##consolecommand__, __LINE__) = orxonox::ClassIdentifier<classname>::getIdentifier(#classname)->addConsoleCommand(orxonox::createConsoleCommand(orxonox::createFunctor(&classname::function), name), bCreateShortcut) |
---|
[1505] | 52 | |
---|
| 53 | |
---|
| 54 | #define SetConsoleCommandShortcut(classname, function) \ |
---|
[3304] | 55 | SetConsoleCommandShortcutAliasGeneric(classname, function, #function) |
---|
[1747] | 56 | #define SetConsoleCommandShortcutAlias(classname, function, name) \ |
---|
[3304] | 57 | SetConsoleCommandShortcutAliasGeneric(classname, function, name) |
---|
| 58 | #define SetConsoleCommandShortcutAliasGeneric(classname, function, name) \ |
---|
| 59 | SetConsoleCommandShortcutGeneric(BOOST_PP_CAT(function##consolecommand__, __LINE__), orxonox::createConsoleCommand(orxonox::createFunctor(&classname::function), name)) |
---|
[1505] | 60 | |
---|
| 61 | #define SetConsoleCommandShortcutExtern(function) \ |
---|
[3304] | 62 | SetConsoleCommandShortcutExternAliasGeneric(function, #function) |
---|
[1747] | 63 | #define SetConsoleCommandShortcutExternAlias(function, name) \ |
---|
[3304] | 64 | SetConsoleCommandShortcutExternAliasGeneric(function, name) |
---|
| 65 | #define SetConsoleCommandShortcutExternAliasGeneric(function, name) \ |
---|
| 66 | SetConsoleCommandShortcutGeneric(BOOST_PP_CAT(function##consolecommand__, __LINE__), orxonox::createConsoleCommand(orxonox::createFunctor(&function), name)) |
---|
[1505] | 67 | |
---|
| 68 | #define SetConsoleCommandShortcutGeneric(fakevariable, command) \ |
---|
[2662] | 69 | orxonox::ConsoleCommand& fakevariable = orxonox::CommandExecutor::addConsoleCommandShortcut(command, true) |
---|
[1505] | 70 | |
---|
| 71 | |
---|
| 72 | namespace orxonox |
---|
| 73 | { |
---|
| 74 | namespace AccessLevel |
---|
| 75 | { |
---|
[3280] | 76 | enum Value |
---|
[1505] | 77 | { |
---|
| 78 | None, |
---|
| 79 | User, |
---|
| 80 | Admin, |
---|
| 81 | Offline, |
---|
| 82 | Debug, |
---|
| 83 | Disabled |
---|
| 84 | }; |
---|
| 85 | } |
---|
| 86 | |
---|
[1755] | 87 | class _CoreExport ConsoleCommand : public Executor |
---|
[1505] | 88 | { |
---|
| 89 | public: |
---|
[7198] | 90 | ConsoleCommand(const FunctorPtr& functor, const std::string& name = ""); |
---|
[1505] | 91 | |
---|
[7186] | 92 | ConsoleCommand& description(const std::string& description); |
---|
| 93 | const std::string& getDescription() const; |
---|
| 94 | |
---|
| 95 | ConsoleCommand& descriptionParam(unsigned int param, const std::string& description); |
---|
| 96 | const std::string& getDescriptionParam(unsigned int param) const; |
---|
| 97 | |
---|
| 98 | ConsoleCommand& descriptionReturnvalue(const std::string& description); |
---|
| 99 | const std::string& getDescriptionReturnvalue(int param) const; |
---|
| 100 | |
---|
[1747] | 101 | inline ConsoleCommand& defaultValues(const MultiType& param1) |
---|
[1755] | 102 | { this->Executor::setDefaultValues(param1); return (*this); } |
---|
[1747] | 103 | inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2) |
---|
[1755] | 104 | { this->Executor::setDefaultValues(param1, param2); return (*this); } |
---|
[1747] | 105 | inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3) |
---|
[1755] | 106 | { this->Executor::setDefaultValues(param1, param2, param3); return (*this); } |
---|
[1747] | 107 | inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) |
---|
[1755] | 108 | { this->Executor::setDefaultValues(param1, param2, param3, param4); return (*this); } |
---|
[1747] | 109 | inline ConsoleCommand& defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) |
---|
[1755] | 110 | { this->Executor::setDefaultValues(param1, param2, param3, param4, param5); return (*this); } |
---|
[1747] | 111 | inline ConsoleCommand& defaultValue(unsigned int index, const MultiType& param) |
---|
[1755] | 112 | { this->Executor::setDefaultValue(index, param); return (*this); } |
---|
[1505] | 113 | |
---|
[3280] | 114 | inline ConsoleCommand& accessLevel(AccessLevel::Value level) |
---|
[1505] | 115 | { this->accessLevel_ = level; return (*this); } |
---|
[3280] | 116 | inline AccessLevel::Value getAccessLevel() const |
---|
[1505] | 117 | { return this->accessLevel_; } |
---|
| 118 | |
---|
[1747] | 119 | ConsoleCommand& argumentCompleter(unsigned int param, ArgumentCompleter* completer); |
---|
[1505] | 120 | ArgumentCompleter* getArgumentCompleter(unsigned int param) const; |
---|
| 121 | |
---|
| 122 | 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 = ""); |
---|
| 123 | const ArgumentCompletionList& getArgumentCompletionList() const |
---|
| 124 | { return this->argumentList_; } |
---|
| 125 | ArgumentCompletionList::const_iterator getArgumentCompletionListBegin() const |
---|
| 126 | { return this->argumentList_.begin(); } |
---|
| 127 | ArgumentCompletionList::const_iterator getArgumentCompletionListEnd() const |
---|
| 128 | { return this->argumentList_.end(); } |
---|
| 129 | |
---|
[2087] | 130 | inline ConsoleCommand& setAsInputCommand() |
---|
| 131 | { |
---|
| 132 | this->keybindMode(KeybindMode::OnHold); |
---|
| 133 | this->defaultValue(0, Vector2(0.0f, 0.0f)); |
---|
| 134 | this->inputConfiguredParam(0); |
---|
| 135 | return *this; |
---|
| 136 | } |
---|
| 137 | |
---|
[3280] | 138 | inline ConsoleCommand& keybindMode(KeybindMode::Value mode) |
---|
[1505] | 139 | { this->keybindMode_ = mode; return *this; } |
---|
[3280] | 140 | inline KeybindMode::Value getKeybindMode() const |
---|
[1505] | 141 | { return this->keybindMode_; } |
---|
| 142 | |
---|
[2087] | 143 | inline ConsoleCommand& inputConfiguredParam(int index) |
---|
| 144 | { this->inputConfiguredParam_ = index; return *this; } |
---|
| 145 | inline int getInputConfiguredParam_() const |
---|
| 146 | { return this->inputConfiguredParam_; } |
---|
[1505] | 147 | |
---|
| 148 | private: |
---|
[3280] | 149 | AccessLevel::Value accessLevel_; |
---|
[1505] | 150 | ArgumentCompleter* argumentCompleter_[5]; |
---|
| 151 | ArgumentCompletionList argumentList_; |
---|
| 152 | |
---|
[3280] | 153 | KeybindMode::Value keybindMode_; |
---|
[2087] | 154 | int inputConfiguredParam_; |
---|
[7186] | 155 | |
---|
| 156 | LanguageEntryLabel description_; |
---|
| 157 | LanguageEntryLabel descriptionReturnvalue_; |
---|
| 158 | LanguageEntryLabel descriptionParam_[MAX_FUNCTOR_ARGUMENTS]; |
---|
[1505] | 159 | }; |
---|
| 160 | |
---|
[7198] | 161 | inline ConsoleCommand* createConsoleCommand(const FunctorPtr& functor, const std::string& name = "") |
---|
[1505] | 162 | { |
---|
| 163 | return new ConsoleCommand(functor, name); |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
[7179] | 167 | |
---|
| 168 | #define _SetConsoleCommand(...) \ |
---|
| 169 | BOOST_PP_EXPAND(BOOST_PP_CAT(_SetConsoleCommand, ORXONOX_VA_NARGS(__VA_ARGS__))(__VA_ARGS__)) |
---|
| 170 | #define _SetConsoleCommand2(name, functionpointer) \ |
---|
| 171 | _SetConsoleCommandGeneric("", name, orxonox::createFunctor(functionpointer)) |
---|
| 172 | #define _SetConsoleCommand3(group, name, functionpointer) \ |
---|
| 173 | _SetConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer)) |
---|
| 174 | #define _SetConsoleCommand4(group, name, functionpointer, object) \ |
---|
| 175 | _SetConsoleCommandGeneric(group, name, orxonox::createFunctor(functionpointer, object)) |
---|
| 176 | |
---|
| 177 | #define _SetConsoleCommandGeneric(group, name, functor) \ |
---|
| 178 | orxonox::_ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::_createConsoleCommand(group, name, functor)) |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | #define _DeclareConsoleCommand(...) \ |
---|
| 182 | BOOST_PP_CAT(_DeclareConsoleCommand, ORXONOX_VA_NARGS(__VA_ARGS__))(__VA_ARGS__) |
---|
| 183 | #define _DeclareConsoleCommand2(name, functionpointer) \ |
---|
| 184 | _DeclareConsoleCommandGeneric("", name, functionpointer) |
---|
| 185 | #define _DeclareConsoleCommand3(group, name, functionpointer) \ |
---|
| 186 | _DeclareConsoleCommandGeneric(group, name, functionpointer) |
---|
| 187 | |
---|
| 188 | #define _DeclareConsoleCommandGeneric(group, name, functionpointer) \ |
---|
[7185] | 189 | orxonox::_ConsoleCommand& BOOST_PP_CAT(__consolecommand_, __LINE__) = (*orxonox::_createConsoleCommand(group, name, orxonox::createFunctor(functionpointer), false)) |
---|
[7179] | 190 | |
---|
| 191 | |
---|
| 192 | #define _ModifyConsoleCommand(...) \ |
---|
| 193 | orxonox::_ConsoleCommand::getCommand(__VA_ARGS__, true)->getManipulator() |
---|
| 194 | |
---|
| 195 | |
---|
| 196 | namespace orxonox |
---|
| 197 | { |
---|
| 198 | class _CoreExport _ConsoleCommand : protected Executor |
---|
| 199 | { |
---|
| 200 | friend struct _ConsoleCommandManipulator; |
---|
| 201 | |
---|
| 202 | public: |
---|
| 203 | struct _ConsoleCommandManipulator |
---|
| 204 | { |
---|
| 205 | public: |
---|
| 206 | _ConsoleCommandManipulator(const _ConsoleCommand* command) : command_(const_cast<_ConsoleCommand*>(command)) {} |
---|
| 207 | |
---|
| 208 | template <class F> |
---|
[7185] | 209 | inline _ConsoleCommandManipulator& setFunction(F function, bool bForce = false) |
---|
| 210 | { if (this->command_) { this->command_->setFunctor(createFunctor(function), bForce); } return *this; } |
---|
[7179] | 211 | template <class F, class O> |
---|
[7185] | 212 | inline _ConsoleCommandManipulator& setFunction(F function, O* object, bool bForce = false) |
---|
| 213 | { if (this->command_) { this->command_->setFunctor(createFunctor(function, object), bForce); } return *this; } |
---|
[7198] | 214 | inline _ConsoleCommandManipulator& setFunction(const FunctorPtr& functor, bool bForce = false) |
---|
[7185] | 215 | { if (this->command_) { this->command_->setFunctor(functor, bForce); } return *this; } |
---|
[7179] | 216 | |
---|
| 217 | template <class F> |
---|
[7185] | 218 | inline _ConsoleCommandManipulator& pushFunction(F function, bool bForce = false) |
---|
| 219 | { if (this->command_) { this->command_->pushFunctor(createFunctor(function), bForce); } return *this; } |
---|
[7179] | 220 | template <class F, class O> |
---|
[7185] | 221 | inline _ConsoleCommandManipulator& pushFunction(F function, O* object, bool bForce = false) |
---|
| 222 | { if (this->command_) { this->command_->pushFunctor(createFunctor(function, object), bForce); } return *this; } |
---|
[7198] | 223 | inline _ConsoleCommandManipulator& pushFunction(const FunctorPtr& functor, bool bForce = false) |
---|
[7185] | 224 | { if (this->command_) { this->command_->pushFunctor(functor, bForce); } return *this; } |
---|
[7179] | 225 | |
---|
| 226 | inline _ConsoleCommandManipulator& popFunction() |
---|
| 227 | { if (this->command_) { this->command_->popFunctor(); } return *this; } |
---|
| 228 | |
---|
| 229 | inline _ConsoleCommandManipulator& setObject(void* object) |
---|
| 230 | { if (this->command_) { this->command_->setObject(object); } return *this; } |
---|
[7185] | 231 | inline _ConsoleCommandManipulator& pushObject(void* object) |
---|
| 232 | { if (this->command_) { this->command_->pushObject(object); } return *this; } |
---|
| 233 | inline _ConsoleCommandManipulator& popObject() |
---|
| 234 | { if (this->command_) { this->command_->popObject(); } return *this; } |
---|
[7179] | 235 | |
---|
[7185] | 236 | inline void* getObject() const |
---|
| 237 | { if (this->command_) { return this->command_->getObject(); } else { return 0; } } |
---|
| 238 | |
---|
[7179] | 239 | inline _ConsoleCommandManipulator& setActive(bool bActive) |
---|
| 240 | { if (this->command_) { this->command_->setActive(bActive); } return *this; } |
---|
| 241 | |
---|
| 242 | private: |
---|
| 243 | _ConsoleCommand* command_; |
---|
| 244 | }; |
---|
| 245 | |
---|
| 246 | public: |
---|
[7198] | 247 | _ConsoleCommand(const std::string& group, const std::string& name, const FunctorPtr& functor, bool bInitialized = true); |
---|
[7179] | 248 | |
---|
| 249 | _ConsoleCommand& addShortcut(); |
---|
| 250 | _ConsoleCommand& addShortcut(const std::string& name); |
---|
| 251 | _ConsoleCommand& addGroup(const std::string& group); |
---|
| 252 | _ConsoleCommand& addGroup(const std::string& group, const std::string& name); |
---|
| 253 | |
---|
[7185] | 254 | inline void setActive(bool bActive) |
---|
| 255 | { this->bActive_ = bActive; } |
---|
[7179] | 256 | inline bool isActive() const |
---|
[7185] | 257 | { return (this->bActive_ && this->bInitialized_); } |
---|
[7179] | 258 | |
---|
[7185] | 259 | inline _ConsoleCommandManipulator getManipulator() const |
---|
| 260 | { return this; } |
---|
| 261 | |
---|
[7179] | 262 | static inline const std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommands() |
---|
| 263 | { return _ConsoleCommand::getCommandMap(); } |
---|
| 264 | |
---|
| 265 | static inline const _ConsoleCommand* getCommand(const std::string& name, bool bPrintError = false) |
---|
| 266 | { return _ConsoleCommand::getCommand("", name, bPrintError); } |
---|
| 267 | static const _ConsoleCommand* getCommand(const std::string& group, const std::string& name, bool bPrintError = false); |
---|
| 268 | |
---|
| 269 | private: |
---|
| 270 | static std::map<std::string, std::map<std::string, _ConsoleCommand*> >& getCommandMap(); |
---|
| 271 | static void registerCommand(const std::string& group, const std::string& name, _ConsoleCommand* command); |
---|
| 272 | |
---|
[7198] | 273 | bool setFunctor(const FunctorPtr& functor, bool bForce = false); |
---|
| 274 | void pushFunctor(const FunctorPtr& functor, bool bForce = false); |
---|
[7185] | 275 | void popFunctor(); |
---|
[7198] | 276 | const FunctorPtr& getFunctor() const; |
---|
[7179] | 277 | |
---|
[7198] | 278 | bool functionHeaderMatches(const FunctorPtr& functor) const; |
---|
[7179] | 279 | |
---|
| 280 | void setObject(void* object); |
---|
[7185] | 281 | void pushObject(void* object); |
---|
| 282 | void popObject(); |
---|
| 283 | void* getObject() const; |
---|
[7179] | 284 | |
---|
[7185] | 285 | bool bActive_; |
---|
| 286 | bool bInitialized_; |
---|
[7179] | 287 | const std::type_info& functionHeader_; |
---|
[7198] | 288 | std::stack<FunctorPtr> functorStack_; |
---|
[7185] | 289 | std::stack<void*> objectStack_; |
---|
[7179] | 290 | }; |
---|
| 291 | |
---|
[7198] | 292 | inline _ConsoleCommand* _createConsoleCommand(const std::string& name, const FunctorPtr& functor, bool bInitialized = true) |
---|
[7179] | 293 | { |
---|
[7185] | 294 | return new _ConsoleCommand("", name, functor, bInitialized); |
---|
[7179] | 295 | } |
---|
| 296 | |
---|
[7198] | 297 | inline _ConsoleCommand* _createConsoleCommand(const std::string& group, const std::string& name, const FunctorPtr& functor, bool bInitialized = true) |
---|
[7179] | 298 | { |
---|
[7185] | 299 | return new _ConsoleCommand(group, name, functor, bInitialized); |
---|
[7179] | 300 | } |
---|
| 301 | } |
---|
| 302 | |
---|
[1505] | 303 | #endif /* _ConsoleCommand_H__ */ |
---|