CmdSystem.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code 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. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. /*
  23. ===============================================================================
  24. idCmdSystemLocal
  25. ===============================================================================
  26. */
  27. typedef struct commandDef_s {
  28. struct commandDef_s * next;
  29. char * name;
  30. cmdFunction_t function;
  31. argCompletion_t argCompletion;
  32. int flags;
  33. char * description;
  34. } commandDef_t;
  35. class idCmdSystemLocal : public idCmdSystem {
  36. public:
  37. virtual void Init( void );
  38. virtual void Shutdown( void );
  39. virtual void AddCommand( const char *cmdName, cmdFunction_t function, int flags, const char *description, argCompletion_t argCompletion = NULL );
  40. virtual void RemoveCommand( const char *cmdName );
  41. virtual void RemoveFlaggedCommands( int flags );
  42. virtual void CommandCompletion( void(*callback)( const char *s ) );
  43. virtual void ArgCompletion( const char *cmdString, void(*callback)( const char *s ) );
  44. virtual void BufferCommandText( cmdExecution_t exec, const char *text );
  45. virtual void ExecuteCommandBuffer( void );
  46. virtual void ArgCompletion_FolderExtension( const idCmdArgs &args, void(*callback)( const char *s ), const char *folder, bool stripFolder, ... );
  47. virtual void ArgCompletion_DeclName( const idCmdArgs &args, void(*callback)( const char *s ), int type );
  48. virtual void BufferCommandArgs( cmdExecution_t exec, const idCmdArgs &args );
  49. virtual void SetupReloadEngine( const idCmdArgs &args );
  50. virtual bool PostReloadEngine( void );
  51. void SetWait( int numFrames ) { wait = numFrames; }
  52. commandDef_t * GetCommands( void ) const { return commands; }
  53. private:
  54. static const int MAX_CMD_BUFFER = 0x10000;
  55. commandDef_t * commands;
  56. int wait;
  57. int textLength;
  58. byte textBuf[MAX_CMD_BUFFER];
  59. idStr completionString;
  60. idStrList completionParms;
  61. // piggybacks on the text buffer, avoids tokenize again and screwing it up
  62. idList<idCmdArgs> tokenizedCmds;
  63. // a command stored to be executed after a reloadEngine and all associated commands have been processed
  64. idCmdArgs postReload;
  65. private:
  66. void ExecuteTokenizedString( const idCmdArgs &args );
  67. void ExecuteCommandText( const char *text );
  68. void InsertCommandText( const char *text );
  69. void AppendCommandText( const char *text );
  70. static void ListByFlags( const idCmdArgs &args, cmdFlags_t flags );
  71. static void List_f( const idCmdArgs &args );
  72. static void SystemList_f( const idCmdArgs &args );
  73. static void RendererList_f( const idCmdArgs &args );
  74. static void SoundList_f( const idCmdArgs &args );
  75. static void GameList_f( const idCmdArgs &args );
  76. static void ToolList_f( const idCmdArgs &args );
  77. static void Exec_f( const idCmdArgs &args );
  78. static void Vstr_f( const idCmdArgs &args );
  79. static void Echo_f( const idCmdArgs &args );
  80. static void Parse_f( const idCmdArgs &args );
  81. static void Wait_f( const idCmdArgs &args );
  82. static void PrintMemInfo_f( const idCmdArgs &args );
  83. };
  84. idCmdSystemLocal cmdSystemLocal;
  85. idCmdSystem * cmdSystem = &cmdSystemLocal;
  86. /*
  87. ============
  88. idCmdSystemLocal::ListByFlags
  89. ============
  90. */
  91. // NOTE: the const wonkyness is required to make msvc happy
  92. template<>
  93. ID_INLINE int idListSortCompare( const commandDef_t * const *a, const commandDef_t * const *b ) {
  94. return idStr::Icmp( (*a)->name, (*b)->name );
  95. }
  96. void idCmdSystemLocal::ListByFlags( const idCmdArgs &args, cmdFlags_t flags ) {
  97. int i;
  98. idStr match;
  99. const commandDef_t *cmd;
  100. idList<const commandDef_t *> cmdList;
  101. if ( args.Argc() > 1 ) {
  102. match = args.Args( 1, -1 );
  103. match.Replace( " ", "" );
  104. } else {
  105. match = "";
  106. }
  107. for ( cmd = cmdSystemLocal.GetCommands(); cmd; cmd = cmd->next ) {
  108. if ( !( cmd->flags & flags ) ) {
  109. continue;
  110. }
  111. if ( match.Length() && idStr( cmd->name ).Filter( match, false ) == 0 ) {
  112. continue;
  113. }
  114. cmdList.Append( cmd );
  115. }
  116. cmdList.Sort();
  117. for ( i = 0; i < cmdList.Num(); i++ ) {
  118. cmd = cmdList[i];
  119. common->Printf( " %-21s %s\n", cmd->name, cmd->description );
  120. }
  121. common->Printf( "%i commands\n", cmdList.Num() );
  122. }
  123. /*
  124. ============
  125. idCmdSystemLocal::List_f
  126. ============
  127. */
  128. void idCmdSystemLocal::List_f( const idCmdArgs &args ) {
  129. idCmdSystemLocal::ListByFlags( args, CMD_FL_ALL );
  130. }
  131. /*
  132. ============
  133. idCmdSystemLocal::SystemList_f
  134. ============
  135. */
  136. void idCmdSystemLocal::SystemList_f( const idCmdArgs &args ) {
  137. idCmdSystemLocal::ListByFlags( args, CMD_FL_SYSTEM );
  138. }
  139. /*
  140. ============
  141. idCmdSystemLocal::RendererList_f
  142. ============
  143. */
  144. void idCmdSystemLocal::RendererList_f( const idCmdArgs &args ) {
  145. idCmdSystemLocal::ListByFlags( args, CMD_FL_RENDERER );
  146. }
  147. /*
  148. ============
  149. idCmdSystemLocal::SoundList_f
  150. ============
  151. */
  152. void idCmdSystemLocal::SoundList_f( const idCmdArgs &args ) {
  153. idCmdSystemLocal::ListByFlags( args, CMD_FL_SOUND );
  154. }
  155. /*
  156. ============
  157. idCmdSystemLocal::GameList_f
  158. ============
  159. */
  160. void idCmdSystemLocal::GameList_f( const idCmdArgs &args ) {
  161. idCmdSystemLocal::ListByFlags( args, CMD_FL_GAME );
  162. }
  163. /*
  164. ============
  165. idCmdSystemLocal::ToolList_f
  166. ============
  167. */
  168. void idCmdSystemLocal::ToolList_f( const idCmdArgs &args ) {
  169. idCmdSystemLocal::ListByFlags( args, CMD_FL_TOOL );
  170. }
  171. /*
  172. ===============
  173. idCmdSystemLocal::Exec_f
  174. ===============
  175. */
  176. void idCmdSystemLocal::Exec_f( const idCmdArgs &args ) {
  177. char * f;
  178. int len;
  179. idStr filename;
  180. if ( args.Argc () != 2 ) {
  181. common->Printf( "exec <filename> : execute a script file\n" );
  182. return;
  183. }
  184. filename = args.Argv(1);
  185. filename.DefaultFileExtension( ".cfg" );
  186. len = fileSystem->ReadFile( filename, reinterpret_cast<void **>(&f), NULL );
  187. if ( !f ) {
  188. common->Printf( "couldn't exec %s\n", args.Argv(1) );
  189. return;
  190. }
  191. common->Printf( "execing %s\n", args.Argv(1) );
  192. cmdSystemLocal.BufferCommandText( CMD_EXEC_INSERT, f );
  193. fileSystem->FreeFile( f );
  194. }
  195. /*
  196. ===============
  197. idCmdSystemLocal::Vstr_f
  198. Inserts the current value of a cvar as command text
  199. ===============
  200. */
  201. void idCmdSystemLocal::Vstr_f( const idCmdArgs &args ) {
  202. const char *v;
  203. if ( args.Argc () != 2 ) {
  204. common->Printf( "vstr <variablename> : execute a variable command\n" );
  205. return;
  206. }
  207. v = cvarSystem->GetCVarString( args.Argv( 1 ) );
  208. cmdSystemLocal.BufferCommandText( CMD_EXEC_APPEND, va( "%s\n", v ) );
  209. }
  210. /*
  211. ===============
  212. idCmdSystemLocal::Echo_f
  213. Just prints the rest of the line to the console
  214. ===============
  215. */
  216. void idCmdSystemLocal::Echo_f( const idCmdArgs &args ) {
  217. int i;
  218. for ( i = 1; i < args.Argc(); i++ ) {
  219. common->Printf( "%s ", args.Argv( i ) );
  220. }
  221. common->Printf( "\n" );
  222. }
  223. /*
  224. ============
  225. idCmdSystemLocal::Wait_f
  226. Causes execution of the remainder of the command buffer to be delayed until next frame.
  227. ============
  228. */
  229. void idCmdSystemLocal::Wait_f( const idCmdArgs &args ) {
  230. if ( args.Argc() == 2 ) {
  231. cmdSystemLocal.SetWait( atoi( args.Argv( 1 ) ) );
  232. } else {
  233. cmdSystemLocal.SetWait( 1 );
  234. }
  235. }
  236. /*
  237. ============
  238. idCmdSystemLocal::Parse_f
  239. This just prints out how the rest of the line was parsed, as a debugging tool.
  240. ============
  241. */
  242. void idCmdSystemLocal::Parse_f( const idCmdArgs &args ) {
  243. int i;
  244. for ( i = 0; i < args.Argc(); i++ ) {
  245. common->Printf( "%i: %s\n", i, args.Argv(i) );
  246. }
  247. }
  248. /*
  249. ============
  250. idCmdSystemLocal::Init
  251. ============
  252. */
  253. void idCmdSystemLocal::Init( void ) {
  254. AddCommand( "listCmds", List_f, CMD_FL_SYSTEM, "lists commands" );
  255. AddCommand( "listSystemCmds", SystemList_f, CMD_FL_SYSTEM, "lists system commands" );
  256. AddCommand( "listRendererCmds", RendererList_f, CMD_FL_SYSTEM, "lists renderer commands" );
  257. AddCommand( "listSoundCmds", SoundList_f, CMD_FL_SYSTEM, "lists sound commands" );
  258. AddCommand( "listGameCmds", GameList_f, CMD_FL_SYSTEM, "lists game commands" );
  259. AddCommand( "listToolCmds", ToolList_f, CMD_FL_SYSTEM, "lists tool commands" );
  260. AddCommand( "exec", Exec_f, CMD_FL_SYSTEM, "executes a config file", ArgCompletion_ConfigName );
  261. AddCommand( "vstr", Vstr_f, CMD_FL_SYSTEM, "inserts the current value of a cvar as command text" );
  262. AddCommand( "echo", Echo_f, CMD_FL_SYSTEM, "prints text" );
  263. AddCommand( "parse", Parse_f, CMD_FL_SYSTEM, "prints tokenized string" );
  264. AddCommand( "wait", Wait_f, CMD_FL_SYSTEM, "delays remaining buffered commands one or more frames" );
  265. completionString = "*";
  266. textLength = 0;
  267. }
  268. /*
  269. ============
  270. idCmdSystemLocal::Shutdown
  271. ============
  272. */
  273. void idCmdSystemLocal::Shutdown( void ) {
  274. commandDef_t *cmd;
  275. for ( cmd = commands; cmd; cmd = commands ) {
  276. commands = commands->next;
  277. Mem_Free( cmd->name );
  278. Mem_Free( cmd->description );
  279. delete cmd;
  280. }
  281. completionString.Clear();
  282. completionParms.Clear();
  283. tokenizedCmds.Clear();
  284. postReload.Clear();
  285. }
  286. /*
  287. ============
  288. idCmdSystemLocal::AddCommand
  289. ============
  290. */
  291. void idCmdSystemLocal::AddCommand( const char *cmdName, cmdFunction_t function, int flags, const char *description, argCompletion_t argCompletion ) {
  292. commandDef_t *cmd;
  293. // fail if the command already exists
  294. for ( cmd = commands; cmd; cmd = cmd->next ) {
  295. if ( idStr::Cmp( cmdName, cmd->name ) == 0 ) {
  296. if ( function != cmd->function ) {
  297. common->Printf( "idCmdSystemLocal::AddCommand: %s already defined\n", cmdName );
  298. }
  299. return;
  300. }
  301. }
  302. cmd = new commandDef_t;
  303. cmd->name = Mem_CopyString( cmdName );
  304. cmd->function = function;
  305. cmd->argCompletion = argCompletion;
  306. cmd->flags = flags;
  307. cmd->description = Mem_CopyString( description );
  308. cmd->next = commands;
  309. commands = cmd;
  310. }
  311. /*
  312. ============
  313. idCmdSystemLocal::RemoveCommand
  314. ============
  315. */
  316. void idCmdSystemLocal::RemoveCommand( const char *cmdName ) {
  317. commandDef_t *cmd, **last;
  318. for ( last = &commands, cmd = *last; cmd; cmd = *last ) {
  319. if ( idStr::Cmp( cmdName, cmd->name ) == 0 ) {
  320. *last = cmd->next;
  321. Mem_Free( cmd->name );
  322. Mem_Free( cmd->description );
  323. delete cmd;
  324. return;
  325. }
  326. last = &cmd->next;
  327. }
  328. }
  329. /*
  330. ============
  331. idCmdSystemLocal::RemoveFlaggedCommands
  332. ============
  333. */
  334. void idCmdSystemLocal::RemoveFlaggedCommands( int flags ) {
  335. commandDef_t *cmd, **last;
  336. for ( last = &commands, cmd = *last; cmd; cmd = *last ) {
  337. if ( cmd->flags & flags ) {
  338. *last = cmd->next;
  339. Mem_Free( cmd->name );
  340. Mem_Free( cmd->description );
  341. delete cmd;
  342. continue;
  343. }
  344. last = &cmd->next;
  345. }
  346. }
  347. /*
  348. ============
  349. idCmdSystemLocal::CommandCompletion
  350. ============
  351. */
  352. void idCmdSystemLocal::CommandCompletion( void(*callback)( const char *s ) ) {
  353. commandDef_t *cmd;
  354. for ( cmd = commands; cmd; cmd = cmd->next ) {
  355. callback( cmd->name );
  356. }
  357. }
  358. /*
  359. ============
  360. idCmdSystemLocal::ArgCompletion
  361. ============
  362. */
  363. void idCmdSystemLocal::ArgCompletion( const char *cmdString, void(*callback)( const char *s ) ) {
  364. commandDef_t *cmd;
  365. idCmdArgs args;
  366. args.TokenizeString( cmdString, false );
  367. for ( cmd = commands; cmd; cmd = cmd->next ) {
  368. if ( !cmd->argCompletion ) {
  369. continue;
  370. }
  371. if ( idStr::Icmp( args.Argv( 0 ), cmd->name ) == 0 ) {
  372. cmd->argCompletion( args, callback );
  373. break;
  374. }
  375. }
  376. }
  377. /*
  378. ============
  379. idCmdSystemLocal::ExecuteTokenizedString
  380. ============
  381. */
  382. void idCmdSystemLocal::ExecuteTokenizedString( const idCmdArgs &args ) {
  383. commandDef_t *cmd, **prev;
  384. // execute the command line
  385. if ( !args.Argc() ) {
  386. return; // no tokens
  387. }
  388. // check registered command functions
  389. for ( prev = &commands; *prev; prev = &cmd->next ) {
  390. cmd = *prev;
  391. if ( idStr::Icmp( args.Argv( 0 ), cmd->name ) == 0 ) {
  392. // rearrange the links so that the command will be
  393. // near the head of the list next time it is used
  394. *prev = cmd->next;
  395. cmd->next = commands;
  396. commands = cmd;
  397. if ( ( cmd->flags & (CMD_FL_CHEAT|CMD_FL_TOOL) ) && session && session->IsMultiplayer() && !cvarSystem->GetCVarBool( "net_allowCheats" ) ) {
  398. common->Printf( "Command '%s' not valid in multiplayer mode.\n", cmd->name );
  399. return;
  400. }
  401. // perform the action
  402. if ( !cmd->function ) {
  403. break;
  404. } else {
  405. cmd->function( args );
  406. }
  407. return;
  408. }
  409. }
  410. // check cvars
  411. if ( cvarSystem->Command( args ) ) {
  412. return;
  413. }
  414. common->Printf( "Unknown command '%s'\n", args.Argv( 0 ) );
  415. }
  416. /*
  417. ============
  418. idCmdSystemLocal::ExecuteCommandText
  419. Tokenizes, then executes.
  420. ============
  421. */
  422. void idCmdSystemLocal::ExecuteCommandText( const char *text ) {
  423. ExecuteTokenizedString( idCmdArgs( text, false ) );
  424. }
  425. /*
  426. ============
  427. idCmdSystemLocal::InsertCommandText
  428. Adds command text immediately after the current command
  429. Adds a \n to the text
  430. ============
  431. */
  432. void idCmdSystemLocal::InsertCommandText( const char *text ) {
  433. int len;
  434. int i;
  435. len = strlen( text ) + 1;
  436. if ( len + textLength > (int)sizeof( textBuf ) ) {
  437. common->Printf( "idCmdSystemLocal::InsertText: buffer overflow\n" );
  438. return;
  439. }
  440. // move the existing command text
  441. for ( i = textLength - 1; i >= 0; i-- ) {
  442. textBuf[ i + len ] = textBuf[ i ];
  443. }
  444. // copy the new text in
  445. memcpy( textBuf, text, len - 1 );
  446. // add a \n
  447. textBuf[ len - 1 ] = '\n';
  448. textLength += len;
  449. }
  450. /*
  451. ============
  452. idCmdSystemLocal::AppendCommandText
  453. Adds command text at the end of the buffer, does NOT add a final \n
  454. ============
  455. */
  456. void idCmdSystemLocal::AppendCommandText( const char *text ) {
  457. int l;
  458. l = strlen( text );
  459. if ( textLength + l >= (int)sizeof( textBuf ) ) {
  460. common->Printf( "idCmdSystemLocal::AppendText: buffer overflow\n" );
  461. return;
  462. }
  463. memcpy( textBuf + textLength, text, l );
  464. textLength += l;
  465. }
  466. /*
  467. ============
  468. idCmdSystemLocal::BufferCommandText
  469. ============
  470. */
  471. void idCmdSystemLocal::BufferCommandText( cmdExecution_t exec, const char *text ) {
  472. switch( exec ) {
  473. case CMD_EXEC_NOW: {
  474. ExecuteCommandText( text );
  475. break;
  476. }
  477. case CMD_EXEC_INSERT: {
  478. InsertCommandText( text );
  479. break;
  480. }
  481. case CMD_EXEC_APPEND: {
  482. AppendCommandText( text );
  483. break;
  484. }
  485. default: {
  486. common->FatalError( "idCmdSystemLocal::BufferCommandText: bad exec type" );
  487. }
  488. }
  489. }
  490. /*
  491. ============
  492. idCmdSystemLocal::BufferCommandArgs
  493. ============
  494. */
  495. void idCmdSystemLocal::BufferCommandArgs( cmdExecution_t exec, const idCmdArgs &args ) {
  496. switch ( exec ) {
  497. case CMD_EXEC_NOW: {
  498. ExecuteTokenizedString( args );
  499. break;
  500. }
  501. case CMD_EXEC_APPEND: {
  502. AppendCommandText( "_execTokenized\n" );
  503. tokenizedCmds.Append( args );
  504. break;
  505. }
  506. default: {
  507. common->FatalError( "idCmdSystemLocal::BufferCommandArgs: bad exec type" );
  508. }
  509. }
  510. }
  511. /*
  512. ============
  513. idCmdSystemLocal::ExecuteCommandBuffer
  514. ============
  515. */
  516. void idCmdSystemLocal::ExecuteCommandBuffer( void ) {
  517. int i;
  518. char * text;
  519. int quotes;
  520. idCmdArgs args;
  521. while( textLength ) {
  522. if ( wait ) {
  523. // skip out while text still remains in buffer, leaving it for next frame
  524. wait--;
  525. break;
  526. }
  527. // find a \n or ; line break
  528. text = (char *)textBuf;
  529. quotes = 0;
  530. for ( i = 0; i < textLength; i++ ) {
  531. if ( text[i] == '"' ) {
  532. quotes++;
  533. }
  534. if ( !( quotes & 1 ) && text[i] == ';' ) {
  535. break; // don't break if inside a quoted string
  536. }
  537. if ( text[i] == '\n' || text[i] == '\r' ) {
  538. break;
  539. }
  540. }
  541. text[i] = 0;
  542. if ( !idStr::Cmp( text, "_execTokenized" ) ) {
  543. args = tokenizedCmds[ 0 ];
  544. tokenizedCmds.RemoveIndex( 0 );
  545. } else {
  546. args.TokenizeString( text, false );
  547. }
  548. // delete the text from the command buffer and move remaining commands down
  549. // this is necessary because commands (exec) can insert data at the
  550. // beginning of the text buffer
  551. if ( i == textLength ) {
  552. textLength = 0;
  553. } else {
  554. i++;
  555. textLength -= i;
  556. memmove( text, text+i, textLength );
  557. }
  558. // execute the command line that we have already tokenized
  559. ExecuteTokenizedString( args );
  560. }
  561. }
  562. /*
  563. ============
  564. idCmdSystemLocal::ArgCompletion_FolderExtension
  565. ============
  566. */
  567. void idCmdSystemLocal::ArgCompletion_FolderExtension( const idCmdArgs &args, void(*callback)( const char *s ), const char *folder, bool stripFolder, ... ) {
  568. int i;
  569. idStr string;
  570. const char *extension;
  571. va_list argPtr;
  572. string = args.Argv( 0 );
  573. string += " ";
  574. string += args.Argv( 1 );
  575. if ( string.Icmp( completionString ) != 0 ) {
  576. idStr parm, path;
  577. idFileList *names;
  578. completionString = string;
  579. completionParms.Clear();
  580. parm = args.Argv( 1 );
  581. parm.ExtractFilePath( path );
  582. if ( stripFolder || path.Length() == 0 ) {
  583. path = folder + path;
  584. }
  585. path.StripTrailing( '/' );
  586. // list folders
  587. names = fileSystem->ListFiles( path, "/", true, true );
  588. for ( i = 0; i < names->GetNumFiles(); i++ ) {
  589. idStr name = names->GetFile( i );
  590. if ( stripFolder ) {
  591. name.Strip( folder );
  592. } else {
  593. name.Strip( "/" );
  594. }
  595. name = args.Argv( 0 ) + ( " " + name ) + "/";
  596. completionParms.Append( name );
  597. }
  598. fileSystem->FreeFileList( names );
  599. // list files
  600. va_start( argPtr, stripFolder );
  601. for ( extension = va_arg( argPtr, const char * ); extension; extension = va_arg( argPtr, const char * ) ) {
  602. names = fileSystem->ListFiles( path, extension, true, true );
  603. for ( i = 0; i < names->GetNumFiles(); i++ ) {
  604. idStr name = names->GetFile( i );
  605. if ( stripFolder ) {
  606. name.Strip( folder );
  607. } else {
  608. name.Strip( "/" );
  609. }
  610. name = args.Argv( 0 ) + ( " " + name );
  611. completionParms.Append( name );
  612. }
  613. fileSystem->FreeFileList( names );
  614. }
  615. va_end( argPtr );
  616. }
  617. for ( i = 0; i < completionParms.Num(); i++ ) {
  618. callback( completionParms[i] );
  619. }
  620. }
  621. /*
  622. ============
  623. idCmdSystemLocal::ArgCompletion_DeclName
  624. ============
  625. */
  626. void idCmdSystemLocal::ArgCompletion_DeclName( const idCmdArgs &args, void(*callback)( const char *s ), int type ) {
  627. int i, num;
  628. if ( declManager == NULL ) {
  629. return;
  630. }
  631. num = declManager->GetNumDecls( (declType_t)type );
  632. for ( i = 0; i < num; i++ ) {
  633. callback( idStr( args.Argv( 0 ) ) + " " + declManager->DeclByIndex( (declType_t)type, i , false )->GetName() );
  634. }
  635. }
  636. /*
  637. ============
  638. idCmdSystemLocal::SetupReloadEngine
  639. ============
  640. */
  641. void idCmdSystemLocal::SetupReloadEngine( const idCmdArgs &args ) {
  642. BufferCommandText( CMD_EXEC_APPEND, "reloadEngine\n" );
  643. postReload = args;
  644. }
  645. /*
  646. ============
  647. idCmdSystemLocal::PostReloadEngine
  648. ============
  649. */
  650. bool idCmdSystemLocal::PostReloadEngine( void ) {
  651. if ( !postReload.Argc() ) {
  652. return false;
  653. }
  654. BufferCommandArgs( CMD_EXEC_APPEND, postReload );
  655. postReload.Clear();
  656. return true;
  657. }