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 | |
---|
35 | #include "CorePrereqs.h" |
---|
36 | |
---|
37 | class SubString; |
---|
38 | |
---|
39 | namespace orxonox |
---|
40 | { |
---|
41 | class _CoreExport CommandExecutor |
---|
42 | { |
---|
43 | enum CommandState |
---|
44 | { |
---|
45 | CS_Empty, |
---|
46 | CS_FunctionClass_Or_Shortcut_Or_Keyword, |
---|
47 | CS_Shortcut_Params, |
---|
48 | CS_Shortcut_Finished, |
---|
49 | CS_Function, |
---|
50 | CS_Function_Params, |
---|
51 | CS_Function_Finished, |
---|
52 | CS_ConfigValueClass, |
---|
53 | CS_ConfigValue, |
---|
54 | CS_ConfigValueType, |
---|
55 | CS_ConfigValueFinished, |
---|
56 | CS_KeybindKey, |
---|
57 | CS_KeybindCommand, |
---|
58 | CS_KeybindFinished, |
---|
59 | CS_Error |
---|
60 | }; |
---|
61 | |
---|
62 | public: |
---|
63 | static bool execute(const std::string& command); |
---|
64 | static std::string complete(const std::string& command); |
---|
65 | static std::string hint(const std::string& command); |
---|
66 | |
---|
67 | static bool addConsoleCommandShortcut(ExecutorStatic* executor); |
---|
68 | static ExecutorStatic* getConsoleCommandShortcut(const std::string& name); |
---|
69 | static ExecutorStatic* getLowercaseConsoleCommandShortcut(const std::string& name); |
---|
70 | |
---|
71 | /** @brief Returns the map that stores all console commands. @return The const_iterator */ |
---|
72 | static inline const std::map<std::string, ExecutorStatic*>& getConsoleCommandShortcutMap() { return CommandExecutor::consoleCommandShortcuts_s; } |
---|
73 | /** @brief Returns a const_iterator to the beginning of the map that stores all console commands. @return The const_iterator */ |
---|
74 | static inline std::map<std::string, ExecutorStatic*>::const_iterator getConsoleCommandShortcutMapBegin() { return CommandExecutor::consoleCommandShortcuts_s.begin(); } |
---|
75 | /** @brief Returns a const_iterator to the end of the map that stores all console commands. @return The const_iterator */ |
---|
76 | static inline std::map<std::string, ExecutorStatic*>::const_iterator getConsoleCommandShortcutMapEnd() { return CommandExecutor::consoleCommandShortcuts_s.end(); } |
---|
77 | |
---|
78 | /** @brief Returns the map that stores all console commands with their names in lowercase. @return The const_iterator */ |
---|
79 | static inline const std::map<std::string, ExecutorStatic*>& getLowercaseConsoleCommandShortcutMap() { return CommandExecutor::consoleCommandShortcuts_LC_s; } |
---|
80 | /** @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 */ |
---|
81 | static inline std::map<std::string, ExecutorStatic*>::const_iterator getLowercaseConsoleCommandShortcutMapBegin() { return CommandExecutor::consoleCommandShortcuts_LC_s.begin(); } |
---|
82 | /** @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 */ |
---|
83 | static inline std::map<std::string, ExecutorStatic*>::const_iterator getLowercaseConsoleCommandShortcutMapEnd() { return CommandExecutor::consoleCommandShortcuts_LC_s.end(); } |
---|
84 | |
---|
85 | private: |
---|
86 | static void parse(const std::string& command, bool bInitialize = true); |
---|
87 | static void initialize(); |
---|
88 | |
---|
89 | static bool argumentsGiven(unsigned int num); |
---|
90 | static unsigned int argumentsGiven(); |
---|
91 | |
---|
92 | static std::string getToken(unsigned int index); |
---|
93 | |
---|
94 | static bool enoughParametersGiven(unsigned int head, Executor* executor); |
---|
95 | |
---|
96 | static void createListOfPossibleShortcuts(const std::string& fragment); |
---|
97 | static void createListOfPossibleFunctionClasses(const std::string& fragment); |
---|
98 | static void createListOfPossibleFunctions(const std::string& fragment, Identifier* identifier); |
---|
99 | static void createListOfPossibleConfigValueClasses(const std::string& fragment); |
---|
100 | static void createListOfPossibleConfigValues(const std::string& fragment, Identifier* identifier); |
---|
101 | static void createListOfPossibleKeys(const std::string& fragment); |
---|
102 | |
---|
103 | static bool compareStringsInList(const std::string* first, const std::string* second); |
---|
104 | |
---|
105 | static std::string dump(const std::list<const std::string*>& list); |
---|
106 | static std::string dump(const ExecutorStatic* executor); |
---|
107 | static std::string dump(const ConfigValueContainer* container); |
---|
108 | |
---|
109 | static std::string getCommonBegin(const std::list<const std::string*>& list); |
---|
110 | |
---|
111 | static Identifier* getIdentifierOfPossibleFunctionClass(const std::string& name); |
---|
112 | static ExecutorStatic* getExecutorOfPossibleShortcut(const std::string& name); |
---|
113 | static ExecutorStatic* getExecutorOfPossibleFunction(const std::string& name, Identifier* identifier); |
---|
114 | static Identifier* getIdentifierOfPossibleConfigValueClass(const std::string& name); |
---|
115 | static ConfigValueContainer* getContainerOfPossibleConfigValue(const std::string& name, Identifier* identifier); |
---|
116 | static ConfigValueContainer* getContainerOfPossibleKey(const std::string& name); |
---|
117 | |
---|
118 | static const char cursor_s = '$'; |
---|
119 | |
---|
120 | static std::string lastProcessedCommand_s; |
---|
121 | static SubString tokens_s; |
---|
122 | static std::list<const std::string*> listOfPossibleFunctionClasses_s; |
---|
123 | static std::list<const std::string*> listOfPossibleShortcuts_s; |
---|
124 | static std::list<const std::string*> listOfPossibleFunctions_s; |
---|
125 | static std::list<const std::string*> listOfPossibleConfigValueClasses_s; |
---|
126 | static std::list<const std::string*> listOfPossibleConfigValues_s; |
---|
127 | static std::list<const std::string*> listOfPossibleKeys_s; |
---|
128 | |
---|
129 | static Identifier* functionclass_s; |
---|
130 | static Identifier* configvalueclass_s; |
---|
131 | static ExecutorStatic* shortcut_s; |
---|
132 | static ExecutorStatic* function_s; |
---|
133 | static ConfigValueContainer* configvalue_s; |
---|
134 | static ConfigValueContainer* key_s; |
---|
135 | |
---|
136 | static std::string errorMessage_s; |
---|
137 | static CommandState state_s; |
---|
138 | |
---|
139 | static std::map<std::string, ExecutorStatic*> consoleCommandShortcuts_s; |
---|
140 | static std::map<std::string, ExecutorStatic*> consoleCommandShortcuts_LC_s; |
---|
141 | }; |
---|
142 | } |
---|
143 | |
---|
144 | #endif /* _CommandExecutor_H__ */ |
---|