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