Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Oct 4, 2015, 9:12:21 PM (9 years ago)
Author:
landauf
Message:

merged branch core7 back to trunk

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/libraries/core/command/ConsoleCommand.cc

    r9348 r10624  
    3535
    3636#include "util/Convert.h"
    37 #include "util/StringUtils.h"
    3837#include "core/Language.h"
    3938#include "core/GameMode.h"
    4039#include "core/input/KeyBinder.h"
    4140#include "core/input/KeyBinderManager.h"
     41#include "ConsoleCommandManager.h"
    4242
    4343namespace orxonox
    4444{
    4545    /**
    46         @brief Constructor: Initializes all values and registers the command.
     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)
     52    {
     53        this->init("", name, executor, bInitialized);
     54    }
     55
     56    /**
     57        @brief Constructor: Initializes all values and registers the command (with a group).
    4758        @param group The group of the command
    4859        @param name The name of the command
     
    5263    ConsoleCommand::ConsoleCommand(const std::string& group, const std::string& name, const ExecutorPtr& executor, bool bInitialized)
    5364    {
     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    {
    5470        this->bActive_ = true;
    5571        this->bHidden_ = false;
     
    6884            this->executor_ = executor;
    6985
    70         ConsoleCommand::registerCommand(group, name, this);
     86        this->names_.push_back(CommandName(group, name));
    7187    }
    7288
     
    7692    ConsoleCommand::~ConsoleCommand()
    7793    {
    78         ConsoleCommand::unregisterCommand(this);
    7994    }
    8095
     
    8499    ConsoleCommand& ConsoleCommand::addShortcut()
    85100    {
    86         ConsoleCommand::registerCommand("", this->baseName_, this);
     101        this->names_.push_back(CommandName("", this->baseName_));
    87102        return *this;
    88103    }
     
    93108    ConsoleCommand& ConsoleCommand::addShortcut(const std::string&  name)
    94109    {
    95         ConsoleCommand::registerCommand("", name, this);
     110        this->names_.push_back(CommandName("", name));
    96111        return *this;
    97112    }
     
    102117    ConsoleCommand& ConsoleCommand::addGroup(const std::string& group)
    103118    {
    104         ConsoleCommand::registerCommand(group, this->baseName_, this);
     119        this->names_.push_back(CommandName(group, this->baseName_));
    105120        return *this;
    106121    }
     
    111126    ConsoleCommand& ConsoleCommand::addGroup(const std::string& group, const std::string&  name)
    112127    {
    113         ConsoleCommand::registerCommand(group, name, this);
     128        this->names_.push_back(CommandName(group, name));
    114129        return *this;
    115130    }
     
    587602        return *this;
    588603    }
    589 
    590     /**
    591         @brief Returns the command with given group an name.
    592         @param group The group of the requested command
    593         @param name The group of the requested command
    594         @param bPrintError If true, an error is printed if the command doesn't exist
    595     */
    596     /* static */ ConsoleCommand* ConsoleCommand::getCommand(const std::string& group, const std::string& name, bool bPrintError)
    597     {
    598         // find the group
    599         std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommand::getCommandMap().find(group);
    600         if (it_group != ConsoleCommand::getCommandMap().end())
    601         {
    602             // find the name
    603             std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.find(name);
    604             if (it_name != it_group->second.end())
    605             {
    606                 // return the pointer
    607                 return it_name->second;
    608             }
    609         }
    610         if (bPrintError)
    611         {
    612             if (group == "")
    613                 orxout(internal_error, context::commands) << "Couldn't find console command with shortcut \"" << name << "\"" << endl;
    614             else
    615                 orxout(internal_error, context::commands) << "Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << endl;
    616         }
    617         return 0;
    618     }
    619 
    620     /**
    621         @brief Returns the command with given group an name in lowercase.
    622         @param group The group of the requested command in lowercase
    623         @param name The group of the requested command in lowercase
    624         @param bPrintError If true, an error is printed if the command doesn't exist
    625     */
    626     /* static */ ConsoleCommand* ConsoleCommand::getCommandLC(const std::string& group, const std::string& name, bool bPrintError)
    627     {
    628         std::string groupLC = getLowercase(group);
    629         std::string nameLC = getLowercase(name);
    630 
    631         // find the group
    632         std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommand::getCommandMapLC().find(groupLC);
    633         if (it_group != ConsoleCommand::getCommandMapLC().end())
    634         {
    635             // find the name
    636             std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.find(nameLC);
    637             if (it_name != it_group->second.end())
    638             {
    639                 // return the pointer
    640                 return it_name->second;
    641             }
    642         }
    643         if (bPrintError)
    644         {
    645             if (group == "")
    646                 orxout(internal_error, context::commands) << "Couldn't find console command with shortcut \"" << name << "\"" << endl;
    647             else
    648                 orxout(internal_error, context::commands) << "Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << endl;
    649         }
    650         return 0;
    651     }
    652 
    653     /**
    654         @brief Returns the static map that stores all console commands.
    655     */
    656     /* static */ std::map<std::string, std::map<std::string, ConsoleCommand*> >& ConsoleCommand::getCommandMap()
    657     {
    658         static std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMap;
    659         return commandMap;
    660     }
    661 
    662     /**
    663         @brief Returns the static map that stores all console commands in lowercase.
    664     */
    665     /* static */ std::map<std::string, std::map<std::string, ConsoleCommand*> >& ConsoleCommand::getCommandMapLC()
    666     {
    667         static std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMapLC;
    668         return commandMapLC;
    669     }
    670 
    671     /**
    672         @brief Registers a new command with given group an name by adding it to the command map.
    673     */
    674     /* static */ void ConsoleCommand::registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command)
    675     {
    676         if (name == "")
    677             return;
    678 
    679         // check if a command with this name already exists
    680         if (ConsoleCommand::getCommand(group, name) != 0)
    681         {
    682             if (group == "")
    683                 orxout(internal_warning, context::commands) << "A console command with shortcut \"" << name << "\" already exists." << endl;
    684             else
    685                 orxout(internal_warning, context::commands) << "A console command with name \"" << name << "\" already exists in group \"" << group << "\"." << endl;
    686         }
    687         else
    688         {
    689             // add the command to the map
    690             ConsoleCommand::getCommandMap()[group][name] = command;
    691             ConsoleCommand::getCommandMapLC()[getLowercase(group)][getLowercase(name)] = command;
    692         }
    693     }
    694 
    695     /**
    696         @brief Removes the command from the command map.
    697     */
    698     /* static */ void ConsoleCommand::unregisterCommand(ConsoleCommand* command)
    699     {
    700         // iterate through all groups
    701         for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = ConsoleCommand::getCommandMap().begin(); it_group != ConsoleCommand::getCommandMap().end(); )
    702         {
    703             // iterate through all commands of each group
    704             for (std::map<std::string, ConsoleCommand*>::iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); )
    705             {
    706                 // erase the command
    707                 if (it_name->second == command)
    708                     it_group->second.erase(it_name++);
    709                 else
    710                     ++it_name;
    711             }
    712 
    713             // erase the group if it is empty now
    714             if (it_group->second.empty())
    715                 ConsoleCommand::getCommandMap().erase(it_group++);
    716             else
    717                 ++it_group;
    718         }
    719 
    720         // now the same for the lowercase-map:
    721 
    722         // iterate through all groups
    723         for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = ConsoleCommand::getCommandMapLC().begin(); it_group != ConsoleCommand::getCommandMapLC().end(); )
    724         {
    725             // iterate through all commands of each group
    726             for (std::map<std::string, ConsoleCommand*>::iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); )
    727             {
    728                 // erase the command
    729                 if (it_name->second == command)
    730                     it_group->second.erase(it_name++);
    731                 else
    732                     ++it_name;
    733             }
    734 
    735             // erase the group if it is empty now
    736             if (it_group->second.empty())
    737                 ConsoleCommand::getCommandMapLC().erase(it_group++);
    738             else
    739                 ++it_group;
    740         }
    741     }
    742 
    743     /**
    744         @brief Deletes all commands
    745     */
    746     /* static */ void ConsoleCommand::destroyAll()
    747     {
    748         // delete entries until the map is empty
    749         while (!ConsoleCommand::getCommandMap().empty() && !ConsoleCommand::getCommandMap().begin()->second.empty())
    750             delete ConsoleCommand::getCommandMap().begin()->second.begin()->second;
    751     }
    752604}
Note: See TracChangeset for help on using the changeset viewer.