ConfigCmdManagerCommands.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #include <OgreRenderWindow.h>
  2. #include <OgreRoot.h>
  3. #include <OgreStringConverter.h>
  4. #include "Console.h"
  5. #include "ConfigCmdManager.h"
  6. #include "ConfigVarManager.h"
  7. #include "EntityManager.h"
  8. #include "Logger.h"
  9. #include "XmlMapFile.h"
  10. #include "XmlMapsFile.h"
  11. #include "QGearsGameState.h"
  12. #include "common/QGearsApplication.h"
  13. void
  14. CmdQuit( const Ogre::StringVector& params )
  15. {
  16. QGears::g_ApplicationState = QGears::G_EXIT;
  17. }
  18. void
  19. CmdEcho( const Ogre::StringVector& params )
  20. {
  21. if( params.size() < 1 )
  22. {
  23. Console::getSingleton().AddTextToOutput( "Usage: /echo <string to output>" );
  24. return;
  25. }
  26. Ogre::String text = "";
  27. for( size_t i = 1; i < params.size(); ++i )
  28. {
  29. if( i != 1 )
  30. {
  31. text += " ";
  32. }
  33. text += params[ i ];
  34. }
  35. Console::getSingleton().AddTextToOutput( text + "\n" );
  36. }
  37. void
  38. CmdConfigVarList( const Ogre::StringVector& params )
  39. {
  40. if( params.size() > 2 )
  41. {
  42. Console::getSingleton().AddTextToOutput( "Usage: /config_var_list [search string]" );
  43. return;
  44. }
  45. int number = 0;
  46. int num_vars = ConfigVarManager::getSingleton().GetConfigVarNumber();
  47. for( int i = 0; i < num_vars; ++i )
  48. {
  49. ConfigVar* var = ConfigVarManager::getSingleton().GetConfigVar( i );
  50. Ogre::String name = var->GetName();
  51. if( params.size() > 1 )
  52. {
  53. int found = name.find( params[ 1 ] );
  54. if( found == 0 )
  55. {
  56. Console::getSingleton().AddTextToOutput( var->GetName() + " = \"" + var->GetS() + "\"" );
  57. ++number;
  58. }
  59. }
  60. else
  61. {
  62. Console::getSingleton().AddTextToOutput( var->GetName() + " = \"" + var->GetS() + "\"" );
  63. ++number;
  64. }
  65. }
  66. Console::getSingleton().AddTextToOutput( Ogre::StringConverter::toString(number) + " config variables.\n" );
  67. }
  68. void
  69. CmdConfigCmdList( const Ogre::StringVector& params )
  70. {
  71. if( params.size() > 2 )
  72. {
  73. Console::getSingleton().AddTextToOutput( "Usage: /config_cmd_list [search string]" );
  74. return;
  75. }
  76. int number = 0;
  77. int num_cmds = ConfigCmdManager::getSingleton().GetConfigCmdNumber();
  78. for( int i = 0; i < num_cmds; ++i )
  79. {
  80. ConfigCmd* cmd = ConfigCmdManager::getSingleton().GetConfigCmd( i );
  81. Ogre::String name = cmd->GetName();
  82. if( params.size() > 1 )
  83. {
  84. int found = name.find( params[ 1 ] );
  85. if( found == 0 )
  86. {
  87. Console::getSingleton().AddTextToOutput( cmd->GetName() );
  88. ++number;
  89. }
  90. }
  91. else
  92. {
  93. Console::getSingleton().AddTextToOutput( cmd->GetName() );
  94. ++number;
  95. }
  96. }
  97. Console::getSingleton().AddTextToOutput( Ogre::StringConverter::toString( number ) + " config commands.\n" );
  98. }
  99. // set cvar value
  100. void
  101. CmdSetConfigVar( const Ogre::StringVector& params )
  102. {
  103. if( params.size() < 2 || params.size() > 3 )
  104. {
  105. Console::getSingleton().AddTextToOutput( "Usage: /set <config variable> [value]" );
  106. return;
  107. }
  108. Ogre::String name = params[ 1 ];
  109. ConfigVar* cvar = ConfigVarManager::getSingleton().Find( name );
  110. if( cvar == NULL )
  111. {
  112. LOG_ERROR( "Config variable \"" + name + "\" not found." );
  113. return;
  114. }
  115. if( params.size() == 3 )
  116. {
  117. cvar->SetS( params[ 2 ] );
  118. Console* console = Console::getSingletonPtr();
  119. if( console != NULL )
  120. {
  121. LOG_TRIVIAL( params[ 1 ] + " changed to \"" + params[ 2 ] + "\"." );
  122. }
  123. }
  124. else
  125. {
  126. // reset to default
  127. cvar->SetS( cvar->GetDefaultValue() );
  128. LOG_TRIVIAL( params[ 1 ] + " changed to default \"" + cvar->GetDefaultValue() + "\"." );
  129. }
  130. }
  131. // toggle cvar value
  132. void
  133. CmdToggleConfigVar( const Ogre::StringVector& params )
  134. {
  135. if( params.size() < 4 )
  136. {
  137. Console::getSingleton().AddTextToOutput( "Usage: /toggle <config variable> [value1] [value2] ..." );
  138. return;
  139. }
  140. Ogre::String name = params[ 1 ];
  141. ConfigVar* cvar = ConfigVarManager::getSingleton().Find( name );
  142. if( cvar == NULL )
  143. {
  144. LOG_ERROR( "Config variable \"" + name + "\" not found." );
  145. return;
  146. }
  147. // sequentially trigger values
  148. int number_of_values = params.size() - 2;
  149. Ogre::String value = cvar->GetS();
  150. int i = 0;
  151. for( ; i < number_of_values; ++i )
  152. {
  153. if( value == params[ i + 2 ] )
  154. {
  155. ++i;
  156. break;
  157. }
  158. }
  159. if( i == number_of_values )
  160. {
  161. i = 0;
  162. }
  163. cvar->SetS( params[ i + 2 ] );
  164. }
  165. // increment cvar value
  166. void
  167. CmdIncrementConfigVar( const Ogre::StringVector& params )
  168. {
  169. if( params.size() != 5 )
  170. {
  171. Console::getSingleton().AddTextToOutput( "Usage: /increment <cvar name> [value min] [value max] [step]" );
  172. return;
  173. }
  174. Ogre::String name = params[ 1 ];
  175. ConfigVar* cvar = ConfigVarManager::getSingleton().Find( name );
  176. if( cvar == NULL )
  177. {
  178. LOG_ERROR( "Config variable \"" + name + "\" not found." );
  179. return;
  180. }
  181. float start_value = Ogre::StringConverter::parseReal( params[ 2 ] );
  182. float end_value = Ogre::StringConverter::parseReal( params[ 3 ] );
  183. float step = Ogre::StringConverter::parseReal( params[ 4 ] );
  184. float new_value = cvar->GetF() + step;
  185. if( new_value > end_value )
  186. {
  187. new_value = end_value;
  188. }
  189. else if( new_value < start_value )
  190. {
  191. new_value = start_value;
  192. }
  193. cvar->SetF( new_value );
  194. }
  195. void
  196. CmdSetLogLevel( const Ogre::StringVector& params )
  197. {
  198. if( params.size() != 2 )
  199. {
  200. Console::getSingleton().AddTextToOutput( "Usage: /log_level <level: 1 - only errors, 2 - errors and warnings, 3 - all>" );
  201. return;
  202. }
  203. int value = Ogre::StringConverter::parseInt( params[ 1 ] );
  204. if( value > 0 && value < 4 )
  205. {
  206. Ogre::LogManager::getSingletonPtr()->getDefaultLog()->setLogDetail( ( Ogre::LoggingLevel )value );
  207. switch( value )
  208. {
  209. case 1: Console::getSingleton().AddTextToOutput( "Logger level changed to \"only errors\".\n" ); break;
  210. case 2: Console::getSingleton().AddTextToOutput( "Logger level changed to \"errors and warnings\".\n" ); break;
  211. case 3: Console::getSingleton().AddTextToOutput( "Logger level changed to \"all\".\n" ); break;
  212. }
  213. }
  214. else
  215. {
  216. Console::getSingleton().AddTextToOutput( "Logger level can't be changed. Value \"" + params[ 1 ] + "\" isn't supported.\n" );
  217. }
  218. }
  219. void
  220. CmdMap( const Ogre::StringVector& params )
  221. {
  222. if( params.size() != 2 )
  223. {
  224. Console::getSingleton().AddTextToOutput( "Usage: /map [map_id]" );
  225. return;
  226. }
  227. EntityManager::getSingleton().Clear();
  228. XmlMapsFile xml( "./data/maps.xml" );
  229. Ogre::String file_name = xml.GetMapFileNameByName( params[ 1 ] );
  230. XmlMapFile xml_map( "./data/" + file_name );
  231. xml_map.LoadMap();
  232. }
  233. void
  234. CmdMapCompletion( Ogre::StringVector& complete_params )
  235. {
  236. XmlMapsFile xml( "./data/maps.xml" );
  237. xml.GetMapNames( complete_params );
  238. }
  239. void
  240. CmdResolution( const Ogre::StringVector& params )
  241. {
  242. if( params.size() < 3 )
  243. {
  244. Console::getSingleton().AddTextToOutput( "Usage: /resolution <width> <height> [full screen]" );
  245. return;
  246. }
  247. Ogre::RenderWindow* window = QGears::Application::getSingleton().getRenderWindow();
  248. if( params.size() >= 4 )
  249. {
  250. window->setFullscreen( Ogre::StringConverter::parseBool( params[ 3 ] ), Ogre::StringConverter::parseInt( params[ 1 ] ), Ogre::StringConverter::parseInt( params[ 2 ] ) );
  251. }
  252. else
  253. {
  254. window->resize( Ogre::StringConverter::parseInt( params[ 1 ] ), Ogre::StringConverter::parseInt( params[ 2 ] ) );
  255. }
  256. }
  257. void
  258. CmdResolutionCompletition( Ogre::StringVector& complete_params )
  259. {
  260. complete_params.push_back( "640 480 0" );
  261. complete_params.push_back( "640 480 1" );
  262. complete_params.push_back( "800 600 0" );
  263. complete_params.push_back( "800 600 1" );
  264. complete_params.push_back( "1024 768 0" );
  265. complete_params.push_back( "1024 768 1" );
  266. complete_params.push_back( "1280 720 0" );
  267. complete_params.push_back( "1280 720 1" );
  268. complete_params.push_back( "1280 1024 0" );
  269. complete_params.push_back( "1280 1024 1" );
  270. }
  271. void
  272. CmdScreenshot( const Ogre::StringVector& params )
  273. {
  274. Ogre::RenderWindow* window = QGears::Application::getSingleton().getRenderWindow();
  275. Ogre::String ret = window->writeContentsToTimestampedFile( "screenshot_", ".tga" );
  276. Console::getSingleton().AddTextToOutput( "Screenshot " + ret + " saved." );
  277. }
  278. /*
  279. void
  280. CmdViewer( const Ogre::StringVector& params )
  281. {
  282. if( params.size() == 2 || params.size() > 3 )
  283. {
  284. Console::getSingleton().AddTextToOutput( "Usage: /viewer [type_of_thing_to_view] [path_to_resource]" );
  285. return;
  286. }
  287. ModuleManager::getSingleton().RunViewer();
  288. if( params.size() == 3 )
  289. {
  290. ViewerModule* module = ( ViewerModule* )( ModuleManager::getSingleton().GetTopModule() );
  291. if( params[ 1 ] == "walkmesh" )
  292. {
  293. module->SetWalkmeshToLoad( params[ 2 ] );
  294. }
  295. else if( params[ 1 ] == "model" )
  296. {
  297. module->SetModelToLoad( params[ 2 ] );
  298. }
  299. else
  300. {
  301. LOG_ERROR( "Unsupported type \"" + params[ 1 ] + "\" in viewer command." );
  302. }
  303. }
  304. }
  305. void
  306. CmdViewerCompletion( Ogre::StringVector& complete_params )
  307. {
  308. // models
  309. complete_params.push_back( "model" );
  310. Ogre::FileInfoListPtr resources = Ogre::ResourceGroupManager::getSingleton().listResourceFileInfo( "Game" );
  311. Ogre::FileInfoList resource_names = *resources;
  312. Ogre::FileInfoList::iterator i = resource_names.begin();
  313. for( ; i != resource_names.end(); ++i )
  314. {
  315. Ogre::String name;
  316. Ogre::String ext;
  317. Ogre::StringUtil::splitBaseFilename( i->filename, name, ext );
  318. if( ext == "mesh" )
  319. {
  320. complete_params.push_back( "model " + i->filename );
  321. }
  322. }
  323. // walkmeshes
  324. complete_params.push_back( "walkmesh" );
  325. XmlMapsFile xml( "./data/game_data/maps.xml" );
  326. Ogre::StringVector tmp;
  327. xml.GetMapNames( tmp );
  328. for( int i = 0; i < tmp.size(); ++i )
  329. {
  330. complete_params.push_back( "walkmesh " + tmp[ i ] );
  331. }
  332. }
  333. */
  334. void
  335. ConfigCmdManager::InitCmd()
  336. {
  337. AddCommand( "quit", "Stops application and quit", "", CmdQuit, NULL );
  338. AddCommand( "echo", "Print command parameters", "", CmdEcho, NULL );
  339. AddCommand( "config_var_list", "List of registered config variables", "[<filter substring>]", CmdConfigVarList, NULL );
  340. AddCommand( "config_cmd_list", "List of registered config commands", "[<filter substring>]", CmdConfigCmdList, NULL );
  341. AddCommand( "set", "Set cvar value", "<cvar name> [value]", CmdSetConfigVar, NULL );
  342. AddCommand( "toggle", "Toggle cvar value", "<cvar name> [value1] [value2] ...", CmdToggleConfigVar, NULL );
  343. AddCommand( "increment", "Increment cvar value", "<cvar name> [value min] [value max] [step]", CmdIncrementConfigVar, NULL );
  344. AddCommand( "set_log_level", "Set log messages level", "", CmdSetLogLevel, NULL );
  345. AddCommand( "map", "Run game module", "", CmdMap, CmdMapCompletion );
  346. AddCommand( "resolution", "Change resolution", "", CmdResolution, CmdResolutionCompletition );
  347. AddCommand( "screenshot", "Capture current screen content", "", CmdScreenshot, NULL );
  348. //AddCommand( "viewer", "Run viewer module", "", CmdViewer, CmdViewerCompletion );
  349. }