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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. std::vector<std::shared_ptr<Command>> Command::commands;
  21. Command::~Command() {
  22. }
  23. void Command::printHelp() {
  24. getLog() << "No help available!" << Log::endl;
  25. }
  26. void Command::fillCommandList() {
  27. commands.clear();
  28. commands.push_back(std::shared_ptr<Command>(new CommandLoad()));
  29. commands.push_back(std::shared_ptr<Command>(new CommandBind()));
  30. commands.push_back(std::shared_ptr<Command>(new CommandSet()));
  31. commands.push_back(std::shared_ptr<Command>(new CommandGet()));
  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 CommandPos()));
  38. commands.push_back(std::shared_ptr<Command>(new CommandViewmodel()));
  39. commands.push_back(std::shared_ptr<Command>(new CommandPigtail()));
  40. commands.push_back(std::shared_ptr<Command>(new CommandPonypos()));
  41. commands.push_back(std::shared_ptr<Command>(new CommandQuit()));
  42. }
  43. int Command::command(std::string c) {
  44. assert(commands.size() > 0);
  45. // Remove comment, if any
  46. size_t comment = c.find_first_of('#');
  47. if (comment != std::string::npos)
  48. c.erase(comment);
  49. // Execute command
  50. std::stringstream command(c);
  51. std::string cmd;
  52. command >> cmd;
  53. command >> std::boolalpha >> std::ws;
  54. if (cmd.length() == 0)
  55. return 0;
  56. // Print help
  57. if (cmd == "help") {
  58. std::string arg;
  59. command >> arg;
  60. if (arg.length() == 0) {
  61. // List all available commands
  62. getLog() << "Available commands:" << Log::endl;
  63. getLog() << std::right << std::setw(11);
  64. getLog() << "help" << " - print command help" << Log::endl;
  65. for (auto& x : commands) {
  66. if (x) {
  67. getLog() << std::right << std::setw(11);
  68. getLog() << x->name() << " - " << x->brief() << Log::endl;
  69. }
  70. }
  71. getLog() << "Use help COMMAND to get additional info" << Log::endl;
  72. getLog() << "Pass BOOLs as true or false" << Log::endl;
  73. return 0;
  74. } else {
  75. // Show help for a specific command
  76. for (auto& x : commands) {
  77. if (x) {
  78. if (x->name() == arg) {
  79. x->printHelp();
  80. return 0;
  81. }
  82. }
  83. }
  84. getLog() << "Unknown command: \"" << arg << "\"" << Log::endl;
  85. return -1;
  86. }
  87. }
  88. // Execute command
  89. for (auto& x : commands) {
  90. if (x) {
  91. if (x->name() == cmd) {
  92. return x->execute(command);
  93. }
  94. }
  95. }
  96. getLog() << "Unknown command: \"" << cmd << "\"" << Log::endl;
  97. return -1;
  98. }
  99. int Command::executeFile(std::string file) {
  100. std::string configFile = expandHomeDirectory(file);
  101. getLog() << "Loading config from \"" << configFile << "\"..." << Log::endl;
  102. std::ifstream f(configFile);
  103. if (!f) {
  104. getLog() << "Could not open file!" << Log::endl;
  105. return -1;
  106. }
  107. for (std::string line; std::getline(f, line);) {
  108. if (line.length() == 0)
  109. continue;
  110. int error = Command::command(line);
  111. if (error != 0)
  112. getLog() << "Error Code: " << error << Log::endl;
  113. }
  114. f.close();
  115. return 0;
  116. }
  117. std::string Command::autoComplete(std::string begin) {
  118. std::vector<std::string> candidates;
  119. std::string help("help");
  120. if (begin.size() <= help.size()) {
  121. if (begin.compare(0, begin.size(), help, 0, begin.size()) == 0) {
  122. candidates.push_back(help);
  123. }
  124. }
  125. for (auto& x : commands) {
  126. if (x) {
  127. std::string name = x->name();
  128. if (begin.size() <= name.size()) {
  129. if (begin.compare(0, begin.size(), name, 0, begin.size()) == 0) {
  130. candidates.push_back(name);
  131. }
  132. }
  133. }
  134. }
  135. if (candidates.size() == 0) {
  136. return "";
  137. } else if (candidates.size() == 1) {
  138. return candidates.at(0);
  139. } else {
  140. std::string common = candidates.at(0);
  141. for (int i = 0; i < candidates.size(); i++) {
  142. getLog() << candidates.at(i);
  143. if (i < (candidates.size() - 1))
  144. getLog() << "/";
  145. for (int c = 0; (c < common.size()) && (c < candidates.at(i).size()); c++) {
  146. if (common.at(c) != candidates.at(i).at(c)) {
  147. common.erase(c);
  148. break;
  149. }
  150. }
  151. }
  152. getLog() << Log::endl;
  153. return common;
  154. }
  155. }