| 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/Exception.h" | 
|---|
| 38 | #include "util/StringUtils.h" | 
|---|
| 39 | #include "CommandExecutor.h" | 
|---|
| 40 | #include "ConsoleCommand.h" | 
|---|
| 41 | #include "PathConfig.h" | 
|---|
| 42 | #include "TclThreadManager.h" | 
|---|
| 43 |  | 
|---|
| 44 | namespace orxonox | 
|---|
| 45 | { | 
|---|
| 46 | SetConsoleCommandShortcut(TclBind, tcl); | 
|---|
| 47 | SetConsoleCommandShortcut(TclBind, bgerror); | 
|---|
| 48 |  | 
|---|
| 49 | TclBind* TclBind::singletonPtr_s = 0; | 
|---|
| 50 |  | 
|---|
| 51 | TclBind::TclBind(const std::string& datapath) | 
|---|
| 52 | { | 
|---|
| 53 | this->interpreter_ = 0; | 
|---|
| 54 | this->bSetTclDataPath_ = false; | 
|---|
| 55 | this->setDataPath(datapath); | 
|---|
| 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 | { | 
|---|
| 66 | // String has POSIX slashes | 
|---|
| 67 | this->tclDataPath_ = datapath + "tcl" + '/'; | 
|---|
| 68 | this->bSetTclDataPath_ = true; | 
|---|
| 69 |  | 
|---|
| 70 | this->initializeTclInterpreter(); | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | void TclBind::initializeTclInterpreter() | 
|---|
| 74 | { | 
|---|
| 75 | if (this->bSetTclDataPath_ && !this->interpreter_) | 
|---|
| 76 | { | 
|---|
| 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()); | 
|---|
| 81 | this->interpreter_->def("execute", TclBind::tcl_execute, Tcl::variadic()); | 
|---|
| 82 | this->interpreter_->def("::orxonox::crossexecute", TclThreadManager::tcl_crossexecute, Tcl::variadic()); | 
|---|
| 83 |  | 
|---|
| 84 | try | 
|---|
| 85 | { | 
|---|
| 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 }"); | 
|---|
| 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 | } | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | Tcl::interpreter* TclBind::createTclInterpreter() | 
|---|
| 99 | { | 
|---|
| 100 | Tcl::interpreter* interpreter = new Tcl::interpreter(); | 
|---|
| 101 | const std::string& libpath = TclBind::getTclLibraryPath(); | 
|---|
| 102 |  | 
|---|
| 103 | try | 
|---|
| 104 | { | 
|---|
| 105 | if (!libpath.empty()) | 
|---|
| 106 | interpreter->eval("set tcl_library \"" + libpath + '"'); | 
|---|
| 107 |  | 
|---|
| 108 | Tcl_Init(interpreter->get()); | 
|---|
| 109 |  | 
|---|
| 110 | interpreter->eval("source \"" + TclBind::getInstance().tclDataPath_ + "/init.tcl\""); | 
|---|
| 111 | } | 
|---|
| 112 | catch (Tcl::tcl_error const &e) | 
|---|
| 113 | {   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;   } | 
|---|
| 114 |  | 
|---|
| 115 | return interpreter; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | std::string TclBind::getTclLibraryPath() | 
|---|
| 119 | { | 
|---|
| 120 | #ifdef DEPENDENCY_PACKAGE_ENABLE | 
|---|
| 121 | if (PathConfig::isDevelopmentRun()) | 
|---|
| 122 | return (std::string(specialConfig::dependencyLibraryDirectory) + "/tcl"); | 
|---|
| 123 | else | 
|---|
| 124 | return (PathConfig::getRootPathString() + "lib/tcl"); | 
|---|
| 125 | #else | 
|---|
| 126 | return ""; | 
|---|
| 127 | #endif | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | std::string TclBind::tcl_query(Tcl::object const &args) | 
|---|
| 131 | { | 
|---|
| 132 | COUT(4) << "Tcl_query: " << args.get() << std::endl; | 
|---|
| 133 |  | 
|---|
| 134 | const std::string& command = stripEnclosingBraces(args.get()); | 
|---|
| 135 |  | 
|---|
| 136 | if (!CommandExecutor::execute(command, false)) | 
|---|
| 137 | { | 
|---|
| 138 | COUT(1) << "Error: Can't execute command \"" << command << "\"!" << std::endl; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | if (CommandExecutor::getLastEvaluation().hasReturnvalue()) | 
|---|
| 142 | return CommandExecutor::getLastEvaluation().getReturnvalue().getString(); | 
|---|
| 143 |  | 
|---|
| 144 | return ""; | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | void TclBind::tcl_execute(Tcl::object const &args) | 
|---|
| 148 | { | 
|---|
| 149 | COUT(4) << "Tcl_execute: " << args.get() << std::endl; | 
|---|
| 150 | const std::string& command = stripEnclosingBraces(args.get()); | 
|---|
| 151 |  | 
|---|
| 152 | if (!CommandExecutor::execute(command, false)) | 
|---|
| 153 | { | 
|---|
| 154 | COUT(1) << "Error: Can't execute command \"" << command << "\"!" << std::endl; | 
|---|
| 155 | } | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | std::string TclBind::tcl(const std::string& tclcode) | 
|---|
| 159 | { | 
|---|
| 160 | if (TclBind::getInstance().interpreter_) | 
|---|
| 161 | { | 
|---|
| 162 | try | 
|---|
| 163 | { | 
|---|
| 164 | const std::string& output = TclBind::getInstance().interpreter_->eval("uplevel #0 " + tclcode); | 
|---|
| 165 | if (!output.empty()) | 
|---|
| 166 | { | 
|---|
| 167 | COUT(0) << "tcl> " << output << std::endl; | 
|---|
| 168 | } | 
|---|
| 169 | return output; | 
|---|
| 170 | } | 
|---|
| 171 | catch (Tcl::tcl_error const &e) | 
|---|
| 172 | {   COUT(1) << "tcl> Error: " << e.what() << std::endl;   } | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 | return ""; | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | void TclBind::bgerror(const std::string& error) | 
|---|
| 179 | { | 
|---|
| 180 | COUT(1) << "Tcl background error: " << stripEnclosingBraces(error) << std::endl; | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | bool TclBind::eval(const std::string& tclcode) | 
|---|
| 184 | { | 
|---|
| 185 | try | 
|---|
| 186 | { | 
|---|
| 187 | TclBind::getInstance().interpreter_->eval(tclcode); | 
|---|
| 188 | return true; | 
|---|
| 189 | } | 
|---|
| 190 | catch (Tcl::tcl_error const &e) | 
|---|
| 191 | {   COUT(1) << "Tcl error: " << e.what() << std::endl;   } | 
|---|
| 192 |  | 
|---|
| 193 | return false; | 
|---|
| 194 | } | 
|---|
| 195 | } | 
|---|