Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1255 was 1255, checked in by landauf, 16 years ago
  • after a total rewrite of the TclThreadManager, there are some new problems, but most of the old ones are fixed. i commit this version to have a backup, not because it's finished.
  • 'exec' command is now called 'source' (tcl and orxonox)
  • 'orxonox' command is now called 'query' (tcl only)
  • added 'crossquery' command (tcl only)
File size: 5.4 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
42    TclBind::TclBind()
43    {
44        this->interpreter_ = 0;
45        this->bSetTclLibPath_ = false;
46    }
47
48    TclBind::~TclBind()
49    {
50        if (this->interpreter_)
51            delete this->interpreter_;
52    }
53
54    TclBind& TclBind::getInstance()
55    {
56        static TclBind instance;
57        return instance;
58    }
59
60    void TclBind::setDataPath(const std::string& datapath)
61    {
62        this->tclLibPath_ = datapath + "/tcl";
63        this->bSetTclLibPath_ = true;
64
65        this->createTclInterpreter();
66    }
67
68    void TclBind::createTclInterpreter()
69    {
70        if (this->bSetTclLibPath_ && !this->interpreter_)
71        {
72            this->interpreter_ = new Tcl::interpreter(this->tclLibPath_);
73            this->interpreter_->def("orxonox::query", TclBind::tcl_query, Tcl::variadic());
74            this->interpreter_->def("orxonox::crossquery", TclThreadManager::tcl_crossquery, Tcl::variadic());
75            this->interpreter_->def("execute", TclBind::tcl_execute, Tcl::variadic());
76
77            try
78            {
79                this->interpreter_->eval("proc query args { orxonox::query $args }");
80                this->interpreter_->eval("proc crossquery {id args} { orxonox::crossquery 0 $id $args }");
81                this->interpreter_->eval("set id 0");
82                this->interpreter_->eval("rename exit tcl::exit; proc exit {} { execute exit }");
83                this->interpreter_->eval("redef_puts");
84            }
85            catch (Tcl::tcl_error const &e)
86            {   COUT(1) << "Tcl error while creating Tcl-interpreter: " << e.what() << std::endl;   }
87            catch (std::exception const &e)
88            {   COUT(1) << "Error while creating Tcl-interpreter: " << e.what() << std::endl;   }
89        }
90    }
91
92    void TclBind::createNewTclInterpreter()
93    {
94        if (this->interpreter_)
95        {
96            delete this->interpreter_;
97            this->interpreter_ = 0;
98        }
99
100        this->createTclInterpreter();
101    }
102
103    std::string TclBind::tcl_query(Tcl::object const &args)
104    {
105std::cout << "Tcl_query: args: " << args.get() << std::endl;
106        std::string command = args.get();
107
108        while (command.size() >= 2 && command[0] == '{' && command[command.size() - 1] == '}')
109            command = command.substr(1, command.size() - 2);
110
111        if (!CommandExecutor::execute(command, false))
112            COUT(1) << "Error: Can't execute command \"" << command << "\"!" << std::endl;
113
114        if (CommandExecutor::getLastEvaluation().hasReturnvalue())
115            return CommandExecutor::getLastEvaluation().getReturnvalue().toString();
116
117        return "";
118    }
119
120    void TclBind::tcl_execute(Tcl::object const &args)
121    {
122std::cout << "Tcl_execute: args: " << args.get() << std::endl;
123        std::string command = args.get();
124
125        while (command.size() >= 2 && command[0] == '{' && command[command.size() - 1] == '}')
126            command = command.substr(1, command.size() - 2);
127
128        if (!CommandExecutor::execute(command, false))
129            COUT(1) << "Error: Can't execute command \"" << command << "\"!" << std::endl;
130    }
131
132    std::string TclBind::tcl(const std::string& tclcode)
133    {
134        try
135        {
136            std::string output = TclBind::getInstance().interpreter_->eval(tclcode);
137            if (output != "")
138                COUT(0) << "tcl> " << output << std::endl;
139            return output;
140        }
141        catch (Tcl::tcl_error const &e)
142        {   COUT(1) << "tcl> Error: " << e.what() << std::endl;   }
143        catch (std::exception const &e)
144        {   COUT(1) << "Error while executing Tcl: " << e.what() << std::endl;   }
145
146        return "";
147    }
148
149    bool TclBind::eval(const std::string& tclcode)
150    {
151        try
152        {
153            TclBind::getInstance().interpreter_->eval(tclcode);
154            return true;
155        }
156        catch (Tcl::tcl_error const &e)
157        {   COUT(1) << "Tcl error: " << e.what() << std::endl;   }
158        catch (std::exception const &e)
159        {   COUT(1) << "Error while executing Tcl: " << e.what() << std::endl;   }
160
161        return false;
162    }
163}
Note: See TracBrowser for help on using the repository browser.