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