Commands.cpp 748 B

123456789101112131415161718192021222324252627282930
  1. #include "server/commands/Commands.h"
  2. #include "server/commands/DefaultCommands.h"
  3. #include "server/commands/SnuviCommands.h"
  4. #include "utils/HashMap.h"
  5. static HashMap<Commands::Name, Commands::Command> commands;
  6. void Commands::init() {
  7. DefaultCommands::init();
  8. SnuviCommands::init();
  9. }
  10. void Commands::add(const Name& name, Command command) {
  11. commands.add(name, command);
  12. }
  13. void Commands::execute(const Raw& raw) {
  14. Arguments args(raw);
  15. if(args.getLength() == 0) {
  16. std::cout << "Invalid command syntax: '" << raw << "'\n";
  17. return;
  18. }
  19. Name command(args[0]);
  20. Command* c = commands.search(command);
  21. if(c == nullptr) {
  22. SnuviCommands::callEvent(args);
  23. return;
  24. }
  25. (*c)(args);
  26. }