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 4.1KB

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