Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Command.cpp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*!
  2. * \file src/commands/Command.cpp
  3. * \brief Commands
  4. *
  5. * \author xythobuz
  6. */
  7. #include <iomanip>
  8. #include "global.h"
  9. #include "Console.h"
  10. #include "commands/Command.h"
  11. #include "commands/CommandAnimate.h"
  12. #include "commands/CommandBind.h"
  13. #include "commands/CommandEngine.h"
  14. #include "commands/CommandGame.h"
  15. #include "commands/CommandMove.h"
  16. #include "commands/CommandRender.h"
  17. #include "commands/CommandSet.h"
  18. #include "commands/CommandSound.h"
  19. std::vector<std::shared_ptr<Command>> Command::commands;
  20. Command::~Command() {
  21. }
  22. void Command::printHelp() {
  23. getConsole() << "No help available!" << Console::endl;
  24. }
  25. void Command::fillCommandList() {
  26. commands.clear();
  27. commands.push_back(std::shared_ptr<Command>(new CommandLoad()));
  28. commands.push_back(std::shared_ptr<Command>(new CommandBind()));
  29. commands.push_back(std::shared_ptr<Command>(new CommandSet()));
  30. commands.push_back(std::shared_ptr<Command>(new CommandScreenshot()));
  31. commands.push_back(std::shared_ptr<Command>(new CommandAnimate()));
  32. commands.push_back(std::shared_ptr<Command>(new CommandMove()));
  33. commands.push_back(std::shared_ptr<Command>(new CommandMode()));
  34. commands.push_back(std::shared_ptr<Command>(new CommandRenderflag()));
  35. commands.push_back(std::shared_ptr<Command>(new CommandSound()));
  36. commands.push_back(std::shared_ptr<Command>(new CommandPos()));
  37. commands.push_back(std::shared_ptr<Command>(new CommandViewmodel()));
  38. commands.push_back(std::shared_ptr<Command>(new CommandPigtail()));
  39. commands.push_back(std::shared_ptr<Command>(new CommandPonypos()));
  40. commands.push_back(std::shared_ptr<Command>(new CommandQuit()));
  41. }
  42. int Command::command(std::string c) {
  43. assert(commands.size() > 0);
  44. // Remove comment, if any
  45. size_t comment = c.find_first_of('#');
  46. if (comment != std::string::npos)
  47. c.erase(comment);
  48. // Execute command
  49. std::stringstream command(c);
  50. std::string cmd;
  51. command >> cmd;
  52. command >> std::boolalpha >> std::ws;
  53. if (cmd.length() == 0)
  54. return 0;
  55. // Print help
  56. if (cmd.compare("help") == 0) {
  57. std::string arg;
  58. command >> arg;
  59. if (arg.length() == 0) {
  60. // List all available commands
  61. getConsole() << "Available commands:" << Console::endl;
  62. getConsole() << std::right << std::setw(11);
  63. getConsole() << "help" << " - print command help" << Console::endl;
  64. for (auto &x : commands) {
  65. if (x) {
  66. getConsole() << std::right << std::setw(11);
  67. getConsole() << x->name() << " - " << x->brief() << Console::endl;
  68. }
  69. }
  70. getConsole() << "Use help COMMAND to get additional info" << Console::endl;
  71. getConsole() << "Pass BOOLs as true or false" << Console::endl;
  72. return 0;
  73. } else {
  74. // Show help for a specific command
  75. for (auto &x : commands) {
  76. if (x) {
  77. if (x->name().compare(arg) == 0) {
  78. x->printHelp();
  79. return 0;
  80. }
  81. }
  82. }
  83. getConsole() << "Unknown command: \"" << arg << "\"" << Console::endl;
  84. return -1;
  85. }
  86. }
  87. // Execute command
  88. for (auto &x : commands) {
  89. if (x) {
  90. if (x->name().compare(cmd) == 0) {
  91. return x->execute(command);
  92. }
  93. }
  94. }
  95. getConsole() << "Unknown command: \"" << cmd << "\"" << Console::endl;
  96. return -1;
  97. }