[1188] | 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 | |
---|
[1151] | 29 | #include <iostream> |
---|
| 30 | #include <string> |
---|
| 31 | |
---|
| 32 | #include "ConsoleCommand.h" |
---|
[1187] | 33 | #include "CommandExecutor.h" |
---|
[1151] | 34 | #include "Debug.h" |
---|
[1255] | 35 | #include "TclThreadManager.h" |
---|
[1151] | 36 | #include "TclBind.h" |
---|
| 37 | |
---|
| 38 | namespace orxonox |
---|
| 39 | { |
---|
[1194] | 40 | ConsoleCommandShortcutGeneric(tcl, createExecutor(createFunctor(&TclBind::tcl), "tcl", AccessLevel::None)); |
---|
[1267] | 41 | ConsoleCommandShortcutGeneric(bgerror, createExecutor(createFunctor(&TclBind::bgerror), "bgerror", AccessLevel::None)); |
---|
[1151] | 42 | |
---|
[1194] | 43 | TclBind::TclBind() |
---|
[1151] | 44 | { |
---|
[1194] | 45 | this->interpreter_ = 0; |
---|
| 46 | this->bSetTclLibPath_ = false; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | TclBind::~TclBind() |
---|
| 50 | { |
---|
| 51 | if (this->interpreter_) |
---|
| 52 | delete this->interpreter_; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | TclBind& TclBind::getInstance() |
---|
| 56 | { |
---|
| 57 | static TclBind instance; |
---|
| 58 | return instance; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | void TclBind::setDataPath(const std::string& datapath) |
---|
| 62 | { |
---|
| 63 | this->tclLibPath_ = datapath + "/tcl"; |
---|
| 64 | this->bSetTclLibPath_ = true; |
---|
| 65 | |
---|
| 66 | this->createTclInterpreter(); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | void TclBind::createTclInterpreter() |
---|
| 70 | { |
---|
| 71 | if (this->bSetTclLibPath_ && !this->interpreter_) |
---|
| 72 | { |
---|
| 73 | this->interpreter_ = new Tcl::interpreter(this->tclLibPath_); |
---|
[1255] | 74 | this->interpreter_->def("orxonox::query", TclBind::tcl_query, Tcl::variadic()); |
---|
| 75 | this->interpreter_->def("orxonox::crossquery", TclThreadManager::tcl_crossquery, Tcl::variadic()); |
---|
[1230] | 76 | this->interpreter_->def("execute", TclBind::tcl_execute, Tcl::variadic()); |
---|
[1255] | 77 | |
---|
| 78 | try |
---|
| 79 | { |
---|
| 80 | this->interpreter_->eval("proc query args { orxonox::query $args }"); |
---|
| 81 | this->interpreter_->eval("proc crossquery {id args} { orxonox::crossquery 0 $id $args }"); |
---|
| 82 | this->interpreter_->eval("set id 0"); |
---|
| 83 | this->interpreter_->eval("rename exit tcl::exit; proc exit {} { execute exit }"); |
---|
| 84 | this->interpreter_->eval("redef_puts"); |
---|
| 85 | } |
---|
| 86 | catch (Tcl::tcl_error const &e) |
---|
| 87 | { COUT(1) << "Tcl error while creating Tcl-interpreter: " << e.what() << std::endl; } |
---|
| 88 | catch (std::exception const &e) |
---|
| 89 | { COUT(1) << "Error while creating Tcl-interpreter: " << e.what() << std::endl; } |
---|
[1194] | 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | void TclBind::createNewTclInterpreter() |
---|
| 94 | { |
---|
| 95 | if (this->interpreter_) |
---|
| 96 | { |
---|
| 97 | delete this->interpreter_; |
---|
| 98 | this->interpreter_ = 0; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | this->createTclInterpreter(); |
---|
| 102 | } |
---|
| 103 | |
---|
[1255] | 104 | std::string TclBind::tcl_query(Tcl::object const &args) |
---|
[1194] | 105 | { |
---|
[1255] | 106 | std::cout << "Tcl_query: args: " << args.get() << std::endl; |
---|
[1188] | 107 | std::string command = args.get(); |
---|
[1189] | 108 | |
---|
[1255] | 109 | while (command.size() >= 2 && command[0] == '{' && command[command.size() - 1] == '}') |
---|
[1188] | 110 | command = command.substr(1, command.size() - 2); |
---|
[1189] | 111 | |
---|
[1198] | 112 | if (!CommandExecutor::execute(command, false)) |
---|
[1188] | 113 | COUT(1) << "Error: Can't execute command \"" << command << "\"!" << std::endl; |
---|
[1189] | 114 | |
---|
[1194] | 115 | if (CommandExecutor::getLastEvaluation().hasReturnvalue()) |
---|
| 116 | return CommandExecutor::getLastEvaluation().getReturnvalue().toString(); |
---|
[1189] | 117 | |
---|
[1187] | 118 | return ""; |
---|
[1151] | 119 | } |
---|
| 120 | |
---|
[1230] | 121 | void TclBind::tcl_execute(Tcl::object const &args) |
---|
[1198] | 122 | { |
---|
[1230] | 123 | std::cout << "Tcl_execute: args: " << args.get() << std::endl; |
---|
| 124 | std::string command = args.get(); |
---|
| 125 | |
---|
[1255] | 126 | while (command.size() >= 2 && command[0] == '{' && command[command.size() - 1] == '}') |
---|
[1230] | 127 | command = command.substr(1, command.size() - 2); |
---|
| 128 | |
---|
| 129 | if (!CommandExecutor::execute(command, false)) |
---|
| 130 | COUT(1) << "Error: Can't execute command \"" << command << "\"!" << std::endl; |
---|
[1198] | 131 | } |
---|
| 132 | |
---|
[1194] | 133 | std::string TclBind::tcl(const std::string& tclcode) |
---|
[1151] | 134 | { |
---|
| 135 | try |
---|
| 136 | { |
---|
[1194] | 137 | std::string output = TclBind::getInstance().interpreter_->eval(tclcode); |
---|
[1188] | 138 | if (output != "") |
---|
| 139 | COUT(0) << "tcl> " << output << std::endl; |
---|
[1151] | 140 | return output; |
---|
| 141 | } |
---|
| 142 | catch (Tcl::tcl_error const &e) |
---|
[1255] | 143 | { COUT(1) << "tcl> Error: " << e.what() << std::endl; } |
---|
[1151] | 144 | catch (std::exception const &e) |
---|
[1255] | 145 | { COUT(1) << "Error while executing Tcl: " << e.what() << std::endl; } |
---|
[1151] | 146 | |
---|
| 147 | return ""; |
---|
| 148 | } |
---|
[1198] | 149 | |
---|
[1267] | 150 | void TclBind::bgerror(std::string error) |
---|
| 151 | { |
---|
| 152 | while (error.size() >= 2 && error[0] == '{' && error[error.size() - 1] == '}') |
---|
| 153 | error = error.substr(1, error.size() - 2); |
---|
| 154 | |
---|
| 155 | COUT(1) << "Tcl background error: " << error << std::endl; |
---|
| 156 | } |
---|
| 157 | |
---|
[1198] | 158 | bool TclBind::eval(const std::string& tclcode) |
---|
| 159 | { |
---|
| 160 | try |
---|
| 161 | { |
---|
| 162 | TclBind::getInstance().interpreter_->eval(tclcode); |
---|
| 163 | return true; |
---|
| 164 | } |
---|
| 165 | catch (Tcl::tcl_error const &e) |
---|
[1255] | 166 | { COUT(1) << "Tcl error: " << e.what() << std::endl; } |
---|
[1198] | 167 | catch (std::exception const &e) |
---|
[1255] | 168 | { COUT(1) << "Error while executing Tcl: " << e.what() << std::endl; } |
---|
[1198] | 169 | |
---|
| 170 | return false; |
---|
| 171 | } |
---|
[1151] | 172 | } |
---|