Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1258 was 1214, checked in by landauf, 17 years ago

merged console-branch back to trunk.
IMPORTANT: update your media directory!

you need TCL to compile. TCL is available here: http://www.tcl.tk/
another option is to check out https://svn.orxonox.net/ogre/tcl8.5.2/ and compile it by yourself. makefiles are in the 'macosx', 'unix' and 'win' subfolders.
FindTCL.cmake searches in the usual locations and in ../libs/tcl8.5.2/

the orxonox console can be activated with numpad-enter. whatever you enter will be parsed by TCL. if TCL doesn't know a command, it gets executed by orxonox.

simple tcl commands are: "puts text" to write "text" into the console, "expr 1+1" to calculate the result of the given expression. just try it by yourself with "puts [expr 1+1]".
[x] means: evaluate x and use the returnvalue as an argument. in this case the returned value is "2" and the resulting command therefore "puts 2".

you can combine orxonox and tcl commands. a simple orxonox command is "log text" that writes text into the console and the logfile. test it with "log [expr 1+1]" to write "2" into all output channels of orxonox. something more advanced: "log [clock seconds]" writes the seconds since 1970 into the logfile. feel free to combine both: "log [clock seconds]: 1+1 is [expr 1+1]"

TCL uses variables. to set a new variable, use "set varname value". you can use the variable wherever you want with $varname. with this we can make the above command a bit more elegant:
set myexpression 1+1
log [clock seconds]: $myexpression is [expr $myexpression]

read more about tcl in the wiki: http://wiki.tcl.tk/

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