Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/core/CommandExecutor.cc @ 1351

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

changed large parts of CommandExecutor and CommandEvaluation. very unfinished.

File size: 9.4 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#include "CommandExecutor.h"
30#include "ConsoleCommand.h"
31#include "util/String.h"
32#include "util/Convert.h"
33#include "Identifier.h"
34#include "Language.h"
35#include "Debug.h"
36#include "TclBind.h"
37
38namespace orxonox
39{
40    CommandExecutor& CommandExecutor::getInstance()
41    {
42        static CommandExecutor instance;
43        return instance;
44    }
45
46    CommandEvaluation& CommandExecutor::getEvaluation()
47    {
48        return CommandExecutor::getInstance().evaluation_;
49    }
50
51    const CommandEvaluation& CommandExecutor::getLastEvaluation()
52    {
53        return CommandExecutor::getInstance().evaluation_;
54    }
55
56    ConsoleCommand& CommandExecutor::addConsoleCommandShortcut(ConsoleCommand* command)
57    {
58        CommandExecutor::getInstance().consoleCommandShortcuts_[command->getName()] = command;
59        CommandExecutor::getInstance().consoleCommandShortcuts_LC_[getLowercase(command->getName())] = command;
60        return (*command);
61    }
62
63    /**
64        @brief Returns the executor of a console command shortcut with given name.
65        @brief name The name of the requested console command shortcut
66        @return The executor of the requested console command shortcut
67    */
68    ConsoleCommand* CommandExecutor::getConsoleCommandShortcut(const std::string& name)
69    {
70        std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_.find(name);
71        if (it != CommandExecutor::getInstance().consoleCommandShortcuts_.end())
72            return (*it).second;
73        else
74            return 0;
75    }
76
77    /**
78        @brief Returns the executor of a console command shortcut with given name in lowercase.
79        @brief name The name of the requested console command shortcut in lowercase
80        @return The executor of the requested console command shortcut
81    */
82    ConsoleCommand* CommandExecutor::getLowercaseConsoleCommandShortcut(const std::string& name)
83    {
84        std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getInstance().consoleCommandShortcuts_LC_.find(name);
85        if (it != CommandExecutor::getInstance().consoleCommandShortcuts_LC_.end())
86            return (*it).second;
87        else
88            return 0;
89    }
90
91    bool CommandExecutor::execute(const std::string& command, bool useTcl)
92    {
93        if (useTcl)
94            return TclBind::eval(command);
95
96        if ((CommandExecutor::getEvaluation().getCommand() != command) || (CommandExecutor::getEvaluation().getState() == CS_Uninitialized))
97            CommandExecutor::parse(command);
98
99        return CommandExecutor::getEvaluation().execute();
100    }
101
102    std::string CommandExecutor::complete(const std::string& command)
103    {
104        if ((CommandExecutor::getEvaluation().getCommand() != command) || (CommandExecutor::getEvaluation().getState() == CS_Uninitialized))
105            CommandExecutor::parse(command);
106
107        return CommandExecutor::getEvaluation().complete();
108    }
109
110    std::string CommandExecutor::hint(const std::string& command)
111    {
112        if ((CommandExecutor::getEvaluation().getCommand() != command) || (CommandExecutor::getEvaluation().getState() == CS_Uninitialized))
113            CommandExecutor::parse(command);
114
115        return CommandExecutor::getEvaluation().hint();
116    }
117
118    CommandEvaluation CommandExecutor::evaluate(const std::string& command)
119    {
120        CommandExecutor::parse(command, true);
121        CommandExecutor::getEvaluation().evaluateParams();
122        return CommandExecutor::getEvaluation();
123    }
124
125    void CommandExecutor::parse(const std::string& command, bool bInitialize)
126    {
127        if (bInitialize)
128            CommandExecutor::getEvaluation().initialize(command);
129        else
130            CommandExecutor::getEvaluation().setNewCommand(false);
131
132        CommandExecutor::getEvaluation().setTokens(command + COMMAND_EXECUTOR_CURSOR);
133        CommandExecutor::getEvaluation().setCommand(command);
134
135        switch (CommandExecutor::getEvaluation().getState())
136        {
137        }
138    }
139
140    bool CommandExecutor::argumentsFinished(unsigned int num)
141    {
142        return (CommandExecutor::argumentsFinished() >= num);
143    }
144
145    unsigned int CommandExecutor::argumentsFinished()
146    {
147        // Because we added a cursor we have +1 arguments
148        if (CommandExecutor::getEvaluation().getTokens().size() >= 1)
149            return (CommandExecutor::getEvaluation().getTokens().size() - 1);
150        else
151            return 0;
152    }
153
154    std::string CommandExecutor::getToken(unsigned int index)
155    {
156        if ((index >= 0) && index < (CommandExecutor::getEvaluation().getOriginalTokens().size()))
157            return CommandExecutor::getEvaluation().getOriginalTokens()[index];
158        else
159            return "";
160    }
161
162    unsigned int CommandExecutor::argumentsGiven()
163    {
164        return CommandExecutor::getEvaluation().getOriginalTokens().size();
165    }
166
167    bool CommandExecutor::enoughArgumentsGiven(ConsoleCommand* command, unsigned int head)
168    {
169        return (CommandExecutor::argumentsGiven() >= (head + command->getParamCount()));
170    }
171
172    void CommandExecutor::createListOfPossibleFunctionClasses(const std::string& fragment)
173    {
174        for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getLowercaseIdentifierMapBegin(); it != Identifier::getLowercaseIdentifierMapEnd(); ++it)
175            if ((*it).second->hasConsoleCommands())
176                if ((*it).first.find(getLowercase(fragment)) == 0 || fragment == "")
177                    CommandExecutor::getEvaluation().getListOfPossibleFunctionClasses().push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second->getName()));
178
179        CommandExecutor::getEvaluation().getListOfPossibleFunctionClasses().sort(CommandExecutor::compareStringsInList);
180    }
181
182    void CommandExecutor::createListOfPossibleFunctions(const std::string& fragment, Identifier* identifier)
183    {
184        if (!identifier)
185        {
186            for (std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getLowercaseConsoleCommandShortcutMapBegin(); it != CommandExecutor::getLowercaseConsoleCommandShortcutMapEnd(); ++it)
187                if ((*it).first.find(getLowercase(fragment)) == 0 || fragment == "")
188                    CommandExecutor::getEvaluation().getListOfPossibleFunctions().push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second->getName()));
189        }
190        else
191        {
192            for (std::map<std::string, ConsoleCommand*>::const_iterator it = identifier->getLowercaseConsoleCommandMapBegin(); it != identifier->getLowercaseConsoleCommandMapEnd(); ++it)
193                if ((*it).first.find(getLowercase(fragment)) == 0 || fragment == "")
194                    CommandExecutor::getEvaluation().getListOfPossibleFunctions().push_back(std::pair<const std::string*, const std::string*>(&(*it).first, &(*it).second->getName()));
195        }
196        CommandExecutor::getEvaluation().getListOfPossibleFunctions().sort(CommandExecutor::compareStringsInList);
197    }
198
199    Identifier* CommandExecutor::getPossibleIdentifier(const std::string& name)
200    {
201        std::map<std::string, Identifier*>::const_iterator it = Identifier::getLowercaseIdentifierMap().find(getLowercase(name));
202        if ((it != Identifier::getLowercaseIdentifierMapEnd()) && (*it).second->hasConsoleCommands())
203            return (*it).second;
204
205        return 0;
206    }
207
208    ConsoleCommand* CommandExecutor::getPossibleCommand(const std::string& name, Identifier* identifier)
209    {
210        if (!identifier)
211        {
212            std::map<std::string, ConsoleCommand*>::const_iterator it = CommandExecutor::getLowercaseConsoleCommandShortcutMap().find(getLowercase(name));
213            if (it != CommandExecutor::getLowercaseConsoleCommandShortcutMapEnd())
214                return (*it).second;
215        }
216        else
217        {
218            std::map<std::string, ConsoleCommand*>::const_iterator it = identifier->getLowercaseConsoleCommandMap().find(getLowercase(name));
219            if (it != identifier->getLowercaseConsoleCommandMapEnd())
220                return (*it).second;
221        }
222        return 0;
223    }
224
225    bool CommandExecutor::compareStringsInList(const std::pair<const std::string*, const std::string*>& first, const std::pair<const std::string*, const std::string*>& second)
226    {
227        return ((*first.first) < (*second.first));
228    }
229}
Note: See TracBrowser for help on using the repository browser.