Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/command/ConsoleCommand.cc @ 11071

Last change on this file since 11071 was 11071, checked in by landauf, 8 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 21.9 KB
RevLine 
[1505]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
[7401]29/**
30    @file
31    @brief Implementation of the ConsoleCommand class.
32*/
33
[1505]34#include "ConsoleCommand.h"
35
[7214]36#include "util/Convert.h"
[7203]37#include "core/Language.h"
[7222]38#include "core/GameMode.h"
[7861]39#include "core/input/KeyBinder.h"
40#include "core/input/KeyBinderManager.h"
[10624]41#include "ConsoleCommandManager.h"
[7179]42
43namespace orxonox
44{
[7401]45    /**
[10624]46        @brief Constructor: Initializes all values and registers the command (without a group).
47        @param name The name of the command
48        @param executor The executor of the command
49        @param bInitialized If true, the executor is used for both, the definition of the function-header AND to executute the command. If false, the command is inactive and needs to be assigned a function before it can be used.
50    */
51    ConsoleCommand::ConsoleCommand(const std::string& name, const ExecutorPtr& executor, bool bInitialized)
[11071]52        : ConsoleCommand("", name, executor, bInitialized)
[10624]53    {
54    }
55
56    /**
57        @brief Constructor: Initializes all values and registers the command (with a group).
[7401]58        @param group The group of the command
59        @param name The name of the command
60        @param executor The executor of the command
61        @param bInitialized If true, the executor is used for both, the definition of the function-header AND to executute the command. If false, the command is inactive and needs to be assigned a function before it can be used.
62    */
[7236]63    ConsoleCommand::ConsoleCommand(const std::string& group, const std::string& name, const ExecutorPtr& executor, bool bInitialized)
[7179]64    {
[10624]65        this->init(group, name, executor, bInitialized);
66    }
67
68    void ConsoleCommand::init(const std::string& group, const std::string& name, const ExecutorPtr& executor, bool bInitialized)
69    {
[7185]70        this->bActive_ = true;
[7215]71        this->bHidden_ = false;
72        this->accessLevel_ = AccessLevel::All;
73
[7214]74        this->baseName_ = name;
[7267]75        this->baseFunctor_ = executor->getFunctor();
[7214]76
[11071]77        for (ArgumentCompleter*& completer : this->argumentCompleter_)
78            completer = nullptr;
[7215]79
80        this->keybindMode_ = KeybindMode::OnPress;
81        this->inputConfiguredParam_ = -1;
82
[7214]83        if (bInitialized)
84            this->executor_ = executor;
85
[11071]86        this->names_.emplace_back(group, name);
[7179]87    }
88
[7401]89    /**
90        @brief Destructor: Unregisters the command.
91    */
[7236]92    ConsoleCommand::~ConsoleCommand()
[7214]93    {
94    }
95
[7401]96    /**
97        @brief Registers the command with the same name, but without group, as shortcut.
98    */
[7236]99    ConsoleCommand& ConsoleCommand::addShortcut()
[7179]100    {
[11071]101        this->names_.emplace_back("", this->baseName_);
[7179]102        return *this;
103    }
104
[7401]105    /**
106        @brief Registers the command with an alias as shortcut.
107    */
[7236]108    ConsoleCommand& ConsoleCommand::addShortcut(const std::string&  name)
[7179]109    {
[11071]110        this->names_.emplace_back("", name);
[7179]111        return *this;
112    }
113
[7401]114    /**
115        @brief Registers the command in a different group but with the same name.
116    */
[7236]117    ConsoleCommand& ConsoleCommand::addGroup(const std::string& group)
[7179]118    {
[11071]119        this->names_.emplace_back(group, this->baseName_);
[7179]120        return *this;
121    }
122
[7401]123    /**
124        @brief Registers an alias of the command in a different group with a different name.
125    */
[7236]126    ConsoleCommand& ConsoleCommand::addGroup(const std::string& group, const std::string&  name)
[7179]127    {
[11071]128        this->names_.emplace_back(group, name);
[7179]129        return *this;
130    }
131
[7401]132    /**
133        @brief Returns true if the command can be executed right now.
134
135        This returns only true, if the following conditions are met:
136         - The command is active
137         - The command has an executor
138         - The executor has a functor
139         - The functor is static or has an object
140    */
[7236]141    bool ConsoleCommand::isActive() const
[7179]142    {
[7218]143        return (this->bActive_ && this->executor_ && this->executor_->getFunctor() && (this->executor_->getFunctor()->getType() == Functor::Type::Static || this->executor_->getFunctor()->getRawObjectPointer()));
[7214]144    }
145
[7401]146    /**
147        @brief Returns true if the current state of the game matches the required access level.
148    */
[7236]149    bool ConsoleCommand::hasAccess() const
[7222]150    {
151        switch (this->accessLevel_)
152        {
153            case AccessLevel::All:        return true;
154            case AccessLevel::Standalone: return GameMode::isStandalone();
155            case AccessLevel::Master:     return GameMode::isMaster();
[7401]156            case AccessLevel::Server:     return GameMode::isServer();
[7222]157            case AccessLevel::Client:     return GameMode::isClient();
[7401]158            case AccessLevel::Online:     return (GameMode::isServer() || GameMode::isClient());
[7222]159            case AccessLevel::Offline:    return GameMode::isStandalone();
160            case AccessLevel::None:       return false;
161            default:                      return false;
162        }
163    }
164
[7401]165    /**
166        @brief Returns true if the headers of the given functor match the declaration of this command.
167    */
[7236]168    bool ConsoleCommand::headersMatch(const FunctorPtr& functor)
[7214]169    {
[7401]170        // get the minimum of the number of parameters of both commands
[7267]171        unsigned int minparams = std::min(this->baseFunctor_->getParamCount(), functor->getParamCount());
[7214]172
[7401]173        // if the reduced headers don't match -> return false
[7267]174        if (this->baseFunctor_->getHeaderIdentifier(minparams) != functor->getHeaderIdentifier(minparams))
[7214]175            return false;
[7401]176        // if the reduced headers match and the new functor has less or equal parameters -> return true
[7267]177        else if (functor->getParamCount() <= this->baseFunctor_->getParamCount())
[7214]178            return true;
[7401]179        // the headers match but the new functor has more arguments and there is no executor with default-values -> return false
[7214]180        else if (!this->executor_)
181            return false;
[7401]182        // the headers match but the new functor has more arguments, check if the executor has enough default-values
[7214]183        else
[7179]184        {
[7267]185            for (unsigned int i = this->baseFunctor_->getParamCount(); i < functor->getParamCount(); ++i)
186            {
[7214]187                if (!this->executor_->defaultValueSet(i))
[7267]188                {
[8858]189                    orxout(internal_warning, context::commands) << "Default value " << i << " is missing" << endl;
[7214]190                    return false;
[7267]191                }
192            }
[7214]193
[7185]194            return true;
[7179]195        }
[7214]196    }
[7185]197
[7401]198    /**
199        @brief Returns true if the headers of the given executor match the declaration of this command.
200    */
[7236]201    bool ConsoleCommand::headersMatch(const ExecutorPtr& executor)
[7214]202    {
[7401]203        // get the minimum of the number of parameters of both commands
[7267]204        unsigned int minparams = std::min(this->baseFunctor_->getParamCount(), executor->getParamCount());
[7214]205
[7401]206        // if the reduced headers don't match -> return false
[7267]207        if (this->baseFunctor_->getHeaderIdentifier(minparams) != executor->getFunctor()->getHeaderIdentifier(minparams))
[7214]208            return false;
[7401]209        // if the reduced headers match and the new functor has less or equal parameters -> return true
[7267]210        else if (executor->getParamCount() <= this->baseFunctor_->getParamCount())
[7214]211            return true;
[7401]212        // the headers match but the new functor has more arguments, check if the new executor has enough default-values
[7214]213        else
[7179]214        {
[7267]215            for (unsigned int i = this->baseFunctor_->getParamCount(); i < executor->getParamCount(); ++i)
216            {
[7214]217                if (!executor->defaultValueSet(i))
[7267]218                {
[8858]219                    orxout(internal_warning, context::commands) << "Default value " << i << " is missing" << endl;
[7214]220                    return false;
[7267]221                }
222            }
[7214]223
224            return true;
225        }
226    }
227
[7401]228    /**
229        @brief Changes the executor.
230        @param executor The new executor
231        @param bForce If true, the executor is always assigned, even if the headers don't match
232        @return Returns true if the assignment was successful
233    */
[7236]234    bool ConsoleCommand::setFunction(const ExecutorPtr& executor, bool bForce)
[7214]235    {
[7401]236        // assign the executor if a) it's a null-pointer, b) its functor is a null-pointer, c) it's forced, d) the headers match
[7214]237        if (!executor || !executor->getFunctor() || bForce || this->headersMatch(executor))
238        {
[7401]239            // assign the executor and clear the object stack (because it's also a new function)
[7214]240            this->executor_ = executor;
[7272]241            this->objectStack_.clear();
[7214]242            return true;
243        }
244        else
245        {
[8858]246            orxout(internal_error, context::commands) << "Couldn't assign new executor to console command \"" << this->baseName_ << "\", headers don't match." << endl;
[7185]247            return false;
[7179]248        }
[7214]249    }
[7185]250
[7401]251    /**
252        @brief Changes the functor of the current executor.
253        @param functor The new functor
254        @param bForce If true, the functor is always assigned, even if the headers don't match
255        @return Returns true if the assignment was successful
256    */
[7236]257    bool ConsoleCommand::setFunction(const FunctorPtr& functor, bool bForce)
[7214]258    {
[7401]259        // assign the functor if a) it's a null-pointer, b) it's forced, c) the headers match
[7214]260        if (!functor || bForce || this->headersMatch(functor))
261        {
[7401]262            // assign the functor (create a new executor if necessary) and clear the object stack
[7214]263            if (this->executor_)
264                this->executor_->setFunctor(functor);
[7218]265            else if (functor)
[7214]266                this->executor_ = createExecutor(functor);
[7272]267            this->objectStack_.clear();
[7214]268
269            return true;
270        }
271        else
272        {
[8858]273            orxout(internal_error, context::commands) << "Couldn't assign new functor to console command \"" << this->baseName_ << "\", headers don't match." << endl;
[7214]274            return false;
275        }
[7179]276    }
277
[7401]278    /**
279        @brief Pushes a new executor to the command-stack.
280        @param executor The new executor
281        @param bForce If true, the executor is always assigned, even if the headers don't match
282    */
[7236]283    void ConsoleCommand::pushFunction(const ExecutorPtr& executor, bool bForce)
[7179]284    {
[7401]285        // prepare the old function to be put on the stack
[7214]286        Command command;
[7270]287        command.executor_ = this->executor_;
[7214]288        if (command.executor_)
[7270]289            command.functor_ = this->executor_->getFunctor();
[7272]290        command.objectStack_ = this->objectStack_;
[7185]291
[7401]292        // check if the new executor can be assigned and push the old function to the stack
[7214]293        if (this->setFunction(executor, bForce))
294            this->commandStack_.push(command);
[7179]295    }
296
[7401]297    /**
298        @brief Pushes a new functor to the command-stack.
299        @param functor The new functor
300        @param bForce If true, the functor is always assigned, even if the headers don't match
301    */
[7236]302    void ConsoleCommand::pushFunction(const FunctorPtr& functor, bool bForce)
[7179]303    {
[7401]304        // prepare the old function to be put on the stack
[7214]305        Command command;
[7270]306        command.executor_ = this->executor_;
[7214]307        if (command.executor_)
[7270]308            command.functor_ = this->executor_->getFunctor();
[7272]309        command.objectStack_ = this->objectStack_;
[7214]310
[7401]311        // check if the new functor can be assigned and push the old function to the stack
[7214]312        if (this->setFunction(functor, bForce))
313            this->commandStack_.push(command);
[7185]314    }
[7179]315
[7401]316    /**
317        @brief Pushes a copy of the current executor and functor on the stack.
318    */
[7236]319    void ConsoleCommand::pushFunction()
[7185]320    {
[7214]321        if (this->executor_)
[11071]322            this->pushFunction(std::make_shared<Executor>(*this->executor_.get()));
[7214]323        else
[8858]324            orxout(internal_error, context::commands) << "Couldn't push copy of executor in console command \"" << this->baseName_ << "\", no executor set." << endl;
[7179]325    }
326
[7401]327    /**
328        @brief Removes the current function from the stack and restores the old state.
329    */
[7236]330    void ConsoleCommand::popFunction()
[7179]331    {
[7214]332        Command command;
[7401]333
334        // check if there's a function on the stack
[7214]335        if (!this->commandStack_.empty())
[7179]336        {
[7401]337            // yes it is - assign it to command and remove it from the stack
[7214]338            command = this->commandStack_.top();
339            this->commandStack_.pop();
[7179]340        }
[7214]341
[7401]342        // restore the old executor (and also restore its functor in case this was changed in the meantime)
[7214]343        this->executor_ = command.executor_;
344        if (command.executor_)
345            this->executor_->setFunctor(command.functor_);
[7272]346        this->objectStack_ = command.objectStack_;
[7179]347    }
348
[7401]349    /**
[11071]350        @brief Sets the functor to nullptr (which also deactivates the command).
[7401]351    */
[7236]352    void ConsoleCommand::resetFunction()
[7218]353    {
354        if (this->executor_)
[11071]355            this->executor_->setFunctor(nullptr);
[7272]356        this->objectStack_.clear();
[7218]357    }
358
[7401]359    /**
360        @brief Returns the current executor which can be used to execute the command.
361    */
[7236]362    const ExecutorPtr& ConsoleCommand::getExecutor() const
[7179]363    {
[7214]364        return this->executor_;
[7179]365    }
366
[7401]367    /**
368        @brief Changes the current object that is used to execute member-functions.
369        @return Returns true if the object was successfully changed
370    */
[7236]371    bool ConsoleCommand::setObject(void* object)
[7214]372    {
[7401]373        // check if there's an executor
[7214]374        if (this->executor_)
[7185]375        {
[7401]376            // check if there's a functor
[7214]377            if (this->executor_->getFunctor())
378            {
[7401]379                // change the object
[7214]380                this->executor_->getFunctor()->setRawObjectPointer(object);
381                return true;
382            }
383            else if (object)
[8858]384                orxout(internal_error, context::commands) << "Can't assign object to console command \"" << this->baseName_ << "\", no functor set." << endl;
[7185]385        }
[7214]386        else if (object)
[8858]387            orxout(internal_error, context::commands) << "Can't assign object to console command \"" << this->baseName_ << "\", no executor set." << endl;
[7214]388
389        return false;
[7179]390    }
391
[7401]392    /**
393        @brief Push a new object to the object-stack.
394    */
[7236]395    void ConsoleCommand::pushObject(void* object)
[7214]396    {
397        void* oldobject = this->getObject();
398        if (this->setObject(object))
[7272]399            this->objectStack_.push_back(oldobject);
[7214]400    }
401
[7401]402    /**
403        @brief Removes the current object from the stack an restores the old object.
404    */
[7236]405    void ConsoleCommand::popObject()
[7185]406    {
[11071]407        void* newobject = nullptr;
[7185]408        if (!this->objectStack_.empty())
409        {
[7272]410            newobject = this->objectStack_.back();
411            this->objectStack_.pop_back();
[7185]412        }
413        this->setObject(newobject);
414    }
415
[7401]416    /**
417        @brief Returns the current object pointer that is used to execute member-functions.
418    */
[7236]419    void* ConsoleCommand::getObject() const
[7185]420    {
[7214]421        if (this->executor_ && this->executor_->getFunctor())
422            return this->executor_->getFunctor()->getRawObjectPointer();
[7185]423        else
[11071]424            return nullptr;
[7185]425    }
426
[7401]427    /**
428        @brief Changes the default values of the current executor.
429    */
430    ConsoleCommand& ConsoleCommand::defaultValues(const MultiType& arg1)
[7215]431    {
432        if (this->executor_)
[7401]433            this->executor_->setDefaultValues(arg1);
[7215]434        else
[8858]435            orxout(internal_error, context::commands) << "Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << endl;
[7215]436
437        return *this;
438    }
439
[7401]440    /**
441        @brief Changes the default values of the current executor.
442    */
443    ConsoleCommand& ConsoleCommand::defaultValues(const MultiType& arg1, const MultiType& arg2)
[7215]444    {
445        if (this->executor_)
[7401]446            this->executor_->setDefaultValues(arg1, arg2);
[7215]447        else
[8858]448            orxout(internal_error, context::commands) << "Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << endl;
[7215]449
450        return *this;
451    }
452
[7401]453    /**
454        @brief Changes the default values of the current executor.
455    */
456    ConsoleCommand& ConsoleCommand::defaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3)
[7215]457    {
458        if (this->executor_)
[7401]459            this->executor_->setDefaultValues(arg1, arg2, arg3);
[7215]460        else
[8858]461            orxout(internal_error, context::commands) << "Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << endl;
[7215]462
463        return *this;
464    }
465
[7401]466    /**
467        @brief Changes the default values of the current executor.
468    */
469    ConsoleCommand& ConsoleCommand::defaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3, const MultiType& arg4)
[7215]470    {
471        if (this->executor_)
[7401]472            this->executor_->setDefaultValues(arg1, arg2, arg3, arg4);
[7215]473        else
[8858]474            orxout(internal_error, context::commands) << "Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << endl;
[7215]475
476        return *this;
477    }
478
[7401]479    /**
480        @brief Changes the default values of the current executor.
481    */
482    ConsoleCommand& ConsoleCommand::defaultValues(const MultiType& arg1, const MultiType& arg2, const MultiType& arg3, const MultiType& arg4, const MultiType& arg5)
[7215]483    {
484        if (this->executor_)
[7401]485            this->executor_->setDefaultValues(arg1, arg2, arg3, arg4, arg5);
[7215]486        else
[8858]487            orxout(internal_error, context::commands) << "Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << endl;
[7215]488
489        return *this;
490    }
491
[7401]492    /**
493        @brief Changes the default value of the argument with given index of the current executor.
494        @param index The index of the argument (the first argument has index 0)
495        @param arg The new default value
496    */
497    ConsoleCommand& ConsoleCommand::defaultValue(unsigned int index, const MultiType& arg)
[7215]498    {
499        if (this->executor_)
[7401]500            this->executor_->setDefaultValue(index, arg);
[7215]501        else
[8858]502            orxout(internal_error, context::commands) << "Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << endl;
[7215]503
504        return *this;
505    }
506
[7401]507    /**
508        @brief Changes the argument completer for the given argument.
509        @param index The index of the argument (the first argument has index 0)
510        @param completer The new argument completer
511    */
512    ConsoleCommand& ConsoleCommand::argumentCompleter(unsigned int index, ArgumentCompleter* completer)
[7215]513    {
[7401]514        if (index < 5)
515            this->argumentCompleter_[index] = completer;
[7215]516        else
[8858]517            orxout(internal_warning, context::commands) << "Couldn't add autocompletion-function for index " << index << " in console command \"" << this->baseName_ << "\": index out of bound." << endl;
[7215]518
519        return *this;
520    }
521
[7401]522    /**
523        @brief Returns the argument completer for the argument with given index.
524    */
525    ArgumentCompleter* ConsoleCommand::getArgumentCompleter(unsigned int index) const
[7215]526    {
[7401]527        if (index < 5)
528            return this->argumentCompleter_[index];
[7215]529        else
[11071]530            return nullptr;
[7215]531    }
532
[7401]533    /**
534        @brief Sets the description of this command.
535    */
[7236]536    ConsoleCommand& ConsoleCommand::description(const std::string& description)
[7215]537    {
538        this->description_ = std::string("ConsoleCommandDescription::" + this->baseName_ + "::function");
539        AddLanguageEntry(this->description_, description);
540        return *this;
541    }
542
[7401]543    /**
544        @brief Returns the description of this command.
545    */
[7236]546    const std::string& ConsoleCommand::getDescription() const
[7215]547    {
548        return GetLocalisation_noerror(this->description_);
549    }
550
[7401]551    /**
552        @brief Sets the description for an argument with given index.
553    */
554    ConsoleCommand& ConsoleCommand::descriptionParam(unsigned int index, const std::string& description)
[7215]555    {
[7401]556        if (index < MAX_FUNCTOR_ARGUMENTS)
[7215]557        {
[7401]558            this->descriptionParam_[index] = std::string("ConsoleCommandDescription::" + this->baseName_ + "::param" + multi_cast<std::string>(index));
559            AddLanguageEntry(this->descriptionParam_[index], description);
[7215]560        }
561        return *this;
562    }
563
[7401]564    /**
565        @brief Returns the description for the argument with given index.
566    */
567    const std::string& ConsoleCommand::getDescriptionParam(unsigned int index) const
[7215]568    {
[7401]569        if (index < MAX_FUNCTOR_ARGUMENTS)
570            return GetLocalisation_noerror(this->descriptionParam_[index]);
[7215]571
572        return this->descriptionParam_[0];
573    }
574
[7401]575    /**
576        @brief Sets the description for the return-value.
577    */
[7236]578    ConsoleCommand& ConsoleCommand::descriptionReturnvalue(const std::string& description)
[7215]579    {
580        this->descriptionReturnvalue_ = std::string("ConsoleCommandDescription::" + this->baseName_ + "::returnvalue");
581        AddLanguageEntry(this->descriptionReturnvalue_, description);
582        return *this;
583    }
584
[7401]585    /**
586        @brief Returns the description for the return-value.
587    */
588    const std::string& ConsoleCommand::getDescriptionReturnvalue(int index) const
[7215]589    {
590        return GetLocalisation_noerror(this->descriptionReturnvalue_);
591    }
592
[7401]593    /**
[7861]594        @brief Changes the keybind mode.
595    */
596    ConsoleCommand& ConsoleCommand::changeKeybindMode(KeybindMode::Value mode)
597    {
[8316]598        if( KeyBinderManager::exists() )
599            KeyBinderManager::getInstance().getCurrent()->changeMode(this, mode);
[7861]600
601        this->keybindMode(mode);
602        return *this;
603    }
[7179]604}
Note: See TracBrowser for help on using the repository browser.