[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" |
---|
| 35 | #include "TclBind.h" |
---|
| 36 | |
---|
| 37 | namespace orxonox |
---|
| 38 | { |
---|
| 39 | ConsoleCommandShortcutExtern(tcl, AccessLevel::None); |
---|
| 40 | |
---|
[1187] | 41 | void Tcl_puts(Tcl::object const &args) |
---|
[1151] | 42 | { |
---|
[1187] | 43 | COUT(0) << args.get() << std::endl; |
---|
[1151] | 44 | } |
---|
| 45 | |
---|
[1188] | 46 | std::string Tcl_execute(Tcl::object const &args) |
---|
[1151] | 47 | { |
---|
[1189] | 48 | std::cout << "Tcl_execute: args: " << args.get() << std::endl; |
---|
[1188] | 49 | std::string command = args.get(); |
---|
[1189] | 50 | |
---|
[1188] | 51 | if (command.size() >= 2 && command[0] == '{' && command[command.size() - 1] == '}') |
---|
| 52 | command = command.substr(1, command.size() - 2); |
---|
[1189] | 53 | |
---|
[1188] | 54 | CommandEvaluation evaluation = CommandExecutor::evaluate(command); |
---|
[1189] | 55 | |
---|
[1188] | 56 | if (!CommandExecutor::execute(evaluation)) |
---|
| 57 | COUT(1) << "Error: Can't execute command \"" << command << "\"!" << std::endl; |
---|
[1189] | 58 | |
---|
[1187] | 59 | if (evaluation.hasReturnvalue()) |
---|
| 60 | return evaluation.getReturnvalue().toString(); |
---|
[1189] | 61 | |
---|
[1187] | 62 | return ""; |
---|
[1151] | 63 | } |
---|
| 64 | |
---|
| 65 | std::string tcl(const std::string& tclcode) |
---|
| 66 | { |
---|
| 67 | try |
---|
| 68 | { |
---|
| 69 | static Tcl::interpreter i; |
---|
[1187] | 70 | i.def("puts", Tcl_puts, Tcl::variadic()); |
---|
[1188] | 71 | i.def("execute", Tcl_execute, Tcl::variadic()); |
---|
| 72 | i.eval("proc unknown {args} { return [execute $args] }"); |
---|
[1151] | 73 | std::string output = i.eval(tclcode); |
---|
[1188] | 74 | if (output != "") |
---|
| 75 | COUT(0) << "tcl> " << output << std::endl; |
---|
[1151] | 76 | return output; |
---|
| 77 | } |
---|
| 78 | catch (Tcl::tcl_error const &e) |
---|
| 79 | { |
---|
[1188] | 80 | COUT(1) << "tcl> Error: " << e.what() << std::endl; |
---|
[1151] | 81 | } |
---|
| 82 | catch (std::exception const &e) |
---|
| 83 | { |
---|
| 84 | COUT(1) << "Error while executing tcl: " << e.what() << std::endl; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | return ""; |
---|
| 88 | } |
---|
| 89 | } |
---|