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