console.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _CONSOLE_H_
  23. #define _CONSOLE_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. #ifndef _BITSET_H_
  28. #include "collection/bitSet.h"
  29. #endif
  30. #include <stdarg.h>
  31. class SimObject;
  32. struct EnumTable;
  33. class Namespace;
  34. /// Indicates that warnings about undefined script variables should be displayed.
  35. ///
  36. /// @note This is set and controlled by script.
  37. extern bool gWarnUndefinedScriptVariables;
  38. enum StringTableConstants
  39. {
  40. StringTagPrefixByte = 0x01 ///< Magic value prefixed to tagged strings.
  41. };
  42. /// Represents an entry in the log.
  43. struct ConsoleLogEntry
  44. {
  45. /// This field indicates the severity of the log entry.
  46. ///
  47. /// Log entries are filtered and displayed differently based on
  48. /// their severity. Errors are highlighted red, while normal entries
  49. /// are displayed as normal text. Often times, the engine will be
  50. /// configured to hide all log entries except warnings or errors,
  51. /// or to perform a special notification when it encounters an error.
  52. enum Level
  53. {
  54. Normal = 0,
  55. Warning,
  56. Error,
  57. NUM_CLASS
  58. } mLevel;
  59. /// Used to associate a log entry with a module.
  60. ///
  61. /// Log entries can come from different sources; for instance,
  62. /// the scripting engine, or the network code. This allows the
  63. /// logging system to be aware of where different log entries
  64. /// originated from.
  65. enum Type
  66. {
  67. General = 0,
  68. Assert,
  69. Script,
  70. GUI,
  71. Network,
  72. NUM_TYPE
  73. } mType;
  74. /// Indicates the actual log entry.
  75. ///
  76. /// This contains a description of the event being logged.
  77. /// For instance, "unable to access file", or "player connected
  78. /// successfully", or nearly anything else you might imagine.
  79. ///
  80. /// Typically, the description should contain a concise, descriptive
  81. /// string describing whatever is being logged. Whenever possible,
  82. /// include useful details like the name of the file being accessed,
  83. /// or the id of the player or GuiControl, so that if a log needs
  84. /// to be used to locate a bug, it can be done as painlessly as
  85. /// possible.
  86. const char *mString;
  87. };
  88. /// Scripting engine representation of an enum.
  89. ///
  90. /// This data structure is used by the scripting engine
  91. /// to expose enumerations to the scripting language. It
  92. /// acts to relate named constants to integer values, just
  93. /// like an enum in C++.
  94. struct EnumTable
  95. {
  96. /// Number of enumerated items in the table.
  97. S32 size;
  98. /// This represents a specific item in the enumeration.
  99. struct Enums
  100. {
  101. S32 index; ///< Index label maps to.
  102. const char *label;///< Label for this index.
  103. };
  104. Enums *table;
  105. /// Constructor.
  106. ///
  107. /// This sets up the EnumTable with predefined data.
  108. ///
  109. /// @param sSize Size of the table.
  110. /// @param sTable Pointer to table of Enums.
  111. ///
  112. /// @see gLiquidTypeTable
  113. /// @see gAlignTable
  114. EnumTable(S32 sSize, Enums *sTable)
  115. { size = sSize; table = sTable; }
  116. };
  117. typedef const char *StringTableEntry;
  118. /// @defgroup tsScripting TorqueScript Bindings
  119. /// TorqueScrit bindings
  120. /// @defgroup console_callbacks Scripting Engine Callbacks
  121. /// @ingroup tsScripting
  122. ///
  123. /// The scripting engine makes heavy use of callbacks to represent
  124. /// function exposed to the scripting language. StringCallback,
  125. /// IntCallback, FloatCallback, VoidCallback, and BoolCallback all
  126. /// represent exposed script functions returning different types.
  127. ///
  128. /// ConsumerCallback is used with the function Con::addConsumer; functions
  129. /// registered with Con::addConsumer are called whenever something is outputted
  130. /// to the console. For instance, the TelnetConsole registers itself with the
  131. /// console so it can echo the console over the network.
  132. ///
  133. /// @note Callbacks to the scripting language - for instance, onExit(), which is
  134. /// a script function called when the engine is shutting down - are handled
  135. /// using Con::executef() and kin.
  136. /// @{
  137. ///
  138. typedef const char * (*StringCallback)(SimObject *obj, S32 argc, const char *argv[]);
  139. typedef S32 (*IntCallback)(SimObject *obj, S32 argc, const char *argv[]);
  140. typedef F32 (*FloatCallback)(SimObject *obj, S32 argc, const char *argv[]);
  141. typedef void (*VoidCallback)(SimObject *obj, S32 argc, const char *argv[]); // We have it return a value so things don't break..
  142. typedef bool (*BoolCallback)(SimObject *obj, S32 argc, const char *argv[]);
  143. typedef void (*ConsumerCallback)(ConsoleLogEntry::Level level, const char *consoleLine);
  144. /// @}
  145. /// @defgroup console_types Scripting Engine Type Functions
  146. /// @ingroup tsScripting
  147. ///
  148. /// @see Con::registerType
  149. /// @{
  150. typedef const char* (*GetDataFunction)(void *dptr, EnumTable *tbl, BitSet32 flag);
  151. typedef void (*SetDataFunction)(void *dptr, S32 argc, const char **argv, EnumTable *tbl, BitSet32 flag);
  152. /// @}
  153. /// This namespace contains the core of the console functionality.
  154. ///
  155. /// @section con_intro Introduction
  156. ///
  157. /// The console is a key part of Torque's architecture. It allows direct run-time control
  158. /// of many aspects of the engine.
  159. ///
  160. /// @nosubgrouping
  161. namespace Con
  162. {
  163. /// Various configuration constants.
  164. enum Constants
  165. {
  166. /// This is the version number associated with DSO files.
  167. ///
  168. /// If you make any changes to the way the scripting language works
  169. /// (such as DSO format changes, adding/removing op-codes) that would
  170. /// break compatibility, then you should increment this.
  171. ///
  172. /// If you make a really major change, increment it to the next multiple
  173. /// of ten.
  174. ///
  175. /// 12/29/04 - BJG - 33->34 Removed some opcodes, part of namespace upgrade.
  176. /// 12/30/04 - BJG - 34->35 Reordered some things, further general shuffling.
  177. /// 11/03/05 - BJG - 35->36 Integrated new debugger code.
  178. // 09/08/06 - THB - 36->37 New opcode for internal names
  179. // 09/15/06 - THB - 37->38 Added unit conversions
  180. // 11/23/06 - THB - 38->39 Added recursive internal name operator
  181. // 02/15/07 - THB - 39->40 Bumping to 40 for TGB since the console has been majorly hacked without the version number being bumped
  182. // 02/16/07 - THB - 40->41 newmsg operator
  183. // 02/16/07 - PAUP - 41->42 DSOs are read with a pointer before every string(ASTnodes changed). Namespace and HashTable revamped
  184. // 05/17/10 - Luma - 42-43 Adding proper sceneObject physics flags, fixes in general
  185. DSOVersion = 43,
  186. MaxLineLength = 512, ///< Maximum length of a line of console input.
  187. MaxDataTypes = 256 ///< Maximum number of registered data types.
  188. };
  189. /// @name Control Functions
  190. ///
  191. /// The console must be initialized and shutdown appropriately during the
  192. /// lifetime of the app. These functions are used to manage this behavior.
  193. ///
  194. /// @note Torque deals with this aspect of console management, so you don't need
  195. /// to call these functions in normal usage of the engine.
  196. /// @{
  197. /// Initializes the console.
  198. ///
  199. /// This performs the following steps:
  200. /// - Calls Namespace::init() to initialize the scripting namespace hierarchy.
  201. /// - Calls ConsoleConstructor::setup() to initialize globally defined console
  202. /// methods and functions.
  203. /// - Registers some basic global script variables.
  204. /// - Calls AbstractClassRep::init() to initialize Torque's class database.
  205. /// - Registers some basic global script functions that couldn't usefully
  206. /// be defined anywhere else.
  207. void init();
  208. /// Shuts down the console.
  209. ///
  210. /// This performs the following steps:
  211. /// - Closes the console log file.
  212. /// - Calls Namespace::shutdown() to shut down the scripting namespace hierarchy.
  213. void shutdown();
  214. /// Is the console active at this time?
  215. bool isActive();
  216. /// @}
  217. /// @name Console Consumers
  218. ///
  219. /// The console distributes its output through Torque by using
  220. /// consumers. Every time a new line is printed to the console,
  221. /// all the ConsumerCallbacks registered using addConsumer are
  222. /// called, in order.
  223. ///
  224. /// @note The GuiConsole control, which provides the on-screen
  225. /// in-game console, uses a different technique to render
  226. /// the console. It calls getLockLog() to lock the Vector
  227. /// of on-screen console entries, then it renders them as
  228. /// needed. While the Vector is locked, the console will
  229. /// not change the Vector. When the GuiConsole control is
  230. /// done with the console entries, it calls unlockLog()
  231. /// to tell the console that it is again safe to modify
  232. /// the Vector.
  233. ///
  234. /// @see TelnetConsole
  235. /// @see TelnetDebugger
  236. /// @see WinConsole
  237. /// @see MacCarbConsole
  238. /// @see iPhoneConsole
  239. /// @see StdConsole
  240. /// @see ConsoleLogger
  241. ///
  242. /// @{
  243. void addConsumer(ConsumerCallback cb);
  244. void removeConsumer(ConsumerCallback cb);
  245. /// @}
  246. /// @name Miscellaneous
  247. /// @{
  248. /// Remove color marking information from a string.
  249. ///
  250. /// @note It does this in-place, so be careful! It may
  251. /// potentially blast data if you're not careful.
  252. /// When in doubt, make a copy of the string first.
  253. void stripColorChars(char* line);
  254. /// Convert from a relative path to an absolute path.
  255. ///
  256. /// This is used in (among other places) the exec() script function, which
  257. /// takes a parameter indicating a script file and executes it. Script paths
  258. /// can be one of:
  259. /// - <b>Absolute:</b> <i>fps/foo/bar.cs</i> Paths of this sort are passed
  260. /// through.
  261. /// - <b>Mod-relative:</b> <i>~/foo/bar.cs</i> Paths of this sort have their
  262. /// replaced with the name of the current mod.
  263. /// - <b>File-relative:</b> <i>./baz/blip.cs</i> Paths of this sort are
  264. /// calculated relative to the path of the current scripting file.
  265. /// - <b>Expando:</b> <i>^Project/image/happy.png</I> Paths of this sort are
  266. /// relative to the path defined by the expando, in this case the "Project"
  267. /// expando.
  268. ///
  269. /// @param pDstPath Pointer to string buffer to fill with absolute path.
  270. /// @param size Size of buffer pointed to by pDstPath.
  271. /// @param pSrcPath Original, possibly relative path.
  272. bool expandPath( char* pDstPath, U32 size, const char* pSrcPath, const char* pWorkingDirectoryHint = NULL, const bool ensureTrailingSlash = false );
  273. void collapsePath( char* pDstPath, U32 size, const char* pSrcPath, const char* pWorkingDirectoryHint = NULL );
  274. bool isBasePath( const char* SrcPath, const char* pBasePath );
  275. void ensureTrailingSlash( char* pDstPath, const char* pSrcPath );
  276. bool stripRepeatSlashes( char* pDstPath, const char* pSrcPath, S32 dstSize );
  277. void addPathExpando( const char* pExpandoName, const char* pPath );
  278. void removePathExpando( const char* pExpandoName );
  279. bool isPathExpando( const char* pExpandoName );
  280. StringTableEntry getPathExpando( const char* pExpandoName );
  281. U32 getPathExpandoCount( void );
  282. StringTableEntry getPathExpandoKey( U32 expandoIndex );
  283. StringTableEntry getPathExpandoValue( U32 expandoIndex );
  284. StringTableEntry getModNameFromPath(const char *path);
  285. /// Returns true if fn is a global scripting function.
  286. ///
  287. /// This looks in the global namespace. It also checks to see if fn
  288. /// is in the StringTable; if not, it returns false.
  289. bool isFunction(const char *fn);
  290. /// This is the basis for tab completion in the console.
  291. ///
  292. /// @note This is an internally used function. You probably don't
  293. /// care much about how this works.
  294. ///
  295. /// This function does some basic parsing to try to ascertain the namespace in which
  296. /// we are attempting to do tab completion, then bumps control off to the appropriate
  297. /// tabComplete function, either in SimObject or Namespace.
  298. ///
  299. /// @param inputBuffer Pointer to buffer containing starting data, or last result.
  300. /// @param cursorPos Location of cursor in this buffer. This is used to indicate
  301. /// what part of the string should be kept and what part should
  302. /// be advanced to the next match if any.
  303. /// @param maxResultLength Maximum amount of result data to put into inputBuffer. This
  304. /// is capped by MaxCompletionBufferSize.
  305. /// @param forwardTab Should we go forward to next match or backwards to previous
  306. /// match? True indicates forward.
  307. U32 tabComplete(char* inputBuffer, U32 cursorPos, U32 maxResultLength, bool forwardTab);
  308. /// @}
  309. /// @name Variable Management
  310. /// @{
  311. /// Add a console variable that references the value of a variable in C++ code.
  312. ///
  313. /// If a value is assigned to the console variable the C++ variable is updated,
  314. /// and vice-versa.
  315. ///
  316. /// @param name Global console variable name to create
  317. /// @param type The type of the C++ variable; see the ConsoleDynamicTypes enum for a complete list.
  318. /// @param pointer Pointer to the variable.
  319. /// @see ConsoleDynamicTypes
  320. bool addVariable(const char *name, S32 type, void *pointer);
  321. /// Remove a console variable.
  322. ///
  323. /// @param name Global console variable name to remove
  324. /// @return true if variable existed before removal.
  325. bool removeVariable(const char *name);
  326. /// Assign a string value to a locally scoped console variable
  327. ///
  328. /// @note The context of the variable is determined by gEvalState; that is,
  329. /// by the currently executing code.
  330. ///
  331. /// @param name Local console variable name to set
  332. /// @param value String value to assign to name
  333. void setLocalVariable(const char *name, const char *value);
  334. /// Retrieve the string value to a locally scoped console variable
  335. ///
  336. /// @note The context of the variable is determined by gEvalState; that is,
  337. /// by the currently executing code.
  338. ///
  339. /// @param name Local console variable name to get
  340. const char* getLocalVariable(const char* name);
  341. /// @}
  342. /// @name Global Variable Accessors
  343. /// @{
  344. /// Assign a string value to a global console variable
  345. /// @param name Global console variable name to set
  346. /// @param value String value to assign to this variable.
  347. void setVariable(const char *name, const char *value);
  348. /// Retrieve the string value of a global console variable
  349. /// @param name Global Console variable name to query
  350. /// @return The string value of the variable or "" if the variable does not exist.
  351. const char* getVariable(const char* name);
  352. /// Same as setVariable(), but for bools.
  353. void setBoolVariable (const char* name,bool var);
  354. /// Same as getVariable(), but for bools.
  355. ///
  356. /// @param name Name of the variable.
  357. /// @param def Default value to supply if no matching variable is found.
  358. bool getBoolVariable (const char* name,bool def = false);
  359. /// Same as setVariable(), but for ints.
  360. void setIntVariable (const char* name,S32 var);
  361. /// Same as getVariable(), but for ints.
  362. ///
  363. /// @param name Name of the variable.
  364. /// @param def Default value to supply if no matching variable is found.
  365. S32 getIntVariable (const char* name,S32 def = 0);
  366. /// Same as setVariable(), but for floats.
  367. void setFloatVariable(const char* name,F32 var);
  368. /// Same as getVariable(), but for floats.
  369. ///
  370. /// @param name Name of the variable.
  371. /// @param def Default value to supply if no matching variable is found.
  372. F32 getFloatVariable(const char* name,F32 def = .0f);
  373. /// @}
  374. /// @name Global Function Registration
  375. /// @{
  376. /// Register a C++ function with the console making it a global function callable from the scripting engine.
  377. ///
  378. /// @param name Name of the new function.
  379. /// @param cb Pointer to the function implementing the scripting call; a console callback function returning a specific type value.
  380. /// @param usage Documentation for this function. @ref console_autodoc
  381. /// @param minArgs Minimum number of arguments this function accepts
  382. /// @param maxArgs Maximum number of arguments this function accepts
  383. void addCommand(const char *name, StringCallback cb, const char *usage, S32 minArgs, S32 maxArgs);
  384. void addCommand(const char *name, IntCallback cb, const char *usage, S32 minArgs, S32 maxArg); ///< @copydoc addCommand(const char *, StringCallback, const char *, S32, S32)
  385. void addCommand(const char *name, FloatCallback cb, const char *usage, S32 minArgs, S32 maxArgs); ///< @copydoc addCommand(const char *, StringCallback, const char *, S32, S32)
  386. void addCommand(const char *name, VoidCallback cb, const char *usage, S32 minArgs, S32 maxArgs); ///< @copydoc addCommand(const char *, StringCallback, const char *, S32, S32)
  387. void addCommand(const char *name, BoolCallback cb, const char *usage, S32 minArgs, S32 maxArgs); ///< @copydoc addCommand(const char *, StringCallback, const char *, S32, S32)
  388. /// @}
  389. /// @name Namespace Function Registration
  390. /// @{
  391. /// Register a C++ function with the console making it callable
  392. /// as a method of the given namespace from the scripting engine.
  393. ///
  394. /// @param nameSpace Name of the namespace to associate the new function with; this is usually the name of a class.
  395. /// @param name Name of the new function.
  396. /// @param cb Pointer to the function implementing the scripting call; a console callback function returning a specific type value.
  397. /// @param usage Documentation for this function. @ref console_autodoc
  398. /// @param minArgs Minimum number of arguments this function accepts
  399. /// @param maxArgs Maximum number of arguments this function accepts
  400. void addCommand(const char *nameSpace, const char *name,StringCallback cb, const char *usage, S32 minArgs, S32 maxArgs);
  401. void addCommand(const char *nameSpace, const char *name,IntCallback cb, const char *usage, S32 minArgs, S32 maxArgs); ///< @copydoc addCommand(const char*, const char *, StringCallback, const char *, S32, S32)
  402. void addCommand(const char *nameSpace, const char *name,FloatCallback cb, const char *usage, S32 minArgs, S32 maxArgs); ///< @copydoc addCommand(const char*, const char *, StringCallback, const char *, S32, S32)
  403. void addCommand(const char *nameSpace, const char *name,VoidCallback cb, const char *usage, S32 minArgs, S32 maxArgs); ///< @copydoc addCommand(const char*, const char *, StringCallback, const char *, S32, S32)
  404. void addCommand(const char *nameSpace, const char *name,BoolCallback cb, const char *usage, S32 minArgs, S32 maxArgs); ///< @copydoc addCommand(const char*, const char *, StringCallback, const char *, S32, S32)
  405. /// @}
  406. /// @name Special Purpose Registration
  407. ///
  408. /// These are special-purpose functions that exist to allow commands to be grouped, so
  409. /// that when we generate console docs, they can be more meaningfully presented.
  410. ///
  411. /// @ref console_autodoc "Click here for more information about console docs and grouping."
  412. ///
  413. /// @{
  414. void markCommandGroup (const char * nsName, const char *name, const char* usage=NULL);
  415. void beginCommandGroup(const char * nsName, const char *name, const char* usage);
  416. void endCommandGroup (const char * nsName, const char *name);
  417. /// @deprecated
  418. void addOverload (const char * nsName, const char *name, const char *altUsage);
  419. /// @}
  420. /// @name Console Output
  421. ///
  422. /// These functions process the formatted string and pass it to all the ConsumerCallbacks that are
  423. /// currently registered. The console log file and the console window callbacks are installed by default.
  424. ///
  425. /// @see addConsumer()
  426. /// @see removeConsumer()
  427. /// @{
  428. /// @param _format A stdlib printf style formatted out put string
  429. /// @param ... Variables to be written
  430. void printf(const char *_format, ...);
  431. /// @note The console window colors warning text as LIGHT GRAY.
  432. /// @param _format A stdlib printf style formatted out put string
  433. /// @param ... Variables to be written
  434. void warnf(const char *_format, ...);
  435. /// @note The console window colors warning text as RED.
  436. /// @param _format A stdlib printf style formatted out put string
  437. /// @param ... Variables to be written
  438. void errorf(const char *_format, ...);
  439. /// @note The console window colors warning text as LIGHT GRAY.
  440. /// @param type Allows you to associate the warning message with an internal module.
  441. /// @param _format A stdlib printf style formatted out put string
  442. /// @param ... Variables to be written
  443. /// @see Con::warnf()
  444. void warnf(ConsoleLogEntry::Type type, const char *_format, ...);
  445. /// @note The console window colors warning text as RED.
  446. /// @param type Allows you to associate the warning message with an internal module.
  447. /// @param _format A stdlib printf style formatted out put string
  448. /// @param ... Variables to be written
  449. /// @see Con::errorf()
  450. void errorf(ConsoleLogEntry::Type type, const char *_format, ...);
  451. /// clear the console log
  452. void cls( void );
  453. /// Prints a separator to the console.
  454. inline void printSeparator( void ) { printf("--------------------------------------------------------------------------------"); }
  455. /// Prints a separator to the console.
  456. inline void printBlankLine( void ) { printf(""); }
  457. /// @}
  458. /// Returns true when called from the main thread, false otherwise
  459. bool isMainThread();
  460. /// @name Console Execution
  461. ///
  462. /// These are functions relating to the execution of script code.
  463. ///
  464. /// @{
  465. /// Call a script function from C/C++ code.
  466. ///
  467. /// @param argc Number of elements in the argv parameter
  468. /// @param argv A character string array containing the name of the function
  469. /// to call followed by the arguments to that function.
  470. /// @code
  471. /// // Call a Torque script function called mAbs, having one parameter.
  472. /// char* argv[] = {"abs", "-9"};
  473. /// char* result = execute(2, argv);
  474. /// @endcode
  475. const char *execute(S32 argc, const char* argv[]);
  476. /// @see execute(S32 argc, const char* argv[])
  477. const char *executef(S32 argc, ...);
  478. /// Call a Torque Script member function of a SimObject from C/C++ code.
  479. /// @param object Object on which to execute the method call.
  480. /// @param argc Number of elements in the argv parameter (must be >2, see argv)
  481. /// @param argv A character string array containing the name of the member function
  482. /// to call followed by an empty parameter (gets filled with object ID)
  483. /// followed by arguments to that function.
  484. /// @code
  485. /// // Call the method setMode() on an object, passing it one parameter.
  486. ///
  487. /// char* argv[] = {"setMode", "", "2"};
  488. /// char* result = execute(mysimobject, 3, argv);
  489. /// @endcode
  490. // [neo, 5/10/2007 - #3010]
  491. // Added flag thisCallOnly to bypass dynamic method calls
  492. const char *execute(SimObject *object, S32 argc, const char *argv[], bool thisCallOnly = false);
  493. /// @see execute(SimObject *, S32 argc, const char *argv[])
  494. const char *executef(SimObject *, S32 argc, ...);
  495. /// Evaluate an arbitrary chunk of code.
  496. ///
  497. /// @param string Buffer containing code to execute.
  498. /// @param echo Should we echo the string to the console?
  499. /// @param fileName Indicate what file this code is coming from; used in error reporting and such.
  500. const char *evaluate(const char* string, bool echo = false, const char *fileName = NULL);
  501. /// Evaluate an arbitrary line of script.
  502. ///
  503. /// This wraps dVsprintf(), so you can substitute parameters into the code being executed.
  504. const char *evaluatef(const char* string, ...);
  505. /// @}
  506. /// @name Console Function Implementation Helpers
  507. ///
  508. /// The functions Con::getIntArg, Con::getFloatArg and Con::getArgBuffer(size) are used to
  509. /// allocate on the console stack string variables that will be passed into the next console
  510. // function called. This allows the console to avoid copying some data.
  511. ///
  512. /// getReturnBuffer lets you allocate stack space to return data in.
  513. /// @{
  514. ///
  515. char *getReturnBuffer(U32 bufferSize);
  516. char *getReturnBuffer(const char *stringToCopy);
  517. char *getArgBuffer(U32 bufferSize);
  518. char *getFloatArg(F64 arg);
  519. char *getIntArg (S32 arg);
  520. char* getBoolArg(bool arg);
  521. /// @}
  522. /// @name Namespaces
  523. /// @{
  524. Namespace *lookupNamespace(const char *nsName);
  525. bool linkNamespaces(const char *parentName, const char *childName);
  526. bool unlinkNamespaces(const char *parentName, const char *childName);
  527. /// @note This should only be called from consoleObject.h
  528. bool classLinkNamespaces(Namespace *parent, Namespace *child);
  529. /// @}
  530. /// @name Logging
  531. /// @{
  532. void getLockLog(ConsoleLogEntry * &log, U32 &size);
  533. void unlockLog(void);
  534. void setLogMode(S32 mode);
  535. /// @}
  536. /// @name Dynamic Type System
  537. /// @{
  538. ///
  539. /* void registerType( const char *typeName, S32 type, S32 size, GetDataFunction gdf, SetDataFunction sdf, bool isDatablockType = false );
  540. void registerType( const char* typeName, S32 type, S32 size, bool isDatablockType = false );
  541. void registerTypeGet( S32 type, GetDataFunction gdf );
  542. void registerTypeSet( S32 type, SetDataFunction sdf );
  543. const char *getTypeName(S32 type);
  544. bool isDatablockType( S32 type ); */
  545. void setData(S32 type, void *dptr, S32 index, S32 argc, const char **argv, EnumTable *tbl = NULL, BitSet32 flag = 0);
  546. const char *getData(S32 type, void *dptr, S32 index, EnumTable *tbl = NULL, BitSet32 flag = 0);
  547. /// @}
  548. };
  549. extern void expandEscape(char *dest, const char *src);
  550. extern bool collapseEscape(char *buf);
  551. extern S32 HashPointer(StringTableEntry ptr);
  552. /// This is the backend for the ConsoleMethod()/ConsoleFunction() macros.
  553. ///
  554. /// See the group ConsoleConstructor Innards for specifics on how this works.
  555. ///
  556. /// @see @ref console_autodoc
  557. /// @nosubgrouping
  558. class ConsoleConstructor
  559. {
  560. public:
  561. /// @name Entry Type Fields
  562. ///
  563. /// One of these is set based on the type of entry we want
  564. /// inserted in the console.
  565. ///
  566. /// @ref console_autodoc
  567. /// @{
  568. StringCallback sc; ///< A function/method that returns a string.
  569. IntCallback ic; ///< A function/method that returns an int.
  570. FloatCallback fc; ///< A function/method that returns a float.
  571. VoidCallback vc; ///< A function/method that returns nothing.
  572. BoolCallback bc; ///< A function/method that returns a bool.
  573. bool group; ///< Indicates that this is a group marker.
  574. bool overload; ///< Indicates that this is an overload marker.
  575. bool ns; ///< Indicates that this is a namespace marker.
  576. /// @deprecated Unused.
  577. /// @}
  578. /// Minimum/maximum number of arguments for the function.
  579. S32 mina, maxa;
  580. const char *usage; ///< Usage string.
  581. const char *funcName; ///< Function name.
  582. const char *className; ///< Class name.
  583. /// @name ConsoleConstructer Innards
  584. ///
  585. /// The ConsoleConstructor class is used as the backend for the ConsoleFunction() and
  586. /// ConsoleMethod() macros. The way it works takes advantage of several properties of
  587. /// C++.
  588. ///
  589. /// The ConsoleFunction()/ConsoleMethod() macros wrap the declaration of a ConsoleConstructor.
  590. ///
  591. /// @code
  592. /// // The definition of a ConsoleFunction using the macro
  593. /// ConsoleFunction(ExpandPath, const char*, 2, 2, "(string filePath)")
  594. /// {
  595. /// argc;
  596. /// char* ret = Con::getReturnBuffer( 1024 );
  597. /// Con::expandPath(ret, 1024, argv[1]);
  598. /// return ret;
  599. /// }
  600. ///
  601. /// // Resulting code
  602. /// static const char* cExpandPath(SimObject *, S32, const char **argv);
  603. /// static ConsoleConstructor
  604. /// gExpandPathobj(NULL,"ExpandPath", cExpandPath,
  605. /// "(string filePath)", 2, 2);
  606. /// static const char* cExpandPath(SimObject *, S32 argc, const char **argv)
  607. /// {
  608. /// argc;
  609. /// char* ret = Con::getReturnBuffer( 1024 );
  610. /// Con::expandPath(ret, 1024, argv[1]);
  611. /// return ret;
  612. /// }
  613. ///
  614. /// // A similar thing happens when you do a ConsoleMethod.
  615. /// @endcode
  616. ///
  617. /// As you can see, several global items are defined when you use the ConsoleFunction method.
  618. /// The macro constructs the name of these items from the parameters you passed it. Your
  619. /// implementation of the console function is is placed in a function with a name based on
  620. /// the actual name of the console funnction. In addition, a ConsoleConstructor is declared.
  621. ///
  622. /// Because it is defined as a global, the constructor for the ConsoleConstructor is called
  623. /// before execution of main() is started. The constructor is called once for each global
  624. /// ConsoleConstructor variable, in the order in which they were defined (this property only holds true
  625. /// within file scope).
  626. ///
  627. /// We have ConsoleConstructor create a linked list at constructor time, by storing a static
  628. /// pointer to the head of the list, and keeping a pointer to the next item in each instance
  629. /// of ConsoleConstructor. init() is a helper function in this process, automatically filling
  630. /// in commonly used fields and updating first and next as needed. In this way, a list of
  631. /// items to add to the console is assemble in memory, ready for use, before we start
  632. /// execution of the program proper.
  633. ///
  634. /// In Con::init(), ConsoleConstructor::setup() is called to process this prepared list. Each
  635. /// item in the list is iterated over, and the appropriate Con namespace functions (usually
  636. /// Con::addCommand) are invoked to register the ConsoleFunctions and ConsoleMethods in
  637. /// the appropriate namespaces.
  638. ///
  639. /// @see Namespace
  640. /// @see Con
  641. /// @{
  642. ConsoleConstructor *next;
  643. static ConsoleConstructor *first;
  644. void init(const char *cName, const char *fName, const char *usg, S32 minArgs, S32 maxArgs);
  645. static void setup();
  646. /// @}
  647. /// @name Basic Console Constructors
  648. /// @{
  649. ConsoleConstructor(const char *className, const char *funcName, StringCallback sfunc, const char* usage, S32 minArgs, S32 maxArgs);
  650. ConsoleConstructor(const char *className, const char *funcName, IntCallback ifunc, const char* usage, S32 minArgs, S32 maxArgs);
  651. ConsoleConstructor(const char *className, const char *funcName, FloatCallback ffunc, const char* usage, S32 minArgs, S32 maxArgs);
  652. ConsoleConstructor(const char *className, const char *funcName, VoidCallback vfunc, const char* usage, S32 minArgs, S32 maxArgs);
  653. ConsoleConstructor(const char *className, const char *funcName, BoolCallback bfunc, const char* usage, S32 minArgs, S32 maxArgs);
  654. /// @}
  655. /// @name Magic Console Constructors
  656. ///
  657. /// These perform various pieces of "magic" related to consoleDoc functionality.
  658. /// @ref console_autodoc
  659. /// @{
  660. /// Indicates a group marker. (A doxygen illusion)
  661. ///
  662. /// @see Con::markCommandGroup
  663. /// @ref console_autodoc
  664. ConsoleConstructor(const char *className, const char *groupName, const char* usage);
  665. /// Indicates a namespace usage string.
  666. ConsoleConstructor(const char *className, const char *usage);
  667. /// @}
  668. };
  669. /// @name Global Console Definition Macros
  670. ///
  671. /// @note If TORQUE_DEBUG is defined, then we gather documentation information, and
  672. /// do some extra sanity checks.
  673. ///
  674. /// @see ConsoleConstructor
  675. /// @ref console_autodoc
  676. /// @{
  677. // O hackery of hackeries
  678. #define conmethod_return_const return (const
  679. #define conmethod_return_S32 return (S32
  680. #define conmethod_return_F32 return (F32
  681. #define conmethod_nullify(val)
  682. #define conmethod_return_void conmethod_nullify(void
  683. #define conmethod_return_bool return (bool
  684. #define conmethod_return_ConsoleInt conmethod_return_S32
  685. #define conmethod_return_ConsoleFloat conmethod_return_F32
  686. #define conmethod_return_ConsoleVoid conmethod_return_void
  687. #define conmethod_return_ConsoleBool conmethod_return_bool
  688. #define conmethod_return_ConsoleString conmethod_return_const char*
  689. #if !defined(TORQUE_SHIPPING)
  690. // Console function return types
  691. #define ConsoleString const char*
  692. #define ConsoleInt S32
  693. #define ConsoleFloat F32
  694. #define ConsoleVoid void
  695. #define ConsoleBool bool
  696. // Console function macros
  697. # define ConsoleFunctionGroupBegin(groupName, usage) \
  698. static ConsoleConstructor gConsoleFunctionGroup##groupName##__GroupBegin(NULL,#groupName,usage);
  699. # define ConsoleFunction(name,returnType,minArgs,maxArgs,usage1) \
  700. static returnType c##name(SimObject *, S32, const char **argv); \
  701. static ConsoleConstructor g##name##obj(NULL,#name,c##name,usage1,minArgs,maxArgs); \
  702. static returnType c##name(SimObject *, S32 argc, const char **argv)
  703. # define ConsoleFunctionWithDocs(name,returnType,minArgs,maxArgs,argString) \
  704. static returnType c##name(SimObject *, S32, const char **argv); \
  705. static ConsoleConstructor g##name##obj(NULL,#name,c##name,#argString,minArgs,maxArgs); \
  706. static returnType c##name(SimObject *, S32 argc, const char **argv)
  707. # define ConsoleFunctionGroupEnd(groupName) \
  708. static ConsoleConstructor gConsoleFunctionGroup##groupName##__GroupEnd(NULL,#groupName,NULL);
  709. // Console method macros
  710. # define ConsoleNamespace(className, usage) \
  711. static ConsoleConstructor className##__Namespace(#className, usage);
  712. # define ConsoleMethodGroupBegin(className, groupName, usage) \
  713. static ConsoleConstructor className##groupName##__GroupBegin(#className,#groupName,usage);
  714. // note: we would want to expand the following macro into (Doxygen) comments!
  715. // we can not do that with a macro. these are here just as a reminder until completion
  716. # define ConsoleMethodRootGroupBeginWithDocs(className)
  717. # define ConsoleMethodGroupBeginWithDocs(className, superclassName)
  718. # define ConsoleMethod(className,name,returnType,minArgs,maxArgs,usage1) \
  719. static inline returnType c##className##name(className *, S32, const char **argv); \
  720. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  721. AssertFatal( dynamic_cast<className*>( object ), "Object passed to " #name " is not a " #className "!" ); \
  722. conmethod_return_##returnType ) c##className##name(static_cast<className*>(object),argc,argv); \
  723. }; \
  724. static ConsoleConstructor className##name##obj(#className,#name,c##className##name##caster,usage1,minArgs,maxArgs); \
  725. static inline returnType c##className##name(className *object, S32 argc, const char **argv)
  726. # define ConsoleMethodWithDocs(className,name,returnType,minArgs,maxArgs,argString) \
  727. static inline returnType c##className##name(className *, S32, const char **argv); \
  728. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  729. AssertFatal( dynamic_cast<className*>( object ), "Object passed to " #name " is not a " #className "!" ); \
  730. conmethod_return_##returnType ) c##className##name(static_cast<className*>(object),argc,argv); \
  731. }; \
  732. static ConsoleConstructor className##name##obj(#className,#name,c##className##name##caster,#argString,minArgs,maxArgs); \
  733. static inline returnType c##className##name(className *object, S32 argc, const char **argv)
  734. # define ConsoleStaticMethod(className,name,returnType,minArgs,maxArgs,usage1) \
  735. static inline returnType c##className##name(S32, const char **); \
  736. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  737. conmethod_return_##returnType ) c##className##name(argc,argv); \
  738. }; \
  739. static ConsoleConstructor \
  740. className##name##obj(#className,#name,c##className##name##caster,usage1,minArgs,maxArgs); \
  741. static inline returnType c##className##name(S32 argc, const char **argv)
  742. # define ConsoleStaticMethodWithDocs(className,name,returnType,minArgs,maxArgs,argString) \
  743. static inline returnType c##className##name(S32, const char **); \
  744. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  745. conmethod_return_##returnType ) c##className##name(argc,argv); \
  746. }; \
  747. static ConsoleConstructor \
  748. className##name##obj(#className,#name,c##className##name##caster,#argString,minArgs,maxArgs); \
  749. static inline returnType c##className##name(S32 argc, const char **argv)
  750. # define ConsoleMethodGroupEnd(className, groupName) \
  751. static ConsoleConstructor className##groupName##__GroupEnd(#className,#groupName,NULL);
  752. # define ConsoleMethodRootGroupEndWithDocs(className)
  753. # define ConsoleMethodGroupEndWithDocs(className)
  754. #else
  755. // These do nothing if we don't want doc information.
  756. # define ConsoleFunctionGroupBegin(groupName, usage)
  757. # define ConsoleFunctionGroupEnd(groupName)
  758. # define ConsoleNamespace(className, usage)
  759. # define ConsoleMethodGroupBegin(className, groupName, usage)
  760. # define ConsoleMethodGroupEnd(className, groupName)
  761. // These are identical to what's above, we just want to null out the usage strings.
  762. # define ConsoleFunction(name,returnType,minArgs,maxArgs,usage1) \
  763. static returnType c##name(SimObject *, S32, const char **); \
  764. static ConsoleConstructor g##name##obj(NULL,#name,c##name,"",minArgs,maxArgs);\
  765. static returnType c##name(SimObject *, S32 argc, const char **argv)
  766. # define ConsoleMethod(className,name,returnType,minArgs,maxArgs,usage1) \
  767. static inline returnType c##className##name(className *, S32, const char **argv); \
  768. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  769. conmethod_return_##returnType ) c##className##name(static_cast<className*>(object),argc,argv); \
  770. }; \
  771. static ConsoleConstructor \
  772. className##name##obj(#className,#name,c##className##name##caster,"",minArgs,maxArgs); \
  773. static inline returnType c##className##name(className *object, S32 argc, const char **argv)
  774. # define ConsoleStaticMethod(className,name,returnType,minArgs,maxArgs,usage1) \
  775. static inline returnType c##className##name(S32, const char **); \
  776. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  777. conmethod_return_##returnType ) c##className##name(argc,argv); \
  778. }; \
  779. static ConsoleConstructor \
  780. className##name##obj(#className,#name,c##className##name##caster,"",minArgs,maxArgs); \
  781. static inline returnType c##className##name(S32 argc, const char **argv)
  782. #endif
  783. /// @}
  784. #endif