[1502] | 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 | |
---|
[1792] | 29 | #include "TclBind.h" |
---|
| 30 | |
---|
[3196] | 31 | #include <exception> |
---|
[1502] | 32 | #include <string> |
---|
[3196] | 33 | #include <cpptcl/cpptcl.h> |
---|
| 34 | |
---|
[3370] | 35 | #include "SpecialConfig.h" |
---|
[3196] | 36 | #include "util/Debug.h" |
---|
[5747] | 37 | #include "util/Exception.h" |
---|
[3280] | 38 | #include "util/StringUtils.h" |
---|
[7203] | 39 | #include "core/PathConfig.h" |
---|
[3196] | 40 | #include "CommandExecutor.h" |
---|
[1502] | 41 | #include "ConsoleCommand.h" |
---|
| 42 | #include "TclThreadManager.h" |
---|
| 43 | |
---|
| 44 | namespace orxonox |
---|
| 45 | { |
---|
[7236] | 46 | SetConsoleCommand("tcl", &TclBind::tcl); |
---|
| 47 | SetConsoleCommand("bgerror", &TclBind::bgerror); |
---|
[1502] | 48 | |
---|
[3370] | 49 | TclBind* TclBind::singletonPtr_s = 0; |
---|
[1792] | 50 | |
---|
[7401] | 51 | /** |
---|
| 52 | @brief Constructor: Initializes the Tcl-interpreter with a given data path. |
---|
| 53 | @param datapath Path to the directory that contains the Orxonox-specific Tcl-files |
---|
| 54 | */ |
---|
[1792] | 55 | TclBind::TclBind(const std::string& datapath) |
---|
[1502] | 56 | { |
---|
| 57 | this->interpreter_ = 0; |
---|
[3370] | 58 | this->bSetTclDataPath_ = false; |
---|
[1792] | 59 | this->setDataPath(datapath); |
---|
[1502] | 60 | } |
---|
| 61 | |
---|
[7401] | 62 | /** |
---|
| 63 | @brief Destructor: Deletes the Tcl-interpreter. |
---|
| 64 | */ |
---|
[1502] | 65 | TclBind::~TclBind() |
---|
| 66 | { |
---|
| 67 | if (this->interpreter_) |
---|
| 68 | delete this->interpreter_; |
---|
| 69 | } |
---|
| 70 | |
---|
[7401] | 71 | /** |
---|
| 72 | @brief Defines the path to the directory that contains the Orxonox-specific Tcl-files and initializes the Tcl-interpreter accordingly. |
---|
| 73 | */ |
---|
[1502] | 74 | void TclBind::setDataPath(const std::string& datapath) |
---|
| 75 | { |
---|
[2710] | 76 | // String has POSIX slashes |
---|
[3370] | 77 | this->tclDataPath_ = datapath + "tcl" + '/'; |
---|
| 78 | this->bSetTclDataPath_ = true; |
---|
[1502] | 79 | |
---|
[3370] | 80 | this->initializeTclInterpreter(); |
---|
[1502] | 81 | } |
---|
| 82 | |
---|
[7401] | 83 | /** |
---|
| 84 | @brief Creates and initializes the Tcl-interpreter by registering all callbacks and defining some useful functions. |
---|
| 85 | */ |
---|
[3370] | 86 | void TclBind::initializeTclInterpreter() |
---|
[1502] | 87 | { |
---|
[3370] | 88 | if (this->bSetTclDataPath_ && !this->interpreter_) |
---|
[1502] | 89 | { |
---|
[3370] | 90 | this->interpreter_ = this->createTclInterpreter(); |
---|
| 91 | |
---|
| 92 | this->interpreter_->def("::orxonox::query", TclBind::tcl_query, Tcl::variadic()); |
---|
| 93 | this->interpreter_->def("::orxonox::crossquery", TclThreadManager::tcl_crossquery, Tcl::variadic()); |
---|
[1502] | 94 | this->interpreter_->def("execute", TclBind::tcl_execute, Tcl::variadic()); |
---|
[3370] | 95 | this->interpreter_->def("::orxonox::crossexecute", TclThreadManager::tcl_crossexecute, Tcl::variadic()); |
---|
[1502] | 96 | |
---|
| 97 | try |
---|
| 98 | { |
---|
[3370] | 99 | this->interpreter_->eval("proc query {args} { ::orxonox::query $args }"); |
---|
| 100 | this->interpreter_->eval("proc crossquery {id args} { ::orxonox::crossquery 0 $id $args }"); |
---|
| 101 | this->interpreter_->eval("proc crossexecute {id args} { ::orxonox::crossquery 0 $id $args }"); |
---|
| 102 | this->interpreter_->eval("proc running {} { return 1 }"); |
---|
[1502] | 103 | this->interpreter_->eval("set id 0"); |
---|
[3370] | 104 | this->interpreter_->eval("rename exit ::tcl::exit; proc exit {} { execute exit }"); |
---|
[1502] | 105 | } |
---|
| 106 | catch (Tcl::tcl_error const &e) |
---|
| 107 | { COUT(1) << "Tcl error while creating Tcl-interpreter: " << e.what() << std::endl; } |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
[7401] | 111 | /** |
---|
| 112 | @brief Creates and initializes a new Tcl-interpreter and calls the Orxonox-specific |
---|
| 113 | init.tcl script that defines some special functions which are required by Orxonox. |
---|
| 114 | */ |
---|
[3370] | 115 | Tcl::interpreter* TclBind::createTclInterpreter() |
---|
[1502] | 116 | { |
---|
[3370] | 117 | Tcl::interpreter* interpreter = new Tcl::interpreter(); |
---|
[6417] | 118 | const std::string& libpath = TclBind::getTclLibraryPath(); |
---|
[3370] | 119 | |
---|
| 120 | try |
---|
[1502] | 121 | { |
---|
[6417] | 122 | if (!libpath.empty()) |
---|
| 123 | interpreter->eval("set tcl_library \"" + libpath + '"'); |
---|
[3370] | 124 | |
---|
| 125 | Tcl_Init(interpreter->get()); |
---|
| 126 | |
---|
| 127 | interpreter->eval("source \"" + TclBind::getInstance().tclDataPath_ + "/init.tcl\""); |
---|
[1502] | 128 | } |
---|
[3370] | 129 | catch (Tcl::tcl_error const &e) |
---|
| 130 | { COUT(1) << "Tcl error while creating Tcl-interpreter: " << e.what() << std::endl; COUT(1) << "Error: Tcl isn't properly initialized. Orxonox might possibly not work like that." << std::endl; } |
---|
[1502] | 131 | |
---|
[3370] | 132 | return interpreter; |
---|
[1502] | 133 | } |
---|
| 134 | |
---|
[7401] | 135 | /** |
---|
| 136 | @brief Returns the path to the Tcl-library (not the Orxonox-specific Tcl-files). |
---|
| 137 | */ |
---|
[3370] | 138 | std::string TclBind::getTclLibraryPath() |
---|
| 139 | { |
---|
| 140 | #ifdef DEPENDENCY_PACKAGE_ENABLE |
---|
[5929] | 141 | if (PathConfig::isDevelopmentRun()) |
---|
[5695] | 142 | return (std::string(specialConfig::dependencyLibraryDirectory) + "/tcl"); |
---|
[3370] | 143 | else |
---|
[5929] | 144 | return (PathConfig::getRootPathString() + "lib/tcl"); |
---|
[3370] | 145 | #else |
---|
| 146 | return ""; |
---|
| 147 | #endif |
---|
| 148 | } |
---|
| 149 | |
---|
[7401] | 150 | /** |
---|
| 151 | @brief Callback: Used to send an Orxonox-command from Tcl to the CommandExecutor and to send its result back to Tcl. |
---|
| 152 | */ |
---|
[1502] | 153 | std::string TclBind::tcl_query(Tcl::object const &args) |
---|
| 154 | { |
---|
| 155 | COUT(4) << "Tcl_query: " << args.get() << std::endl; |
---|
| 156 | |
---|
[6417] | 157 | const std::string& command = stripEnclosingBraces(args.get()); |
---|
[1502] | 158 | |
---|
[7228] | 159 | int error; |
---|
[7238] | 160 | CommandEvaluation evaluation = CommandExecutor::evaluate(command); |
---|
| 161 | const std::string& result = evaluation.query(&error); |
---|
[7228] | 162 | switch (error) |
---|
[1502] | 163 | { |
---|
[7228] | 164 | case CommandExecutor::Error: COUT(1) << "Error: Can't execute command \"" << command << "\", command doesn't exist. (B)" << std::endl; break; |
---|
| 165 | case CommandExecutor::Incomplete: COUT(1) << "Error: Can't execute command \"" << command << "\", not enough arguments given. (B)" << std::endl; break; |
---|
| 166 | case CommandExecutor::Deactivated: COUT(1) << "Error: Can't execute command \"" << command << "\", command is not active. (B)" << std::endl; break; |
---|
| 167 | case CommandExecutor::Denied: COUT(1) << "Error: Can't execute command \"" << command << "\", access denied. (B)" << std::endl; break; |
---|
[1502] | 168 | } |
---|
| 169 | |
---|
[7238] | 170 | if (error == CommandExecutor::Error) |
---|
| 171 | COUT(3) << "Did you mean \"" << evaluation.getCommandSuggestion() << "\"?" << std::endl; |
---|
| 172 | |
---|
[7189] | 173 | return result; |
---|
[1502] | 174 | } |
---|
| 175 | |
---|
[7401] | 176 | /** |
---|
| 177 | @brief Callback: Used to send an Orxonox-command from Tcl to the CommandExecutor. |
---|
| 178 | */ |
---|
[1502] | 179 | void TclBind::tcl_execute(Tcl::object const &args) |
---|
| 180 | { |
---|
| 181 | COUT(4) << "Tcl_execute: " << args.get() << std::endl; |
---|
[6417] | 182 | const std::string& command = stripEnclosingBraces(args.get()); |
---|
[1502] | 183 | |
---|
[7228] | 184 | if (CommandExecutor::execute(command, false)) |
---|
[1502] | 185 | { |
---|
| 186 | COUT(1) << "Error: Can't execute command \"" << command << "\"!" << std::endl; |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | |
---|
[7401] | 190 | /** |
---|
| 191 | @brief Console command, executes Tcl code. Can be used to bind Tcl-commands to a key, because native |
---|
| 192 | Tcl-commands can not be evaluated and are thus not supported by the key-binder. |
---|
| 193 | */ |
---|
[1502] | 194 | std::string TclBind::tcl(const std::string& tclcode) |
---|
| 195 | { |
---|
| 196 | if (TclBind::getInstance().interpreter_) |
---|
| 197 | { |
---|
| 198 | try |
---|
| 199 | { |
---|
[6417] | 200 | const std::string& output = TclBind::getInstance().interpreter_->eval("uplevel #0 " + tclcode); |
---|
| 201 | if (!output.empty()) |
---|
[1502] | 202 | { |
---|
| 203 | COUT(0) << "tcl> " << output << std::endl; |
---|
| 204 | } |
---|
| 205 | return output; |
---|
| 206 | } |
---|
| 207 | catch (Tcl::tcl_error const &e) |
---|
| 208 | { COUT(1) << "tcl> Error: " << e.what() << std::endl; } |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | return ""; |
---|
| 212 | } |
---|
| 213 | |
---|
[7401] | 214 | /** |
---|
| 215 | @brief Console command and implementation of the Tcl-feature "bgerror" which is called if an error |
---|
| 216 | occurred in the background of a Tcl-script. |
---|
| 217 | */ |
---|
[6417] | 218 | void TclBind::bgerror(const std::string& error) |
---|
[1502] | 219 | { |
---|
| 220 | COUT(1) << "Tcl background error: " << stripEnclosingBraces(error) << std::endl; |
---|
| 221 | } |
---|
| 222 | |
---|
[7401] | 223 | /** |
---|
| 224 | @brief Executes Tcl-code and returns the return-value. |
---|
| 225 | @param tclcode A string that contains Tcl-code |
---|
| 226 | @param error A pointer to an integer (or NULL) that is used to write an error-code (see @ref CommandExecutorErrorCodes "CommandExecutor error codes") |
---|
| 227 | @return Returns the return-value of the executed code (or an empty string if there's no return-value) |
---|
| 228 | */ |
---|
[7228] | 229 | std::string TclBind::eval(const std::string& tclcode, int* error) |
---|
[1502] | 230 | { |
---|
[7228] | 231 | if (error) |
---|
| 232 | *error = CommandExecutor::Success; |
---|
[7189] | 233 | |
---|
[1502] | 234 | try |
---|
| 235 | { |
---|
[7401] | 236 | // execute the code |
---|
[7189] | 237 | return TclBind::getInstance().interpreter_->eval(tclcode); |
---|
[1502] | 238 | } |
---|
| 239 | catch (Tcl::tcl_error const &e) |
---|
| 240 | { COUT(1) << "Tcl error: " << e.what() << std::endl; } |
---|
| 241 | |
---|
[7228] | 242 | if (error) |
---|
| 243 | *error = CommandExecutor::Error; |
---|
[7189] | 244 | return ""; |
---|
[1502] | 245 | } |
---|
| 246 | } |
---|