#include "ChatCommandExecutor.h" #include "Game.h" #include "SaveCommand.h" ChatCommandExecutor::ChatCommandExecutor() : ReferenceCounter() { knownCommands.add(new SaveCommand()); } bool ChatCommandExecutor::execute(Framework::Text line, Entity* zActor) { if (line.hatAt(0, "/")) { for (ChatCommand* command : knownCommands) { if (line.hatAt(0, Framework::Text("/") + command->getName())) { if (line.getLength() > command->getName().getLength() + 1 && !line.hatAt(command->getName().getLength() + 1, " ")) { continue; } // TODO: check security level Framework::RCArray params; int start = command->getName().getLength() + 2; bool escaped = 0; for (int i = command->getName().getLength() + 2; i < line.getLength(); i++) { if (line.hatAt(i, " ") && !escaped) { if (start < i) { if (line.hatAt(start, "'") && line.hatAt(i - 1, "'")) { params.add(line.getTeilText(start + 1, i - 1)); } else { params.add(line.getTeilText(start, i)); } } start = i + 1; } else if (line.hatAt(i, "'")) { escaped = !escaped; } } int index = 0; for (ChatCommandParameter* param : command->getParams()) { if (params.getEintragAnzahl() > index && param->isLegalValue(*params.z(index))) { index++; } else { if (param->isOptional() && zActor) { params.add(new Framework::Text( param->getDefaultValue(zActor)), index++); } else { Framework::Text error = "Illegal parameter at position "; error += (index + 1); error += ": "; if (params.getEintragAnzahl() > index) { error += *params.z(index); } else { error += "(no parameter was given)"; } error += "\n"; error += command->getHelp(); if (zActor) { Game::INSTANCE->zChat()->sendMessageTo( error, zActor, Chat::CHANNEL_ERROR); } else { std::cout << error << std::endl; } return true; } } } if (index != params.getEintragAnzahl()) { Framework::Text error = "Illegal number of parameters. " "First unknown parameter: "; error += *params.z(index); error += "\n"; error += command->getHelp(); if (zActor) { Game::INSTANCE->zChat()->sendMessageTo( error, zActor, Chat::CHANNEL_ERROR); } else { std::cout << error << std::endl; } } command->execute(params, zActor); return true; } } } return false; }