Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/orxonox/core/CommandExecutor.h @ 972

Last change on this file since 972 was 972, checked in by landauf, 16 years ago

CommandExecutor autocompletes now to correct casing (even if you're writing all lowercase or UPPERCASE or sOMethINg eLSe)

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