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.

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