[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 | |
---|
[7228] | 48 | /* static */ CommandExecutor& CommandExecutor::getInstance() |
---|
[1505] | 49 | { |
---|
| 50 | static CommandExecutor instance; |
---|
| 51 | return instance; |
---|
| 52 | } |
---|
| 53 | |
---|
[7228] | 54 | /* static */ int CommandExecutor::execute(const std::string& command, bool useTcl) |
---|
[1505] | 55 | { |
---|
[7228] | 56 | int error; |
---|
| 57 | CommandExecutor::queryMT(command, &error, useTcl); |
---|
| 58 | return error; |
---|
[1505] | 59 | } |
---|
| 60 | |
---|
[7228] | 61 | /* static */ MultiType CommandExecutor::queryMT(const std::string& command, int* error, bool useTcl) |
---|
[1505] | 62 | { |
---|
| 63 | if (useTcl) |
---|
[7228] | 64 | return TclBind::eval(command, error); |
---|
[7189] | 65 | else |
---|
[7229] | 66 | { |
---|
| 67 | CommandEvaluation evaluation; |
---|
| 68 | if (!CommandExecutor::getInstance().getCached(command, evaluation)) |
---|
| 69 | { |
---|
| 70 | evaluation = CommandExecutor::evaluate(command); |
---|
[7230] | 71 | evaluation.evaluateParams(); |
---|
[7229] | 72 | CommandExecutor::getInstance().cache(command, evaluation); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | return evaluation.query(error); |
---|
| 76 | } |
---|
[1505] | 77 | } |
---|
| 78 | |
---|
[7228] | 79 | /* static */ std::string CommandExecutor::query(const std::string& command, int* error, bool useTcl) |
---|
[3035] | 80 | { |
---|
[7228] | 81 | return CommandExecutor::queryMT(command, error, useTcl).getString(); |
---|
[3035] | 82 | } |
---|
| 83 | |
---|
[7228] | 84 | /* static */ CommandEvaluation CommandExecutor::evaluate(const std::string& command) |
---|
[3035] | 85 | { |
---|
[7228] | 86 | CommandEvaluation evaluation; |
---|
| 87 | evaluation.initialize(command); |
---|
[3035] | 88 | |
---|
[7236] | 89 | evaluation.hintCommand_ = ConsoleCommand::getCommand(__CC_CommandExecutor_name, __CC_autocomplete_name); |
---|
[1505] | 90 | |
---|
[7228] | 91 | if (evaluation.getNumberOfArguments() >= 1) |
---|
[1505] | 92 | { |
---|
[7236] | 93 | evaluation.execCommand_ = ConsoleCommand::getCommandLC(evaluation.getToken(0)); |
---|
[7228] | 94 | if (evaluation.execCommand_) |
---|
| 95 | evaluation.execArgumentsOffset_ = 1; |
---|
| 96 | else if (evaluation.getNumberOfArguments() >= 2) |
---|
[1505] | 97 | { |
---|
[7236] | 98 | evaluation.execCommand_ = ConsoleCommand::getCommandLC(evaluation.getToken(0), evaluation.getToken(1)); |
---|
[7228] | 99 | if (evaluation.execCommand_) |
---|
| 100 | evaluation.execArgumentsOffset_ = 2; |
---|
[1505] | 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
[7228] | 104 | if (evaluation.execCommand_ && evaluation.getNumberOfArguments() > evaluation.execArgumentsOffset_) |
---|
[1505] | 105 | { |
---|
[7228] | 106 | evaluation.hintCommand_ = evaluation.execCommand_; |
---|
| 107 | evaluation.hintArgumentsOffset_ = evaluation.execArgumentsOffset_; |
---|
[1505] | 108 | } |
---|
| 109 | |
---|
[7228] | 110 | return evaluation; |
---|
[1505] | 111 | } |
---|
[7229] | 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 | } |
---|
[7233] | 154 | |
---|
[7234] | 155 | /* static */ MultiType CommandExecutor::unhide(const std::string& command) |
---|
[7233] | 156 | { |
---|
[7234] | 157 | return CommandExecutor::queryMT(command); |
---|
[7233] | 158 | } |
---|
[1505] | 159 | } |
---|