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