Changeset 10624 for code/trunk/src/libraries/core/command/ConsoleCommand.cc
- Timestamp:
- Oct 4, 2015, 9:12:21 PM (10 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
-
code/trunk/src/libraries/core/command/ConsoleCommand.cc
r9348 r10624 35 35 36 36 #include "util/Convert.h" 37 #include "util/StringUtils.h"38 37 #include "core/Language.h" 39 38 #include "core/GameMode.h" 40 39 #include "core/input/KeyBinder.h" 41 40 #include "core/input/KeyBinderManager.h" 41 #include "ConsoleCommandManager.h" 42 42 43 43 namespace orxonox 44 44 { 45 45 /** 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). 47 58 @param group The group of the command 48 59 @param name The name of the command … … 52 63 ConsoleCommand::ConsoleCommand(const std::string& group, const std::string& name, const ExecutorPtr& executor, bool bInitialized) 53 64 { 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 { 54 70 this->bActive_ = true; 55 71 this->bHidden_ = false; … … 68 84 this->executor_ = executor; 69 85 70 ConsoleCommand::registerCommand(group, name, this);86 this->names_.push_back(CommandName(group, name)); 71 87 } 72 88 … … 76 92 ConsoleCommand::~ConsoleCommand() 77 93 { 78 ConsoleCommand::unregisterCommand(this);79 94 } 80 95 … … 84 99 ConsoleCommand& ConsoleCommand::addShortcut() 85 100 { 86 ConsoleCommand::registerCommand("", this->baseName_, this);101 this->names_.push_back(CommandName("", this->baseName_)); 87 102 return *this; 88 103 } … … 93 108 ConsoleCommand& ConsoleCommand::addShortcut(const std::string& name) 94 109 { 95 ConsoleCommand::registerCommand("", name, this);110 this->names_.push_back(CommandName("", name)); 96 111 return *this; 97 112 } … … 102 117 ConsoleCommand& ConsoleCommand::addGroup(const std::string& group) 103 118 { 104 ConsoleCommand::registerCommand(group, this->baseName_, this);119 this->names_.push_back(CommandName(group, this->baseName_)); 105 120 return *this; 106 121 } … … 111 126 ConsoleCommand& ConsoleCommand::addGroup(const std::string& group, const std::string& name) 112 127 { 113 ConsoleCommand::registerCommand(group, name, this);128 this->names_.push_back(CommandName(group, name)); 114 129 return *this; 115 130 } … … 587 602 return *this; 588 603 } 589 590 /**591 @brief Returns the command with given group an name.592 @param group The group of the requested command593 @param name The group of the requested command594 @param bPrintError If true, an error is printed if the command doesn't exist595 */596 /* static */ ConsoleCommand* ConsoleCommand::getCommand(const std::string& group, const std::string& name, bool bPrintError)597 {598 // find the group599 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 name603 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 pointer607 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 else615 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 lowercase623 @param name The group of the requested command in lowercase624 @param bPrintError If true, an error is printed if the command doesn't exist625 */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 group632 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 name636 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 pointer640 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 else648 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 exists680 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 else685 orxout(internal_warning, context::commands) << "A console command with name \"" << name << "\" already exists in group \"" << group << "\"." << endl;686 }687 else688 {689 // add the command to the map690 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 groups701 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 group704 for (std::map<std::string, ConsoleCommand*>::iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); )705 {706 // erase the command707 if (it_name->second == command)708 it_group->second.erase(it_name++);709 else710 ++it_name;711 }712 713 // erase the group if it is empty now714 if (it_group->second.empty())715 ConsoleCommand::getCommandMap().erase(it_group++);716 else717 ++it_group;718 }719 720 // now the same for the lowercase-map:721 722 // iterate through all groups723 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 group726 for (std::map<std::string, ConsoleCommand*>::iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); )727 {728 // erase the command729 if (it_name->second == command)730 it_group->second.erase(it_name++);731 else732 ++it_name;733 }734 735 // erase the group if it is empty now736 if (it_group->second.empty())737 ConsoleCommand::getCommandMapLC().erase(it_group++);738 else739 ++it_group;740 }741 }742 743 /**744 @brief Deletes all commands745 */746 /* static */ void ConsoleCommand::destroyAll()747 {748 // delete entries until the map is empty749 while (!ConsoleCommand::getCommandMap().empty() && !ConsoleCommand::getCommandMap().begin()->second.empty())750 delete ConsoleCommand::getCommandMap().begin()->second.begin()->second;751 }752 604 }
Note: See TracChangeset
for help on using the changeset viewer.