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 | /** |
---|
30 | @file |
---|
31 | @brief Implementation of the ConsoleCommand class. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "ConsoleCommandManager.h" |
---|
35 | |
---|
36 | #include "ConsoleCommand.h" |
---|
37 | #include "util/StringUtils.h" |
---|
38 | |
---|
39 | namespace orxonox |
---|
40 | { |
---|
41 | /** |
---|
42 | @brief Returns the command with given group an name. |
---|
43 | @param group The group of the requested command |
---|
44 | @param name The group of the requested command |
---|
45 | @param bPrintError If true, an error is printed if the command doesn't exist |
---|
46 | */ |
---|
47 | /* static */ ConsoleCommand* ConsoleCommandManager::getCommand(const std::string& group, const std::string& name, bool bPrintError) |
---|
48 | { |
---|
49 | // find the group |
---|
50 | std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommandMap().find(group); |
---|
51 | if (it_group != ConsoleCommandManager::getCommandMap().end()) |
---|
52 | { |
---|
53 | // find the name |
---|
54 | std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.find(name); |
---|
55 | if (it_name != it_group->second.end()) |
---|
56 | { |
---|
57 | // return the pointer |
---|
58 | return it_name->second; |
---|
59 | } |
---|
60 | } |
---|
61 | if (bPrintError) |
---|
62 | { |
---|
63 | if (group == "") |
---|
64 | orxout(internal_error, context::commands) << "Couldn't find console command with shortcut \"" << name << "\"" << endl; |
---|
65 | else |
---|
66 | orxout(internal_error, context::commands) << "Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << endl; |
---|
67 | } |
---|
68 | return 0; |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | @brief Returns the command with given group an name in lowercase. |
---|
73 | @param group The group of the requested command in lowercase |
---|
74 | @param name The group of the requested command in lowercase |
---|
75 | @param bPrintError If true, an error is printed if the command doesn't exist |
---|
76 | */ |
---|
77 | /* static */ ConsoleCommand* ConsoleCommandManager::getCommandLC(const std::string& group, const std::string& name, bool bPrintError) |
---|
78 | { |
---|
79 | std::string groupLC = getLowercase(group); |
---|
80 | std::string nameLC = getLowercase(name); |
---|
81 | |
---|
82 | // find the group |
---|
83 | std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommandManager::getCommandMapLC().find(groupLC); |
---|
84 | if (it_group != ConsoleCommandManager::getCommandMapLC().end()) |
---|
85 | { |
---|
86 | // find the name |
---|
87 | std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.find(nameLC); |
---|
88 | if (it_name != it_group->second.end()) |
---|
89 | { |
---|
90 | // return the pointer |
---|
91 | return it_name->second; |
---|
92 | } |
---|
93 | } |
---|
94 | if (bPrintError) |
---|
95 | { |
---|
96 | if (group == "") |
---|
97 | orxout(internal_error, context::commands) << "Couldn't find console command with shortcut \"" << name << "\"" << endl; |
---|
98 | else |
---|
99 | orxout(internal_error, context::commands) << "Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << endl; |
---|
100 | } |
---|
101 | return 0; |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | @brief Returns the static map that stores all console commands. |
---|
106 | */ |
---|
107 | /* static */ std::map<std::string, std::map<std::string, ConsoleCommand*> >& ConsoleCommandManager::getCommandMap() |
---|
108 | { |
---|
109 | static std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMap; |
---|
110 | return commandMap; |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | @brief Returns the static map that stores all console commands in lowercase. |
---|
115 | */ |
---|
116 | /* static */ std::map<std::string, std::map<std::string, ConsoleCommand*> >& ConsoleCommandManager::getCommandMapLC() |
---|
117 | { |
---|
118 | static std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMapLC; |
---|
119 | return commandMapLC; |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | @brief Registers a new command with given group an name by adding it to the command map. |
---|
124 | */ |
---|
125 | /* static */ void ConsoleCommandManager::registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command) |
---|
126 | { |
---|
127 | if (name == "") |
---|
128 | return; |
---|
129 | |
---|
130 | // check if a command with this name already exists |
---|
131 | if (ConsoleCommandManager::getCommand(group, name) != 0) |
---|
132 | { |
---|
133 | if (group == "") |
---|
134 | orxout(internal_warning, context::commands) << "A console command with shortcut \"" << name << "\" already exists." << endl; |
---|
135 | else |
---|
136 | orxout(internal_warning, context::commands) << "A console command with name \"" << name << "\" already exists in group \"" << group << "\"." << endl; |
---|
137 | } |
---|
138 | else |
---|
139 | { |
---|
140 | // add the command to the map |
---|
141 | ConsoleCommandManager::getCommandMap()[group][name] = command; |
---|
142 | ConsoleCommandManager::getCommandMapLC()[getLowercase(group)][getLowercase(name)] = command; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | @brief Removes the command from the command map. |
---|
148 | */ |
---|
149 | /* static */ void ConsoleCommandManager::unregisterCommand(ConsoleCommand* command) |
---|
150 | { |
---|
151 | // iterate through all groups |
---|
152 | for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = ConsoleCommandManager::getCommandMap().begin(); it_group != ConsoleCommandManager::getCommandMap().end(); ) |
---|
153 | { |
---|
154 | // iterate through all commands of each group |
---|
155 | for (std::map<std::string, ConsoleCommand*>::iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); ) |
---|
156 | { |
---|
157 | // erase the command |
---|
158 | if (it_name->second == command) |
---|
159 | it_group->second.erase(it_name++); |
---|
160 | else |
---|
161 | ++it_name; |
---|
162 | } |
---|
163 | |
---|
164 | // erase the group if it is empty now |
---|
165 | if (it_group->second.empty()) |
---|
166 | ConsoleCommandManager::getCommandMap().erase(it_group++); |
---|
167 | else |
---|
168 | ++it_group; |
---|
169 | } |
---|
170 | |
---|
171 | // now the same for the lowercase-map: |
---|
172 | |
---|
173 | // iterate through all groups |
---|
174 | for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = ConsoleCommandManager::getCommandMapLC().begin(); it_group != ConsoleCommandManager::getCommandMapLC().end(); ) |
---|
175 | { |
---|
176 | // iterate through all commands of each group |
---|
177 | for (std::map<std::string, ConsoleCommand*>::iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); ) |
---|
178 | { |
---|
179 | // erase the command |
---|
180 | if (it_name->second == command) |
---|
181 | it_group->second.erase(it_name++); |
---|
182 | else |
---|
183 | ++it_name; |
---|
184 | } |
---|
185 | |
---|
186 | // erase the group if it is empty now |
---|
187 | if (it_group->second.empty()) |
---|
188 | ConsoleCommandManager::getCommandMapLC().erase(it_group++); |
---|
189 | else |
---|
190 | ++it_group; |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | /** |
---|
195 | @brief Deletes all commands |
---|
196 | */ |
---|
197 | /* static */ void ConsoleCommandManager::destroyAll() |
---|
198 | { |
---|
199 | // delete entries until the map is empty |
---|
200 | while (!ConsoleCommandManager::getCommandMap().empty() && !ConsoleCommandManager::getCommandMap().begin()->second.empty()) |
---|
201 | delete ConsoleCommandManager::getCommandMap().begin()->second.begin()->second; |
---|
202 | } |
---|
203 | } |
---|