Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Command.cpp 5.1KB

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