[947] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
| 22 | * Fabian 'x3n' Landau |
---|
| 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | #ifndef _CommandExecutor_H__ |
---|
| 29 | #define _CommandExecutor_H__ |
---|
| 30 | |
---|
| 31 | #include <string> |
---|
| 32 | #include <map> |
---|
| 33 | #include <list> |
---|
| 34 | |
---|
[949] | 35 | #include "util/SubString.h" |
---|
[947] | 36 | #include "CorePrereqs.h" |
---|
| 37 | |
---|
[949] | 38 | #define COMMAND_EXECUTOR_CURSOR '$' |
---|
[947] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
[952] | 42 | enum CommandState |
---|
[947] | 43 | { |
---|
[952] | 44 | CS_Empty, |
---|
| 45 | CS_FunctionClass_Or_Shortcut_Or_Keyword, |
---|
| 46 | CS_Shortcut_Params, |
---|
| 47 | CS_Shortcut_Finished, |
---|
| 48 | CS_Function, |
---|
| 49 | CS_Function_Params, |
---|
| 50 | CS_Function_Finished, |
---|
| 51 | CS_ConfigValueClass, |
---|
| 52 | CS_ConfigValue, |
---|
| 53 | CS_ConfigValueType, |
---|
| 54 | CS_ConfigValueFinished, |
---|
| 55 | CS_KeybindKey, |
---|
| 56 | CS_KeybindCommand, |
---|
| 57 | CS_KeybindFinished, |
---|
| 58 | CS_Error |
---|
| 59 | }; |
---|
[947] | 60 | |
---|
[953] | 61 | enum KeybindMode {}; // temporary |
---|
| 62 | |
---|
| 63 | /////////////////////// |
---|
| 64 | // CommandEvaluation // |
---|
| 65 | /////////////////////// |
---|
[952] | 66 | class _CoreExport CommandEvaluation |
---|
| 67 | { |
---|
[947] | 68 | public: |
---|
[953] | 69 | KeybindMode getKeybindMode(); |
---|
| 70 | void setAdditionalParameter(const std::string& param); |
---|
| 71 | bool isValid() const; |
---|
| 72 | |
---|
[952] | 73 | std::string processedCommand_; |
---|
| 74 | SubString tokens_; |
---|
[953] | 75 | std::string additionalParameter_; |
---|
| 76 | |
---|
[952] | 77 | std::list<const std::string*> listOfPossibleFunctionClasses_; |
---|
| 78 | std::list<const std::string*> listOfPossibleShortcuts_; |
---|
| 79 | std::list<const std::string*> listOfPossibleFunctions_; |
---|
| 80 | std::list<const std::string*> listOfPossibleConfigValueClasses_; |
---|
| 81 | std::list<const std::string*> listOfPossibleConfigValues_; |
---|
| 82 | std::list<const std::string*> listOfPossibleKeys_; |
---|
| 83 | |
---|
| 84 | Identifier* functionclass_; |
---|
| 85 | Identifier* configvalueclass_; |
---|
| 86 | ExecutorStatic* shortcut_; |
---|
| 87 | ExecutorStatic* function_; |
---|
| 88 | ConfigValueContainer* configvalue_; |
---|
| 89 | ConfigValueContainer* key_; |
---|
| 90 | |
---|
| 91 | std::string errorMessage_; |
---|
| 92 | CommandState state_; |
---|
| 93 | }; |
---|
| 94 | |
---|
[953] | 95 | ///////////////////// |
---|
| 96 | // CommandExecutor // |
---|
| 97 | ///////////////////// |
---|
[952] | 98 | class _CoreExport CommandExecutor |
---|
| 99 | { |
---|
| 100 | public: |
---|
[947] | 101 | static bool execute(const std::string& command); |
---|
[952] | 102 | static bool execute(const CommandEvaluation& evaluation); |
---|
| 103 | |
---|
[947] | 104 | static std::string complete(const std::string& command); |
---|
[952] | 105 | static std::string complete(const CommandEvaluation& evaluation); |
---|
| 106 | |
---|
[947] | 107 | static std::string hint(const std::string& command); |
---|
[952] | 108 | static std::string hint(const CommandEvaluation& evaluation); |
---|
[947] | 109 | |
---|
[953] | 110 | static CommandEvaluation evaluate(const std::string& command); |
---|
[952] | 111 | |
---|
[947] | 112 | static bool addConsoleCommandShortcut(ExecutorStatic* executor); |
---|
| 113 | static ExecutorStatic* getConsoleCommandShortcut(const std::string& name); |
---|
| 114 | static ExecutorStatic* getLowercaseConsoleCommandShortcut(const std::string& name); |
---|
| 115 | |
---|
| 116 | /** @brief Returns the map that stores all console commands. @return The const_iterator */ |
---|
[951] | 117 | static inline const std::map<std::string, ExecutorStatic*>& getConsoleCommandShortcutMap() { return CommandExecutor::getInstance().consoleCommandShortcuts_; } |
---|
[947] | 118 | /** @brief Returns a const_iterator to the beginning of the map that stores all console commands. @return The const_iterator */ |
---|
[951] | 119 | static inline std::map<std::string, ExecutorStatic*>::const_iterator getConsoleCommandShortcutMapBegin() { return CommandExecutor::getInstance().consoleCommandShortcuts_.begin(); } |
---|
[947] | 120 | /** @brief Returns a const_iterator to the end of the map that stores all console commands. @return The const_iterator */ |
---|
[951] | 121 | static inline std::map<std::string, ExecutorStatic*>::const_iterator getConsoleCommandShortcutMapEnd() { return CommandExecutor::getInstance().consoleCommandShortcuts_.end(); } |
---|
[947] | 122 | |
---|
| 123 | /** @brief Returns the map that stores all console commands with their names in lowercase. @return The const_iterator */ |
---|
[951] | 124 | static inline const std::map<std::string, ExecutorStatic*>& getLowercaseConsoleCommandShortcutMap() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_; } |
---|
[947] | 125 | /** @brief Returns a const_iterator to the beginning of the map that stores all console commands with their names in lowercase. @return The const_iterator */ |
---|
[951] | 126 | static inline std::map<std::string, ExecutorStatic*>::const_iterator getLowercaseConsoleCommandShortcutMapBegin() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_.begin(); } |
---|
[947] | 127 | /** @brief Returns a const_iterator to the end of the map that stores all console commands with their names in lowercase. @return The const_iterator */ |
---|
[951] | 128 | static inline std::map<std::string, ExecutorStatic*>::const_iterator getLowercaseConsoleCommandShortcutMapEnd() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_.end(); } |
---|
[947] | 129 | |
---|
| 130 | private: |
---|
[950] | 131 | CommandExecutor() {} |
---|
| 132 | CommandExecutor(const CommandExecutor& other) {} |
---|
| 133 | ~CommandExecutor() {} |
---|
| 134 | |
---|
[949] | 135 | static CommandExecutor& getInstance(); |
---|
[952] | 136 | static CommandEvaluation& getEvaluation(); |
---|
[949] | 137 | |
---|
[947] | 138 | static void parse(const std::string& command, bool bInitialize = true); |
---|
[953] | 139 | static void initialize(const std::string& command); |
---|
[947] | 140 | |
---|
| 141 | static bool argumentsGiven(unsigned int num); |
---|
| 142 | static unsigned int argumentsGiven(); |
---|
| 143 | |
---|
| 144 | static std::string getToken(unsigned int index); |
---|
| 145 | |
---|
| 146 | static bool enoughParametersGiven(unsigned int head, Executor* executor); |
---|
| 147 | |
---|
| 148 | static void createListOfPossibleShortcuts(const std::string& fragment); |
---|
| 149 | static void createListOfPossibleFunctionClasses(const std::string& fragment); |
---|
| 150 | static void createListOfPossibleFunctions(const std::string& fragment, Identifier* identifier); |
---|
| 151 | static void createListOfPossibleConfigValueClasses(const std::string& fragment); |
---|
| 152 | static void createListOfPossibleConfigValues(const std::string& fragment, Identifier* identifier); |
---|
| 153 | static void createListOfPossibleKeys(const std::string& fragment); |
---|
| 154 | |
---|
| 155 | static bool compareStringsInList(const std::string* first, const std::string* second); |
---|
| 156 | |
---|
| 157 | static std::string dump(const std::list<const std::string*>& list); |
---|
| 158 | static std::string dump(const ExecutorStatic* executor); |
---|
| 159 | static std::string dump(const ConfigValueContainer* container); |
---|
| 160 | |
---|
[948] | 161 | static std::string getCommonBegin(const std::list<const std::string*>& list); |
---|
| 162 | |
---|
[947] | 163 | static Identifier* getIdentifierOfPossibleFunctionClass(const std::string& name); |
---|
| 164 | static ExecutorStatic* getExecutorOfPossibleShortcut(const std::string& name); |
---|
| 165 | static ExecutorStatic* getExecutorOfPossibleFunction(const std::string& name, Identifier* identifier); |
---|
| 166 | static Identifier* getIdentifierOfPossibleConfigValueClass(const std::string& name); |
---|
| 167 | static ConfigValueContainer* getContainerOfPossibleConfigValue(const std::string& name, Identifier* identifier); |
---|
| 168 | static ConfigValueContainer* getContainerOfPossibleKey(const std::string& name); |
---|
| 169 | |
---|
[952] | 170 | CommandEvaluation evaluation_; |
---|
[947] | 171 | |
---|
[951] | 172 | std::map<std::string, ExecutorStatic*> consoleCommandShortcuts_; |
---|
| 173 | std::map<std::string, ExecutorStatic*> consoleCommandShortcuts_LC_; |
---|
[947] | 174 | }; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | #endif /* _CommandExecutor_H__ */ |
---|