console.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. /// Prints a separator to the console.
  452. inline void printSeparator( void ) { printf("--------------------------------------------------------------------------------"); }
  453. /// Prints a separator to the console.
  454. inline void printBlankLine( void ) { printf(""); }
  455. /// @}
  456. /// Returns true when called from the main thread, false otherwise
  457. bool isMainThread();
  458. /// @name Console Execution
  459. ///
  460. /// These are functions relating to the execution of script code.
  461. ///
  462. /// @{
  463. /// Call a script function from C/C++ code.
  464. ///
  465. /// @param argc Number of elements in the argv parameter
  466. /// @param argv A character string array containing the name of the function
  467. /// to call followed by the arguments to that function.
  468. /// @code
  469. /// // Call a Torque script function called mAbs, having one parameter.
  470. /// char* argv[] = {"abs", "-9"};
  471. /// char* result = execute(2, argv);
  472. /// @endcode
  473. const char *execute(S32 argc, const char* argv[]);
  474. /// @see execute(S32 argc, const char* argv[])
  475. const char *executef(S32 argc, ...);
  476. /// Call a Torque Script member function of a SimObject from C/C++ code.
  477. /// @param object Object on which to execute the method call.
  478. /// @param argc Number of elements in the argv parameter (must be >2, see argv)
  479. /// @param argv A character string array containing the name of the member function
  480. /// to call followed by an empty parameter (gets filled with object ID)
  481. /// followed by arguments to that function.
  482. /// @code
  483. /// // Call the method setMode() on an object, passing it one parameter.
  484. ///
  485. /// char* argv[] = {"setMode", "", "2"};
  486. /// char* result = execute(mysimobject, 3, argv);
  487. /// @endcode
  488. // [neo, 5/10/2007 - #3010]
  489. // Added flag thisCallOnly to bypass dynamic method calls
  490. const char *execute(SimObject *object, S32 argc, const char *argv[], bool thisCallOnly = false);
  491. /// @see execute(SimObject *, S32 argc, const char *argv[])
  492. const char *executef(SimObject *, S32 argc, ...);
  493. /// Evaluate an arbitrary chunk of code.
  494. ///
  495. /// @param string Buffer containing code to execute.
  496. /// @param echo Should we echo the string to the console?
  497. /// @param fileName Indicate what file this code is coming from; used in error reporting and such.
  498. const char *evaluate(const char* string, bool echo = false, const char *fileName = NULL);
  499. /// Evaluate an arbitrary line of script.
  500. ///
  501. /// This wraps dVsprintf(), so you can substitute parameters into the code being executed.
  502. const char *evaluatef(const char* string, ...);
  503. /// @}
  504. /// @name Console Function Implementation Helpers
  505. ///
  506. /// The functions Con::getIntArg, Con::getFloatArg and Con::getArgBuffer(size) are used to
  507. /// allocate on the console stack string variables that will be passed into the next console
  508. // function called. This allows the console to avoid copying some data.
  509. ///
  510. /// getReturnBuffer lets you allocate stack space to return data in.
  511. /// @{
  512. ///
  513. char *getReturnBuffer(U32 bufferSize);
  514. char *getReturnBuffer(const char *stringToCopy);
  515. char *getArgBuffer(U32 bufferSize);
  516. char *getFloatArg(F64 arg);
  517. char *getIntArg (S32 arg);
  518. char* getBoolArg(bool arg);
  519. /// @}
  520. /// @name Namespaces
  521. /// @{
  522. Namespace *lookupNamespace(const char *nsName);
  523. bool linkNamespaces(const char *parentName, const char *childName);
  524. bool unlinkNamespaces(const char *parentName, const char *childName);
  525. /// @note This should only be called from consoleObject.h
  526. bool classLinkNamespaces(Namespace *parent, Namespace *child);
  527. /// @}
  528. /// @name Logging
  529. /// @{
  530. void getLockLog(ConsoleLogEntry * &log, U32 &size);
  531. void unlockLog(void);
  532. void setLogMode(S32 mode);
  533. /// @}
  534. /// @name Dynamic Type System
  535. /// @{
  536. ///
  537. /* void registerType( const char *typeName, S32 type, S32 size, GetDataFunction gdf, SetDataFunction sdf, bool isDatablockType = false );
  538. void registerType( const char* typeName, S32 type, S32 size, bool isDatablockType = false );
  539. void registerTypeGet( S32 type, GetDataFunction gdf );
  540. void registerTypeSet( S32 type, SetDataFunction sdf );
  541. const char *getTypeName(S32 type);
  542. bool isDatablockType( S32 type ); */
  543. void setData(S32 type, void *dptr, S32 index, S32 argc, const char **argv, EnumTable *tbl = NULL, BitSet32 flag = 0);
  544. const char *getData(S32 type, void *dptr, S32 index, EnumTable *tbl = NULL, BitSet32 flag = 0);
  545. /// @}
  546. };
  547. extern void expandEscape(char *dest, const char *src);
  548. extern bool collapseEscape(char *buf);
  549. extern S32 HashPointer(StringTableEntry ptr);
  550. /// This is the backend for the ConsoleMethod()/ConsoleFunction() macros.
  551. ///
  552. /// See the group ConsoleConstructor Innards for specifics on how this works.
  553. ///
  554. /// @see @ref console_autodoc
  555. /// @nosubgrouping
  556. class ConsoleConstructor
  557. {
  558. public:
  559. /// @name Entry Type Fields
  560. ///
  561. /// One of these is set based on the type of entry we want
  562. /// inserted in the console.
  563. ///
  564. /// @ref console_autodoc
  565. /// @{
  566. StringCallback sc; ///< A function/method that returns a string.
  567. IntCallback ic; ///< A function/method that returns an int.
  568. FloatCallback fc; ///< A function/method that returns a float.
  569. VoidCallback vc; ///< A function/method that returns nothing.
  570. BoolCallback bc; ///< A function/method that returns a bool.
  571. bool group; ///< Indicates that this is a group marker.
  572. bool overload; ///< Indicates that this is an overload marker.
  573. bool ns; ///< Indicates that this is a namespace marker.
  574. /// @deprecated Unused.
  575. /// @}
  576. /// Minimum/maximum number of arguments for the function.
  577. S32 mina, maxa;
  578. const char *usage; ///< Usage string.
  579. const char *funcName; ///< Function name.
  580. const char *className; ///< Class name.
  581. /// @name ConsoleConstructer Innards
  582. ///
  583. /// The ConsoleConstructor class is used as the backend for the ConsoleFunction() and
  584. /// ConsoleMethod() macros. The way it works takes advantage of several properties of
  585. /// C++.
  586. ///
  587. /// The ConsoleFunction()/ConsoleMethod() macros wrap the declaration of a ConsoleConstructor.
  588. ///
  589. /// @code
  590. /// // The definition of a ConsoleFunction using the macro
  591. /// ConsoleFunction(ExpandPath, const char*, 2, 2, "(string filePath)")
  592. /// {
  593. /// argc;
  594. /// char* ret = Con::getReturnBuffer( 1024 );
  595. /// Con::expandPath(ret, 1024, argv[1]);
  596. /// return ret;
  597. /// }
  598. ///
  599. /// // Resulting code
  600. /// static const char* cExpandPath(SimObject *, S32, const char **argv);
  601. /// static ConsoleConstructor
  602. /// gExpandPathobj(NULL,"ExpandPath", cExpandPath,
  603. /// "(string filePath)", 2, 2);
  604. /// static const char* cExpandPath(SimObject *, S32 argc, const char **argv)
  605. /// {
  606. /// argc;
  607. /// char* ret = Con::getReturnBuffer( 1024 );
  608. /// Con::expandPath(ret, 1024, argv[1]);
  609. /// return ret;
  610. /// }
  611. ///
  612. /// // A similar thing happens when you do a ConsoleMethod.
  613. /// @endcode
  614. ///
  615. /// As you can see, several global items are defined when you use the ConsoleFunction method.
  616. /// The macro constructs the name of these items from the parameters you passed it. Your
  617. /// implementation of the console function is is placed in a function with a name based on
  618. /// the actual name of the console funnction. In addition, a ConsoleConstructor is declared.
  619. ///
  620. /// Because it is defined as a global, the constructor for the ConsoleConstructor is called
  621. /// before execution of main() is started. The constructor is called once for each global
  622. /// ConsoleConstructor variable, in the order in which they were defined (this property only holds true
  623. /// within file scope).
  624. ///
  625. /// We have ConsoleConstructor create a linked list at constructor time, by storing a static
  626. /// pointer to the head of the list, and keeping a pointer to the next item in each instance
  627. /// of ConsoleConstructor. init() is a helper function in this process, automatically filling
  628. /// in commonly used fields and updating first and next as needed. In this way, a list of
  629. /// items to add to the console is assemble in memory, ready for use, before we start
  630. /// execution of the program proper.
  631. ///
  632. /// In Con::init(), ConsoleConstructor::setup() is called to process this prepared list. Each
  633. /// item in the list is iterated over, and the appropriate Con namespace functions (usually
  634. /// Con::addCommand) are invoked to register the ConsoleFunctions and ConsoleMethods in
  635. /// the appropriate namespaces.
  636. ///
  637. /// @see Namespace
  638. /// @see Con
  639. /// @{
  640. ConsoleConstructor *next;
  641. static ConsoleConstructor *first;
  642. void init(const char *cName, const char *fName, const char *usg, S32 minArgs, S32 maxArgs);
  643. static void setup();
  644. /// @}
  645. /// @name Basic Console Constructors
  646. /// @{
  647. ConsoleConstructor(const char *className, const char *funcName, StringCallback sfunc, const char* usage, S32 minArgs, S32 maxArgs);
  648. ConsoleConstructor(const char *className, const char *funcName, IntCallback ifunc, const char* usage, S32 minArgs, S32 maxArgs);
  649. ConsoleConstructor(const char *className, const char *funcName, FloatCallback ffunc, const char* usage, S32 minArgs, S32 maxArgs);
  650. ConsoleConstructor(const char *className, const char *funcName, VoidCallback vfunc, const char* usage, S32 minArgs, S32 maxArgs);
  651. ConsoleConstructor(const char *className, const char *funcName, BoolCallback bfunc, const char* usage, S32 minArgs, S32 maxArgs);
  652. /// @}
  653. /// @name Magic Console Constructors
  654. ///
  655. /// These perform various pieces of "magic" related to consoleDoc functionality.
  656. /// @ref console_autodoc
  657. /// @{
  658. /// Indicates a group marker. (A doxygen illusion)
  659. ///
  660. /// @see Con::markCommandGroup
  661. /// @ref console_autodoc
  662. ConsoleConstructor(const char *className, const char *groupName, const char* usage);
  663. /// Indicates a namespace usage string.
  664. ConsoleConstructor(const char *className, const char *usage);
  665. /// @}
  666. };
  667. /// @name Global Console Definition Macros
  668. ///
  669. /// @note If TORQUE_DEBUG is defined, then we gather documentation information, and
  670. /// do some extra sanity checks.
  671. ///
  672. /// @see ConsoleConstructor
  673. /// @ref console_autodoc
  674. /// @{
  675. // O hackery of hackeries
  676. #define conmethod_return_const return (const
  677. #define conmethod_return_S32 return (S32
  678. #define conmethod_return_F32 return (F32
  679. #define conmethod_nullify(val)
  680. #define conmethod_return_void conmethod_nullify(void
  681. #define conmethod_return_bool return (bool
  682. #define conmethod_return_ConsoleInt conmethod_return_S32
  683. #define conmethod_return_ConsoleFloat conmethod_return_F32
  684. #define conmethod_return_ConsoleVoid conmethod_return_void
  685. #define conmethod_return_ConsoleBool conmethod_return_bool
  686. #define conmethod_return_ConsoleString conmethod_return_const char*
  687. #if !defined(TORQUE_SHIPPING)
  688. // Console function return types
  689. #define ConsoleString const char*
  690. #define ConsoleInt S32
  691. #define ConsoleFloat F32
  692. #define ConsoleVoid void
  693. #define ConsoleBool bool
  694. // Console function macros
  695. # define ConsoleFunctionGroupBegin(groupName, usage) \
  696. static ConsoleConstructor gConsoleFunctionGroup##groupName##__GroupBegin(NULL,#groupName,usage);
  697. # define ConsoleFunction(name,returnType,minArgs,maxArgs,usage1) \
  698. static returnType c##name(SimObject *, S32, const char **argv); \
  699. static ConsoleConstructor g##name##obj(NULL,#name,c##name,usage1,minArgs,maxArgs); \
  700. static returnType c##name(SimObject *, S32 argc, const char **argv)
  701. # define ConsoleFunctionWithDocs(name,returnType,minArgs,maxArgs,argString) \
  702. static returnType c##name(SimObject *, S32, const char **argv); \
  703. static ConsoleConstructor g##name##obj(NULL,#name,c##name,#argString,minArgs,maxArgs); \
  704. static returnType c##name(SimObject *, S32 argc, const char **argv)
  705. # define ConsoleFunctionGroupEnd(groupName) \
  706. static ConsoleConstructor gConsoleFunctionGroup##groupName##__GroupEnd(NULL,#groupName,NULL);
  707. // Console method macros
  708. # define ConsoleNamespace(className, usage) \
  709. static ConsoleConstructor className##__Namespace(#className, usage);
  710. # define ConsoleMethodGroupBegin(className, groupName, usage) \
  711. static ConsoleConstructor className##groupName##__GroupBegin(#className,#groupName,usage);
  712. // note: we would want to expand the following macro into (Doxygen) comments!
  713. // we can not do that with a macro. these are here just as a reminder until completion
  714. # define ConsoleMethodGroupBeginWithDocs(className, superclassName)
  715. # define ConsoleMethod(className,name,returnType,minArgs,maxArgs,usage1) \
  716. static inline returnType c##className##name(className *, S32, const char **argv); \
  717. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  718. AssertFatal( dynamic_cast<className*>( object ), "Object passed to " #name " is not a " #className "!" ); \
  719. conmethod_return_##returnType ) c##className##name(static_cast<className*>(object),argc,argv); \
  720. }; \
  721. static ConsoleConstructor className##name##obj(#className,#name,c##className##name##caster,usage1,minArgs,maxArgs); \
  722. static inline returnType c##className##name(className *object, S32 argc, const char **argv)
  723. # define ConsoleMethodWithDocs(className,name,returnType,minArgs,maxArgs,argString) \
  724. static inline returnType c##className##name(className *, S32, const char **argv); \
  725. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  726. AssertFatal( dynamic_cast<className*>( object ), "Object passed to " #name " is not a " #className "!" ); \
  727. conmethod_return_##returnType ) c##className##name(static_cast<className*>(object),argc,argv); \
  728. }; \
  729. static ConsoleConstructor className##name##obj(#className,#name,c##className##name##caster,#argString,minArgs,maxArgs); \
  730. static inline returnType c##className##name(className *object, S32 argc, const char **argv)
  731. # define ConsoleStaticMethod(className,name,returnType,minArgs,maxArgs,usage1) \
  732. static inline returnType c##className##name(S32, const char **); \
  733. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  734. conmethod_return_##returnType ) c##className##name(argc,argv); \
  735. }; \
  736. static ConsoleConstructor \
  737. className##name##obj(#className,#name,c##className##name##caster,usage1,minArgs,maxArgs); \
  738. static inline returnType c##className##name(S32 argc, const char **argv)
  739. # define ConsoleStaticMethodWithDocs(className,name,returnType,minArgs,maxArgs,argString) \
  740. static inline returnType c##className##name(S32, const char **); \
  741. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  742. conmethod_return_##returnType ) c##className##name(argc,argv); \
  743. }; \
  744. static ConsoleConstructor \
  745. className##name##obj(#className,#name,c##className##name##caster,#argString,minArgs,maxArgs); \
  746. static inline returnType c##className##name(S32 argc, const char **argv)
  747. # define ConsoleMethodGroupEnd(className, groupName) \
  748. static ConsoleConstructor className##groupName##__GroupEnd(#className,#groupName,NULL);
  749. #else
  750. // These do nothing if we don't want doc information.
  751. # define ConsoleFunctionGroupBegin(groupName, usage)
  752. # define ConsoleFunctionGroupEnd(groupName)
  753. # define ConsoleNamespace(className, usage)
  754. # define ConsoleMethodGroupBegin(className, groupName, usage)
  755. # define ConsoleMethodGroupEnd(className, groupName)
  756. // These are identical to what's above, we just want to null out the usage strings.
  757. # define ConsoleFunction(name,returnType,minArgs,maxArgs,usage1) \
  758. static returnType c##name(SimObject *, S32, const char **); \
  759. static ConsoleConstructor g##name##obj(NULL,#name,c##name,"",minArgs,maxArgs);\
  760. static returnType c##name(SimObject *, S32 argc, const char **argv)
  761. # define ConsoleMethod(className,name,returnType,minArgs,maxArgs,usage1) \
  762. static inline returnType c##className##name(className *, S32, const char **argv); \
  763. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  764. conmethod_return_##returnType ) c##className##name(static_cast<className*>(object),argc,argv); \
  765. }; \
  766. static ConsoleConstructor \
  767. className##name##obj(#className,#name,c##className##name##caster,"",minArgs,maxArgs); \
  768. static inline returnType c##className##name(className *object, S32 argc, const char **argv)
  769. # define ConsoleStaticMethod(className,name,returnType,minArgs,maxArgs,usage1) \
  770. static inline returnType c##className##name(S32, const char **); \
  771. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  772. conmethod_return_##returnType ) c##className##name(argc,argv); \
  773. }; \
  774. static ConsoleConstructor \
  775. className##name##obj(#className,#name,c##className##name##caster,"",minArgs,maxArgs); \
  776. static inline returnType c##className##name(S32 argc, const char **argv)
  777. #endif
  778. /// @}
  779. #endif