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.

CommandSet.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*!
  2. * \file src/commands/CommandSet.cpp
  3. * \brief Set command
  4. *
  5. * \author xythobuz
  6. */
  7. #include "global.h"
  8. #include "Camera.h"
  9. #include "Console.h"
  10. #include "Font.h"
  11. #include "OpenRaider.h"
  12. #include "Sound.h"
  13. #include "Window.h"
  14. #include "utils/strings.h"
  15. #include "commands/CommandSet.h"
  16. std::string CommandSet::name() {
  17. return "set";
  18. }
  19. std::string CommandSet::brief() {
  20. return "set a parameter";
  21. }
  22. void CommandSet::printHelp() {
  23. getConsole() << "set-Command Usage:" << Console::endl;
  24. getConsole() << " set VAR VAL" << Console::endl;
  25. getConsole() << "Available Variables:" << Console::endl;
  26. getConsole() << " basedir STRING" << Console::endl;
  27. getConsole() << " pakdir STRING" << Console::endl;
  28. getConsole() << " audiodir STRING" << Console::endl;
  29. getConsole() << " datadir STRING" << Console::endl;
  30. getConsole() << " font STRING" << Console::endl;
  31. getConsole() << " size INT INT" << Console::endl;
  32. getConsole() << " fullscreen BOOL" << Console::endl;
  33. getConsole() << " audio BOOL" << Console::endl;
  34. getConsole() << " volume BOOL" << Console::endl;
  35. getConsole() << " mouse_x FLOAT" << Console::endl;
  36. getConsole() << " mouse_y FLOAT" << Console::endl;
  37. getConsole() << " fps BOOL" << Console::endl;
  38. getConsole() << "Enclose STRINGs with \"\"!" << Console::endl;
  39. }
  40. namespace {
  41. char *expandDirectoryNames(const char *s) {
  42. assert(s != NULL);
  43. assert(s[0] != '\0');
  44. char *result = bufferString("%s", s);
  45. if (getOpenRaider().mPakDir != NULL) {
  46. char *tmp = stringReplace(s, "$(pakdir)", getOpenRaider().mPakDir);
  47. delete [] result;
  48. result = tmp;
  49. }
  50. if (getOpenRaider().mAudioDir != NULL) {
  51. char *tmp = stringReplace(s, "$(audiodir)", getOpenRaider().mAudioDir);
  52. delete [] result;
  53. result = tmp;
  54. }
  55. if (getOpenRaider().mDataDir != NULL) {
  56. char *tmp = stringReplace(s, "$(datadir)", getOpenRaider().mDataDir);
  57. delete [] result;
  58. result = tmp;
  59. }
  60. if (getOpenRaider().mBaseDir != NULL) {
  61. char *tmp = stringReplace(result, "$(basedir)", getOpenRaider().mBaseDir);
  62. delete [] result;
  63. result = tmp;
  64. }
  65. return result;
  66. }
  67. }
  68. #define CHANGE_DIR_WITH_EXPANSION(a) do { \
  69. std::string temp; \
  70. args >> temp; \
  71. const char *value = temp.c_str(); \
  72. char *quotes = stringRemoveQuotes(value); \
  73. char *tmp = expandDirectoryNames(quotes); \
  74. a = fullPath(tmp, 0); \
  75. delete [] tmp; \
  76. delete [] quotes; \
  77. } while(false)
  78. int CommandSet::execute(std::istream& args) {
  79. std::string var;
  80. args >> var;
  81. if (var.compare("size") == 0) {
  82. unsigned int w = DEFAULT_WIDTH, h = DEFAULT_HEIGHT;
  83. if (!(args >> w >> h)) {
  84. getConsole() << "set-size-Error: Invalid value(s)" << Console::endl;
  85. return -2;
  86. }
  87. getWindow().setSize(w, h);
  88. } else if (var.compare("fullscreen") == 0) {
  89. bool fullscreen = false;
  90. if (!(args >> fullscreen)) {
  91. getConsole() << "set-fullscreen-Error: Invalid value" << Console::endl;
  92. return -3;
  93. }
  94. getWindow().setFullscreen(fullscreen);
  95. } else if (var.compare("audio") == 0) {
  96. bool audio = false;
  97. if (!(args >> audio)) {
  98. getConsole() << "set-audio-Error: Invalid value" << Console::endl;
  99. return -4;
  100. }
  101. getSound().setEnabled(audio);
  102. } else if (var.compare("volume") == 0) {
  103. float vol = 1.0f;
  104. if (!(args >> vol)) {
  105. getConsole() << "set-volume-Error: Invalid value" << Console::endl;
  106. return -5;
  107. }
  108. getSound().setVolume(vol);
  109. } else if (var.compare("mouse_x") == 0) {
  110. float sense = 1.0f;
  111. if (!(args >> sense)) {
  112. getConsole() << "set-mouse_x-Error: Invalid value" << Console::endl;
  113. return -6;
  114. }
  115. getCamera().setSensitivityX(OR_DEG_TO_RAD(sense));
  116. } else if (var.compare("mouse_y") == 0) {
  117. float sense = 1.0f;
  118. if (!(args >> sense)) {
  119. getConsole() << "set-mouse_y-Error: Invalid value" << Console::endl;
  120. return -7;
  121. }
  122. getCamera().setSensitivityY(OR_DEG_TO_RAD(sense));
  123. } else if (var.compare("fps") == 0) {
  124. bool fps = false;
  125. if (!(args >> fps)) {
  126. getConsole() << "set-fps-Error: Invalid value" << Console::endl;
  127. return -8;
  128. }
  129. getOpenRaider().mFPS = fps;
  130. } else if (var.compare("basedir") == 0) {
  131. CHANGE_DIR_WITH_EXPANSION(getOpenRaider().mBaseDir);
  132. } else if (var.compare("pakdir") == 0) {
  133. CHANGE_DIR_WITH_EXPANSION(getOpenRaider().mPakDir);
  134. } else if (var.compare("audiodir") == 0) {
  135. CHANGE_DIR_WITH_EXPANSION(getOpenRaider().mAudioDir);
  136. } else if (var.compare("datadir") == 0) {
  137. CHANGE_DIR_WITH_EXPANSION(getOpenRaider().mDataDir);
  138. } else if (var.compare("font") == 0) {
  139. std::string temp;
  140. args >> temp;
  141. const char *value = temp.c_str();
  142. char *quotes = stringReplace(value, "\"", "");
  143. char *tmp = expandDirectoryNames(quotes);
  144. getFont().setFont(tmp);
  145. delete [] tmp;
  146. delete [] quotes;
  147. } else {
  148. getConsole() << "set-Error: Unknown variable (" << var.c_str() << ")" << Console::endl;
  149. return -1;
  150. }
  151. return 0;
  152. }