console.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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. // 02/07/13 - JU - 43->44 Expanded the width of stringtable entries to 64bits
  186. DSOVersion = 44,
  187. MaxLineLength = 512, ///< Maximum length of a line of console input.
  188. MaxDataTypes = 256 ///< Maximum number of registered data types.
  189. };
  190. /// @name Control Functions
  191. ///
  192. /// The console must be initialized and shutdown appropriately during the
  193. /// lifetime of the app. These functions are used to manage this behavior.
  194. ///
  195. /// @note Torque deals with this aspect of console management, so you don't need
  196. /// to call these functions in normal usage of the engine.
  197. /// @{
  198. /// Initializes the console.
  199. ///
  200. /// This performs the following steps:
  201. /// - Calls Namespace::init() to initialize the scripting namespace hierarchy.
  202. /// - Calls ConsoleConstructor::setup() to initialize globally defined console
  203. /// methods and functions.
  204. /// - Registers some basic global script variables.
  205. /// - Calls AbstractClassRep::init() to initialize Torque's class database.
  206. /// - Registers some basic global script functions that couldn't usefully
  207. /// be defined anywhere else.
  208. void init();
  209. /// Shuts down the console.
  210. ///
  211. /// This performs the following steps:
  212. /// - Closes the console log file.
  213. /// - Calls Namespace::shutdown() to shut down the scripting namespace hierarchy.
  214. void shutdown();
  215. /// Is the console active at this time?
  216. bool isActive();
  217. /// @}
  218. /// @name Console Consumers
  219. ///
  220. /// The console distributes its output through Torque by using
  221. /// consumers. Every time a new line is printed to the console,
  222. /// all the ConsumerCallbacks registered using addConsumer are
  223. /// called, in order.
  224. ///
  225. /// @note The GuiConsole control, which provides the on-screen
  226. /// in-game console, uses a different technique to render
  227. /// the console. It calls getLockLog() to lock the Vector
  228. /// of on-screen console entries, then it renders them as
  229. /// needed. While the Vector is locked, the console will
  230. /// not change the Vector. When the GuiConsole control is
  231. /// done with the console entries, it calls unlockLog()
  232. /// to tell the console that it is again safe to modify
  233. /// the Vector.
  234. ///
  235. /// @see TelnetConsole
  236. /// @see TelnetDebugger
  237. /// @see WinConsole
  238. /// @see MacCarbConsole
  239. /// @see iPhoneConsole
  240. /// @see StdConsole
  241. /// @see ConsoleLogger
  242. ///
  243. /// @{
  244. void addConsumer(ConsumerCallback cb);
  245. void removeConsumer(ConsumerCallback cb);
  246. /// @}
  247. /// @name Miscellaneous
  248. /// @{
  249. /// Remove color marking information from a string.
  250. ///
  251. /// @note It does this in-place, so be careful! It may
  252. /// potentially blast data if you're not careful.
  253. /// When in doubt, make a copy of the string first.
  254. void stripColorChars(char* line);
  255. /// Convert from a relative path to an absolute path.
  256. ///
  257. /// This is used in (among other places) the exec() script function, which
  258. /// takes a parameter indicating a script file and executes it. Script paths
  259. /// can be one of:
  260. /// - <b>Absolute:</b> <i>fps/foo/bar.cs</i> Paths of this sort are passed
  261. /// through.
  262. /// - <b>Mod-relative:</b> <i>~/foo/bar.cs</i> Paths of this sort have their
  263. /// replaced with the name of the current mod.
  264. /// - <b>File-relative:</b> <i>./baz/blip.cs</i> Paths of this sort are
  265. /// calculated relative to the path of the current scripting file.
  266. /// - <b>Expando:</b> <i>^Project/image/happy.png</I> Paths of this sort are
  267. /// relative to the path defined by the expando, in this case the "Project"
  268. /// expando.
  269. ///
  270. /// @param pDstPath Pointer to string buffer to fill with absolute path.
  271. /// @param size Size of buffer pointed to by pDstPath.
  272. /// @param pSrcPath Original, possibly relative path.
  273. bool expandPath( char* pDstPath, U32 size, const char* pSrcPath, const char* pWorkingDirectoryHint = NULL, const bool ensureTrailingSlash = false );
  274. void collapsePath( char* pDstPath, U32 size, const char* pSrcPath, const char* pWorkingDirectoryHint = NULL );
  275. bool isBasePath( const char* SrcPath, const char* pBasePath );
  276. void ensureTrailingSlash( char* pDstPath, const char* pSrcPath );
  277. bool stripRepeatSlashes( char* pDstPath, const char* pSrcPath, S32 dstSize );
  278. void addPathExpando( const char* pExpandoName, const char* pPath );
  279. void removePathExpando( const char* pExpandoName );
  280. bool isPathExpando( const char* pExpandoName );
  281. StringTableEntry getPathExpando( const char* pExpandoName );
  282. U32 getPathExpandoCount( void );
  283. StringTableEntry getPathExpandoKey( U32 expandoIndex );
  284. StringTableEntry getPathExpandoValue( U32 expandoIndex );
  285. StringTableEntry getModNameFromPath(const char *path);
  286. /// Returns true if fn is a global scripting function.
  287. ///
  288. /// This looks in the global namespace. It also checks to see if fn
  289. /// is in the StringTable; if not, it returns false.
  290. bool isFunction(const char *fn);
  291. /// This is the basis for tab completion in the console.
  292. ///
  293. /// @note This is an internally used function. You probably don't
  294. /// care much about how this works.
  295. ///
  296. /// This function does some basic parsing to try to ascertain the namespace in which
  297. /// we are attempting to do tab completion, then bumps control off to the appropriate
  298. /// tabComplete function, either in SimObject or Namespace.
  299. ///
  300. /// @param inputBuffer Pointer to buffer containing starting data, or last result.
  301. /// @param cursorPos Location of cursor in this buffer. This is used to indicate
  302. /// what part of the string should be kept and what part should
  303. /// be advanced to the next match if any.
  304. /// @param maxResultLength Maximum amount of result data to put into inputBuffer. This
  305. /// is capped by MaxCompletionBufferSize.
  306. /// @param forwardTab Should we go forward to next match or backwards to previous
  307. /// match? True indicates forward.
  308. U32 tabComplete(char* inputBuffer, U32 cursorPos, U32 maxResultLength, bool forwardTab);
  309. /// @}
  310. /// @name Variable Management
  311. /// @{
  312. /// Add a console variable that references the value of a variable in C++ code.
  313. ///
  314. /// If a value is assigned to the console variable the C++ variable is updated,
  315. /// and vice-versa.
  316. ///
  317. /// @param name Global console variable name to create
  318. /// @param type The type of the C++ variable; see the ConsoleDynamicTypes enum for a complete list.
  319. /// @param pointer Pointer to the variable.
  320. /// @see ConsoleDynamicTypes
  321. bool addVariable(const char *name, S32 type, void *pointer);
  322. /// Remove a console variable.
  323. ///
  324. /// @param name Global console variable name to remove
  325. /// @return true if variable existed before removal.
  326. bool removeVariable(const char *name);
  327. /// Assign a string value to a locally scoped console variable
  328. ///
  329. /// @note The context of the variable is determined by gEvalState; that is,
  330. /// by the currently executing code.
  331. ///
  332. /// @param name Local console variable name to set
  333. /// @param value String value to assign to name
  334. void setLocalVariable(const char *name, const char *value);
  335. /// Retrieve the string value to a locally scoped console variable
  336. ///
  337. /// @note The context of the variable is determined by gEvalState; that is,
  338. /// by the currently executing code.
  339. ///
  340. /// @param name Local console variable name to get
  341. const char* getLocalVariable(const char* name);
  342. /// @}
  343. /// @name Global Variable Accessors
  344. /// @{
  345. /// Assign a string value to a global console variable
  346. /// @param name Global console variable name to set
  347. /// @param value String value to assign to this variable.
  348. void setVariable(const char *name, const char *value);
  349. /// Retrieve the string value of a global console variable
  350. /// @param name Global Console variable name to query
  351. /// @return The string value of the variable or "" if the variable does not exist.
  352. const char* getVariable(const char* name);
  353. /// Same as setVariable(), but for bools.
  354. void setBoolVariable (const char* name,bool var);
  355. /// Same as getVariable(), but for bools.
  356. ///
  357. /// @param name Name of the variable.
  358. /// @param def Default value to supply if no matching variable is found.
  359. bool getBoolVariable (const char* name,bool def = false);
  360. /// Same as setVariable(), but for ints.
  361. void setIntVariable (const char* name,S32 var);
  362. /// Same as getVariable(), but for ints.
  363. ///
  364. /// @param name Name of the variable.
  365. /// @param def Default value to supply if no matching variable is found.
  366. S32 getIntVariable (const char* name,S32 def = 0);
  367. /// Same as setVariable(), but for floats.
  368. void setFloatVariable(const char* name,F32 var);
  369. /// Same as getVariable(), but for floats.
  370. ///
  371. /// @param name Name of the variable.
  372. /// @param def Default value to supply if no matching variable is found.
  373. F32 getFloatVariable(const char* name,F32 def = .0f);
  374. /// @}
  375. /// @name Global Function Registration
  376. /// @{
  377. /// Register a C++ function with the console making it a global function callable from the scripting engine.
  378. ///
  379. /// @param name Name of the new function.
  380. /// @param cb Pointer to the function implementing the scripting call; a console callback function returning a specific type value.
  381. /// @param usage Documentation for this function. @ref console_autodoc
  382. /// @param minArgs Minimum number of arguments this function accepts
  383. /// @param maxArgs Maximum number of arguments this function accepts
  384. void addCommand(const char *name, StringCallback cb, const char *usage, S32 minArgs, S32 maxArgs);
  385. void addCommand(const char *name, IntCallback cb, const char *usage, S32 minArgs, S32 maxArg); ///< @copydoc addCommand(const char *, StringCallback, const char *, S32, S32)
  386. void addCommand(const char *name, FloatCallback cb, const char *usage, S32 minArgs, S32 maxArgs); ///< @copydoc addCommand(const char *, StringCallback, const char *, S32, S32)
  387. void addCommand(const char *name, VoidCallback cb, const char *usage, S32 minArgs, S32 maxArgs); ///< @copydoc addCommand(const char *, StringCallback, const char *, S32, S32)
  388. void addCommand(const char *name, BoolCallback cb, const char *usage, S32 minArgs, S32 maxArgs); ///< @copydoc addCommand(const char *, StringCallback, const char *, S32, S32)
  389. /// @}
  390. /// @name Namespace Function Registration
  391. /// @{
  392. /// Register a C++ function with the console making it callable
  393. /// as a method of the given namespace from the scripting engine.
  394. ///
  395. /// @param nameSpace Name of the namespace to associate the new function with; this is usually the name of a class.
  396. /// @param name Name of the new function.
  397. /// @param cb Pointer to the function implementing the scripting call; a console callback function returning a specific type value.
  398. /// @param usage Documentation for this function. @ref console_autodoc
  399. /// @param minArgs Minimum number of arguments this function accepts
  400. /// @param maxArgs Maximum number of arguments this function accepts
  401. void addCommand(const char *nameSpace, const char *name,StringCallback cb, const char *usage, S32 minArgs, S32 maxArgs);
  402. 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)
  403. 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)
  404. 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)
  405. 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)
  406. /// @}
  407. /// @name Special Purpose Registration
  408. ///
  409. /// These are special-purpose functions that exist to allow commands to be grouped, so
  410. /// that when we generate console docs, they can be more meaningfully presented.
  411. ///
  412. /// @ref console_autodoc "Click here for more information about console docs and grouping."
  413. ///
  414. /// @{
  415. void markCommandGroup (const char * nsName, const char *name, const char* usage=NULL);
  416. void beginCommandGroup(const char * nsName, const char *name, const char* usage);
  417. void endCommandGroup (const char * nsName, const char *name);
  418. /// @deprecated
  419. void addOverload (const char * nsName, const char *name, const char *altUsage);
  420. /// @}
  421. /// @name Console Output
  422. ///
  423. /// These functions process the formatted string and pass it to all the ConsumerCallbacks that are
  424. /// currently registered. The console log file and the console window callbacks are installed by default.
  425. ///
  426. /// @see addConsumer()
  427. /// @see removeConsumer()
  428. /// @{
  429. /// @param _format A stdlib printf style formatted out put string
  430. /// @param ... Variables to be written
  431. void printf(const char *_format, ...);
  432. /// @note The console window colors warning text as LIGHT GRAY.
  433. /// @param _format A stdlib printf style formatted out put string
  434. /// @param ... Variables to be written
  435. void warnf(const char *_format, ...);
  436. /// @note The console window colors warning text as RED.
  437. /// @param _format A stdlib printf style formatted out put string
  438. /// @param ... Variables to be written
  439. void errorf(const char *_format, ...);
  440. /// @note The console window colors warning text as LIGHT GRAY.
  441. /// @param type Allows you to associate the warning message with an internal module.
  442. /// @param _format A stdlib printf style formatted out put string
  443. /// @param ... Variables to be written
  444. /// @see Con::warnf()
  445. void warnf(ConsoleLogEntry::Type type, const char *_format, ...);
  446. /// @note The console window colors warning text as RED.
  447. /// @param type Allows you to associate the warning message with an internal module.
  448. /// @param _format A stdlib printf style formatted out put string
  449. /// @param ... Variables to be written
  450. /// @see Con::errorf()
  451. void errorf(ConsoleLogEntry::Type type, const char *_format, ...);
  452. /// clear the console log
  453. void cls( void );
  454. /// Prints a separator to the console.
  455. inline void printSeparator( void ) { printf("--------------------------------------------------------------------------------"); }
  456. /// Prints a separator to the console.
  457. inline void printBlankLine( void ) { printf(""); }
  458. /// @}
  459. /// Returns true when called from the main thread, false otherwise
  460. bool isMainThread();
  461. /// @name Console Execution
  462. ///
  463. /// These are functions relating to the execution of script code.
  464. ///
  465. /// @{
  466. /// Call a script function from C/C++ code.
  467. ///
  468. /// @param argc Number of elements in the argv parameter
  469. /// @param argv A character string array containing the name of the function
  470. /// to call followed by the arguments to that function.
  471. /// @code
  472. /// // Call a Torque script function called mAbs, having one parameter.
  473. /// char* argv[] = {"abs", "-9"};
  474. /// char* result = execute(2, argv);
  475. /// @endcode
  476. const char *execute(S32 argc, const char* argv[]);
  477. /// @see execute(S32 argc, const char* argv[])
  478. const char *executef(S32 argc, ...);
  479. /// Call a Torque Script member function of a SimObject from C/C++ code.
  480. /// @param object Object on which to execute the method call.
  481. /// @param argc Number of elements in the argv parameter (must be >2, see argv)
  482. /// @param argv A character string array containing the name of the member function
  483. /// to call followed by an empty parameter (gets filled with object ID)
  484. /// followed by arguments to that function.
  485. /// @code
  486. /// // Call the method setMode() on an object, passing it one parameter.
  487. ///
  488. /// char* argv[] = {"setMode", "", "2"};
  489. /// char* result = execute(mysimobject, 3, argv);
  490. /// @endcode
  491. // [neo, 5/10/2007 - #3010]
  492. // Added flag thisCallOnly to bypass dynamic method calls
  493. const char *execute(SimObject *object, S32 argc, const char *argv[], bool thisCallOnly = false);
  494. /// @see execute(SimObject *, S32 argc, const char *argv[])
  495. const char *executef(SimObject *, S32 argc, ...);
  496. /// Evaluate an arbitrary chunk of code.
  497. ///
  498. /// @param string Buffer containing code to execute.
  499. /// @param echo Should we echo the string to the console?
  500. /// @param fileName Indicate what file this code is coming from; used in error reporting and such.
  501. const char *evaluate(const char* string, bool echo = false, const char *fileName = NULL);
  502. /// Evaluate an arbitrary line of script.
  503. ///
  504. /// This wraps dVsprintf(), so you can substitute parameters into the code being executed.
  505. const char *evaluatef(const char* string, ...);
  506. /// @}
  507. /// @name Console Function Implementation Helpers
  508. ///
  509. /// The functions Con::getIntArg, Con::getFloatArg and Con::getArgBuffer(size) are used to
  510. /// allocate on the console stack string variables that will be passed into the next console
  511. // function called. This allows the console to avoid copying some data.
  512. ///
  513. /// getReturnBuffer lets you allocate stack space to return data in.
  514. /// @{
  515. ///
  516. char *getReturnBuffer(U32 bufferSize);
  517. char *getReturnBuffer(const char *stringToCopy);
  518. char *getArgBuffer(U32 bufferSize);
  519. char *getFloatArg(F64 arg);
  520. char *getIntArg (S32 arg);
  521. char* getBoolArg(bool arg);
  522. /// @}
  523. /// @name Namespaces
  524. /// @{
  525. Namespace *lookupNamespace(const char *nsName);
  526. bool linkNamespaces(const char *parentName, const char *childName);
  527. bool unlinkNamespaces(const char *parentName, const char *childName);
  528. /// @note This should only be called from consoleObject.h
  529. bool classLinkNamespaces(Namespace *parent, Namespace *child);
  530. /// @}
  531. /// @name Logging
  532. /// @{
  533. void getLockLog(ConsoleLogEntry * &log, U32 &size);
  534. void unlockLog(void);
  535. void setLogMode(S32 mode);
  536. /// @}
  537. /// @name Dynamic Type System
  538. /// @{
  539. ///
  540. /* void registerType( const char *typeName, S32 type, S32 size, GetDataFunction gdf, SetDataFunction sdf, bool isDatablockType = false );
  541. void registerType( const char* typeName, S32 type, S32 size, bool isDatablockType = false );
  542. void registerTypeGet( S32 type, GetDataFunction gdf );
  543. void registerTypeSet( S32 type, SetDataFunction sdf );
  544. const char *getTypeName(S32 type);
  545. bool isDatablockType( S32 type ); */
  546. void setData(S32 type, void *dptr, S32 index, S32 argc, const char **argv, EnumTable *tbl = NULL, BitSet32 flag = 0);
  547. const char *getData(S32 type, void *dptr, S32 index, EnumTable *tbl = NULL, BitSet32 flag = 0);
  548. /// @}
  549. };
  550. extern void expandEscape(char *dest, const char *src);
  551. extern bool collapseEscape(char *buf);
  552. extern U32 HashPointer(StringTableEntry ptr);
  553. /// This is the backend for the ConsoleMethod()/ConsoleFunction() macros.
  554. ///
  555. /// See the group ConsoleConstructor Innards for specifics on how this works.
  556. ///
  557. /// @see @ref console_autodoc
  558. /// @nosubgrouping
  559. class ConsoleConstructor
  560. {
  561. public:
  562. /// @name Entry Type Fields
  563. ///
  564. /// One of these is set based on the type of entry we want
  565. /// inserted in the console.
  566. ///
  567. /// @ref console_autodoc
  568. /// @{
  569. StringCallback sc; ///< A function/method that returns a string.
  570. IntCallback ic; ///< A function/method that returns an int.
  571. FloatCallback fc; ///< A function/method that returns a float.
  572. VoidCallback vc; ///< A function/method that returns nothing.
  573. BoolCallback bc; ///< A function/method that returns a bool.
  574. bool group; ///< Indicates that this is a group marker.
  575. bool overload; ///< Indicates that this is an overload marker.
  576. bool ns; ///< Indicates that this is a namespace marker.
  577. /// @deprecated Unused.
  578. /// @}
  579. /// Minimum/maximum number of arguments for the function.
  580. S32 mina, maxa;
  581. const char *usage; ///< Usage string.
  582. const char *funcName; ///< Function name.
  583. const char *className; ///< Class name.
  584. /// @name ConsoleConstructer Innards
  585. ///
  586. /// The ConsoleConstructor class is used as the backend for the ConsoleFunction() and
  587. /// ConsoleMethod() macros. The way it works takes advantage of several properties of
  588. /// C++.
  589. ///
  590. /// The ConsoleFunction()/ConsoleMethod() macros wrap the declaration of a ConsoleConstructor.
  591. ///
  592. /// @code
  593. /// // The definition of a ConsoleFunction using the macro
  594. /// ConsoleFunction(ExpandPath, const char*, 2, 2, "(string filePath)")
  595. /// {
  596. /// argc;
  597. /// char* ret = Con::getReturnBuffer( 1024 );
  598. /// Con::expandPath(ret, 1024, argv[1]);
  599. /// return ret;
  600. /// }
  601. ///
  602. /// // Resulting code
  603. /// static const char* cExpandPath(SimObject *, S32, const char **argv);
  604. /// static ConsoleConstructor
  605. /// gExpandPathobj(NULL,"ExpandPath", cExpandPath,
  606. /// "(string filePath)", 2, 2);
  607. /// static const char* cExpandPath(SimObject *, S32 argc, const char **argv)
  608. /// {
  609. /// argc;
  610. /// char* ret = Con::getReturnBuffer( 1024 );
  611. /// Con::expandPath(ret, 1024, argv[1]);
  612. /// return ret;
  613. /// }
  614. ///
  615. /// // A similar thing happens when you do a ConsoleMethod.
  616. /// @endcode
  617. ///
  618. /// As you can see, several global items are defined when you use the ConsoleFunction method.
  619. /// The macro constructs the name of these items from the parameters you passed it. Your
  620. /// implementation of the console function is is placed in a function with a name based on
  621. /// the actual name of the console funnction. In addition, a ConsoleConstructor is declared.
  622. ///
  623. /// Because it is defined as a global, the constructor for the ConsoleConstructor is called
  624. /// before execution of main() is started. The constructor is called once for each global
  625. /// ConsoleConstructor variable, in the order in which they were defined (this property only holds true
  626. /// within file scope).
  627. ///
  628. /// We have ConsoleConstructor create a linked list at constructor time, by storing a static
  629. /// pointer to the head of the list, and keeping a pointer to the next item in each instance
  630. /// of ConsoleConstructor. init() is a helper function in this process, automatically filling
  631. /// in commonly used fields and updating first and next as needed. In this way, a list of
  632. /// items to add to the console is assemble in memory, ready for use, before we start
  633. /// execution of the program proper.
  634. ///
  635. /// In Con::init(), ConsoleConstructor::setup() is called to process this prepared list. Each
  636. /// item in the list is iterated over, and the appropriate Con namespace functions (usually
  637. /// Con::addCommand) are invoked to register the ConsoleFunctions and ConsoleMethods in
  638. /// the appropriate namespaces.
  639. ///
  640. /// @see Namespace
  641. /// @see Con
  642. /// @{
  643. ConsoleConstructor *next;
  644. static ConsoleConstructor *first;
  645. void init(const char *cName, const char *fName, const char *usg, S32 minArgs, S32 maxArgs);
  646. static void setup();
  647. /// @}
  648. /// @name Basic Console Constructors
  649. /// @{
  650. ConsoleConstructor(const char *className, const char *funcName, StringCallback sfunc, const char* usage, S32 minArgs, S32 maxArgs);
  651. ConsoleConstructor(const char *className, const char *funcName, IntCallback ifunc, const char* usage, S32 minArgs, S32 maxArgs);
  652. ConsoleConstructor(const char *className, const char *funcName, FloatCallback ffunc, const char* usage, S32 minArgs, S32 maxArgs);
  653. ConsoleConstructor(const char *className, const char *funcName, VoidCallback vfunc, const char* usage, S32 minArgs, S32 maxArgs);
  654. ConsoleConstructor(const char *className, const char *funcName, BoolCallback bfunc, const char* usage, S32 minArgs, S32 maxArgs);
  655. /// @}
  656. /// @name Magic Console Constructors
  657. ///
  658. /// These perform various pieces of "magic" related to consoleDoc functionality.
  659. /// @ref console_autodoc
  660. /// @{
  661. /// Indicates a group marker. (A doxygen illusion)
  662. ///
  663. /// @see Con::markCommandGroup
  664. /// @ref console_autodoc
  665. ConsoleConstructor(const char *className, const char *groupName, const char* usage);
  666. /// Indicates a namespace usage string.
  667. ConsoleConstructor(const char *className, const char *usage);
  668. /// @}
  669. };
  670. /// @name Global Console Definition Macros
  671. ///
  672. /// @note If TORQUE_DEBUG is defined, then we gather documentation information, and
  673. /// do some extra sanity checks.
  674. ///
  675. /// @see ConsoleConstructor
  676. /// @ref console_autodoc
  677. /// @{
  678. // O hackery of hackeries
  679. #define conmethod_return_const return (const
  680. #define conmethod_return_S32 return (S32
  681. #define conmethod_return_F32 return (F32
  682. #define conmethod_nullify(val)
  683. #define conmethod_return_void conmethod_nullify(void
  684. #define conmethod_return_bool return (bool
  685. #define conmethod_return_ConsoleInt conmethod_return_S32
  686. #define conmethod_return_ConsoleFloat conmethod_return_F32
  687. #define conmethod_return_ConsoleVoid conmethod_return_void
  688. #define conmethod_return_ConsoleBool conmethod_return_bool
  689. #define conmethod_return_ConsoleString conmethod_return_const char*
  690. #if !defined(TORQUE_SHIPPING)
  691. // Console function return types
  692. #define ConsoleString const char*
  693. #define ConsoleInt S32
  694. #define ConsoleFloat F32
  695. #define ConsoleVoid void
  696. #define ConsoleBool bool
  697. // Console function macros
  698. # define ConsoleFunctionGroupBegin(groupName, usage) \
  699. static ConsoleConstructor gConsoleFunctionGroup##groupName##__GroupBegin(NULL,#groupName,usage);
  700. # define ConsoleFunction(name,returnType,minArgs,maxArgs,usage1) \
  701. static returnType c##name(SimObject *, S32, const char **argv); \
  702. static ConsoleConstructor g##name##obj(NULL,#name,c##name,usage1,minArgs,maxArgs); \
  703. static returnType c##name(SimObject *, S32 argc, const char **argv)
  704. # define ConsoleFunctionWithDocs(name,returnType,minArgs,maxArgs,argString) \
  705. static returnType c##name(SimObject *, S32, const char **argv); \
  706. static ConsoleConstructor g##name##obj(NULL,#name,c##name,#argString,minArgs,maxArgs); \
  707. static returnType c##name(SimObject *, S32 argc, const char **argv)
  708. # define ConsoleFunctionGroupEnd(groupName) \
  709. static ConsoleConstructor gConsoleFunctionGroup##groupName##__GroupEnd(NULL,#groupName,NULL);
  710. // Console method macros
  711. # define ConsoleNamespace(className, usage) \
  712. static ConsoleConstructor className##__Namespace(#className, usage);
  713. # define ConsoleMethodGroupBegin(className, groupName, usage) \
  714. static ConsoleConstructor className##groupName##__GroupBegin(#className,#groupName,usage);
  715. // note: we would want to expand the following macro into (Doxygen) comments!
  716. // we can not do that with a macro. these are here just as a reminder until completion
  717. # define ConsoleMethodRootGroupBeginWithDocs(className)
  718. # define ConsoleMethodGroupBeginWithDocs(className, superclassName)
  719. # define ConsoleMethod(className,name,returnType,minArgs,maxArgs,usage1) \
  720. static inline returnType c##className##name(className *, S32, const char **argv); \
  721. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  722. AssertFatal( dynamic_cast<className*>( object ), "Object passed to " #name " is not a " #className "!" ); \
  723. conmethod_return_##returnType ) c##className##name(static_cast<className*>(object),argc,argv); \
  724. }; \
  725. static ConsoleConstructor className##name##obj(#className,#name,c##className##name##caster,usage1,minArgs,maxArgs); \
  726. static inline returnType c##className##name(className *object, S32 argc, const char **argv)
  727. # define ConsoleMethodWithDocs(className,name,returnType,minArgs,maxArgs,argString) \
  728. static inline returnType c##className##name(className *, S32, const char **argv); \
  729. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  730. AssertFatal( dynamic_cast<className*>( object ), "Object passed to " #name " is not a " #className "!" ); \
  731. conmethod_return_##returnType ) c##className##name(static_cast<className*>(object),argc,argv); \
  732. }; \
  733. static ConsoleConstructor className##name##obj(#className,#name,c##className##name##caster,#argString,minArgs,maxArgs); \
  734. static inline returnType c##className##name(className *object, S32 argc, const char **argv)
  735. # define ConsoleStaticMethod(className,name,returnType,minArgs,maxArgs,usage1) \
  736. static inline returnType c##className##name(S32, const char **); \
  737. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  738. conmethod_return_##returnType ) c##className##name(argc,argv); \
  739. }; \
  740. static ConsoleConstructor \
  741. className##name##obj(#className,#name,c##className##name##caster,usage1,minArgs,maxArgs); \
  742. static inline returnType c##className##name(S32 argc, const char **argv)
  743. # define ConsoleStaticMethodWithDocs(className,name,returnType,minArgs,maxArgs,argString) \
  744. static inline returnType c##className##name(S32, const char **); \
  745. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  746. conmethod_return_##returnType ) c##className##name(argc,argv); \
  747. }; \
  748. static ConsoleConstructor \
  749. className##name##obj(#className,#name,c##className##name##caster,#argString,minArgs,maxArgs); \
  750. static inline returnType c##className##name(S32 argc, const char **argv)
  751. # define ConsoleMethodGroupEnd(className, groupName) \
  752. static ConsoleConstructor className##groupName##__GroupEnd(#className,#groupName,NULL);
  753. # define ConsoleMethodRootGroupEndWithDocs(className)
  754. # define ConsoleMethodGroupEndWithDocs(className)
  755. #else
  756. // These do nothing if we don't want doc information.
  757. # define ConsoleFunctionGroupBegin(groupName, usage)
  758. # define ConsoleFunctionGroupEnd(groupName)
  759. # define ConsoleNamespace(className, usage)
  760. # define ConsoleMethodGroupBegin(className, groupName, usage)
  761. # define ConsoleMethodGroupEnd(className, groupName)
  762. // These are identical to what's above, we just want to null out the usage strings.
  763. # define ConsoleFunction(name,returnType,minArgs,maxArgs,usage1) \
  764. static returnType c##name(SimObject *, S32, const char **); \
  765. static ConsoleConstructor g##name##obj(NULL,#name,c##name,"",minArgs,maxArgs);\
  766. static returnType c##name(SimObject *, S32 argc, const char **argv)
  767. # define ConsoleMethod(className,name,returnType,minArgs,maxArgs,usage1) \
  768. static inline returnType c##className##name(className *, S32, const char **argv); \
  769. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  770. conmethod_return_##returnType ) c##className##name(static_cast<className*>(object),argc,argv); \
  771. }; \
  772. static ConsoleConstructor \
  773. className##name##obj(#className,#name,c##className##name##caster,"",minArgs,maxArgs); \
  774. static inline returnType c##className##name(className *object, S32 argc, const char **argv)
  775. # define ConsoleStaticMethod(className,name,returnType,minArgs,maxArgs,usage1) \
  776. static inline returnType c##className##name(S32, const char **); \
  777. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  778. conmethod_return_##returnType ) c##className##name(argc,argv); \
  779. }; \
  780. static ConsoleConstructor \
  781. className##name##obj(#className,#name,c##className##name##caster,"",minArgs,maxArgs); \
  782. static inline returnType c##className##name(S32 argc, const char **argv)
  783. #endif
  784. /// @}
  785. #endif