Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/core/TclBind.cc @ 1446

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

merged console branch into network branch

after several heavy troubles it compiles, but there is still a bug I couldn't fix: orxonox crashes as soon as one presses a key after opening the console… maybe someone else sees the problem?

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