Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core4/src/core/TclBind.cc @ 4036

Last change on this file since 4036 was 3250, checked in by rgrieder, 16 years ago

Executing decision on #300: Renaming String.h to StringUtils.h (String.h doesn't exactly suggest you would find utilities for string manipulations there).

  • Property svn:eol-style set to native
File size: 5.5 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 "TclBind.h"
30
31#include <exception>
32#include <string>
33#include <cpptcl/cpptcl.h>
34
35#include "util/Debug.h"
36#include "util/StringUtils.h"
37#include "CommandExecutor.h"
38#include "ConsoleCommand.h"
39#include "TclThreadManager.h"
40
41namespace orxonox
42{
43    SetConsoleCommandShortcut(TclBind, tcl);
44    SetConsoleCommandShortcut(TclBind, bgerror);
45
46    TclBind* TclBind::singletonRef_s = 0;
47
48    TclBind::TclBind(const std::string& datapath)
49    {
50        assert(singletonRef_s == 0);
51        singletonRef_s = this;
52        this->interpreter_ = 0;
53        this->bSetTclLibPath_ = false;
54        this->setDataPath(datapath);
55    }
56
57    TclBind::~TclBind()
58    {
59        if (this->interpreter_)
60            delete this->interpreter_;
61        singletonRef_s = 0;
62    }
63
64    void TclBind::setDataPath(const std::string& datapath)
65    {
66        // String has POSIX slashes
67        this->tclLibPath_ = datapath + "tcl" + TCL_VERSION + '/';
68        this->bSetTclLibPath_ = true;
69
70        this->createTclInterpreter();
71    }
72
73    void TclBind::createTclInterpreter()
74    {
75        if (this->bSetTclLibPath_ && !this->interpreter_)
76        {
77            this->interpreter_ = new Tcl::interpreter(this->tclLibPath_);
78            this->interpreter_->def("orxonox::query", TclBind::tcl_query, Tcl::variadic());
79            this->interpreter_->def("orxonox::crossquery", TclThreadManager::tcl_crossquery, Tcl::variadic());
80            this->interpreter_->def("execute", TclBind::tcl_execute, Tcl::variadic());
81
82            try
83            {
84                this->interpreter_->eval("proc query args { orxonox::query $args }");
85                this->interpreter_->eval("proc crossquery {id args} { orxonox::crossquery 0 $id $args }");
86                this->interpreter_->eval("set id 0");
87                this->interpreter_->eval("rename exit tcl::exit; proc exit {} { execute exit }");
88                this->interpreter_->eval("redef_puts");
89            }
90            catch (Tcl::tcl_error const &e)
91            {   COUT(1) << "Tcl error while creating Tcl-interpreter: " << e.what() << std::endl;   }
92            catch (std::exception const &e)
93            {   COUT(1) << "Error while creating Tcl-interpreter: " << e.what() << std::endl;   }
94        }
95    }
96
97    void TclBind::createNewTclInterpreter()
98    {
99        if (this->interpreter_)
100        {
101            delete this->interpreter_;
102            this->interpreter_ = 0;
103        }
104
105        this->createTclInterpreter();
106    }
107
108    std::string TclBind::tcl_query(Tcl::object const &args)
109    {
110        COUT(4) << "Tcl_query: " << args.get() << std::endl;
111
112        std::string command = stripEnclosingBraces(args.get());
113
114        if (!CommandExecutor::execute(command, false))
115        {
116            COUT(1) << "Error: Can't execute command \"" << command << "\"!" << std::endl;
117        }
118
119        if (CommandExecutor::getLastEvaluation().hasReturnvalue())
120            return CommandExecutor::getLastEvaluation().getReturnvalue().getString();
121
122        return "";
123    }
124
125    void TclBind::tcl_execute(Tcl::object const &args)
126    {
127        COUT(4) << "Tcl_execute: " << args.get() << std::endl;
128        std::string command = stripEnclosingBraces(args.get());
129
130        if (!CommandExecutor::execute(command, false))
131        {
132            COUT(1) << "Error: Can't execute command \"" << command << "\"!" << std::endl;
133        }
134    }
135
136    std::string TclBind::tcl(const std::string& tclcode)
137    {
138        if (TclBind::getInstance().interpreter_)
139        {
140            try
141            {
142                std::string output = TclBind::getInstance().interpreter_->eval(tclcode);
143                if (output != "")
144                {
145                    COUT(0) << "tcl> " << output << std::endl;
146                }
147                return output;
148            }
149            catch (Tcl::tcl_error const &e)
150            {   COUT(1) << "tcl> Error: " << e.what() << std::endl;   }
151            catch (std::exception const &e)
152            {   COUT(1) << "Error while executing Tcl: " << e.what() << std::endl;   }
153        }
154
155        return "";
156    }
157
158    void TclBind::bgerror(std::string error)
159    {
160        COUT(1) << "Tcl background error: " << stripEnclosingBraces(error) << std::endl;
161    }
162
163    bool TclBind::eval(const std::string& tclcode)
164    {
165        try
166        {
167            TclBind::getInstance().interpreter_->eval(tclcode);
168            return true;
169        }
170        catch (Tcl::tcl_error const &e)
171        {   COUT(1) << "Tcl error: " << e.what() << std::endl;   }
172        catch (std::exception const &e)
173        {   COUT(1) << "Error while executing Tcl: " << e.what() << std::endl;   }
174
175        return false;
176    }
177}
Note: See TracBrowser for help on using the repository browser.