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.1KB

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