ConfigCmdManagerCommands.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <OgreRenderWindow.h>
  17. #include <OgreRoot.h>
  18. #include <OgreStringConverter.h>
  19. #include "Console.h"
  20. #include "ConfigCmdManager.h"
  21. #include "ConfigVarManager.h"
  22. #include "EntityManager.h"
  23. #include "Logger.h"
  24. #include "XmlMapFile.h"
  25. #include "XmlMapsFile.h"
  26. #include "QGearsGameState.h"
  27. #include "common/QGearsApplication.h"
  28. /**
  29. * Command to quit the application.
  30. *
  31. * @param params[in] Command parameters. Ignored.
  32. */
  33. void CmdQuit(const Ogre::StringVector& params){
  34. QGears::g_ApplicationState = QGears::G_EXIT;
  35. }
  36. /**
  37. * Command to print to console.
  38. *
  39. * @param params[in] Command parameters. All of them will be concatenated and
  40. * printed. If none are supplied, a command usage text will be printed instead.
  41. */
  42. void CmdEcho(const Ogre::StringVector& params){
  43. if (params.size() < 1){
  44. Console::getSingleton().AddTextToOutput(
  45. "Usage: /echo <string to output>"
  46. );
  47. return;
  48. }
  49. Ogre::String text = "";
  50. for (size_t i = 1; i < params.size(); ++ i){
  51. if (i != 1)text += " ";
  52. text += params[i];
  53. }
  54. Console::getSingleton().AddTextToOutput(text + "\n");
  55. }
  56. /**
  57. * Searches variables in the variable list and prints them.
  58. *
  59. * @param params[in] Command parameters. The first one is the command name. If
  60. * no more are passed, all variables will be printed. If another parameter is
  61. * passed, the variables with that name (if any) will be printed. If more than
  62. * two parameter are passed, a command usage string will be printed instead.
  63. */
  64. void CmdConfigVarList(const Ogre::StringVector& params){
  65. if (params.size() > 2){
  66. Console::getSingleton().AddTextToOutput(
  67. "Usage: /config_var_list [search string]"
  68. );
  69. return;
  70. }
  71. int number = 0;
  72. int num_vars = ConfigVarManager::getSingleton().GetConfigVarNumber();
  73. for (int i = 0; i < num_vars; ++ i){
  74. ConfigVar* var = ConfigVarManager::getSingleton().GetConfigVar(i);
  75. Ogre::String name = var->GetName();
  76. if (params.size() > 1){
  77. int found = name.find(params[1]);
  78. if (found == 0){
  79. Console::getSingleton().AddTextToOutput(
  80. var->GetName() + " = \"" + var->GetS() + "\""
  81. );
  82. ++number;
  83. }
  84. }
  85. else{
  86. Console::getSingleton().AddTextToOutput(
  87. var->GetName() + " = \"" + var->GetS() + "\""
  88. );
  89. ++number;
  90. }
  91. }
  92. Console::getSingleton().AddTextToOutput(
  93. Ogre::StringConverter::toString(number) + " config variables.\n"
  94. );
  95. }
  96. /**
  97. * Searches the command list and prints the comands.
  98. *
  99. * @param params[in] Command parameters. The first one is the command name. If
  100. * no more is passed, all commands will be printed. If another parameter is
  101. * passed, the command with that name (if any) will be printed. If more than
  102. * two parameter are passed, a command usage string will be printed instead.
  103. */
  104. void CmdConfigCmdList(const Ogre::StringVector& params){
  105. if (params.size() > 2){
  106. Console::getSingleton().AddTextToOutput(
  107. "Usage: /config_cmd_list [search string]"
  108. );
  109. return;
  110. }
  111. int number = 0;
  112. int num_cmds = ConfigCmdManager::getSingleton().GetConfigCmdNumber();
  113. for (int i = 0; i < num_cmds; ++ i){
  114. ConfigCmd* cmd = ConfigCmdManager::getSingleton().GetConfigCmd(i);
  115. Ogre::String name = cmd->GetName();
  116. if (params.size() > 1){
  117. int found = name.find(params[1]);
  118. if (found == 0){
  119. Console::getSingleton().AddTextToOutput(cmd->GetName());
  120. ++number;
  121. }
  122. }
  123. else{
  124. Console::getSingleton().AddTextToOutput(cmd->GetName());
  125. ++number;
  126. }
  127. }
  128. Console::getSingleton().AddTextToOutput(
  129. Ogre::StringConverter::toString(number) + " config commands.\n"
  130. );
  131. }
  132. /**
  133. * Sets the value of a configuration value.
  134. *
  135. * @param params[in] Command parameters. The first one is the command name. The
  136. * second one is a variable name. The third one is optional and is a value for
  137. * the variable. If a value is supplied, the variable will be given that value.
  138. * If not, the variable will be reset to it's default value. If there is no
  139. * variable by that name, nothing will be done. In any case, a feddback will be
  140. * printed to console. If less than two or more than three parameters are
  141. * passed, a usage text wil be printed and nothing will be done.
  142. */
  143. void CmdSetConfigVar(const Ogre::StringVector& params)
  144. {
  145. if (params.size() < 2 || params.size() > 3){
  146. Console::getSingleton().AddTextToOutput(
  147. "Usage: /set <config variable> [value]"
  148. );
  149. return;
  150. }
  151. Ogre::String name = params[1];
  152. ConfigVar* cvar = ConfigVarManager::getSingleton().Find(name);
  153. if (cvar == NULL){
  154. LOG_ERROR("Config variable \"" + name + "\" not found.");
  155. return;
  156. }
  157. if (params.size() == 3){
  158. cvar->SetS(params[2]);
  159. Console* console = Console::getSingletonPtr();
  160. if (console != NULL)
  161. LOG_TRIVIAL(params[1] + " changed to \"" + params[2] + "\".");
  162. }
  163. else{
  164. // Reset to default
  165. cvar->SetS(cvar->GetDefaultValue());
  166. LOG_TRIVIAL(
  167. params[1] + " changed to default \""
  168. + cvar->GetDefaultValue() + "\"."
  169. );
  170. }
  171. }
  172. /**
  173. * Changes the value of a configuration value conditionally.
  174. *
  175. * @param params[in] Command parameters. The first one is the command name. The
  176. * next ones are possible values for the variables. If the value of the
  177. * variable is the current one, the next one will be assigned. Once the value
  178. * is changed once, no more steps will be taken and the function will return.
  179. * If the last provided value is the current value of the variable, it will not
  180. * be changed.
  181. */
  182. void CmdToggleConfigVar(const Ogre::StringVector& params){
  183. if (params.size() < 4){
  184. Console::getSingleton().AddTextToOutput(
  185. "Usage: /toggle <config variable> [value1] [value2] ...");
  186. return;
  187. }
  188. Ogre::String name = params[1];
  189. ConfigVar* cvar = ConfigVarManager::getSingleton().Find(name);
  190. if (cvar == NULL){
  191. LOG_ERROR("Config variable \"" + name + "\" not found.");
  192. return;
  193. }
  194. // Sequentially trigger values
  195. int number_of_values = params.size() - 2;
  196. Ogre::String value = cvar->GetS();
  197. int i = 0;
  198. for (; i < number_of_values; ++ i){
  199. if (value == params[i + 2]){
  200. ++ i;
  201. break;
  202. }
  203. }
  204. if (i == number_of_values) i = 0;
  205. cvar->SetS(params[i + 2]);
  206. }
  207. /**
  208. * Increments the value of a configuration variable.
  209. *
  210. * @param params[in] Command parameters. Exactly five must be provided. The
  211. * first one is the command name. The second one is the variable to increment.
  212. * The third one is the minimum value the variable will take. The fourth one is
  213. * the maximum value the variable will take. The fifth value is the increment
  214. * to apply to the variable. The variable in the second parameter will be
  215. * incremented by the value in the fith one, but it will be capped between the
  216. * third and fourth one. If there are more or less than five parameters, a
  217. * usage text will be printed and nothing will be done.
  218. */
  219. void CmdIncrementConfigVar(const Ogre::StringVector& params){
  220. if (params.size() != 5){
  221. Console::getSingleton().AddTextToOutput(
  222. "Usage: /increment <cvar name> [value min] [value max] [step]"
  223. );
  224. return;
  225. }
  226. Ogre::String name = params[1];
  227. ConfigVar* cvar = ConfigVarManager::getSingleton().Find(name);
  228. if (cvar == NULL){
  229. LOG_ERROR("Config variable \"" + name + "\" not found.");
  230. return;
  231. }
  232. float start_value = Ogre::StringConverter::parseReal(params[2]);
  233. float end_value = Ogre::StringConverter::parseReal(params[3]);
  234. float step = Ogre::StringConverter::parseReal(params[4]);
  235. float new_value = cvar->GetF() + step;
  236. if (new_value > end_value) new_value = end_value;
  237. else if (new_value < start_value) new_value = start_value;
  238. cvar->SetF(new_value);
  239. }
  240. /**
  241. * Configures the log level.
  242. *
  243. * @param params[in] Command parameters. Exactly two must be provided. The
  244. * first one is the command name. The second one is the log level. Accepted
  245. * values are 1 (only errors), 2 (errors and warnings) and 3 (all). If there
  246. * are more or less than five parameters, a usage text will be printed and
  247. * nothing will be done.
  248. */
  249. void CmdSetLogLevel(const Ogre::StringVector& params){
  250. if (params.size() != 2){
  251. Console::getSingleton().AddTextToOutput(
  252. "Usage: /log_level "
  253. "<level: 1 - only errors, 2 - errors and warnings, 3 - all>"
  254. );
  255. return;
  256. }
  257. int value = Ogre::StringConverter::parseInt(params[1]);
  258. if (value > 0 && value < 4){
  259. Ogre::LogManager::getSingletonPtr()->getDefaultLog()->setLogDetail(
  260. (Ogre::LoggingLevel) value
  261. );
  262. switch(value){
  263. case 1:
  264. Console::getSingleton().AddTextToOutput(
  265. "Logger level changed to \"only errors\".\n"
  266. );
  267. break;
  268. case 2:
  269. Console::getSingleton().AddTextToOutput(
  270. "Logger level changed to \"errors and warnings\".\n"
  271. );
  272. break;
  273. case 3:
  274. Console::getSingleton().AddTextToOutput(
  275. "Logger level changed to \"all\".\n"
  276. );
  277. break;
  278. }
  279. }
  280. else{
  281. Console::getSingleton().AddTextToOutput(
  282. "Logger level can't be changed. Value \""
  283. + params[1] + "\" isn't supported.\n"
  284. );
  285. }
  286. }
  287. /**
  288. * Changes the game map.
  289. *
  290. * @param params[in] Command parameters. Exactly two must be provided. The
  291. * first one is the command name. The second one is the map ID. If there
  292. * are more or less than five parameters, a usage text will be printed and
  293. * nothing will be done.
  294. */
  295. void CmdMap(const Ogre::StringVector& params){
  296. if (params.size() != 2){
  297. Console::getSingleton().AddTextToOutput("Usage: /map [map_id]");
  298. return;
  299. }
  300. EntityManager::getSingleton().Clear();
  301. XmlMapsFile xml("./data/maps.xml");
  302. Ogre::String file_name = xml.GetMapFileNameByName(params[1]);
  303. XmlMapFile xml_map("./data/" + file_name);
  304. xml_map.LoadMap();
  305. }
  306. /**
  307. * Loads a list of map names.
  308. *
  309. * @param complete_params[in] The map names will be loaded here.
  310. */
  311. void CmdMapCompletion(Ogre::StringVector& complete_params){
  312. XmlMapsFile xml("./data/maps.xml");
  313. xml.GetMapNames(complete_params);
  314. }
  315. /**
  316. * Sets the resolution and full screen mode.
  317. *
  318. * @param params[in] Command parameters. Three or four must be provided. The
  319. * first one is the command name. The second one is the resolution width. The
  320. * third one is the resolution height. The fourth one is optional and can be
  321. * used to toggle the full screen. "true", "yes" or 1 will set the game in full
  322. * screen mode. Anything else will set it to windowed mode.
  323. */
  324. void CmdResolution(const Ogre::StringVector& params){
  325. if (params.size() < 3){
  326. Console::getSingleton().AddTextToOutput(
  327. "Usage: /resolution <width> <height> [full screen]"
  328. );
  329. return;
  330. }
  331. Ogre::RenderWindow* window
  332. = QGears::Application::getSingleton().getRenderWindow();
  333. if (params.size() >= 4){
  334. window->setFullscreen(
  335. Ogre::StringConverter::parseBool(params[3]),
  336. Ogre::StringConverter::parseInt(params[1]),
  337. Ogre::StringConverter::parseInt(params[2])
  338. );
  339. }
  340. else{
  341. window->resize(
  342. Ogre::StringConverter::parseInt(params[1]),
  343. Ogre::StringConverter::parseInt(params[2])
  344. );
  345. }
  346. }
  347. /**
  348. * Loads a list of resolution modes.
  349. *
  350. * A resolution mode is represented by a string with the format "[w] [h] [f]",
  351. * where [w] is the resolution width, in pixels, [h] is the resolution height,
  352. * in pixels and [f] is the full screen state (0 for windowed mode, 1 for full
  353. * screen)
  354. *
  355. * @param complete_params[in] The resolution modes will be loaded here.
  356. */
  357. void CmdResolutionCompletition(Ogre::StringVector& complete_params){
  358. complete_params.push_back("640 480 0");
  359. complete_params.push_back("640 480 1");
  360. complete_params.push_back("800 600 0");
  361. complete_params.push_back("800 600 1");
  362. complete_params.push_back("1024 768 0");
  363. complete_params.push_back("1024 768 1");
  364. complete_params.push_back("1280 720 0");
  365. complete_params.push_back("1280 720 1");
  366. complete_params.push_back("1280 1024 0");
  367. complete_params.push_back("1280 1024 1");
  368. }
  369. /**
  370. * Saves a screenshot of the current game window.
  371. *
  372. * @param params Command parameters. Unused.
  373. */
  374. void CmdScreenshot(const Ogre::StringVector& params){
  375. Ogre::RenderWindow* window
  376. = QGears::Application::getSingleton().getRenderWindow();
  377. Ogre::String ret
  378. = window->writeContentsToTimestampedFile("screenshot_", ".tga");
  379. Console::getSingleton().AddTextToOutput("Screenshot " + ret + " saved.");
  380. }
  381. /*
  382. void CmdViewer(const Ogre::StringVector& params){
  383. if (params.size() == 2 || params.size() > 3){
  384. Console::getSingleton().AddTextToOutput(
  385. "Usage: /viewer [type_of_thing_to_view] [path_to_resource]"
  386. );
  387. return;
  388. }
  389. ModuleManager::getSingleton().RunViewer();
  390. if (params.size() == 3){
  391. ViewerModule* module
  392. = (ViewerModule*)(ModuleManager::getSingleton().GetTopModule());
  393. if (params[1] == "walkmesh") module->SetWalkmeshToLoad(params[2]);
  394. else if (params[1] == "model") module->SetModelToLoad(params[2]);
  395. else{
  396. LOG_ERROR(
  397. "Unsupported type \"" + params[1] + "\" in viewer command."
  398. );
  399. }
  400. }
  401. }
  402. void CmdViewerCompletion(Ogre::StringVector& complete_params){
  403. // models
  404. complete_params.push_back("model");
  405. Ogre::FileInfoListPtr resources
  406. = Ogre::ResourceGroupManager::getSingleton().listResourceFileInfo("Game");
  407. Ogre::FileInfoList resource_names = *resources;
  408. Ogre::FileInfoList::iterator i = resource_names.begin();
  409. for (; i != resource_names.end(); ++ i){
  410. Ogre::String name;
  411. Ogre::String ext;
  412. Ogre::StringUtil::splitBaseFilename(i->filename, name, ext);
  413. if (ext == "mesh") complete_params.push_back("model " + i->filename);
  414. }
  415. // walkmeshes
  416. complete_params.push_back("walkmesh");
  417. XmlMapsFile xml("./data/game_data/maps.xml");
  418. Ogre::StringVector tmp;
  419. xml.GetMapNames(tmp);
  420. for (int i = 0; i < tmp.size(); ++i)
  421. complete_params.push_back("walkmesh " + tmp[i]);
  422. }
  423. */
  424. /**
  425. * Initializes all available commands.
  426. */
  427. void ConfigCmdManager::InitCmd(){
  428. AddCommand("quit", "Stops application and quit", "", CmdQuit, NULL);
  429. AddCommand("echo", "Print command parameters", "", CmdEcho, NULL);
  430. AddCommand(
  431. "config_var_list", "List of registered config variables",
  432. "[<filter substring>]", CmdConfigVarList, NULL
  433. );
  434. AddCommand(
  435. "config_cmd_list", "List of registered config commands",
  436. "[<filter substring>]", CmdConfigCmdList, NULL
  437. );
  438. AddCommand(
  439. "set", "Set cvar value", "<cvar name> [value]", CmdSetConfigVar, NULL
  440. );
  441. AddCommand(
  442. "toggle", "Toggle cvar value",
  443. "<cvar name> [value1] [value2] ...", CmdToggleConfigVar, NULL
  444. );
  445. AddCommand(
  446. "increment",
  447. "Increment cvar value", "<cvar name> [value min] [value max] [step]",
  448. CmdIncrementConfigVar, NULL
  449. );
  450. AddCommand(
  451. "set_log_level", "Set log messages level", "", CmdSetLogLevel, NULL
  452. );
  453. AddCommand("map", "Run game module", "", CmdMap, CmdMapCompletion);
  454. AddCommand(
  455. "resolution", "Change resolution", "",
  456. CmdResolution, CmdResolutionCompletition
  457. );
  458. AddCommand(
  459. "screenshot", "Capture current screen content", "", CmdScreenshot, NULL
  460. );
  461. //AddCommand(
  462. // "viewer", "Run viewer module", "", CmdViewer, CmdViewerCompletion
  463. //);
  464. }