Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/core/TclBind.cc @ 1267

Last change on this file since 1267 was 1267, checked in by landauf, 16 years ago

telnet remote control works

type "source remote.tcl" into the ingame shell
connect with tcl to port 2560
login with "orxonox rocks"
type any command you like… it will be executed in orxonox ;)
of course server-admins will change the password (and the port)

File size: 5.8 KB
Line 
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 "TclThreadManager.h"
36#include "TclBind.h"
37
38namespace orxonox
39{
40    ConsoleCommandShortcutGeneric(tcl, createExecutor(createFunctor(&TclBind::tcl), "tcl", AccessLevel::None));
41    ConsoleCommandShortcutGeneric(bgerror, createExecutor(createFunctor(&TclBind::bgerror), "bgerror", AccessLevel::None));
42
43    TclBind::TclBind()
44    {
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_);
74            this->interpreter_->def("orxonox::query", TclBind::tcl_query, Tcl::variadic());
75            this->interpreter_->def("orxonox::crossquery", TclThreadManager::tcl_crossquery, Tcl::variadic());
76            this->interpreter_->def("execute", TclBind::tcl_execute, Tcl::variadic());
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;   }
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
104    std::string TclBind::tcl_query(Tcl::object const &args)
105    {
106std::cout << "Tcl_query: args: " << args.get() << std::endl;
107        std::string command = args.get();
108
109        while (command.size() >= 2 && command[0] == '{' && command[command.size() - 1] == '}')
110            command = command.substr(1, command.size() - 2);
111
112        if (!CommandExecutor::execute(command, false))
113            COUT(1) << "Error: Can't execute command \"" << command << "\"!" << std::endl;
114
115        if (CommandExecutor::getLastEvaluation().hasReturnvalue())
116            return CommandExecutor::getLastEvaluation().getReturnvalue().toString();
117
118        return "";
119    }
120
121    void TclBind::tcl_execute(Tcl::object const &args)
122    {
123std::cout << "Tcl_execute: args: " << args.get() << std::endl;
124        std::string command = args.get();
125
126        while (command.size() >= 2 && command[0] == '{' && command[command.size() - 1] == '}')
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;
131    }
132
133    std::string TclBind::tcl(const std::string& tclcode)
134    {
135        try
136        {
137            std::string output = TclBind::getInstance().interpreter_->eval(tclcode);
138            if (output != "")
139                COUT(0) << "tcl> " << output << std::endl;
140            return output;
141        }
142        catch (Tcl::tcl_error const &e)
143        {   COUT(1) << "tcl> Error: " << e.what() << std::endl;   }
144        catch (std::exception const &e)
145        {   COUT(1) << "Error while executing Tcl: " << e.what() << std::endl;   }
146
147        return "";
148    }
149
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
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)
166        {   COUT(1) << "Tcl error: " << e.what() << std::endl;   }
167        catch (std::exception const &e)
168        {   COUT(1) << "Error while executing Tcl: " << e.what() << std::endl;   }
169
170        return false;
171    }
172}
Note: See TracBrowser for help on using the repository browser.