Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/CommandExecutor.h @ 1056

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

don't panic, no codechanges!
added a link to www.orxonox.net

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