[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" |
---|
[955] | 36 | #include "util/MultiTypeMath.h" |
---|
[947] | 37 | #include "CorePrereqs.h" |
---|
| 38 | |
---|
[972] | 39 | #define COMMAND_EXECUTOR_CURSOR "$" |
---|
[947] | 40 | |
---|
| 41 | namespace orxonox |
---|
| 42 | { |
---|
[952] | 43 | enum CommandState |
---|
[947] | 44 | { |
---|
[955] | 45 | CS_Uninitialized, |
---|
[952] | 46 | CS_Empty, |
---|
| 47 | CS_FunctionClass_Or_Shortcut_Or_Keyword, |
---|
| 48 | CS_Shortcut_Params, |
---|
| 49 | CS_Shortcut_Finished, |
---|
| 50 | CS_Function, |
---|
| 51 | CS_Function_Params, |
---|
| 52 | CS_Function_Finished, |
---|
| 53 | CS_ConfigValueClass, |
---|
| 54 | CS_ConfigValue, |
---|
| 55 | CS_ConfigValueType, |
---|
| 56 | CS_ConfigValueFinished, |
---|
| 57 | CS_KeybindKey, |
---|
| 58 | CS_KeybindCommand, |
---|
| 59 | CS_KeybindFinished, |
---|
| 60 | CS_Error |
---|
| 61 | }; |
---|
[947] | 62 | |
---|
[993] | 63 | void exec(const std::string& filename); |
---|
[994] | 64 | std::string echo(const std::string& text); |
---|
[993] | 65 | |
---|
[994] | 66 | void write(const std::string& filename, const std::string& text); |
---|
| 67 | void append(const std::string& filename, const std::string& text); |
---|
| 68 | std::string read(const std::string& filename); |
---|
| 69 | |
---|
[953] | 70 | enum KeybindMode {}; // temporary |
---|
| 71 | |
---|
| 72 | /////////////////////// |
---|
| 73 | // CommandEvaluation // |
---|
| 74 | /////////////////////// |
---|
[952] | 75 | class _CoreExport CommandEvaluation |
---|
| 76 | { |
---|
[957] | 77 | friend class CommandExecutor; |
---|
| 78 | |
---|
[947] | 79 | public: |
---|
[955] | 80 | CommandEvaluation(); |
---|
| 81 | |
---|
[953] | 82 | KeybindMode getKeybindMode(); |
---|
| 83 | bool isValid() const; |
---|
| 84 | |
---|
[965] | 85 | inline void setAdditionalParameter(const std::string& param) |
---|
[967] | 86 | { this->additionalParameter_ = param; this->bEvaluatedParams_ = false; } |
---|
[965] | 87 | inline std::string getAdditionalParameter() const |
---|
| 88 | { return (this->additionalParameter_ != "") ? (" " + this->additionalParameter_) : ""; } |
---|
| 89 | |
---|
[967] | 90 | void setEvaluatedParameter(unsigned int index, MultiTypeMath param); |
---|
| 91 | MultiTypeMath getEvaluatedParameter(unsigned int index) const; |
---|
| 92 | |
---|
| 93 | void evaluateParams(); |
---|
| 94 | |
---|
[994] | 95 | MultiTypeMath getReturnvalue() const; |
---|
| 96 | |
---|
[957] | 97 | private: |
---|
[952] | 98 | std::string processedCommand_; |
---|
| 99 | SubString tokens_; |
---|
[953] | 100 | std::string additionalParameter_; |
---|
| 101 | |
---|
[972] | 102 | std::list<std::pair<const std::string*, const std::string*> > listOfPossibleFunctionClasses_; |
---|
| 103 | std::list<std::pair<const std::string*, const std::string*> > listOfPossibleShortcuts_; |
---|
| 104 | std::list<std::pair<const std::string*, const std::string*> > listOfPossibleFunctions_; |
---|
| 105 | std::list<std::pair<const std::string*, const std::string*> > listOfPossibleConfigValueClasses_; |
---|
| 106 | std::list<std::pair<const std::string*, const std::string*> > listOfPossibleConfigValues_; |
---|
| 107 | std::list<std::pair<const std::string*, const std::string*> > listOfPossibleKeys_; |
---|
[952] | 108 | |
---|
| 109 | Identifier* functionclass_; |
---|
| 110 | Identifier* configvalueclass_; |
---|
| 111 | ExecutorStatic* shortcut_; |
---|
| 112 | ExecutorStatic* function_; |
---|
| 113 | ConfigValueContainer* configvalue_; |
---|
| 114 | ConfigValueContainer* key_; |
---|
| 115 | |
---|
| 116 | std::string errorMessage_; |
---|
| 117 | CommandState state_; |
---|
[967] | 118 | |
---|
| 119 | bool bEvaluatedParams_; |
---|
| 120 | MultiTypeMath param_[5]; |
---|
| 121 | ExecutorStatic* evaluatedExecutor_; |
---|
[952] | 122 | }; |
---|
| 123 | |
---|
[953] | 124 | ///////////////////// |
---|
| 125 | // CommandExecutor // |
---|
| 126 | ///////////////////// |
---|
[952] | 127 | class _CoreExport CommandExecutor |
---|
| 128 | { |
---|
| 129 | public: |
---|
[947] | 130 | static bool execute(const std::string& command); |
---|
[952] | 131 | static bool execute(const CommandEvaluation& evaluation); |
---|
| 132 | |
---|
[947] | 133 | static std::string complete(const std::string& command); |
---|
[952] | 134 | static std::string complete(const CommandEvaluation& evaluation); |
---|
| 135 | |
---|
[947] | 136 | static std::string hint(const std::string& command); |
---|
[952] | 137 | static std::string hint(const CommandEvaluation& evaluation); |
---|
[947] | 138 | |
---|
[953] | 139 | static CommandEvaluation evaluate(const std::string& command); |
---|
[952] | 140 | |
---|
[1001] | 141 | static Executor& addConsoleCommandShortcut(ExecutorStatic* executor); |
---|
[947] | 142 | static ExecutorStatic* getConsoleCommandShortcut(const std::string& name); |
---|
| 143 | static ExecutorStatic* getLowercaseConsoleCommandShortcut(const std::string& name); |
---|
| 144 | |
---|
| 145 | /** @brief Returns the map that stores all console commands. @return The const_iterator */ |
---|
[951] | 146 | static inline const std::map<std::string, ExecutorStatic*>& getConsoleCommandShortcutMap() { return CommandExecutor::getInstance().consoleCommandShortcuts_; } |
---|
[947] | 147 | /** @brief Returns a const_iterator to the beginning of the map that stores all console commands. @return The const_iterator */ |
---|
[951] | 148 | static inline std::map<std::string, ExecutorStatic*>::const_iterator getConsoleCommandShortcutMapBegin() { return CommandExecutor::getInstance().consoleCommandShortcuts_.begin(); } |
---|
[947] | 149 | /** @brief Returns a const_iterator to the end of the map that stores all console commands. @return The const_iterator */ |
---|
[951] | 150 | static inline std::map<std::string, ExecutorStatic*>::const_iterator getConsoleCommandShortcutMapEnd() { return CommandExecutor::getInstance().consoleCommandShortcuts_.end(); } |
---|
[947] | 151 | |
---|
| 152 | /** @brief Returns the map that stores all console commands with their names in lowercase. @return The const_iterator */ |
---|
[951] | 153 | static inline const std::map<std::string, ExecutorStatic*>& getLowercaseConsoleCommandShortcutMap() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_; } |
---|
[947] | 154 | /** @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] | 155 | static inline std::map<std::string, ExecutorStatic*>::const_iterator getLowercaseConsoleCommandShortcutMapBegin() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_.begin(); } |
---|
[947] | 156 | /** @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] | 157 | static inline std::map<std::string, ExecutorStatic*>::const_iterator getLowercaseConsoleCommandShortcutMapEnd() { return CommandExecutor::getInstance().consoleCommandShortcuts_LC_.end(); } |
---|
[947] | 158 | |
---|
| 159 | private: |
---|
[950] | 160 | CommandExecutor() {} |
---|
| 161 | CommandExecutor(const CommandExecutor& other) {} |
---|
| 162 | ~CommandExecutor() {} |
---|
| 163 | |
---|
[949] | 164 | static CommandExecutor& getInstance(); |
---|
[952] | 165 | static CommandEvaluation& getEvaluation(); |
---|
[949] | 166 | |
---|
[947] | 167 | static void parse(const std::string& command, bool bInitialize = true); |
---|
[953] | 168 | static void initialize(const std::string& command); |
---|
[947] | 169 | |
---|
| 170 | static bool argumentsGiven(unsigned int num); |
---|
| 171 | static unsigned int argumentsGiven(); |
---|
| 172 | |
---|
| 173 | static std::string getToken(unsigned int index); |
---|
| 174 | |
---|
| 175 | static bool enoughParametersGiven(unsigned int head, Executor* executor); |
---|
| 176 | |
---|
| 177 | static void createListOfPossibleShortcuts(const std::string& fragment); |
---|
| 178 | static void createListOfPossibleFunctionClasses(const std::string& fragment); |
---|
| 179 | static void createListOfPossibleFunctions(const std::string& fragment, Identifier* identifier); |
---|
| 180 | static void createListOfPossibleConfigValueClasses(const std::string& fragment); |
---|
| 181 | static void createListOfPossibleConfigValues(const std::string& fragment, Identifier* identifier); |
---|
| 182 | static void createListOfPossibleKeys(const std::string& fragment); |
---|
| 183 | |
---|
[972] | 184 | static bool compareStringsInList(const std::pair<const std::string*, const std::string*>& first, const std::pair<const std::string*, const std::string*>& second); |
---|
[947] | 185 | |
---|
[972] | 186 | static std::string dump(const std::list<std::pair<const std::string*, const std::string*> >& list); |
---|
[947] | 187 | static std::string dump(const ExecutorStatic* executor); |
---|
| 188 | static std::string dump(const ConfigValueContainer* container); |
---|
| 189 | |
---|
[972] | 190 | static std::string getCommonBegin(const std::list<std::pair<const std::string*, const std::string*> >& list); |
---|
[948] | 191 | |
---|
[947] | 192 | static Identifier* getIdentifierOfPossibleFunctionClass(const std::string& name); |
---|
| 193 | static ExecutorStatic* getExecutorOfPossibleShortcut(const std::string& name); |
---|
| 194 | static ExecutorStatic* getExecutorOfPossibleFunction(const std::string& name, Identifier* identifier); |
---|
| 195 | static Identifier* getIdentifierOfPossibleConfigValueClass(const std::string& name); |
---|
| 196 | static ConfigValueContainer* getContainerOfPossibleConfigValue(const std::string& name, Identifier* identifier); |
---|
| 197 | static ConfigValueContainer* getContainerOfPossibleKey(const std::string& name); |
---|
| 198 | |
---|
[952] | 199 | CommandEvaluation evaluation_; |
---|
[947] | 200 | |
---|
[951] | 201 | std::map<std::string, ExecutorStatic*> consoleCommandShortcuts_; |
---|
| 202 | std::map<std::string, ExecutorStatic*> consoleCommandShortcuts_LC_; |
---|
[947] | 203 | }; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | #endif /* _CommandExecutor_H__ */ |
---|