Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added a tcl-thread, but there are still some bugs and problems

File size: 4.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 "TclBind.h"
36
37namespace orxonox
38{
39    ConsoleCommandShortcutGeneric(tcl, createExecutor(createFunctor(&TclBind::tcl), "tcl", AccessLevel::None));
40
41    TclBind::TclBind()
42    {
43        this->interpreter_ = 0;
44        this->bSetTclLibPath_ = false;
45    }
46
47    TclBind::~TclBind()
48    {
49        if (this->interpreter_)
50            delete this->interpreter_;
51    }
52
53    TclBind& TclBind::getInstance()
54    {
55        static TclBind instance;
56        return instance;
57    }
58
59    void TclBind::setDataPath(const std::string& datapath)
60    {
61        this->tclLibPath_ = datapath + "/tcl";
62        this->bSetTclLibPath_ = true;
63
64        this->createTclInterpreter();
65    }
66
67    void TclBind::createTclInterpreter()
68    {
69        if (this->bSetTclLibPath_ && !this->interpreter_)
70        {
71            this->interpreter_ = new Tcl::interpreter(this->tclLibPath_);
72            this->interpreter_->def("orxonox", TclBind::tcl_orxonox, Tcl::variadic());
73            this->interpreter_->def("execute", TclBind::tcl_execute, Tcl::variadic());
74            this->interpreter_->eval("rename exit tclexit; proc exit {} { orxonox exit }");
75            this->interpreter_->eval("redef_puts");
76        }
77    }
78
79    void TclBind::createNewTclInterpreter()
80    {
81        if (this->interpreter_)
82        {
83            delete this->interpreter_;
84            this->interpreter_ = 0;
85        }
86
87        this->createTclInterpreter();
88    }
89
90    void TclBind::tcl_puts(Tcl::object const &args)
91    {
92        COUT(0) << args.get() << std::endl;
93    }
94
95    std::string TclBind::tcl_orxonox(Tcl::object const &args)
96    {
97std::cout << "Tcl_orxonox: args: " << args.get() << std::endl;
98        std::string command = args.get();
99
100        if (command.size() >= 2 && command[0] == '{' && command[command.size() - 1] == '}')
101            command = command.substr(1, command.size() - 2);
102
103        if (!CommandExecutor::execute(command, false))
104            COUT(1) << "Error: Can't execute command \"" << command << "\"!" << std::endl;
105
106        if (CommandExecutor::getLastEvaluation().hasReturnvalue())
107            return CommandExecutor::getLastEvaluation().getReturnvalue().toString();
108
109        return "";
110    }
111
112    void TclBind::tcl_execute(Tcl::object const &args)
113    {
114std::cout << "Tcl_execute: args: " << args.get() << std::endl;
115        std::string command = args.get();
116
117        if (command.size() >= 2 && command[0] == '{' && command[command.size() - 1] == '}')
118            command = command.substr(1, command.size() - 2);
119
120        if (!CommandExecutor::execute(command, false))
121            COUT(1) << "Error: Can't execute command \"" << command << "\"!" << std::endl;
122    }
123
124    std::string TclBind::tcl(const std::string& tclcode)
125    {
126        try
127        {
128            std::string output = TclBind::getInstance().interpreter_->eval(tclcode);
129            if (output != "")
130                COUT(0) << "tcl> " << output << std::endl;
131            return output;
132        }
133        catch (Tcl::tcl_error const &e)
134        {
135            COUT(1) << "tcl> Error: " << e.what() << std::endl;
136        }
137        catch (std::exception const &e)
138        {
139            COUT(1) << "Error while executing tcl: " << e.what() << std::endl;
140        }
141
142        return "";
143    }
144
145    bool TclBind::eval(const std::string& tclcode)
146    {
147        try
148        {
149            TclBind::getInstance().interpreter_->eval(tclcode);
150            return true;
151        }
152        catch (Tcl::tcl_error const &e)
153        {
154            COUT(1) << "Tcl error: " << e.what() << std::endl;
155        }
156        catch (std::exception const &e)
157        {
158            COUT(1) << "Error while executing tcl: " << e.what() << std::endl;
159        }
160
161        return false;
162    }
163}
Note: See TracBrowser for help on using the repository browser.