[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 | |
---|
| 35 | #include "util/Debug.h" |
---|
[3280] | 36 | #include "util/StringUtils.h" |
---|
[3196] | 37 | #include "CommandExecutor.h" |
---|
[1502] | 38 | #include "ConsoleCommand.h" |
---|
| 39 | #include "TclThreadManager.h" |
---|
[3344] | 40 | #include "SpecialConfig.h" |
---|
[1502] | 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
[1747] | 44 | SetConsoleCommandShortcut(TclBind, tcl); |
---|
| 45 | SetConsoleCommandShortcut(TclBind, bgerror); |
---|
[1502] | 46 | |
---|
[1792] | 47 | TclBind* TclBind::singletonRef_s = 0; |
---|
| 48 | |
---|
| 49 | TclBind::TclBind(const std::string& datapath) |
---|
[1502] | 50 | { |
---|
[1792] | 51 | assert(singletonRef_s == 0); |
---|
| 52 | singletonRef_s = this; |
---|
[1502] | 53 | this->interpreter_ = 0; |
---|
[3344] | 54 | this->bSetTclDataPath_ = false; |
---|
[1792] | 55 | this->setDataPath(datapath); |
---|
[1502] | 56 | } |
---|
| 57 | |
---|
| 58 | TclBind::~TclBind() |
---|
| 59 | { |
---|
| 60 | if (this->interpreter_) |
---|
| 61 | delete this->interpreter_; |
---|
[1792] | 62 | singletonRef_s = 0; |
---|
[1502] | 63 | } |
---|
| 64 | |
---|
| 65 | void TclBind::setDataPath(const std::string& datapath) |
---|
| 66 | { |
---|
[2710] | 67 | // String has POSIX slashes |
---|
[3344] | 68 | this->tclDataPath_ = datapath + "tcl" + '/'; |
---|
| 69 | this->bSetTclDataPath_ = true; |
---|
[1502] | 70 | |
---|
[3344] | 71 | this->initializeTclInterpreter(); |
---|
[1502] | 72 | } |
---|
| 73 | |
---|
[3344] | 74 | void TclBind::initializeTclInterpreter() |
---|
[1502] | 75 | { |
---|
[3344] | 76 | if (this->bSetTclDataPath_ && !this->interpreter_) |
---|
[1502] | 77 | { |
---|
[3344] | 78 | this->interpreter_ = this->createTclInterpreter(); |
---|
| 79 | |
---|
[1502] | 80 | this->interpreter_->def("orxonox::query", TclBind::tcl_query, Tcl::variadic()); |
---|
| 81 | this->interpreter_->def("orxonox::crossquery", TclThreadManager::tcl_crossquery, Tcl::variadic()); |
---|
| 82 | this->interpreter_->def("execute", TclBind::tcl_execute, Tcl::variadic()); |
---|
[3318] | 83 | this->interpreter_->def("orxonox::crossexecute", TclThreadManager::tcl_crossexecute, Tcl::variadic()); |
---|
[1502] | 84 | |
---|
| 85 | try |
---|
| 86 | { |
---|
[3318] | 87 | this->interpreter_->eval("proc query args { orxonox::query [join $args] }"); |
---|
| 88 | this->interpreter_->eval("proc crossquery {id args} { orxonox::crossquery 0 $id [join $args] }"); |
---|
| 89 | this->interpreter_->eval("proc crossexecute {id args} { orxonox::crossquery 0 $id [join $args] }"); |
---|
[1502] | 90 | this->interpreter_->eval("set id 0"); |
---|
| 91 | this->interpreter_->eval("rename exit tcl::exit; proc exit {} { execute exit }"); |
---|
| 92 | } |
---|
| 93 | catch (Tcl::tcl_error const &e) |
---|
| 94 | { COUT(1) << "Tcl error while creating Tcl-interpreter: " << e.what() << std::endl; } |
---|
| 95 | catch (std::exception const &e) |
---|
| 96 | { COUT(1) << "Error while creating Tcl-interpreter: " << e.what() << std::endl; } |
---|
[3344] | 97 | catch (...) |
---|
| 98 | { COUT(1) << "Error while creating Tcl-interpreter." << std::endl; } |
---|
[1502] | 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
[3344] | 102 | Tcl::interpreter* TclBind::createTclInterpreter() |
---|
[1502] | 103 | { |
---|
[3344] | 104 | #ifdef DEPENDENCY_PACKAGE_ENABLE |
---|
| 105 | Tcl::interpreter* interpreter = new Tcl::interpreter(ORXONOX_TCL_LIBRARY_PATH); |
---|
| 106 | #else |
---|
| 107 | Tcl::interpreter* interpreter = new Tcl::interpreter(); |
---|
| 108 | #endif |
---|
| 109 | try |
---|
[1502] | 110 | { |
---|
[3344] | 111 | interpreter->eval("source " + TclBind::getInstance().tclDataPath_ + "/init.tcl"); |
---|
[1502] | 112 | } |
---|
[3344] | 113 | catch (Tcl::tcl_error const &e) |
---|
| 114 | { COUT(1) << "Tcl error while creating Tcl-interpreter: " << e.what() << std::endl; } |
---|
| 115 | catch (std::exception const &e) |
---|
| 116 | { COUT(1) << "Error while creating Tcl-interpreter: " << e.what() << std::endl; } |
---|
| 117 | catch (...) |
---|
| 118 | { COUT(1) << "Error while creating Tcl-interpreter." << std::endl; } |
---|
[1502] | 119 | |
---|
[3344] | 120 | return interpreter; |
---|
[1502] | 121 | } |
---|
| 122 | |
---|
| 123 | std::string TclBind::tcl_query(Tcl::object const &args) |
---|
| 124 | { |
---|
| 125 | COUT(4) << "Tcl_query: " << args.get() << std::endl; |
---|
| 126 | |
---|
| 127 | std::string command = stripEnclosingBraces(args.get()); |
---|
| 128 | |
---|
| 129 | if (!CommandExecutor::execute(command, false)) |
---|
| 130 | { |
---|
| 131 | COUT(1) << "Error: Can't execute command \"" << command << "\"!" << std::endl; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | if (CommandExecutor::getLastEvaluation().hasReturnvalue()) |
---|
[1747] | 135 | return CommandExecutor::getLastEvaluation().getReturnvalue().getString(); |
---|
[1502] | 136 | |
---|
| 137 | return ""; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | void TclBind::tcl_execute(Tcl::object const &args) |
---|
| 141 | { |
---|
| 142 | COUT(4) << "Tcl_execute: " << args.get() << std::endl; |
---|
| 143 | std::string command = stripEnclosingBraces(args.get()); |
---|
| 144 | |
---|
| 145 | if (!CommandExecutor::execute(command, false)) |
---|
| 146 | { |
---|
| 147 | COUT(1) << "Error: Can't execute command \"" << command << "\"!" << std::endl; |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | std::string TclBind::tcl(const std::string& tclcode) |
---|
| 152 | { |
---|
| 153 | if (TclBind::getInstance().interpreter_) |
---|
| 154 | { |
---|
| 155 | try |
---|
| 156 | { |
---|
| 157 | std::string output = TclBind::getInstance().interpreter_->eval(tclcode); |
---|
| 158 | if (output != "") |
---|
| 159 | { |
---|
| 160 | COUT(0) << "tcl> " << output << std::endl; |
---|
| 161 | } |
---|
| 162 | return output; |
---|
| 163 | } |
---|
| 164 | catch (Tcl::tcl_error const &e) |
---|
| 165 | { COUT(1) << "tcl> Error: " << e.what() << std::endl; } |
---|
| 166 | catch (std::exception const &e) |
---|
| 167 | { COUT(1) << "Error while executing Tcl: " << e.what() << std::endl; } |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | return ""; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | void TclBind::bgerror(std::string error) |
---|
| 174 | { |
---|
| 175 | COUT(1) << "Tcl background error: " << stripEnclosingBraces(error) << std::endl; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | bool TclBind::eval(const std::string& tclcode) |
---|
| 179 | { |
---|
| 180 | try |
---|
| 181 | { |
---|
| 182 | TclBind::getInstance().interpreter_->eval(tclcode); |
---|
| 183 | return true; |
---|
| 184 | } |
---|
| 185 | catch (Tcl::tcl_error const &e) |
---|
| 186 | { COUT(1) << "Tcl error: " << e.what() << std::endl; } |
---|
| 187 | catch (std::exception const &e) |
---|
| 188 | { COUT(1) << "Error while executing Tcl: " << e.what() << std::endl; } |
---|
| 189 | |
---|
| 190 | return false; |
---|
| 191 | } |
---|
| 192 | } |
---|