Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands3/src/libraries/core/command/CommandExecutor.cc @ 7236

Last change on this file since 7236 was 7236, checked in by landauf, 14 years ago

replaced the temporary names of all ConsoleCommand related classes and functions by their real names

  • Property svn:eol-style set to native
File size: 5.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
31#include "ConsoleCommand.h"
32#include "TclBind.h"
33#include "Shell.h"
34
35namespace orxonox
36{
37    static const std::string __CC_CommandExecutor_name = "CommandExecutor";
38    static const std::string __CC_autocomplete_name = "autocomplete";
39
40    SetConsoleCommand(__CC_CommandExecutor_name, __CC_autocomplete_name, &CommandExecutor::_autocomplete)
41        .hide()
42        .argumentCompleter(0, autocompletion::groupsandcommands())
43        .argumentCompleter(1, autocompletion::subcommands());
44
45    SetConsoleCommand("unhide", &CommandExecutor::unhide)
46        .argumentCompleter(0, autocompletion::hiddencommand());
47
48    /* static */ CommandExecutor& CommandExecutor::getInstance()
49    {
50        static CommandExecutor instance;
51        return instance;
52    }
53
54    /* static */ int CommandExecutor::execute(const std::string& command, bool useTcl)
55    {
56        int error;
57        CommandExecutor::queryMT(command, &error, useTcl);
58        return error;
59    }
60
61    /* static */ MultiType CommandExecutor::queryMT(const std::string& command, int* error, bool useTcl)
62    {
63        if (useTcl)
64            return TclBind::eval(command, error);
65        else
66        {
67            CommandEvaluation evaluation;
68            if (!CommandExecutor::getInstance().getCached(command, evaluation))
69            {
70                evaluation = CommandExecutor::evaluate(command);
71                evaluation.evaluateParams();
72                CommandExecutor::getInstance().cache(command, evaluation);
73            }
74
75            return evaluation.query(error);
76        }
77    }
78
79    /* static */ std::string CommandExecutor::query(const std::string& command, int* error, bool useTcl)
80    {
81        return CommandExecutor::queryMT(command, error, useTcl).getString();
82    }
83
84    /* static */ CommandEvaluation CommandExecutor::evaluate(const std::string& command)
85    {
86        CommandEvaluation evaluation;
87        evaluation.initialize(command);
88
89        evaluation.hintCommand_ = ConsoleCommand::getCommand(__CC_CommandExecutor_name, __CC_autocomplete_name);
90
91        if (evaluation.getNumberOfArguments() >= 1)
92        {
93            evaluation.execCommand_ = ConsoleCommand::getCommandLC(evaluation.getToken(0));
94            if (evaluation.execCommand_)
95                evaluation.execArgumentsOffset_ = 1;
96            else if (evaluation.getNumberOfArguments() >= 2)
97            {
98                evaluation.execCommand_ = ConsoleCommand::getCommandLC(evaluation.getToken(0), evaluation.getToken(1));
99                if (evaluation.execCommand_)
100                    evaluation.execArgumentsOffset_ = 2;
101            }
102        }
103
104        if (evaluation.execCommand_ && evaluation.getNumberOfArguments() > evaluation.execArgumentsOffset_)
105        {
106            evaluation.hintCommand_ = evaluation.execCommand_;
107            evaluation.hintArgumentsOffset_ = evaluation.execArgumentsOffset_;
108        }
109
110        return evaluation;
111    }
112
113    bool CommandExecutor::getCached(const std::string& command, CommandEvaluation& evaluation)
114    {
115        if (Shell::getCacheSize() == 0)
116            return false;
117
118        std::map<std::string, CacheEntry>::iterator it = this->cache_.find(command);
119        if (it != this->cache_.end())
120        {
121            // update ordered list of cached commands (move it to the front)
122            this->cachelist_.erase(it->second.iterator_);
123            this->cachelist_.push_front(command);
124            it->second.iterator_ = this->cachelist_.begin();
125
126            // assign the cached evaluation
127            evaluation = it->second.evaluation_;
128            return true;
129        }
130        return false;
131    }
132
133    void CommandExecutor::cache(const std::string& command, const CommandEvaluation& evaluation)
134    {
135        if (Shell::getCacheSize() == 0)
136            return;
137
138        // push command to the front of the ordered list
139        this->cachelist_.push_front(command);
140
141        // create a cache entry and store it in the cache
142        CacheEntry entry;
143        entry.evaluation_ = evaluation;
144        entry.iterator_ = this->cachelist_.begin();
145        this->cache_[command] = entry;
146
147        // remove the last command in the ordered list from the cache if it exceeds the maximum size of the cache
148        if (this->cachelist_.size() > Shell::getCacheSize())
149        {
150            this->cache_.erase(this->cachelist_.back());
151            this->cachelist_.pop_back();
152        }
153    }
154
155    /* static */ MultiType CommandExecutor::unhide(const std::string& command)
156    {
157        return CommandExecutor::queryMT(command);
158    }
159}
Note: See TracBrowser for help on using the repository browser.