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.

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