Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/resource/src/core/TclBind.cc @ 3344

Last change on this file since 3344 was 3344, checked in by landauf, 15 years ago

tcl uses it's native library, orxonox adds new features later (defined in media/tcl/init.tcl)

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