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