Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/consolecommands3/src/libraries/core/command/ConsoleCommand.cc @ 7216

Last change on this file since 7216 was 7216, checked in by landauf, 14 years ago

removed old console command implementation (doesn't compile)

  • Property svn:eol-style set to native
File size: 16.1 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 "ConsoleCommand.h"
30
31#include "util/Convert.h"
32#include "core/Language.h"
33#include "core/BaseObject.h" // remove this
34
35namespace orxonox
36{
37    _SetConsoleCommand("BaseObject", "setName", &BaseObject::setName, (BaseObject*)0);
38    _ConsoleCommand::_ConsoleCommandManipulator test(_ModifyConsoleCommand("BaseObject", "setName").setFunction(&BaseObject::setActive));
39
40    _ConsoleCommand::_ConsoleCommand(const std::string& group, const std::string& name, const ExecutorPtr& executor, bool bInitialized)
41    {
42        this->bActive_ = true;
43        this->bHidden_ = false;
44        this->accessLevel_ = AccessLevel::All;
45
46        this->baseName_ = name;
47        this->baseExecutor_ = executor;
48
49        this->argumentCompleter_[0] = 0;
50        this->argumentCompleter_[1] = 0;
51        this->argumentCompleter_[2] = 0;
52        this->argumentCompleter_[3] = 0;
53        this->argumentCompleter_[4] = 0;
54
55        this->keybindMode_ = KeybindMode::OnPress;
56        this->inputConfiguredParam_ = -1;
57
58        if (bInitialized)
59            this->executor_ = executor;
60
61        _ConsoleCommand::registerCommand(group, name, this);
62    }
63
64    _ConsoleCommand::~_ConsoleCommand()
65    {
66        _ConsoleCommand::unregisterCommand(this);
67    }
68
69    _ConsoleCommand& _ConsoleCommand::addShortcut()
70    {
71        _ConsoleCommand::registerCommand("", this->baseName_, this);
72        return *this;
73    }
74
75    _ConsoleCommand& _ConsoleCommand::addShortcut(const std::string&  name)
76    {
77        _ConsoleCommand::registerCommand("", name, this);
78        return *this;
79    }
80
81    _ConsoleCommand& _ConsoleCommand::addGroup(const std::string& group)
82    {
83        _ConsoleCommand::registerCommand(group, this->baseName_, this);
84        return *this;
85    }
86
87    _ConsoleCommand& _ConsoleCommand::addGroup(const std::string& group, const std::string&  name)
88    {
89        _ConsoleCommand::registerCommand(group, name, this);
90        return *this;
91    }
92
93    bool _ConsoleCommand::isActive() const
94    {
95        return (this->bActive_ && this->executor_ && this->executor_->getFunctor());
96    }
97
98    bool _ConsoleCommand::headersMatch(const FunctorPtr& functor)
99    {
100        unsigned int minparams = std::min(this->baseExecutor_->getParamCount(), functor->getParamCount());
101
102        if (this->baseExecutor_->getFunctor()->getHeaderIdentifier(minparams) != functor->getHeaderIdentifier(minparams))
103            return false;
104        else if (functor->getParamCount() <= this->baseExecutor_->getParamCount())
105            return true;
106        else if (!this->executor_)
107            return false;
108        else
109        {
110            for (unsigned int i = this->baseExecutor_->getParamCount(); i < functor->getParamCount(); ++i)
111                if (!this->executor_->defaultValueSet(i))
112                    return false;
113
114            return true;
115        }
116    }
117
118    bool _ConsoleCommand::headersMatch(const ExecutorPtr& executor)
119    {
120        unsigned int minparams = std::min(this->baseExecutor_->getParamCount(), executor->getParamCount());
121
122        if (this->baseExecutor_->getFunctor()->getHeaderIdentifier(minparams) != executor->getFunctor()->getHeaderIdentifier(minparams))
123            return false;
124        else if (executor->getParamCount() <= this->baseExecutor_->getParamCount())
125            return true;
126        else
127        {
128            for (unsigned int i = this->baseExecutor_->getParamCount(); i < executor->getParamCount(); ++i)
129                if (!executor->defaultValueSet(i))
130                    return false;
131
132            return true;
133        }
134    }
135
136    bool _ConsoleCommand::setFunction(const ExecutorPtr& executor, bool bForce)
137    {
138        if (!executor || !executor->getFunctor() || bForce || this->headersMatch(executor))
139        {
140            this->executor_ = executor;
141            return true;
142        }
143        else
144        {
145            COUT(1) << "Error: Couldn't assign new executor to console command \"" << this->baseName_ << "\", headers don't match." << std::endl;
146            return false;
147        }
148    }
149
150    bool _ConsoleCommand::setFunction(const FunctorPtr& functor, bool bForce)
151    {
152        if (!functor || bForce || this->headersMatch(functor))
153        {
154            if (this->executor_)
155                this->executor_->setFunctor(functor);
156            else
157                this->executor_ = createExecutor(functor);
158
159            return true;
160        }
161        else
162        {
163            COUT(1) << "Error: Couldn't assign new functor to console command \"" << this->baseName_ << "\", headers don't match." << std::endl;
164            return false;
165        }
166    }
167
168    void _ConsoleCommand::pushFunction(const ExecutorPtr& executor, bool bForce)
169    {
170        Command command;
171        command.executor_ = this->getExecutor();
172        if (command.executor_)
173            command.functor_ = this->getExecutor()->getFunctor();
174
175        if (this->setFunction(executor, bForce))
176            this->commandStack_.push(command);
177    }
178
179    void _ConsoleCommand::pushFunction(const FunctorPtr& functor, bool bForce)
180    {
181        Command command;
182        command.executor_ = this->getExecutor();
183        if (command.executor_)
184            command.functor_ = this->getExecutor()->getFunctor();
185
186        if (this->setFunction(functor, bForce))
187            this->commandStack_.push(command);
188    }
189
190    void _ConsoleCommand::pushFunction()
191    {
192        if (this->executor_)
193            this->pushFunction(new Executor(*this->executor_.get()));
194        else
195            COUT(1) << "Error: Couldn't push copy of executor in console command \"" << this->baseName_ << "\", no executor set." << std::endl;
196    }
197
198    void _ConsoleCommand::popFunction()
199    {
200        Command command;
201        if (!this->commandStack_.empty())
202        {
203            command = this->commandStack_.top();
204            this->commandStack_.pop();
205        }
206
207        this->executor_ = command.executor_;
208        if (command.executor_)
209            this->executor_->setFunctor(command.functor_);
210    }
211
212    const ExecutorPtr& _ConsoleCommand::getExecutor() const
213    {
214        return this->executor_;
215    }
216
217    bool _ConsoleCommand::setObject(void* object)
218    {
219        if (this->executor_)
220        {
221            if (this->executor_->getFunctor())
222            {
223                this->executor_->getFunctor()->setRawObjectPointer(object);
224                return true;
225            }
226            else if (object)
227                COUT(1) << "Error: Can't assign object to console command \"" << this->baseName_ << "\", no functor set." << std::endl;
228        }
229        else if (object)
230            COUT(1) << "Error: Can't assign object to console command \"" << this->baseName_ << "\", no executor set." << std::endl;
231
232        return false;
233    }
234
235    void _ConsoleCommand::pushObject(void* object)
236    {
237        void* oldobject = this->getObject();
238        if (this->setObject(object))
239            this->objectStack_.push(oldobject);
240    }
241
242    void _ConsoleCommand::popObject()
243    {
244        void* newobject = 0;
245        if (!this->objectStack_.empty())
246        {
247            newobject = this->objectStack_.top();
248            this->objectStack_.pop();
249        }
250        this->setObject(newobject);
251    }
252
253    void* _ConsoleCommand::getObject() const
254    {
255        if (this->executor_ && this->executor_->getFunctor())
256            return this->executor_->getFunctor()->getRawObjectPointer();
257        else
258            return 0;
259    }
260
261    _ConsoleCommand& _ConsoleCommand::defaultValues(const MultiType& param1)
262    {
263        if (this->executor_)
264            this->executor_->setDefaultValues(param1);
265        else
266            COUT(1) << "Error: Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << std::endl;
267
268        return *this;
269    }
270
271    _ConsoleCommand& _ConsoleCommand::defaultValues(const MultiType& param1, const MultiType& param2)
272    {
273        if (this->executor_)
274            this->executor_->setDefaultValues(param1, param2);
275        else
276            COUT(1) << "Error: Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << std::endl;
277
278        return *this;
279    }
280
281    _ConsoleCommand& _ConsoleCommand::defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3)
282    {
283        if (this->executor_)
284            this->executor_->setDefaultValues(param1, param2, param3);
285        else
286            COUT(1) << "Error: Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << std::endl;
287
288        return *this;
289    }
290
291    _ConsoleCommand& _ConsoleCommand::defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4)
292    {
293        if (this->executor_)
294            this->executor_->setDefaultValues(param1, param2, param3, param4);
295        else
296            COUT(1) << "Error: Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << std::endl;
297
298        return *this;
299    }
300
301    _ConsoleCommand& _ConsoleCommand::defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5)
302    {
303        if (this->executor_)
304            this->executor_->setDefaultValues(param1, param2, param3, param4, param5);
305        else
306            COUT(1) << "Error: Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << std::endl;
307
308        return *this;
309    }
310
311    _ConsoleCommand& _ConsoleCommand::defaultValue(unsigned int index, const MultiType& param)
312    {
313        if (this->executor_)
314            this->executor_->setDefaultValue(index, param);
315        else
316            COUT(1) << "Error: Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << std::endl;
317
318        return *this;
319    }
320
321    _ConsoleCommand& _ConsoleCommand::argumentCompleter(unsigned int param, ArgumentCompleter* completer)
322    {
323        if (param < 5)
324            this->argumentCompleter_[param] = completer;
325        else
326            COUT(2) << "Warning: Couldn't add autocompletion-function for param " << param << " in console command \"" << this->baseName_ << "\": index out of bound." << std::endl;
327
328        return *this;
329    }
330
331    ArgumentCompleter* _ConsoleCommand::getArgumentCompleter(unsigned int param) const
332    {
333        if (param < 5)
334            return this->argumentCompleter_[param];
335        else
336            return 0;
337    }
338
339    void _ConsoleCommand::createArgumentCompletionList(unsigned int param, const std::string& param1, const std::string& param2, const std::string& param3, const std::string& param4, const std::string& param5)
340    {
341        if (param < 5 && this->argumentCompleter_[param])
342            this->argumentList_ = (*this->argumentCompleter_[param])(param1, param2, param3, param4, param5);
343        else
344            this->argumentList_.clear();
345    }
346
347    _ConsoleCommand& _ConsoleCommand::description(const std::string& description)
348    {
349        this->description_ = std::string("ConsoleCommandDescription::" + this->baseName_ + "::function");
350        AddLanguageEntry(this->description_, description);
351        return *this;
352    }
353
354    const std::string& _ConsoleCommand::getDescription() const
355    {
356        return GetLocalisation_noerror(this->description_);
357    }
358
359    _ConsoleCommand& _ConsoleCommand::descriptionParam(unsigned int param, const std::string& description)
360    {
361        if (param < MAX_FUNCTOR_ARGUMENTS)
362        {
363            this->descriptionParam_[param] = std::string("ConsoleCommandDescription::" + this->baseName_ + "::param" + multi_cast<std::string>(param));
364            AddLanguageEntry(this->descriptionParam_[param], description);
365        }
366        return *this;
367    }
368
369    const std::string& _ConsoleCommand::getDescriptionParam(unsigned int param) const
370    {
371        if (param < MAX_FUNCTOR_ARGUMENTS)
372            return GetLocalisation_noerror(this->descriptionParam_[param]);
373
374        return this->descriptionParam_[0];
375    }
376
377    _ConsoleCommand& _ConsoleCommand::descriptionReturnvalue(const std::string& description)
378    {
379        this->descriptionReturnvalue_ = std::string("ConsoleCommandDescription::" + this->baseName_ + "::returnvalue");
380        AddLanguageEntry(this->descriptionReturnvalue_, description);
381        return *this;
382    }
383
384    const std::string& _ConsoleCommand::getDescriptionReturnvalue(int param) const
385    {
386        return GetLocalisation_noerror(this->descriptionReturnvalue_);
387    }
388
389    /* static */ const _ConsoleCommand* _ConsoleCommand::getCommand(const std::string& group, const std::string& name, bool bPrintError)
390    {
391        std::map<std::string, std::map<std::string, _ConsoleCommand*> >::const_iterator it_group = _ConsoleCommand::getCommandMap().find(group);
392        if (it_group != _ConsoleCommand::getCommandMap().end())
393        {
394            std::map<std::string, _ConsoleCommand*>::const_iterator it_name = it_group->second.find(name);
395            if (it_name != it_group->second.end())
396            {
397                return it_name->second;
398            }
399        }
400        if (bPrintError)
401        {
402            if (group == "")
403                COUT(1) << "Error: Couldn't find console command with shortcut \"" << name << "\"" << std::endl;
404            else
405                COUT(1) << "Error: Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << std::endl;
406        }
407        return 0;
408    }
409
410    /* static */ std::map<std::string, std::map<std::string, _ConsoleCommand*> >& _ConsoleCommand::getCommandMap()
411    {
412        static std::map<std::string, std::map<std::string, _ConsoleCommand*> > commandMap;
413        return commandMap;
414    }
415
416    /* static */ void _ConsoleCommand::registerCommand(const std::string& group, const std::string& name, _ConsoleCommand* command)
417    {
418        if (name == "")
419            return;
420
421        if (_ConsoleCommand::getCommand(group, name) != 0)
422        {
423            if (group == "")
424                COUT(2) << "Warning: A console command with shortcut \"" << name << "\" already exists." << std::endl;
425            else
426                COUT(2) << "Warning: A console command with name \"" << name << "\" already exists in group \"" << group << "\"." << std::endl;
427        }
428        else
429        {
430            _ConsoleCommand::getCommandMap()[group][name] = command;
431        }
432    }
433
434    /* static */ void _ConsoleCommand::unregisterCommand(_ConsoleCommand* command)
435    {
436        for (std::map<std::string, std::map<std::string, _ConsoleCommand*> >::iterator it_group = _ConsoleCommand::getCommandMap().begin(); it_group != _ConsoleCommand::getCommandMap().end(); )
437        {
438            for (std::map<std::string, _ConsoleCommand*>::iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); )
439            {
440                if (it_name->second == command)
441                    it_group->second.erase(it_name++);
442                else
443                    ++it_name;
444            }
445
446            if (it_group->second.empty())
447                _ConsoleCommand::getCommandMap().erase(it_group++);
448            else
449                ++it_group;
450        }
451    }
452
453    /* static */ void _ConsoleCommand::destroyAll()
454    {
455        while (!_ConsoleCommand::getCommandMap().empty() && !_ConsoleCommand::getCommandMap().begin().empty())
456            _ConsoleCommand::getCommandMap().begin().erase(_ConsoleCommand::getCommandMap().begin().begin());
457    }
458}
Note: See TracBrowser for help on using the repository browser.