[1505] | 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" |
---|
[3196] | 30 | |
---|
[1505] | 31 | #include "ConsoleCommand.h" |
---|
| 32 | #include "TclBind.h" |
---|
[7228] | 33 | #include "Shell.h" |
---|
[1505] | 34 | |
---|
| 35 | namespace orxonox |
---|
| 36 | { |
---|
[7228] | 37 | static const std::string __CC_CommandExecutor_name = "CommandExecutor"; |
---|
| 38 | static const std::string __CC_autocomplete_name = "autocomplete"; |
---|
| 39 | |
---|
[7236] | 40 | SetConsoleCommand(__CC_CommandExecutor_name, __CC_autocomplete_name, &CommandExecutor::_autocomplete) |
---|
[7228] | 41 | .hide() |
---|
| 42 | .argumentCompleter(0, autocompletion::groupsandcommands()) |
---|
| 43 | .argumentCompleter(1, autocompletion::subcommands()); |
---|
| 44 | |
---|
[7236] | 45 | SetConsoleCommand("unhide", &CommandExecutor::unhide) |
---|
[7234] | 46 | .argumentCompleter(0, autocompletion::hiddencommand()); |
---|
[7233] | 47 | |
---|
[7277] | 48 | SetConsoleCommand("alias", &CommandExecutor::alias) |
---|
| 49 | .argumentCompleter(1, autocompletion::command()); |
---|
| 50 | |
---|
[7228] | 51 | /* static */ CommandExecutor& CommandExecutor::getInstance() |
---|
[1505] | 52 | { |
---|
| 53 | static CommandExecutor instance; |
---|
| 54 | return instance; |
---|
| 55 | } |
---|
| 56 | |
---|
[7228] | 57 | /* static */ int CommandExecutor::execute(const std::string& command, bool useTcl) |
---|
[1505] | 58 | { |
---|
[7228] | 59 | int error; |
---|
| 60 | CommandExecutor::queryMT(command, &error, useTcl); |
---|
| 61 | return error; |
---|
[1505] | 62 | } |
---|
| 63 | |
---|
[7228] | 64 | /* static */ MultiType CommandExecutor::queryMT(const std::string& command, int* error, bool useTcl) |
---|
[1505] | 65 | { |
---|
| 66 | if (useTcl) |
---|
[7228] | 67 | return TclBind::eval(command, error); |
---|
[7189] | 68 | else |
---|
[7229] | 69 | { |
---|
| 70 | CommandEvaluation evaluation; |
---|
| 71 | if (!CommandExecutor::getInstance().getCached(command, evaluation)) |
---|
| 72 | { |
---|
| 73 | evaluation = CommandExecutor::evaluate(command); |
---|
[7230] | 74 | evaluation.evaluateParams(); |
---|
[7229] | 75 | CommandExecutor::getInstance().cache(command, evaluation); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | return evaluation.query(error); |
---|
| 79 | } |
---|
[1505] | 80 | } |
---|
| 81 | |
---|
[7228] | 82 | /* static */ std::string CommandExecutor::query(const std::string& command, int* error, bool useTcl) |
---|
[3035] | 83 | { |
---|
[7228] | 84 | return CommandExecutor::queryMT(command, error, useTcl).getString(); |
---|
[3035] | 85 | } |
---|
| 86 | |
---|
[7228] | 87 | /* static */ CommandEvaluation CommandExecutor::evaluate(const std::string& command) |
---|
[3035] | 88 | { |
---|
[7228] | 89 | CommandEvaluation evaluation; |
---|
| 90 | evaluation.initialize(command); |
---|
[3035] | 91 | |
---|
[7236] | 92 | evaluation.hintCommand_ = ConsoleCommand::getCommand(__CC_CommandExecutor_name, __CC_autocomplete_name); |
---|
[1505] | 93 | |
---|
[7228] | 94 | if (evaluation.getNumberOfArguments() >= 1) |
---|
[1505] | 95 | { |
---|
[7236] | 96 | evaluation.execCommand_ = ConsoleCommand::getCommandLC(evaluation.getToken(0)); |
---|
[7228] | 97 | if (evaluation.execCommand_) |
---|
| 98 | evaluation.execArgumentsOffset_ = 1; |
---|
| 99 | else if (evaluation.getNumberOfArguments() >= 2) |
---|
[1505] | 100 | { |
---|
[7236] | 101 | evaluation.execCommand_ = ConsoleCommand::getCommandLC(evaluation.getToken(0), evaluation.getToken(1)); |
---|
[7228] | 102 | if (evaluation.execCommand_) |
---|
| 103 | evaluation.execArgumentsOffset_ = 2; |
---|
[1505] | 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
[7228] | 107 | if (evaluation.execCommand_ && evaluation.getNumberOfArguments() > evaluation.execArgumentsOffset_) |
---|
[1505] | 108 | { |
---|
[7228] | 109 | evaluation.hintCommand_ = evaluation.execCommand_; |
---|
| 110 | evaluation.hintArgumentsOffset_ = evaluation.execArgumentsOffset_; |
---|
[1505] | 111 | } |
---|
| 112 | |
---|
[7228] | 113 | return evaluation; |
---|
[1505] | 114 | } |
---|
[7229] | 115 | |
---|
| 116 | bool CommandExecutor::getCached(const std::string& command, CommandEvaluation& evaluation) |
---|
| 117 | { |
---|
| 118 | if (Shell::getCacheSize() == 0) |
---|
| 119 | return false; |
---|
| 120 | |
---|
| 121 | std::map<std::string, CacheEntry>::iterator it = this->cache_.find(command); |
---|
| 122 | if (it != this->cache_.end()) |
---|
| 123 | { |
---|
| 124 | // update ordered list of cached commands (move it to the front) |
---|
| 125 | this->cachelist_.erase(it->second.iterator_); |
---|
| 126 | this->cachelist_.push_front(command); |
---|
| 127 | it->second.iterator_ = this->cachelist_.begin(); |
---|
| 128 | |
---|
| 129 | // assign the cached evaluation |
---|
| 130 | evaluation = it->second.evaluation_; |
---|
| 131 | return true; |
---|
| 132 | } |
---|
| 133 | return false; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | void CommandExecutor::cache(const std::string& command, const CommandEvaluation& evaluation) |
---|
| 137 | { |
---|
| 138 | if (Shell::getCacheSize() == 0) |
---|
| 139 | return; |
---|
| 140 | |
---|
| 141 | // push command to the front of the ordered list |
---|
| 142 | this->cachelist_.push_front(command); |
---|
| 143 | |
---|
| 144 | // create a cache entry and store it in the cache |
---|
| 145 | CacheEntry entry; |
---|
| 146 | entry.evaluation_ = evaluation; |
---|
| 147 | entry.iterator_ = this->cachelist_.begin(); |
---|
| 148 | this->cache_[command] = entry; |
---|
| 149 | |
---|
| 150 | // remove the last command in the ordered list from the cache if it exceeds the maximum size of the cache |
---|
| 151 | if (this->cachelist_.size() > Shell::getCacheSize()) |
---|
| 152 | { |
---|
| 153 | this->cache_.erase(this->cachelist_.back()); |
---|
| 154 | this->cachelist_.pop_back(); |
---|
| 155 | } |
---|
| 156 | } |
---|
[7233] | 157 | |
---|
[7234] | 158 | /* static */ MultiType CommandExecutor::unhide(const std::string& command) |
---|
[7233] | 159 | { |
---|
[7234] | 160 | return CommandExecutor::queryMT(command); |
---|
[7233] | 161 | } |
---|
[7277] | 162 | |
---|
| 163 | /* static */ void CommandExecutor::alias(const std::string& alias, const std::string& command) |
---|
| 164 | { |
---|
| 165 | CommandEvaluation evaluation = CommandExecutor::evaluate(command); |
---|
| 166 | if (evaluation.isValid()) |
---|
| 167 | { |
---|
| 168 | ExecutorPtr executor = new Executor(*evaluation.getConsoleCommand()->getExecutor().get()); |
---|
| 169 | |
---|
| 170 | if (!evaluation.evaluateParams()) |
---|
| 171 | { |
---|
| 172 | for (size_t i = 0; i < MAX_FUNCTOR_ARGUMENTS; ++i) |
---|
| 173 | executor->setDefaultValue(i, evaluation.getEvaluatedParameter(i)); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | SubString tokens(alias, " "); |
---|
| 177 | |
---|
| 178 | if ((tokens.size() == 1 && ConsoleCommand::getCommand(tokens[0])) || (tokens.size() == 2 && ConsoleCommand::getCommand(tokens[0], tokens[1]))) |
---|
| 179 | { |
---|
| 180 | COUT(1) << "Error: A command with name \"" << alias << "\" already exists." << std::endl; |
---|
| 181 | return; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | if (tokens.size() == 1) |
---|
| 185 | createConsoleCommand(tokens[0], executor); |
---|
| 186 | else if (tokens.size() == 2) |
---|
| 187 | createConsoleCommand(tokens[0], tokens[1], executor); |
---|
| 188 | else |
---|
| 189 | COUT(1) << "Error: \"" << alias << "\" is not a valid alias name (must have one or two words)." << std::endl; |
---|
| 190 | } |
---|
| 191 | else |
---|
| 192 | COUT(1) << "Error: \"" << command << "\" is not a valid command (did you mean \"" << evaluation.getCommandSuggestion() << "\"?)." << std::endl; |
---|
| 193 | } |
---|
[1505] | 194 | } |
---|