Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/core/TclBind.cc @ 2855

Last change on this file since 2855 was 2710, checked in by rgrieder, 16 years ago

Merged buildsystem3 containing buildsystem2 containing Adi's buildsystem branch back to the trunk.
Please update the media directory if you were not using buildsystem3 before.

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