ConfigCmdHandlerCommands.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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 "EntityManager.h"
  21. #include "ScriptManager.h"
  22. #include "Logger.h"
  23. #include "XmlMapFile.h"
  24. #include "XmlMapsFile.h"
  25. #include "VGearsGameState.h"
  26. #include "common/VGearsApplication.h"
  27. #include "ConfigCmdHandler.h"
  28. #include "ConfigVarHandler.h"
  29. /**
  30. * Command to quit the application.
  31. *
  32. * @param[in] params Command parameters. Ignored.
  33. */
  34. void CmdQuit(const Ogre::StringVector& params){
  35. VGears::g_ApplicationState = VGears::G_EXIT;
  36. }
  37. /**
  38. * Command to print to console.
  39. *
  40. * @param[in] params Command parameters. All of them will be concatenated and printed. If none are
  41. * supplied, a command usage text will be printed instead.
  42. */
  43. void CmdEcho(const Ogre::StringVector& params){
  44. if (params.size() < 1){
  45. Console::getSingleton().AddTextToOutput(
  46. "Usage: /echo <string to output>"
  47. );
  48. return;
  49. }
  50. Ogre::String text = "";
  51. for (size_t i = 1; i < params.size(); ++ i){
  52. if (i != 1)text += " ";
  53. text += params[i];
  54. }
  55. Console::getSingleton().AddTextToOutput(text + "\n");
  56. }
  57. /**
  58. * Searches variables in the variable list and prints them.
  59. *
  60. * @param[in] params Command parameters. The first one is the command name. If no more are passed,
  61. * all variables will be printed. If another parameter is passed, the variables with that name (if
  62. * any) will be printed. If more than two parameter are passed, a command usage string will be
  63. * printed instead.
  64. */
  65. void CmdConfigVarList(const Ogre::StringVector& params){
  66. if (params.size() > 2){
  67. Console::getSingleton().AddTextToOutput("Usage: /config_var_list [search string]");
  68. return;
  69. }
  70. int number = 0;
  71. int num_vars = ConfigVarHandler::getSingleton().GetConfigVarNumber();
  72. for (int i = 0; i < num_vars; ++ i){
  73. ConfigVar* var = ConfigVarHandler::getSingleton().GetConfigVar(i);
  74. Ogre::String name = var->GetName();
  75. if (params.size() > 1){
  76. int found = name.find(params[1]);
  77. if (found == 0){
  78. Console::getSingleton().AddTextToOutput(
  79. var->GetName() + " = \"" + var->GetS() + "\""
  80. );
  81. ++ number;
  82. }
  83. }
  84. else{
  85. Console::getSingleton().AddTextToOutput(var->GetName() + " = \"" + var->GetS() + "\"");
  86. ++ number;
  87. }
  88. }
  89. Console::getSingleton().AddTextToOutput(
  90. Ogre::StringConverter::toString(number) + " config variables.\n"
  91. );
  92. }
  93. /**
  94. * Searches the command list and prints the comands.
  95. *
  96. * @param[in] params Command parameters. The first one is the command name. If no more is passed,
  97. * all commands will be printed. If another parameter is passed, the command with that name (if
  98. * any) will be printed. If more than two parameter are passed, a command usage string will be
  99. * printed instead.
  100. */
  101. void CmdConfigCmdList(const Ogre::StringVector& params){
  102. if (params.size() > 2){
  103. Console::getSingleton().AddTextToOutput("Usage: /config_cmd_list [search string]");
  104. return;
  105. }
  106. int number = 0;
  107. int num_cmds = ConfigCmdHandler::getSingleton().GetConfigCmdNumber();
  108. for (int i = 0; i < num_cmds; ++ i){
  109. ConfigCmd* cmd = ConfigCmdHandler::getSingleton().GetConfigCmd(i);
  110. Ogre::String name = cmd->GetName();
  111. if (params.size() > 1){
  112. int found = name.find(params[1]);
  113. if (found == 0){
  114. Console::getSingleton().AddTextToOutput(cmd->GetName());
  115. ++ number;
  116. }
  117. }
  118. else{
  119. Console::getSingleton().AddTextToOutput(cmd->GetName());
  120. ++ number;
  121. }
  122. }
  123. Console::getSingleton().AddTextToOutput(
  124. Ogre::StringConverter::toString(number) + " config commands.\n"
  125. );
  126. }
  127. /**
  128. * Sets the value of a configuration value.
  129. *
  130. * @param[in] params Command parameters. The first one is the command name. The second one is a
  131. * variable name. The third one is optional and is a value for the variable. If a value is supplied,
  132. * the variable will be given that value. If not, the variable will be reset to it's default value.
  133. * If there is no variable by that name, nothing will be done. In any case, a feedback will be
  134. * printed to console. If less than two or more than three parameters are passed, a usage text will
  135. * be printed and nothing will be done.
  136. */
  137. void CmdSetConfigVar(const Ogre::StringVector& params){
  138. if (params.size() < 2 || params.size() > 3){
  139. Console::getSingleton().AddTextToOutput("Usage: /set <config variable> [value]");
  140. return;
  141. }
  142. Ogre::String name = params[1];
  143. ConfigVar* cvar = ConfigVarHandler::getSingleton().Find(name);
  144. if (cvar == NULL){
  145. LOG_ERROR("Config variable \"" + name + "\" not found.");
  146. return;
  147. }
  148. if (params.size() == 3){
  149. cvar->SetS(params[2]);
  150. Console* console = Console::getSingletonPtr();
  151. if (console != NULL) LOG_TRIVIAL(params[1] + " changed to \"" + params[2] + "\".");
  152. }
  153. else{
  154. // Reset to default
  155. cvar->SetS(cvar->GetDefaultValue());
  156. LOG_TRIVIAL(params[1] + " changed to default \"" + cvar->GetDefaultValue() + "\".");
  157. }
  158. }
  159. /**
  160. * Changes the value of a configuration value conditionally.
  161. *
  162. * @param[in] params Command parameters. The first one is the command name. The next ones are
  163. * possible values for the variables. If the value of the variable is the current one, the next one
  164. * will be assigned. Once the value is changed once, no more steps will be taken and the function
  165. * will return. If the last provided value is the current value of the variable, it will not be
  166. * changed.
  167. */
  168. void CmdToggleConfigVar(const Ogre::StringVector& params){
  169. if (params.size() < 4){
  170. Console::getSingleton().AddTextToOutput(
  171. "Usage: /toggle <config variable> [value1] [value2] ...");
  172. return;
  173. }
  174. Ogre::String name = params[1];
  175. ConfigVar* cvar = ConfigVarHandler::getSingleton().Find(name);
  176. if (cvar == NULL){
  177. LOG_ERROR("Config variable \"" + name + "\" not found.");
  178. return;
  179. }
  180. // Sequentially trigger values
  181. int number_of_values = params.size() - 2;
  182. Ogre::String value = cvar->GetS();
  183. int i = 0;
  184. for (; i < number_of_values; ++ i){
  185. if (value == params[i + 2]){
  186. ++ i;
  187. break;
  188. }
  189. }
  190. if (i == number_of_values) i = 0;
  191. cvar->SetS(params[i + 2]);
  192. }
  193. /**
  194. * Increments the value of a configuration variable.
  195. *
  196. * @param[in] params Command parameters. Exactly five must be provided. The first one is the command
  197. * name. The second one is the variable to increment. The third one is the minimum value the
  198. * variable will take. The fourth one is the maximum value the variable will take. The fifth value
  199. * is the increment to apply to the variable. The variable in the second parameter will be
  200. * incremented by the value in the fith one, but it will be capped between the third and fourth one.
  201. * If there are more or less than five parameters, a usage text will be printed and nothing will be
  202. * done.
  203. */
  204. void CmdIncrementConfigVar(const Ogre::StringVector& params){
  205. if (params.size() != 5){
  206. Console::getSingleton().AddTextToOutput(
  207. "Usage: /increment <cvar name> [value min] [value max] [step]"
  208. );
  209. return;
  210. }
  211. Ogre::String name = params[1];
  212. ConfigVar* cvar = ConfigVarHandler::getSingleton().Find(name);
  213. if (cvar == NULL){
  214. LOG_ERROR("Config variable \"" + name + "\" not found.");
  215. return;
  216. }
  217. float start_value = Ogre::StringConverter::parseReal(params[2]);
  218. float end_value = Ogre::StringConverter::parseReal(params[3]);
  219. float step = Ogre::StringConverter::parseReal(params[4]);
  220. float new_value = cvar->GetF() + step;
  221. if (new_value > end_value) new_value = end_value;
  222. else if (new_value < start_value) new_value = start_value;
  223. cvar->SetF(new_value);
  224. }
  225. /**
  226. * Configures the log level.
  227. *
  228. * @param[in] params Command parameters. Exactly two must be provided. The first one is the command
  229. * name. The second one is the log level. Accepted values are 1 (only errors), 2 (errors and
  230. * warnings) and 3 (all). If there are more or less than five parameters, a usage text will be
  231. * printed and nothing will be done.
  232. */
  233. void CmdSetLogLevel(const Ogre::StringVector& params){
  234. if (params.size() != 2){
  235. Console::getSingleton().AddTextToOutput(
  236. "Usage: /log_level <level: 1 - only errors, 2 - errors and warnings, 3 - all>"
  237. );
  238. return;
  239. }
  240. int value = Ogre::StringConverter::parseInt(params[1]);
  241. if (value > 0 && value < 4){
  242. Ogre::LogManager::getSingletonPtr()->getDefaultLog()->setLogDetail(
  243. (Ogre::LoggingLevel) value
  244. );
  245. switch(value){
  246. case 1:
  247. Console::getSingleton().AddTextToOutput(
  248. "Logger level changed to \"only errors\".\n"
  249. );
  250. break;
  251. case 2:
  252. Console::getSingleton().AddTextToOutput(
  253. "Logger level changed to \"errors and warnings\".\n"
  254. );
  255. break;
  256. case 3:
  257. Console::getSingleton().AddTextToOutput("Logger level changed to \"all\".\n");
  258. break;
  259. }
  260. }
  261. else{
  262. Console::getSingleton().AddTextToOutput(
  263. "Logger level can't be changed. Value \""
  264. + params[1] + "\" isn't supported.\n"
  265. );
  266. }
  267. }
  268. /**
  269. * Changes the game map.
  270. *
  271. * @param[in] params Command parameters. Exactly two must be provided. The first one is the command
  272. * name. The second one is the map ID. If there are more or less than five parameters, a usage text
  273. * will be printed and nothing will be done.
  274. */
  275. void CmdMap(const Ogre::StringVector& params){
  276. if (params.size() != 2){
  277. Console::getSingleton().AddTextToOutput("Usage: /map [map_id]");
  278. return;
  279. }
  280. // The same as the map function of the scripts (the old list of maps, ./data/maps.xml, doesn't
  281. // exist: the fields are in ./data/fields/_fields.xml).
  282. ScriptManager::getSingleton().RunString("map(\"" + params[1] + "\")");
  283. }
  284. /**
  285. * Opens a shop, to test the shop menu by hand.
  286. *
  287. * @param[in] params Command parameters: the command name and the ID of the shop (0-79).
  288. */
  289. void CmdShop(const Ogre::StringVector& params){
  290. if (params.size() != 2){
  291. Console::getSingleton().AddTextToOutput("Usage: /shop <shop_id>");
  292. return;
  293. }
  294. // The scripts open shops with open_shop(), that waits for the menu to close and can only run
  295. // in a script. Showing the menu directly doesn't need that.
  296. ScriptManager::getSingleton().RunString(
  297. "UiContainer.ShopMenu.shop_id = tonumber(\"" + params[1] + "\") or -1; "
  298. "UiContainer.ShopMenu:show()"
  299. );
  300. }
  301. /**
  302. * Enables or disables characters in the PHS.
  303. *
  304. * @param[in] params Command parameters: the command name, the ID of the character (0-8) or "all", and
  305. * optionally "on" (the default) or "off".
  306. */
  307. void CmdPhs(const Ogre::StringVector& params){
  308. if (params.size() < 2 || params.size() > 3){
  309. Console::getSingleton().AddTextToOutput("Usage: /phs <character_id|all> [on|off]");
  310. return;
  311. }
  312. const bool enable = params.size() < 3 || (params[2] != "off" && params[2] != "0");
  313. const std::string value = enable ? "true" : "false";
  314. std::string first = "0";
  315. std::string last = "8";
  316. if (params[1] != "all"){
  317. first = params[1];
  318. last = params[1];
  319. }
  320. ScriptManager::getSingleton().RunString(
  321. "for id = (tonumber(\"" + first + "\") or -1), (tonumber(\"" + last + "\") or -2) do "
  322. "if type(Characters[id]) == \"table\" then Characters[id].enabled = " + value + " end end"
  323. );
  324. Console::getSingleton().AddTextToOutput(
  325. std::string(enable ? "Enabled " : "Disabled ") + (params[1] == "all" ? "all characters" : "character " + params[1]) + " in the PHS"
  326. );
  327. }
  328. /**
  329. * Loads a list of map names.
  330. *
  331. * @param[in] complete_params The map names will be loaded here.
  332. */
  333. void CmdMapCompletion(Ogre::StringVector& complete_params){
  334. XmlMapsFile xml("./data/fields/_fields.xml");
  335. xml.GetMapNames(complete_params);
  336. }
  337. /**
  338. * Sets the resolution and full screen mode.
  339. *
  340. * @param[in] params Command parameters. Three or four must be provided. The first one is the
  341. * command name. The second one is the resolution width. The third one is the resolution height. The
  342. * fourth one is optional and can be used to toggle the full screen. "true", "yes" or 1 will set the
  343. * game in full screen mode. Anything else will set it to windowed mode.
  344. */
  345. void CmdResolution(const Ogre::StringVector& params){
  346. if (params.size() < 3){
  347. Console::getSingleton().AddTextToOutput(
  348. "Usage: /resolution <width> <height> [full screen]"
  349. );
  350. return;
  351. }
  352. Ogre::RenderWindow* window = VGears::Application::getSingleton().getRenderWindow();
  353. if (params.size() >= 4){
  354. window->setFullscreen(
  355. Ogre::StringConverter::parseBool(params[3]), Ogre::StringConverter::parseInt(params[1]),
  356. Ogre::StringConverter::parseInt(params[2])
  357. );
  358. }
  359. else{
  360. window->resize(
  361. Ogre::StringConverter::parseInt(params[1]), Ogre::StringConverter::parseInt(params[2])
  362. );
  363. window->getViewport(0)->setDimensions(0.0f, 0.0f, 1.0f, 1.0f);
  364. }
  365. }
  366. /**
  367. * Loads a list of resolution modes.
  368. *
  369. * A resolution mode is represented by a string with the format "[w] [h] [f]", where [w] is the
  370. * resolution width, in pixels, [h] is the resolution height, in pixels and [f] is the full screen
  371. * state (0 for windowed mode, 1 for full screen)
  372. *
  373. * @param[in] complete_params The resolution modes will be loaded here.
  374. */
  375. void CmdResolutionCompletition(Ogre::StringVector& complete_params){
  376. complete_params.push_back("640 480 0");
  377. complete_params.push_back("640 480 1");
  378. complete_params.push_back("800 600 0");
  379. complete_params.push_back("800 600 1");
  380. complete_params.push_back("1024 768 0");
  381. complete_params.push_back("1024 768 1");
  382. complete_params.push_back("1280 720 0");
  383. complete_params.push_back("1280 720 1");
  384. complete_params.push_back("1280 1024 0");
  385. complete_params.push_back("1280 1024 1");
  386. }
  387. /**
  388. * Saves a screenshot of the current game window.
  389. *
  390. * @param[in] params Command parameters. Unused.
  391. */
  392. void CmdScreenshot(const Ogre::StringVector& params){
  393. Ogre::RenderWindow* window = VGears::Application::getSingleton().getRenderWindow();
  394. Ogre::String ret = window->writeContentsToTimestampedFile("screenshot_", ".png");
  395. Console::getSingleton().AddTextToOutput("Screenshot " + ret + " saved.");
  396. }
  397. /**
  398. * Starts a screen fade, like the FADE opcode does. It is meant to test the fades by hand.
  399. *
  400. * @param[in] params Command parameters: the command name, the fade type, red, green, blue and
  401. * speed.
  402. */
  403. void CmdFade(const Ogre::StringVector& params){
  404. if (params.size() != 6){
  405. Console::getSingleton().AddTextToOutput(
  406. "Usage: /fade <type 1-10> <red 0-255> <green 0-255> <blue 0-255> <speed>"
  407. );
  408. return;
  409. }
  410. EntityManager::getSingleton().GetScreenFade()->ScriptFade(
  411. Ogre::StringConverter::parseInt(params[1]), Ogre::StringConverter::parseInt(params[2]),
  412. Ogre::StringConverter::parseInt(params[3]), Ogre::StringConverter::parseInt(params[4]),
  413. Ogre::StringConverter::parseInt(params[5])
  414. );
  415. }
  416. /**
  417. * Starts a screen fade, like the NFADE opcode does. It is meant to test the fades by hand.
  418. *
  419. * @param[in] params Command parameters: the command name, the fade type (0 clears it, 11 adds
  420. * and 12 subtracts), red, green, blue and frames.
  421. */
  422. void CmdNFade(const Ogre::StringVector& params){
  423. if (params.size() != 6){
  424. Console::getSingleton().AddTextToOutput(
  425. "Usage: /nfade <type 0, 11 or 12> <red 0-255> <green 0-255> <blue 0-255> <frames>"
  426. );
  427. return;
  428. }
  429. EntityManager::getSingleton().GetScreenFade()->ScriptNFade(
  430. Ogre::StringConverter::parseInt(params[1]), Ogre::StringConverter::parseInt(params[2]),
  431. Ogre::StringConverter::parseInt(params[3]), Ogre::StringConverter::parseInt(params[4]),
  432. Ogre::StringConverter::parseInt(params[5])
  433. );
  434. }
  435. /*
  436. void CmdViewer(const Ogre::StringVector& params){
  437. if (params.size() == 2 || params.size() > 3){
  438. Console::getSingleton().AddTextToOutput(
  439. "Usage: /viewer [type_of_thing_to_view] [path_to_resource]"
  440. );
  441. return;
  442. }
  443. ModuleManager::getSingleton().RunViewer();
  444. if (params.size() == 3){
  445. ViewerModule* module
  446. = (ViewerModule*)(ModuleManager::getSingleton().GetTopModule());
  447. if (params[1] == "walkmesh") module->SetWalkmeshToLoad(params[2]);
  448. else if (params[1] == "model") module->SetModelToLoad(params[2]);
  449. else{
  450. LOG_ERROR(
  451. "Unsupported type \"" + params[1] + "\" in viewer command."
  452. );
  453. }
  454. }
  455. }
  456. void CmdViewerCompletion(Ogre::StringVector& complete_params){
  457. // models
  458. complete_params.push_back("model");
  459. Ogre::FileInfoListPtr resources
  460. = Ogre::ResourceGroupManager::getSingleton().listResourceFileInfo("Game");
  461. Ogre::FileInfoList resource_names = *resources;
  462. Ogre::FileInfoList::iterator i = resource_names.begin();
  463. for (; i != resource_names.end(); ++ i){
  464. Ogre::String name;
  465. Ogre::String ext;
  466. Ogre::StringUtil::splitBaseFilename(i->filename, name, ext);
  467. if (ext == "mesh") complete_params.push_back("model " + i->filename);
  468. }
  469. // walkmeshes
  470. complete_params.push_back("walkmesh");
  471. XmlMapsFile xml("./data/game_data/maps.xml");
  472. Ogre::StringVector tmp;
  473. xml.GetMapNames(tmp);
  474. for (int i = 0; i < tmp.size(); ++i)
  475. complete_params.push_back("walkmesh " + tmp[i]);
  476. }
  477. */
  478. /**
  479. * Initializes all available commands.
  480. */
  481. void ConfigCmdHandler::InitCmd(){
  482. AddCommand("quit", "Stops application and quit", "", CmdQuit, NULL);
  483. AddCommand("echo", "Print command parameters", "", CmdEcho, NULL);
  484. AddCommand(
  485. "config_var_list", "List of registered config variables", "[<filter substring>]",
  486. CmdConfigVarList, NULL
  487. );
  488. AddCommand(
  489. "config_cmd_list", "List of registered config commands",
  490. "[<filter substring>]", CmdConfigCmdList, NULL
  491. );
  492. AddCommand("set", "Set cvar value", "<cvar name> [value]", CmdSetConfigVar, NULL);
  493. AddCommand(
  494. "toggle", "Toggle cvar value", "<cvar name> [value1] [value2] ...", CmdToggleConfigVar, NULL
  495. );
  496. AddCommand(
  497. "increment", "Increment cvar value", "<cvar name> [value min] [value max] [step]",
  498. CmdIncrementConfigVar, NULL
  499. );
  500. AddCommand("set_log_level", "Set log messages level", "", CmdSetLogLevel, NULL);
  501. AddCommand("map", "Run game module", "", CmdMap, CmdMapCompletion);
  502. AddCommand("shop", "Open a shop", "<shop_id>", CmdShop, NULL);
  503. AddCommand("phs", "Enable or disable characters in the PHS", "<character_id|all> [on|off]", CmdPhs, NULL);
  504. AddCommand("resolution", "Change resolution", "", CmdResolution, CmdResolutionCompletition);
  505. AddCommand("screenshot", "Capture current screen content", "", CmdScreenshot, NULL);
  506. AddCommand("fade", "Start a screen fade", "<type> <r> <g> <b> <speed>", CmdFade, NULL);
  507. AddCommand("nfade", "Start a screen nfade", "<type> <r> <g> <b> <frames>", CmdNFade, NULL);
  508. //AddCommand(
  509. // "viewer", "Run viewer module", "", CmdViewer, CmdViewerCompletion
  510. //);
  511. }