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