[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 | |
---|
| 29 | #include "ConsoleCommand.h" |
---|
| 30 | |
---|
[7214] | 31 | #include "util/Convert.h" |
---|
[7228] | 32 | #include "util/StringUtils.h" |
---|
[7203] | 33 | #include "core/Language.h" |
---|
[7222] | 34 | #include "core/GameMode.h" |
---|
[7179] | 35 | |
---|
| 36 | namespace orxonox |
---|
| 37 | { |
---|
[7236] | 38 | ConsoleCommand::ConsoleCommand(const std::string& group, const std::string& name, const ExecutorPtr& executor, bool bInitialized) |
---|
[7179] | 39 | { |
---|
[7185] | 40 | this->bActive_ = true; |
---|
[7215] | 41 | this->bHidden_ = false; |
---|
| 42 | this->accessLevel_ = AccessLevel::All; |
---|
| 43 | |
---|
[7214] | 44 | this->baseName_ = name; |
---|
[7267] | 45 | this->baseFunctor_ = executor->getFunctor(); |
---|
[7214] | 46 | |
---|
[7215] | 47 | this->argumentCompleter_[0] = 0; |
---|
| 48 | this->argumentCompleter_[1] = 0; |
---|
| 49 | this->argumentCompleter_[2] = 0; |
---|
| 50 | this->argumentCompleter_[3] = 0; |
---|
| 51 | this->argumentCompleter_[4] = 0; |
---|
| 52 | |
---|
| 53 | this->keybindMode_ = KeybindMode::OnPress; |
---|
| 54 | this->inputConfiguredParam_ = -1; |
---|
| 55 | |
---|
[7214] | 56 | if (bInitialized) |
---|
| 57 | this->executor_ = executor; |
---|
| 58 | |
---|
[7236] | 59 | ConsoleCommand::registerCommand(group, name, this); |
---|
[7179] | 60 | } |
---|
| 61 | |
---|
[7236] | 62 | ConsoleCommand::~ConsoleCommand() |
---|
[7214] | 63 | { |
---|
[7236] | 64 | ConsoleCommand::unregisterCommand(this); |
---|
[7214] | 65 | } |
---|
| 66 | |
---|
[7236] | 67 | ConsoleCommand& ConsoleCommand::addShortcut() |
---|
[7179] | 68 | { |
---|
[7236] | 69 | ConsoleCommand::registerCommand("", this->baseName_, this); |
---|
[7179] | 70 | return *this; |
---|
| 71 | } |
---|
| 72 | |
---|
[7236] | 73 | ConsoleCommand& ConsoleCommand::addShortcut(const std::string& name) |
---|
[7179] | 74 | { |
---|
[7236] | 75 | ConsoleCommand::registerCommand("", name, this); |
---|
[7179] | 76 | return *this; |
---|
| 77 | } |
---|
| 78 | |
---|
[7236] | 79 | ConsoleCommand& ConsoleCommand::addGroup(const std::string& group) |
---|
[7179] | 80 | { |
---|
[7236] | 81 | ConsoleCommand::registerCommand(group, this->baseName_, this); |
---|
[7179] | 82 | return *this; |
---|
| 83 | } |
---|
| 84 | |
---|
[7236] | 85 | ConsoleCommand& ConsoleCommand::addGroup(const std::string& group, const std::string& name) |
---|
[7179] | 86 | { |
---|
[7236] | 87 | ConsoleCommand::registerCommand(group, name, this); |
---|
[7179] | 88 | return *this; |
---|
| 89 | } |
---|
| 90 | |
---|
[7236] | 91 | bool ConsoleCommand::isActive() const |
---|
[7179] | 92 | { |
---|
[7218] | 93 | return (this->bActive_ && this->executor_ && this->executor_->getFunctor() && (this->executor_->getFunctor()->getType() == Functor::Type::Static || this->executor_->getFunctor()->getRawObjectPointer())); |
---|
[7214] | 94 | } |
---|
| 95 | |
---|
[7236] | 96 | bool ConsoleCommand::hasAccess() const |
---|
[7222] | 97 | { |
---|
| 98 | switch (this->accessLevel_) |
---|
| 99 | { |
---|
| 100 | case AccessLevel::All: return true; |
---|
| 101 | case AccessLevel::Standalone: return GameMode::isStandalone(); |
---|
| 102 | case AccessLevel::Master: return GameMode::isMaster(); |
---|
| 103 | case AccessLevel::Server: return GameMode::hasServer(); |
---|
| 104 | case AccessLevel::Client: return GameMode::isClient(); |
---|
| 105 | case AccessLevel::Online: return (GameMode::hasServer() || GameMode::isClient()); |
---|
| 106 | case AccessLevel::Offline: return GameMode::isStandalone(); |
---|
| 107 | case AccessLevel::None: return false; |
---|
| 108 | default: return false; |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | |
---|
[7236] | 112 | bool ConsoleCommand::headersMatch(const FunctorPtr& functor) |
---|
[7214] | 113 | { |
---|
[7267] | 114 | unsigned int minparams = std::min(this->baseFunctor_->getParamCount(), functor->getParamCount()); |
---|
[7214] | 115 | |
---|
[7267] | 116 | if (this->baseFunctor_->getHeaderIdentifier(minparams) != functor->getHeaderIdentifier(minparams)) |
---|
[7214] | 117 | return false; |
---|
[7267] | 118 | else if (functor->getParamCount() <= this->baseFunctor_->getParamCount()) |
---|
[7214] | 119 | return true; |
---|
| 120 | else if (!this->executor_) |
---|
| 121 | return false; |
---|
| 122 | else |
---|
[7179] | 123 | { |
---|
[7267] | 124 | for (unsigned int i = this->baseFunctor_->getParamCount(); i < functor->getParamCount(); ++i) |
---|
| 125 | { |
---|
[7214] | 126 | if (!this->executor_->defaultValueSet(i)) |
---|
[7267] | 127 | { |
---|
| 128 | COUT(2) << "Default value " << i << " is missing" << std::endl; |
---|
[7214] | 129 | return false; |
---|
[7267] | 130 | } |
---|
| 131 | } |
---|
[7214] | 132 | |
---|
[7185] | 133 | return true; |
---|
[7179] | 134 | } |
---|
[7214] | 135 | } |
---|
[7185] | 136 | |
---|
[7236] | 137 | bool ConsoleCommand::headersMatch(const ExecutorPtr& executor) |
---|
[7214] | 138 | { |
---|
[7267] | 139 | unsigned int minparams = std::min(this->baseFunctor_->getParamCount(), executor->getParamCount()); |
---|
[7214] | 140 | |
---|
[7267] | 141 | if (this->baseFunctor_->getHeaderIdentifier(minparams) != executor->getFunctor()->getHeaderIdentifier(minparams)) |
---|
[7214] | 142 | return false; |
---|
[7267] | 143 | else if (executor->getParamCount() <= this->baseFunctor_->getParamCount()) |
---|
[7214] | 144 | return true; |
---|
| 145 | else |
---|
[7179] | 146 | { |
---|
[7267] | 147 | for (unsigned int i = this->baseFunctor_->getParamCount(); i < executor->getParamCount(); ++i) |
---|
| 148 | { |
---|
[7214] | 149 | if (!executor->defaultValueSet(i)) |
---|
[7267] | 150 | { |
---|
| 151 | COUT(2) << "Default value " << i << " is missing" << std::endl; |
---|
[7214] | 152 | return false; |
---|
[7267] | 153 | } |
---|
| 154 | } |
---|
[7214] | 155 | |
---|
| 156 | return true; |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | |
---|
[7236] | 160 | bool ConsoleCommand::setFunction(const ExecutorPtr& executor, bool bForce) |
---|
[7214] | 161 | { |
---|
| 162 | if (!executor || !executor->getFunctor() || bForce || this->headersMatch(executor)) |
---|
| 163 | { |
---|
| 164 | this->executor_ = executor; |
---|
| 165 | return true; |
---|
| 166 | } |
---|
| 167 | else |
---|
| 168 | { |
---|
| 169 | COUT(1) << "Error: Couldn't assign new executor to console command \"" << this->baseName_ << "\", headers don't match." << std::endl; |
---|
[7185] | 170 | return false; |
---|
[7179] | 171 | } |
---|
[7214] | 172 | } |
---|
[7185] | 173 | |
---|
[7236] | 174 | bool ConsoleCommand::setFunction(const FunctorPtr& functor, bool bForce) |
---|
[7214] | 175 | { |
---|
| 176 | if (!functor || bForce || this->headersMatch(functor)) |
---|
| 177 | { |
---|
| 178 | if (this->executor_) |
---|
| 179 | this->executor_->setFunctor(functor); |
---|
[7218] | 180 | else if (functor) |
---|
[7214] | 181 | this->executor_ = createExecutor(functor); |
---|
| 182 | |
---|
| 183 | return true; |
---|
| 184 | } |
---|
| 185 | else |
---|
| 186 | { |
---|
| 187 | COUT(1) << "Error: Couldn't assign new functor to console command \"" << this->baseName_ << "\", headers don't match." << std::endl; |
---|
| 188 | return false; |
---|
| 189 | } |
---|
[7179] | 190 | } |
---|
| 191 | |
---|
[7236] | 192 | void ConsoleCommand::pushFunction(const ExecutorPtr& executor, bool bForce) |
---|
[7179] | 193 | { |
---|
[7214] | 194 | Command command; |
---|
[7270] | 195 | command.executor_ = this->executor_; |
---|
[7214] | 196 | if (command.executor_) |
---|
[7270] | 197 | command.functor_ = this->executor_->getFunctor(); |
---|
[7185] | 198 | |
---|
[7214] | 199 | if (this->setFunction(executor, bForce)) |
---|
| 200 | this->commandStack_.push(command); |
---|
[7179] | 201 | } |
---|
| 202 | |
---|
[7236] | 203 | void ConsoleCommand::pushFunction(const FunctorPtr& functor, bool bForce) |
---|
[7179] | 204 | { |
---|
[7214] | 205 | Command command; |
---|
[7270] | 206 | command.executor_ = this->executor_; |
---|
[7214] | 207 | if (command.executor_) |
---|
[7270] | 208 | command.functor_ = this->executor_->getFunctor(); |
---|
[7214] | 209 | |
---|
| 210 | if (this->setFunction(functor, bForce)) |
---|
| 211 | this->commandStack_.push(command); |
---|
[7185] | 212 | } |
---|
[7179] | 213 | |
---|
[7236] | 214 | void ConsoleCommand::pushFunction() |
---|
[7185] | 215 | { |
---|
[7214] | 216 | if (this->executor_) |
---|
| 217 | this->pushFunction(new Executor(*this->executor_.get())); |
---|
| 218 | else |
---|
| 219 | COUT(1) << "Error: Couldn't push copy of executor in console command \"" << this->baseName_ << "\", no executor set." << std::endl; |
---|
[7179] | 220 | } |
---|
| 221 | |
---|
[7236] | 222 | void ConsoleCommand::popFunction() |
---|
[7179] | 223 | { |
---|
[7214] | 224 | Command command; |
---|
| 225 | if (!this->commandStack_.empty()) |
---|
[7179] | 226 | { |
---|
[7214] | 227 | command = this->commandStack_.top(); |
---|
| 228 | this->commandStack_.pop(); |
---|
[7179] | 229 | } |
---|
[7214] | 230 | |
---|
| 231 | this->executor_ = command.executor_; |
---|
| 232 | if (command.executor_) |
---|
| 233 | this->executor_->setFunctor(command.functor_); |
---|
[7179] | 234 | } |
---|
| 235 | |
---|
[7236] | 236 | void ConsoleCommand::resetFunction() |
---|
[7218] | 237 | { |
---|
| 238 | if (this->executor_) |
---|
| 239 | this->executor_->setFunctor(0); |
---|
| 240 | } |
---|
| 241 | |
---|
[7236] | 242 | const ExecutorPtr& ConsoleCommand::getExecutor() const |
---|
[7179] | 243 | { |
---|
[7214] | 244 | return this->executor_; |
---|
[7179] | 245 | } |
---|
| 246 | |
---|
[7236] | 247 | bool ConsoleCommand::setObject(void* object) |
---|
[7214] | 248 | { |
---|
| 249 | if (this->executor_) |
---|
[7185] | 250 | { |
---|
[7214] | 251 | if (this->executor_->getFunctor()) |
---|
| 252 | { |
---|
| 253 | this->executor_->getFunctor()->setRawObjectPointer(object); |
---|
| 254 | return true; |
---|
| 255 | } |
---|
| 256 | else if (object) |
---|
| 257 | COUT(1) << "Error: Can't assign object to console command \"" << this->baseName_ << "\", no functor set." << std::endl; |
---|
[7185] | 258 | } |
---|
[7214] | 259 | else if (object) |
---|
| 260 | COUT(1) << "Error: Can't assign object to console command \"" << this->baseName_ << "\", no executor set." << std::endl; |
---|
| 261 | |
---|
| 262 | return false; |
---|
[7179] | 263 | } |
---|
| 264 | |
---|
[7236] | 265 | void ConsoleCommand::pushObject(void* object) |
---|
[7214] | 266 | { |
---|
| 267 | void* oldobject = this->getObject(); |
---|
| 268 | if (this->setObject(object)) |
---|
| 269 | this->objectStack_.push(oldobject); |
---|
| 270 | } |
---|
| 271 | |
---|
[7236] | 272 | void ConsoleCommand::popObject() |
---|
[7185] | 273 | { |
---|
| 274 | void* newobject = 0; |
---|
| 275 | if (!this->objectStack_.empty()) |
---|
| 276 | { |
---|
| 277 | newobject = this->objectStack_.top(); |
---|
| 278 | this->objectStack_.pop(); |
---|
| 279 | } |
---|
| 280 | this->setObject(newobject); |
---|
| 281 | } |
---|
| 282 | |
---|
[7236] | 283 | void* ConsoleCommand::getObject() const |
---|
[7185] | 284 | { |
---|
[7214] | 285 | if (this->executor_ && this->executor_->getFunctor()) |
---|
| 286 | return this->executor_->getFunctor()->getRawObjectPointer(); |
---|
[7185] | 287 | else |
---|
| 288 | return 0; |
---|
| 289 | } |
---|
| 290 | |
---|
[7236] | 291 | ConsoleCommand& ConsoleCommand::defaultValues(const MultiType& param1) |
---|
[7215] | 292 | { |
---|
| 293 | if (this->executor_) |
---|
| 294 | this->executor_->setDefaultValues(param1); |
---|
| 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 | |
---|
[7236] | 301 | ConsoleCommand& ConsoleCommand::defaultValues(const MultiType& param1, const MultiType& param2) |
---|
[7215] | 302 | { |
---|
| 303 | if (this->executor_) |
---|
| 304 | this->executor_->setDefaultValues(param1, param2); |
---|
| 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 | |
---|
[7236] | 311 | ConsoleCommand& ConsoleCommand::defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3) |
---|
[7215] | 312 | { |
---|
| 313 | if (this->executor_) |
---|
| 314 | this->executor_->setDefaultValues(param1, param2, param3); |
---|
| 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 | |
---|
[7236] | 321 | ConsoleCommand& ConsoleCommand::defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4) |
---|
[7215] | 322 | { |
---|
| 323 | if (this->executor_) |
---|
| 324 | this->executor_->setDefaultValues(param1, param2, param3, param4); |
---|
| 325 | else |
---|
| 326 | COUT(1) << "Error: Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << std::endl; |
---|
| 327 | |
---|
| 328 | return *this; |
---|
| 329 | } |
---|
| 330 | |
---|
[7236] | 331 | ConsoleCommand& ConsoleCommand::defaultValues(const MultiType& param1, const MultiType& param2, const MultiType& param3, const MultiType& param4, const MultiType& param5) |
---|
[7215] | 332 | { |
---|
| 333 | if (this->executor_) |
---|
| 334 | this->executor_->setDefaultValues(param1, param2, param3, param4, param5); |
---|
| 335 | else |
---|
| 336 | COUT(1) << "Error: Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << std::endl; |
---|
| 337 | |
---|
| 338 | return *this; |
---|
| 339 | } |
---|
| 340 | |
---|
[7236] | 341 | ConsoleCommand& ConsoleCommand::defaultValue(unsigned int index, const MultiType& param) |
---|
[7215] | 342 | { |
---|
| 343 | if (this->executor_) |
---|
| 344 | this->executor_->setDefaultValue(index, param); |
---|
| 345 | else |
---|
| 346 | COUT(1) << "Error: Can't set default values in console command \"" << this->baseName_ << "\", no executor set." << std::endl; |
---|
| 347 | |
---|
| 348 | return *this; |
---|
| 349 | } |
---|
| 350 | |
---|
[7236] | 351 | ConsoleCommand& ConsoleCommand::argumentCompleter(unsigned int param, ArgumentCompleter* completer) |
---|
[7215] | 352 | { |
---|
| 353 | if (param < 5) |
---|
| 354 | this->argumentCompleter_[param] = completer; |
---|
| 355 | else |
---|
| 356 | COUT(2) << "Warning: Couldn't add autocompletion-function for param " << param << " in console command \"" << this->baseName_ << "\": index out of bound." << std::endl; |
---|
| 357 | |
---|
| 358 | return *this; |
---|
| 359 | } |
---|
| 360 | |
---|
[7236] | 361 | ArgumentCompleter* ConsoleCommand::getArgumentCompleter(unsigned int param) const |
---|
[7215] | 362 | { |
---|
| 363 | if (param < 5) |
---|
| 364 | return this->argumentCompleter_[param]; |
---|
| 365 | else |
---|
| 366 | return 0; |
---|
| 367 | } |
---|
| 368 | |
---|
[7236] | 369 | ConsoleCommand& ConsoleCommand::description(const std::string& description) |
---|
[7215] | 370 | { |
---|
| 371 | this->description_ = std::string("ConsoleCommandDescription::" + this->baseName_ + "::function"); |
---|
| 372 | AddLanguageEntry(this->description_, description); |
---|
| 373 | return *this; |
---|
| 374 | } |
---|
| 375 | |
---|
[7236] | 376 | const std::string& ConsoleCommand::getDescription() const |
---|
[7215] | 377 | { |
---|
| 378 | return GetLocalisation_noerror(this->description_); |
---|
| 379 | } |
---|
| 380 | |
---|
[7236] | 381 | ConsoleCommand& ConsoleCommand::descriptionParam(unsigned int param, const std::string& description) |
---|
[7215] | 382 | { |
---|
| 383 | if (param < MAX_FUNCTOR_ARGUMENTS) |
---|
| 384 | { |
---|
| 385 | this->descriptionParam_[param] = std::string("ConsoleCommandDescription::" + this->baseName_ + "::param" + multi_cast<std::string>(param)); |
---|
| 386 | AddLanguageEntry(this->descriptionParam_[param], description); |
---|
| 387 | } |
---|
| 388 | return *this; |
---|
| 389 | } |
---|
| 390 | |
---|
[7236] | 391 | const std::string& ConsoleCommand::getDescriptionParam(unsigned int param) const |
---|
[7215] | 392 | { |
---|
| 393 | if (param < MAX_FUNCTOR_ARGUMENTS) |
---|
| 394 | return GetLocalisation_noerror(this->descriptionParam_[param]); |
---|
| 395 | |
---|
| 396 | return this->descriptionParam_[0]; |
---|
| 397 | } |
---|
| 398 | |
---|
[7236] | 399 | ConsoleCommand& ConsoleCommand::descriptionReturnvalue(const std::string& description) |
---|
[7215] | 400 | { |
---|
| 401 | this->descriptionReturnvalue_ = std::string("ConsoleCommandDescription::" + this->baseName_ + "::returnvalue"); |
---|
| 402 | AddLanguageEntry(this->descriptionReturnvalue_, description); |
---|
| 403 | return *this; |
---|
| 404 | } |
---|
| 405 | |
---|
[7236] | 406 | const std::string& ConsoleCommand::getDescriptionReturnvalue(int param) const |
---|
[7215] | 407 | { |
---|
| 408 | return GetLocalisation_noerror(this->descriptionReturnvalue_); |
---|
| 409 | } |
---|
| 410 | |
---|
[7236] | 411 | /* static */ ConsoleCommand* ConsoleCommand::getCommand(const std::string& group, const std::string& name, bool bPrintError) |
---|
[7179] | 412 | { |
---|
[7236] | 413 | std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommand::getCommandMap().find(group); |
---|
| 414 | if (it_group != ConsoleCommand::getCommandMap().end()) |
---|
[7179] | 415 | { |
---|
[7236] | 416 | std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.find(name); |
---|
[7179] | 417 | if (it_name != it_group->second.end()) |
---|
| 418 | { |
---|
| 419 | return it_name->second; |
---|
| 420 | } |
---|
| 421 | } |
---|
| 422 | if (bPrintError) |
---|
| 423 | { |
---|
| 424 | if (group == "") |
---|
[7198] | 425 | COUT(1) << "Error: Couldn't find console command with shortcut \"" << name << "\"" << std::endl; |
---|
[7179] | 426 | else |
---|
[7198] | 427 | COUT(1) << "Error: Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << std::endl; |
---|
[7179] | 428 | } |
---|
| 429 | return 0; |
---|
| 430 | } |
---|
| 431 | |
---|
[7236] | 432 | /* static */ ConsoleCommand* ConsoleCommand::getCommandLC(const std::string& group, const std::string& name, bool bPrintError) |
---|
[7228] | 433 | { |
---|
| 434 | std::string groupLC = getLowercase(group); |
---|
| 435 | std::string nameLC = getLowercase(name); |
---|
| 436 | |
---|
[7236] | 437 | std::map<std::string, std::map<std::string, ConsoleCommand*> >::const_iterator it_group = ConsoleCommand::getCommandMapLC().find(groupLC); |
---|
| 438 | if (it_group != ConsoleCommand::getCommandMapLC().end()) |
---|
[7228] | 439 | { |
---|
[7236] | 440 | std::map<std::string, ConsoleCommand*>::const_iterator it_name = it_group->second.find(nameLC); |
---|
[7228] | 441 | if (it_name != it_group->second.end()) |
---|
| 442 | { |
---|
| 443 | return it_name->second; |
---|
| 444 | } |
---|
| 445 | } |
---|
| 446 | if (bPrintError) |
---|
| 447 | { |
---|
| 448 | if (group == "") |
---|
| 449 | COUT(1) << "Error: Couldn't find console command with shortcut \"" << name << "\"" << std::endl; |
---|
| 450 | else |
---|
| 451 | COUT(1) << "Error: Couldn't find console command with group \"" << group << "\" and name \"" << name << "\"" << std::endl; |
---|
| 452 | } |
---|
| 453 | return 0; |
---|
| 454 | } |
---|
| 455 | |
---|
[7236] | 456 | /* static */ std::map<std::string, std::map<std::string, ConsoleCommand*> >& ConsoleCommand::getCommandMap() |
---|
[7179] | 457 | { |
---|
[7236] | 458 | static std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMap; |
---|
[7179] | 459 | return commandMap; |
---|
| 460 | } |
---|
| 461 | |
---|
[7236] | 462 | /* static */ std::map<std::string, std::map<std::string, ConsoleCommand*> >& ConsoleCommand::getCommandMapLC() |
---|
[7228] | 463 | { |
---|
[7236] | 464 | static std::map<std::string, std::map<std::string, ConsoleCommand*> > commandMapLC; |
---|
[7228] | 465 | return commandMapLC; |
---|
| 466 | } |
---|
| 467 | |
---|
[7236] | 468 | /* static */ void ConsoleCommand::registerCommand(const std::string& group, const std::string& name, ConsoleCommand* command) |
---|
[7179] | 469 | { |
---|
| 470 | if (name == "") |
---|
| 471 | return; |
---|
| 472 | |
---|
[7236] | 473 | if (ConsoleCommand::getCommand(group, name) != 0) |
---|
[7179] | 474 | { |
---|
| 475 | if (group == "") |
---|
[7214] | 476 | COUT(2) << "Warning: A console command with shortcut \"" << name << "\" already exists." << std::endl; |
---|
[7179] | 477 | else |
---|
[7214] | 478 | COUT(2) << "Warning: A console command with name \"" << name << "\" already exists in group \"" << group << "\"." << std::endl; |
---|
[7179] | 479 | } |
---|
| 480 | else |
---|
| 481 | { |
---|
[7236] | 482 | ConsoleCommand::getCommandMap()[group][name] = command; |
---|
| 483 | ConsoleCommand::getCommandMapLC()[getLowercase(group)][getLowercase(name)] = command; |
---|
[7179] | 484 | } |
---|
| 485 | } |
---|
[7214] | 486 | |
---|
[7236] | 487 | /* static */ void ConsoleCommand::unregisterCommand(ConsoleCommand* command) |
---|
[7214] | 488 | { |
---|
[7236] | 489 | for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = ConsoleCommand::getCommandMap().begin(); it_group != ConsoleCommand::getCommandMap().end(); ) |
---|
[7214] | 490 | { |
---|
[7236] | 491 | for (std::map<std::string, ConsoleCommand*>::iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); ) |
---|
[7214] | 492 | { |
---|
| 493 | if (it_name->second == command) |
---|
| 494 | it_group->second.erase(it_name++); |
---|
| 495 | else |
---|
| 496 | ++it_name; |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | if (it_group->second.empty()) |
---|
[7236] | 500 | ConsoleCommand::getCommandMap().erase(it_group++); |
---|
[7214] | 501 | else |
---|
| 502 | ++it_group; |
---|
| 503 | } |
---|
[7228] | 504 | |
---|
[7236] | 505 | for (std::map<std::string, std::map<std::string, ConsoleCommand*> >::iterator it_group = ConsoleCommand::getCommandMapLC().begin(); it_group != ConsoleCommand::getCommandMapLC().end(); ) |
---|
[7228] | 506 | { |
---|
[7236] | 507 | for (std::map<std::string, ConsoleCommand*>::iterator it_name = it_group->second.begin(); it_name != it_group->second.end(); ) |
---|
[7228] | 508 | { |
---|
| 509 | if (it_name->second == command) |
---|
| 510 | it_group->second.erase(it_name++); |
---|
| 511 | else |
---|
| 512 | ++it_name; |
---|
| 513 | } |
---|
| 514 | |
---|
| 515 | if (it_group->second.empty()) |
---|
[7236] | 516 | ConsoleCommand::getCommandMapLC().erase(it_group++); |
---|
[7228] | 517 | else |
---|
| 518 | ++it_group; |
---|
| 519 | } |
---|
[7214] | 520 | } |
---|
[7216] | 521 | |
---|
[7236] | 522 | /* static */ void ConsoleCommand::destroyAll() |
---|
[7216] | 523 | { |
---|
[7236] | 524 | while (!ConsoleCommand::getCommandMap().empty() && !ConsoleCommand::getCommandMap().begin()->second.empty()) |
---|
| 525 | delete ConsoleCommand::getCommandMap().begin()->second.begin()->second; |
---|
[7216] | 526 | } |
---|
[7179] | 527 | } |
---|