console.h 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "core/bitSet.h"
  29. #endif
  30. #include <stdarg.h>
  31. #include "core/util/journal/journaledSignal.h"
  32. class SimObject;
  33. class Namespace;
  34. struct ConsoleFunctionHeader;
  35. class EngineEnumTable;
  36. typedef EngineEnumTable EnumTable;
  37. template< typename T > S32 TYPEID();
  38. /// @defgroup console_system Console System
  39. /// The Console system is the basis for logging, SimObject, and TorqueScript itself.
  40. ///
  41. /// @{
  42. /// Indicates that warnings about undefined script variables should be displayed.
  43. ///
  44. /// @note This is set and controlled by script.
  45. extern bool gWarnUndefinedScriptVariables;
  46. enum StringTableConstants
  47. {
  48. StringTagPrefixByte = 0x01 ///< Magic value prefixed to tagged strings.
  49. };
  50. /// Represents an entry in the log.
  51. struct ConsoleLogEntry
  52. {
  53. /// This field indicates the severity of the log entry.
  54. ///
  55. /// Log entries are filtered and displayed differently based on
  56. /// their severity. Errors are highlighted red, while normal entries
  57. /// are displayed as normal text. Often times, the engine will be
  58. /// configured to hide all log entries except warnings or errors,
  59. /// or to perform a special notification when it encounters an error.
  60. enum Level
  61. {
  62. Normal = 0,
  63. Warning,
  64. Error,
  65. NUM_CLASS
  66. } mLevel;
  67. /// Used to associate a log entry with a module.
  68. ///
  69. /// Log entries can come from different sources; for instance,
  70. /// the scripting engine, or the network code. This allows the
  71. /// logging system to be aware of where different log entries
  72. /// originated from.
  73. enum Type
  74. {
  75. General = 0,
  76. Assert,
  77. Script,
  78. GUI,
  79. Network,
  80. GGConnect,
  81. NUM_TYPE
  82. } mType;
  83. /// Indicates the actual log entry.
  84. ///
  85. /// This contains a description of the event being logged.
  86. /// For instance, "unable to access file", or "player connected
  87. /// successfully", or nearly anything else you might imagine.
  88. ///
  89. /// Typically, the description should contain a concise, descriptive
  90. /// string describing whatever is being logged. Whenever possible,
  91. /// include useful details like the name of the file being accessed,
  92. /// or the id of the player or GuiControl, so that if a log needs
  93. /// to be used to locate a bug, it can be done as painlessly as
  94. /// possible.
  95. const char *mString;
  96. };
  97. typedef const char *StringTableEntry;
  98. /// @defgroup console_callbacks Scripting Engine Callbacks
  99. ///
  100. /// The scripting engine makes heavy use of callbacks to represent
  101. /// function exposed to the scripting language. StringCallback,
  102. /// IntCallback, FloatCallback, VoidCallback, and BoolCallback all
  103. /// represent exposed script functions returning different types.
  104. ///
  105. /// ConsumerCallback is used with the function Con::addConsumer; functions
  106. /// registered with Con::addConsumer are called whenever something is outputted
  107. /// to the console. For instance, the TelnetConsole registers itself with the
  108. /// console so it can echo the console over the network.
  109. ///
  110. /// @note Callbacks to the scripting language - for instance, onExit(), which is
  111. /// a script function called when the engine is shutting down - are handled
  112. /// using Con::executef() and kin.
  113. /// @{
  114. ///
  115. typedef const char * (*StringCallback)(SimObject *obj, S32 argc, const char *argv[]);
  116. typedef S32 (*IntCallback)(SimObject *obj, S32 argc, const char *argv[]);
  117. typedef F32 (*FloatCallback)(SimObject *obj, S32 argc, const char *argv[]);
  118. typedef void (*VoidCallback)(SimObject *obj, S32 argc, const char *argv[]); // We have it return a value so things don't break..
  119. typedef bool (*BoolCallback)(SimObject *obj, S32 argc, const char *argv[]);
  120. typedef void (*ConsumerCallback)(U32 level, const char *consoleLine);
  121. /// @}
  122. /// @defgroup console_types Scripting Engine Type Functions
  123. ///
  124. /// @see Con::registerType
  125. /// @{
  126. typedef const char* (*GetDataFunction)(void *dptr, EnumTable *tbl, BitSet32 flag);
  127. typedef void (*SetDataFunction)(void *dptr, S32 argc, const char **argv, EnumTable *tbl, BitSet32 flag);
  128. /// @}
  129. /// This namespace contains the core of the console functionality.
  130. ///
  131. /// @section con_intro Introduction
  132. ///
  133. /// The console is a key part of Torque's architecture. It allows direct run-time control
  134. /// of many aspects of the engine.
  135. ///
  136. /// @nosubgrouping
  137. namespace Con
  138. {
  139. /// Various configuration constants.
  140. enum Constants
  141. {
  142. /// This is the version number associated with DSO files.
  143. ///
  144. /// If you make any changes to the way the scripting language works
  145. /// (such as DSO format changes, adding/removing op-codes) that would
  146. /// break compatibility, then you should increment this.
  147. ///
  148. /// If you make a really major change, increment it to the next multiple
  149. /// of ten.
  150. ///
  151. /// 12/29/04 - BJG - 33->34 Removed some opcodes, part of namespace upgrade.
  152. /// 12/30/04 - BJG - 34->35 Reordered some things, further general shuffling.
  153. /// 11/03/05 - BJG - 35->36 Integrated new debugger code.
  154. // 09/08/06 - THB - 36->37 New opcode for internal names
  155. // 09/15/06 - THB - 37->38 Added unit conversions
  156. // 11/23/06 - THB - 38->39 Added recursive internal name operator
  157. // 02/15/07 - THB - 39->40 Bumping to 40 for TGB since the console has been
  158. // majorly hacked without the version number being bumped
  159. // 02/16/07 - THB - 40->41 newmsg operator
  160. // 06/15/07 - THB - 41->42 script types
  161. /// 07/31/07 - THB - 42->43 Patch from Andreas Kirsch: Added opcode to support nested new declarations.
  162. /// 09/12/07 - CAF - 43->44 remove newmsg operator
  163. /// 09/27/07 - RDB - 44->45 Patch from Andreas Kirsch: Added opcode to support correct void return
  164. /// 01/13/09 - TMS - 45->46 Added script assert
  165. /// 09/07/14 - jamesu - 46->47 64bit support
  166. DSOVersion = 47,
  167. MaxLineLength = 512, ///< Maximum length of a line of console input.
  168. MaxDataTypes = 256 ///< Maximum number of registered data types.
  169. };
  170. /// @name Control Functions
  171. ///
  172. /// The console must be initialized and shutdown appropriately during the
  173. /// lifetime of the app. These functions are used to manage this behavior.
  174. ///
  175. /// @note Torque deals with this aspect of console management, so you don't need
  176. /// to call these functions in normal usage of the engine.
  177. /// @{
  178. /// Initializes the console.
  179. ///
  180. /// This performs the following steps:
  181. /// - Calls Namespace::init() to initialize the scripting namespace hierarchy.
  182. /// - Calls ConsoleConstructor::setup() to initialize globally defined console
  183. /// methods and functions.
  184. /// - Registers some basic global script variables.
  185. /// - Calls AbstractClassRep::init() to initialize Torque's class database.
  186. /// - Registers some basic global script functions that couldn't usefully
  187. /// be defined anywhere else.
  188. void init();
  189. /// Shuts down the console.
  190. ///
  191. /// This performs the following steps:
  192. /// - Closes the console log file.
  193. /// - Calls Namespace::shutdown() to shut down the scripting namespace hierarchy.
  194. void shutdown();
  195. /// Is the console active at this time?
  196. bool isActive();
  197. /// @}
  198. /// @name Console Consumers
  199. ///
  200. /// The console distributes its output through Torque by using
  201. /// consumers. Every time a new line is printed to the console,
  202. /// all the ConsumerCallbacks registered using addConsumer are
  203. /// called, in order.
  204. ///
  205. /// @note The GuiConsole control, which provides the on-screen
  206. /// in-game console, uses a different technique to render
  207. /// the console. It calls getLockLog() to lock the Vector
  208. /// of on-screen console entries, then it renders them as
  209. /// needed. While the Vector is locked, the console will
  210. /// not change the Vector. When the GuiConsole control is
  211. /// done with the console entries, it calls unlockLog()
  212. /// to tell the console that it is again safe to modify
  213. /// the Vector.
  214. ///
  215. /// @see TelnetConsole
  216. /// @see TelnetDebugger
  217. /// @see WinConsole
  218. /// @see MacCarbConsole
  219. /// @see StdConsole
  220. /// @see ConsoleLogger
  221. ///
  222. /// @{
  223. ///
  224. void addConsumer(ConsumerCallback cb);
  225. void removeConsumer(ConsumerCallback cb);
  226. typedef JournaledSignal<void(RawData)> ConsoleInputEvent;
  227. /// Called from the native consoles to provide lines of console input
  228. /// to process. This will schedule it for execution ASAP.
  229. extern ConsoleInputEvent smConsoleInput;
  230. /// @}
  231. /// @name Miscellaneous
  232. /// @{
  233. /// Remove color marking information from a string.
  234. ///
  235. /// @note It does this in-place, so be careful! It may
  236. /// potentially blast data if you're not careful.
  237. /// When in doubt, make a copy of the string first.
  238. void stripColorChars(char* line);
  239. /// Convert from a relative script path to an absolute script path.
  240. ///
  241. /// This is used in (among other places) the exec() script function, which
  242. /// takes a parameter indicating a script file and executes it. Script paths
  243. /// can be one of:
  244. /// - <b>Absolute:</b> <i>fps/foo/bar.cs</i> Paths of this sort are passed
  245. /// through.
  246. /// - <b>Mod-relative:</b> <i>~/foo/bar.cs</i> Paths of this sort have their
  247. /// replaced with the name of the current mod.
  248. /// - <b>File-relative:</b> <i>./baz/blip.cs</i> Paths of this sort are
  249. /// calculated relative to the path of the current scripting file.
  250. ///
  251. /// @note This function determines paths relative to the currently executing
  252. /// CodeBlock. Calling it outside of script execution will result in
  253. /// it directly copying src to filename, since it won't know to what the
  254. /// path is relative!
  255. ///
  256. /// @param filename Pointer to string buffer to fill with absolute path.
  257. /// @param size Size of buffer pointed to by filename.
  258. /// @param src Original, possibly relative script path.
  259. bool expandScriptFilename(char *filename, U32 size, const char *src);
  260. bool expandGameScriptFilename(char *filename, U32 size, const char *src);
  261. bool expandToolScriptFilename(char *filename, U32 size, const char *src);
  262. bool collapseScriptFilename(char *filename, U32 size, const char *src);
  263. bool isCurrentScriptToolScript();
  264. StringTableEntry getModNameFromPath(const char *path);
  265. /// Returns true if fn is a global scripting function.
  266. ///
  267. /// This looks in the global namespace. It also checks to see if fn
  268. /// is in the StringTable; if not, it returns false.
  269. bool isFunction(const char *fn);
  270. /// This is the basis for tab completion in the console.
  271. ///
  272. /// @note This is an internally used function. You probably don't
  273. /// care much about how this works.
  274. ///
  275. /// This function does some basic parsing to try to ascertain the namespace in which
  276. /// we are attempting to do tab completion, then bumps control off to the appropriate
  277. /// tabComplete function, either in SimObject or Namespace.
  278. ///
  279. /// @param inputBuffer Pointer to buffer containing starting data, or last result.
  280. /// @param cursorPos Location of cursor in this buffer. This is used to indicate
  281. /// what part of the string should be kept and what part should
  282. /// be advanced to the next match if any.
  283. /// @param maxResultLength Maximum amount of result data to put into inputBuffer. This
  284. /// is capped by MaxCompletionBufferSize.
  285. /// @param forwardTab Should we go forward to next match or backwards to previous
  286. /// match? True indicates forward.
  287. U32 tabComplete(char* inputBuffer, U32 cursorPos, U32 maxResultLength, bool forwardTab);
  288. /// @}
  289. /// @name Variable Management
  290. /// @{
  291. /// The delegate signature for the variable assignment notifications.
  292. ///
  293. /// @see addVariableNotify, removeVariableNotify
  294. typedef Delegate<void()> NotifyDelegate;
  295. /// Add a console variable that references the value of a variable in C++ code.
  296. ///
  297. /// If a value is assigned to the console variable the C++ variable is updated,
  298. /// and vice-versa.
  299. ///
  300. /// @param name Global console variable name to create.
  301. /// @param type The type of the C++ variable; see the ConsoleDynamicTypes enum for a complete list.
  302. /// @param pointer Pointer to the variable.
  303. /// @param usage Documentation string.
  304. ///
  305. /// @see ConsoleDynamicTypes
  306. void addVariable( const char *name,
  307. S32 type,
  308. void *pointer,
  309. const char* usage = NULL );
  310. /// Add a console constant that references the value of a constant in C++ code.
  311. ///
  312. /// @param name Global console constant name to create.
  313. /// @param type The type of the C++ constant; see the ConsoleDynamicTypes enum for a complete list.
  314. /// @param pointer Pointer to the constant.
  315. /// @param usage Documentation string.
  316. ///
  317. /// @see ConsoleDynamicTypes
  318. void addConstant( const char *name,
  319. S32 type,
  320. const void *pointer,
  321. const char* usage = NULL );
  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. /// Add a callback for notification when a variable
  328. /// is assigned a new value.
  329. ///
  330. /// @param name An existing global console variable name.
  331. /// @param callback The notification delegate function.
  332. ///
  333. void addVariableNotify( const char *name, const NotifyDelegate &callback );
  334. /// Remove an existing variable assignment notification callback.
  335. ///
  336. /// @param name An existing global console variable name.
  337. /// @param callback The notification delegate function.
  338. ///
  339. void removeVariableNotify( const char *name, const NotifyDelegate &callback );
  340. /// Assign a string value to a locally scoped console variable
  341. ///
  342. /// @note The context of the variable is determined by gEvalState; that is,
  343. /// by the currently executing code.
  344. ///
  345. /// @param name Local console variable name to set
  346. /// @param value String value to assign to name
  347. void setLocalVariable(const char *name, const char *value);
  348. /// Retrieve the string value to a locally scoped console variable
  349. ///
  350. /// @note The context of the variable is determined by gEvalState; that is,
  351. /// by the currently executing code.
  352. ///
  353. /// @param name Local console variable name to get
  354. const char* getLocalVariable(const char* name);
  355. /// @}
  356. /// @name Global Variable Accessors
  357. /// @{
  358. /// Assign a string value to a global console variable
  359. /// @param name Global console variable name to set
  360. /// @param value String value to assign to this variable.
  361. void setVariable(const char *name, const char *value);
  362. /// Retrieve the string value of a global console variable
  363. /// @param name Global Console variable name to query
  364. /// @return The string value of the variable or "" if the variable does not exist.
  365. const char* getVariable(const char* name);
  366. /// Same as setVariable(), but for bools.
  367. void setBoolVariable (const char* name,bool var);
  368. /// Same as getVariable(), but for bools.
  369. ///
  370. /// @param name Name of the variable.
  371. /// @param def Default value to supply if no matching variable is found.
  372. bool getBoolVariable (const char* name,bool def = false);
  373. /// Same as setVariable(), but for ints.
  374. void setIntVariable (const char* name,S32 var);
  375. /// Same as getVariable(), but for ints.
  376. ///
  377. /// @param name Name of the variable.
  378. /// @param def Default value to supply if no matching variable is found.
  379. S32 getIntVariable (const char* name,S32 def = 0);
  380. /// Same as setVariable(), but for floats.
  381. void setFloatVariable(const char* name,F32 var);
  382. /// Same as getVariable(), but for floats.
  383. ///
  384. /// @param name Name of the variable.
  385. /// @param def Default value to supply if no matching variable is found.
  386. F32 getFloatVariable(const char* name,F32 def = .0f);
  387. /// @}
  388. /// @name Global Function Registration
  389. /// @{
  390. /// Register a C++ function with the console making it a global function callable from the scripting engine.
  391. ///
  392. /// @param name Name of the new function.
  393. /// @param cb Pointer to the function implementing the scripting call; a console callback function returning a specific type value.
  394. /// @param usage Documentation for this function. @ref console_autodoc
  395. /// @param minArgs Minimum number of arguments this function accepts
  396. /// @param maxArgs Maximum number of arguments this function accepts
  397. /// @param toolOnly Wether this is a TORQUE_TOOLS only function.
  398. /// @param header The extended function header.
  399. void addCommand( const char* name, StringCallback cb, const char* usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL );
  400. void addCommand( const char* name, IntCallback cb, const char* usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL ); ///< @copydoc addCommand( const char *, StringCallback, const char *, S32, S32, bool, ConsoleFunctionHeader* )
  401. void addCommand( const char* name, FloatCallback cb, const char* usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL ); ///< @copydoc addCommand( const char *, StringCallback, const char *, S32, S32, bool, ConsoleFunctionHeader* )
  402. void addCommand( const char* name, VoidCallback cb, const char* usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL ); ///< @copydoc addCommand( const char *, StringCallback, const char *, S32, S32, bool, ConsoleFunctionHeader* )
  403. void addCommand( const char* name, BoolCallback cb, const char* usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL ); ///< @copydoc addCommand( const char *, StringCallback, const char *, S32, S32, bool, ConsoleFunctionHeader* )
  404. /// @}
  405. /// @name Namespace Function Registration
  406. /// @{
  407. /// Register a C++ function with the console making it callable
  408. /// as a method of the given namespace from the scripting engine.
  409. ///
  410. /// @param nameSpace Name of the namespace to associate the new function with; this is usually the name of a class.
  411. /// @param name Name of the new function.
  412. /// @param cb Pointer to the function implementing the scripting call; a console callback function returning a specific type value.
  413. /// @param usage Documentation for this function. @ref console_autodoc
  414. /// @param minArgs Minimum number of arguments this function accepts
  415. /// @param maxArgs Maximum number of arguments this function accepts
  416. /// @param toolOnly Wether this is a TORQUE_TOOLS only function.
  417. /// @param header The extended function header.
  418. void addCommand(const char *nameSpace, const char *name,StringCallback cb, const char *usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL );
  419. void addCommand(const char *nameSpace, const char *name,IntCallback cb, const char *usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL ); ///< @copydoc addCommand( const char*, const char *, StringCallback, const char *, S32, S32, bool, ConsoleFunctionHeader* )
  420. void addCommand(const char *nameSpace, const char *name,FloatCallback cb, const char *usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL ); ///< @copydoc addCommand( const char*, const char *, StringCallback, const char *, S32, S32, bool, ConsoleFunctionHeader* )
  421. void addCommand(const char *nameSpace, const char *name,VoidCallback cb, const char *usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL ); ///< @copydoc addCommand( const char*, const char *, StringCallback, const char *, S32, S32, bool, ConsoleFunctionHeader* )
  422. void addCommand(const char *nameSpace, const char *name,BoolCallback cb, const char *usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL ); ///< @copydoc addCommand( const char*, const char *, StringCallback, const char *, S32, S32, bool, ConsoleFunctionHeader* )
  423. /// @}
  424. /// @name Special Purpose Registration
  425. ///
  426. /// These are special-purpose functions that exist to allow commands to be grouped, so
  427. /// that when we generate console docs, they can be more meaningfully presented.
  428. ///
  429. /// @ref console_autodoc "Click here for more information about console docs and grouping."
  430. ///
  431. /// @{
  432. void markCommandGroup (const char * nsName, const char *name, const char* usage=NULL);
  433. void beginCommandGroup(const char * nsName, const char *name, const char* usage);
  434. void endCommandGroup (const char * nsName, const char *name);
  435. void noteScriptCallback( const char *className, const char *funcName, const char *usage, ConsoleFunctionHeader* header = NULL );
  436. /// @}
  437. /// @name Console Output
  438. ///
  439. /// These functions process the formatted string and pass it to all the ConsumerCallbacks that are
  440. /// currently registered. The console log file and the console window callbacks are installed by default.
  441. ///
  442. /// @see addConsumer()
  443. /// @see removeConsumer()
  444. /// @{
  445. /// @param _format A stdlib printf style formatted out put string
  446. /// @param ... Variables to be written
  447. void printf(const char *_format, ...);
  448. /// @note The console window colors warning text as LIGHT GRAY.
  449. /// @param _format A stdlib printf style formatted out put string
  450. /// @param ... Variables to be written
  451. void warnf(const char *_format, ...);
  452. /// @note The console window colors warning text as RED.
  453. /// @param _format A stdlib printf style formatted out put string
  454. /// @param ... Variables to be written
  455. void errorf(const char *_format, ...);
  456. /// @note The console window colors warning text as LIGHT GRAY.
  457. /// @param type Allows you to associate the warning message with an internal module.
  458. /// @param _format A stdlib printf style formatted out put string
  459. /// @param ... Variables to be written
  460. /// @see Con::warnf()
  461. void warnf(ConsoleLogEntry::Type type, const char *_format, ...);
  462. /// @note The console window colors warning text as RED.
  463. /// @param type Allows you to associate the warning message with an internal module.
  464. /// @param _format A stdlib printf style formatted out put string
  465. /// @param ... Variables to be written
  466. /// @see Con::errorf()
  467. void errorf(ConsoleLogEntry::Type type, const char *_format, ...);
  468. /// @}
  469. /// Returns true when called from the main thread, false otherwise
  470. bool isMainThread();
  471. /// @name Console Execution
  472. ///
  473. /// These are functions relating to the execution of script code.
  474. ///
  475. /// @{
  476. /// Call a script function from C/C++ code.
  477. ///
  478. /// @param argc Number of elements in the argv parameter
  479. /// @param argv A character string array containing the name of the function
  480. /// to call followed by the arguments to that function.
  481. /// @code
  482. /// // Call a Torque script function called mAbs, having one parameter.
  483. /// char* argv[] = {"abs", "-9"};
  484. /// char* result = execute(2, argv);
  485. /// @endcode
  486. const char *execute(S32 argc, const char* argv[]);
  487. /// @see execute(S32 argc, const char* argv[])
  488. #define ARG const char*
  489. const char *executef( ARG);
  490. const char *executef( ARG, ARG);
  491. const char *executef( ARG, ARG, ARG);
  492. const char *executef( ARG, ARG, ARG, ARG);
  493. const char *executef( ARG, ARG, ARG, ARG, ARG);
  494. const char *executef( ARG, ARG, ARG, ARG, ARG, ARG);
  495. const char *executef( ARG, ARG, ARG, ARG, ARG, ARG, ARG);
  496. const char *executef( ARG, ARG, ARG, ARG, ARG, ARG, ARG, ARG);
  497. const char *executef( ARG, ARG, ARG, ARG, ARG, ARG, ARG, ARG, ARG);
  498. const char *executef( ARG, ARG, ARG, ARG, ARG, ARG, ARG, ARG, ARG, ARG);
  499. #undef ARG
  500. /// Call a Torque Script member function of a SimObject from C/C++ code.
  501. /// @param object Object on which to execute the method call.
  502. /// @param argc Number of elements in the argv parameter (must be >2, see argv)
  503. /// @param argv A character string array containing the name of the member function
  504. /// to call followed by an empty parameter (gets filled with object ID)
  505. /// followed by arguments to that function.
  506. /// @code
  507. /// // Call the method setMode() on an object, passing it one parameter.
  508. ///
  509. /// char* argv[] = {"setMode", "", "2"};
  510. /// char* result = execute(mysimobject, 3, argv);
  511. /// @endcode
  512. const char *execute(SimObject *object, S32 argc, const char *argv[], bool thisCallOnly = false);
  513. /// @see execute(SimObject *, S32 argc, const char *argv[])
  514. #define ARG const char*
  515. const char *executef(SimObject *, ARG);
  516. const char *executef(SimObject *, ARG, ARG);
  517. const char *executef(SimObject *, ARG, ARG, ARG);
  518. const char *executef(SimObject *, ARG, ARG, ARG, ARG);
  519. const char *executef(SimObject *, ARG, ARG, ARG, ARG, ARG);
  520. const char *executef(SimObject *, ARG, ARG, ARG, ARG, ARG, ARG);
  521. const char *executef(SimObject *, ARG, ARG, ARG, ARG, ARG, ARG, ARG);
  522. const char *executef(SimObject *, ARG, ARG, ARG, ARG, ARG, ARG, ARG, ARG);
  523. const char *executef(SimObject *, ARG, ARG, ARG, ARG, ARG, ARG, ARG, ARG, ARG);
  524. const char *executef(SimObject *, ARG, ARG, ARG, ARG, ARG, ARG, ARG, ARG, ARG, ARG);
  525. const char *executef(SimObject *, ARG, ARG, ARG, ARG, ARG, ARG, ARG, ARG, ARG, ARG, ARG);
  526. #undef ARG
  527. /// Evaluate an arbitrary chunk of code.
  528. ///
  529. /// @param string Buffer containing code to execute.
  530. /// @param echo Should we echo the string to the console?
  531. /// @param fileName Indicate what file this code is coming from; used in error reporting and such.
  532. const char *evaluate(const char* string, bool echo = false, const char *fileName = NULL);
  533. /// Evaluate an arbitrary line of script.
  534. ///
  535. /// This wraps dVsprintf(), so you can substitute parameters into the code being executed.
  536. const char *evaluatef(const char* string, ...);
  537. /// @}
  538. /// @name Console Function Implementation Helpers
  539. ///
  540. /// The functions Con::getIntArg, Con::getFloatArg and Con::getArgBuffer(size) are used to
  541. /// allocate on the console stack string variables that will be passed into the next console
  542. // function called. This allows the console to avoid copying some data.
  543. ///
  544. /// getReturnBuffer lets you allocate stack space to return data in.
  545. /// @{
  546. ///
  547. char* getReturnBuffer(U32 bufferSize);
  548. char* getReturnBuffer(const char *stringToCopy);
  549. char* getReturnBuffer( const String& str );
  550. char* getReturnBuffer( const StringBuilder& str );
  551. char* getArgBuffer(U32 bufferSize);
  552. char* getFloatArg(F64 arg);
  553. char* getIntArg (S32 arg);
  554. char* getStringArg( const char *arg );
  555. char* getStringArg( const String& arg );
  556. /// @}
  557. /// @name Namespaces
  558. /// @{
  559. Namespace *lookupNamespace(const char *nsName);
  560. bool linkNamespaces(const char *parentName, const char *childName);
  561. bool unlinkNamespaces(const char *parentName, const char *childName);
  562. /// @note This should only be called from consoleObject.h
  563. bool classLinkNamespaces(Namespace *parent, Namespace *child);
  564. const char *getNamespaceList(Namespace *ns);
  565. /// @}
  566. /// @name Logging
  567. /// @{
  568. void getLockLog(ConsoleLogEntry * &log, U32 &size);
  569. void unlockLog(void);
  570. void setLogMode(S32 mode);
  571. /// @}
  572. /// @name Instant Group
  573. /// @{
  574. void pushInstantGroup( String name = String() );
  575. void popInstantGroup();
  576. /// @}
  577. /// @name Dynamic Type System
  578. /// @{
  579. ///
  580. /* void registerType( const char *typeName, S32 type, S32 size, GetDataFunction gdf, SetDataFunction sdf, bool isDatablockType = false );
  581. void registerType( const char* typeName, S32 type, S32 size, bool isDatablockType = false );
  582. void registerTypeGet( S32 type, GetDataFunction gdf );
  583. void registerTypeSet( S32 type, SetDataFunction sdf );
  584. const char *getTypeName(S32 type);
  585. bool isDatablockType( S32 type ); */
  586. void setData(S32 type, void *dptr, S32 index, S32 argc, const char **argv, const EnumTable *tbl = NULL, BitSet32 flag = 0);
  587. const char *getData(S32 type, void *dptr, S32 index, const EnumTable *tbl = NULL, BitSet32 flag = 0);
  588. const char *getFormattedData(S32 type, const char *data, const EnumTable *tbl = NULL, BitSet32 flag = 0);
  589. /// @}
  590. };
  591. extern void expandEscape(char *dest, const char *src);
  592. extern bool collapseEscape(char *buf);
  593. extern U32 HashPointer(StringTableEntry ptr);
  594. /// Extended information about a console function.
  595. struct ConsoleFunctionHeader
  596. {
  597. /// Return type string.
  598. const char* mReturnString;
  599. /// List of arguments taken by the function. Used for documentation.
  600. const char* mArgString;
  601. /// List of default argument values. Used for documentation.
  602. const char* mDefaultArgString;
  603. /// Whether this is a static method in a class.
  604. bool mIsStatic;
  605. ConsoleFunctionHeader(
  606. const char* returnString,
  607. const char* argString,
  608. const char* defaultArgString,
  609. bool isStatic = false )
  610. : mReturnString( returnString ),
  611. mArgString( argString ),
  612. mDefaultArgString( defaultArgString ),
  613. mIsStatic( isStatic ) {}
  614. };
  615. /// This is the backend for the ConsoleMethod()/ConsoleFunction() macros.
  616. ///
  617. /// See the group ConsoleConstructor Innards for specifics on how this works.
  618. ///
  619. /// @see @ref console_autodoc
  620. /// @nosubgrouping
  621. class ConsoleConstructor
  622. {
  623. public:
  624. /// @name Entry Type Fields
  625. ///
  626. /// One of these is set based on the type of entry we want
  627. /// inserted in the console.
  628. ///
  629. /// @ref console_autodoc
  630. /// @{
  631. StringCallback sc; ///< A function/method that returns a string.
  632. IntCallback ic; ///< A function/method that returns an int.
  633. FloatCallback fc; ///< A function/method that returns a float.
  634. VoidCallback vc; ///< A function/method that returns nothing.
  635. BoolCallback bc; ///< A function/method that returns a bool.
  636. bool group; ///< Indicates that this is a group marker.
  637. bool ns; ///< Indicates that this is a namespace marker.
  638. /// @deprecated Unused.
  639. bool callback; ///< Is this a callback into script?
  640. /// @}
  641. /// Minimum number of arguments expected by the function.
  642. S32 mina;
  643. /// Maximum number of arguments accepted by the funtion. Zero for varargs.
  644. S32 maxa;
  645. /// Name of the function/method.
  646. const char* funcName;
  647. /// Name of the class namespace to which to add the method.
  648. const char* className;
  649. /// Usage string for documentation.
  650. const char* usage;
  651. /// Whether this is a TORQUE_TOOLS only function.
  652. bool toolOnly;
  653. /// The extended function header.
  654. ConsoleFunctionHeader* header;
  655. /// @name ConsoleConstructor Innards
  656. ///
  657. /// The ConsoleConstructor class is used as the backend for the ConsoleFunction() and
  658. /// ConsoleMethod() macros. The way it works takes advantage of several properties of
  659. /// C++.
  660. ///
  661. /// The ConsoleFunction()/ConsoleMethod() macros wrap the declaration of a ConsoleConstructor.
  662. ///
  663. /// @code
  664. /// // The definition of a ConsoleFunction using the macro
  665. /// ConsoleFunction(ExpandFilename, const char*, 2, 2, "(string filename)")
  666. /// {
  667. /// argc;
  668. /// char* ret = Con::getReturnBuffer( 1024 );
  669. /// Con::expandScriptFilename(ret, 1024, argv[1]);
  670. /// return ret;
  671. /// }
  672. ///
  673. /// // Resulting code
  674. /// static const char* cExpandFilename(SimObject *, S32, const char **argv);
  675. /// static ConsoleConstructor
  676. /// gExpandFilenameobj(NULL,"ExpandFilename", cExpandFilename,
  677. /// "(string filename)", 2, 2);
  678. /// static const char* cExpandFilename(SimObject *, S32 argc, const char **argv)
  679. /// {
  680. /// argc;
  681. /// char* ret = Con::getReturnBuffer( 1024 );
  682. /// Con::expandScriptFilename(ret, 1024, argv[1]);
  683. /// return ret;
  684. /// }
  685. ///
  686. /// // A similar thing happens when you do a ConsoleMethod.
  687. /// @endcode
  688. ///
  689. /// As you can see, several global items are defined when you use the ConsoleFunction method.
  690. /// The macro constructs the name of these items from the parameters you passed it. Your
  691. /// implementation of the console function is is placed in a function with a name based on
  692. /// the actual name of the console funnction. In addition, a ConsoleConstructor is declared.
  693. ///
  694. /// Because it is defined as a global, the constructor for the ConsoleConstructor is called
  695. /// before execution of main() is started. The constructor is called once for each global
  696. /// ConsoleConstructor variable, in the order in which they were defined (this property only holds true
  697. /// within file scope).
  698. ///
  699. /// We have ConsoleConstructor create a linked list at constructor time, by storing a static
  700. /// pointer to the head of the list, and keeping a pointer to the next item in each instance
  701. /// of ConsoleConstructor. init() is a helper function in this process, automatically filling
  702. /// in commonly used fields and updating first and next as needed. In this way, a list of
  703. /// items to add to the console is assemble in memory, ready for use, before we start
  704. /// execution of the program proper.
  705. ///
  706. /// In Con::init(), ConsoleConstructor::setup() is called to process this prepared list. Each
  707. /// item in the list is iterated over, and the appropriate Con namespace functions (usually
  708. /// Con::addCommand) are invoked to register the ConsoleFunctions and ConsoleMethods in
  709. /// the appropriate namespaces.
  710. ///
  711. /// @see Namespace
  712. /// @see Con
  713. /// @{
  714. ///
  715. ConsoleConstructor *next;
  716. static ConsoleConstructor *first;
  717. void init( const char* cName, const char* fName, const char *usg, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL );
  718. static void setup();
  719. /// Validate there are no duplicate entries for this item.
  720. void validate();
  721. /// @}
  722. /// @name Basic Console Constructors
  723. /// @{
  724. ConsoleConstructor( const char* className, const char* funcName, StringCallback sfunc, const char* usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL );
  725. ConsoleConstructor( const char* className, const char* funcName, IntCallback ifunc, const char* usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL );
  726. ConsoleConstructor( const char* className, const char* funcName, FloatCallback ffunc, const char* usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL );
  727. ConsoleConstructor( const char* className, const char* funcName, VoidCallback vfunc, const char* usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL );
  728. ConsoleConstructor( const char* className, const char* funcName, BoolCallback bfunc, const char* usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL );
  729. /// @}
  730. /// @name Magic Console Constructors
  731. ///
  732. /// These perform various pieces of "magic" related to consoleDoc functionality.
  733. /// @ref console_autodoc
  734. /// @{
  735. /// Indicates a group marker. (A doxygen illusion)
  736. ///
  737. /// @see Con::markCommandGroup
  738. /// @ref console_autodoc
  739. ConsoleConstructor( const char *className, const char *groupName, const char* usage );
  740. /// Indicates a callback declared with the DECLARE_SCRIPT_CALLBACK macro and friends.
  741. ConsoleConstructor( const char *className, const char *callbackName, const char *usage, ConsoleFunctionHeader* header );
  742. /// @}
  743. };
  744. /// An arbitrary fragment of auto-doc text for the script reference.
  745. struct ConsoleDocFragment
  746. {
  747. /// The class in which to put the fragment. If NULL, the fragment
  748. /// will be placed globally.
  749. const char* mClass;
  750. /// The definition to output for this fragment. NULL for fragments
  751. /// not associated with a definition.
  752. const char* mDefinition;
  753. /// The documentation text.
  754. const char* mText;
  755. /// Next fragment in the global link chain.
  756. ConsoleDocFragment* mNext;
  757. /// First fragment in the global link chain.
  758. static ConsoleDocFragment* smFirst;
  759. ConsoleDocFragment( const char* text, const char* inClass = NULL, const char* definition = NULL )
  760. : mText( text ),
  761. mClass( inClass ),
  762. mDefinition( definition ),
  763. mNext( smFirst )
  764. {
  765. smFirst = this;
  766. }
  767. };
  768. /// @name Global Console Definition Macros
  769. ///
  770. /// @note If TORQUE_DEBUG is defined, then we gather documentation information, and
  771. /// do some extra sanity checks.
  772. ///
  773. /// @see ConsoleConstructor
  774. /// @ref console_autodoc
  775. /// @{
  776. /// Define a C++ method that calls back to script on an object.
  777. ///
  778. /// @see consoleCallback.h
  779. #define DECLARE_CALLBACK( returnType, name, args ) \
  780. virtual returnType name ## _callback args
  781. // O hackery of hackeries
  782. #define conmethod_return_const return (const
  783. #define conmethod_return_S32 return (S32
  784. #define conmethod_return_F32 return (F32
  785. #define conmethod_nullify(val)
  786. #define conmethod_return_void conmethod_nullify(void
  787. #define conmethod_return_bool return (bool
  788. #if !defined(TORQUE_SHIPPING)
  789. // Console function macros
  790. # define ConsoleFunctionGroupBegin(groupName, usage) \
  791. static ConsoleConstructor cfg_ConsoleFunctionGroup_##groupName##_GroupBegin(NULL,#groupName,usage)
  792. # define ConsoleFunction(name,returnType,minArgs,maxArgs,usage1) \
  793. returnType cf_##name(SimObject *, S32, const char **argv); \
  794. ConsoleConstructor cc_##name##_obj(NULL,#name,cf_##name,usage1,minArgs,maxArgs); \
  795. returnType cf_##name(SimObject *, S32 argc, const char **argv)
  796. # define ConsoleToolFunction(name,returnType,minArgs,maxArgs,usage1) \
  797. returnType ctf_##name(SimObject *, S32, const char **argv); \
  798. ConsoleConstructor cc_##name##_obj(NULL,#name,ctf_##name,usage1,minArgs,maxArgs, true); \
  799. returnType ctf_##name(SimObject *, S32 argc, const char **argv)
  800. # define ConsoleFunctionGroupEnd(groupName) \
  801. static ConsoleConstructor cfg_##groupName##_GroupEnd(NULL,#groupName,NULL)
  802. // Console method macros
  803. # define ConsoleNamespace(className, usage) \
  804. ConsoleConstructor cc_##className##_Namespace(#className, usage)
  805. # define ConsoleMethodGroupBegin(className, groupName, usage) \
  806. static ConsoleConstructor cc_##className##_##groupName##_GroupBegin(#className,#groupName,usage)
  807. # define ConsoleMethod(className,name,returnType,minArgs,maxArgs,usage1) \
  808. inline returnType cm_##className##_##name(className *, S32, const char **argv); \
  809. returnType cm_##className##_##name##_caster(SimObject *object, S32 argc, const char **argv) { \
  810. AssertFatal( dynamic_cast<className*>( object ), "Object passed to " #name " is not a " #className "!" ); \
  811. conmethod_return_##returnType ) cm_##className##_##name(static_cast<className*>(object),argc,argv); \
  812. }; \
  813. ConsoleConstructor cc_##className##_##name##_obj(#className,#name,cm_##className##_##name##_caster,usage1,minArgs,maxArgs); \
  814. inline returnType cm_##className##_##name(className *object, S32 argc, const char **argv)
  815. # define ConsoleStaticMethod(className,name,returnType,minArgs,maxArgs,usage1) \
  816. inline returnType cm_##className##_##name(S32, const char **); \
  817. returnType cm_##className##_##name##_caster(SimObject *object, S32 argc, const char **argv) { \
  818. conmethod_return_##returnType ) cm_##className##_##name(argc,argv); \
  819. }; \
  820. ConsoleConstructor \
  821. cc_##className##_##name##_obj(#className,#name,cm_##className##_##name##_caster,usage1,minArgs,maxArgs); \
  822. inline returnType cm_##className##_##name(S32 argc, const char **argv)
  823. # define ConsoleMethodGroupEnd(className, groupName) \
  824. static ConsoleConstructor cc_##className##_##groupName##_GroupEnd(#className,#groupName,NULL)
  825. /// Add a fragment of auto-doc text to the console API reference.
  826. /// @note There can only be one ConsoleDoc per source file.
  827. # define ConsoleDoc( text ) \
  828. namespace { \
  829. ConsoleDocFragment _sDocFragment( text ); \
  830. }
  831. #else
  832. // These do nothing if we don't want doc information.
  833. # define ConsoleFunctionGroupBegin(groupName, usage)
  834. # define ConsoleFunctionGroupEnd(groupName)
  835. # define ConsoleNamespace(className, usage)
  836. # define ConsoleMethodGroupBegin(className, groupName, usage)
  837. # define ConsoleMethodGroupEnd(className, groupName)
  838. // These are identical to what's above, we just want to null out the usage strings.
  839. # define ConsoleFunction(name,returnType,minArgs,maxArgs,usage1) \
  840. static returnType c##name(SimObject *, S32, const char **); \
  841. static ConsoleConstructor g##name##obj(NULL,#name,c##name,"",minArgs,maxArgs);\
  842. static returnType c##name(SimObject *, S32 argc, const char **argv)
  843. # define ConsoleToolFunction(name,returnType,minArgs,maxArgs,usage1) \
  844. static returnType c##name(SimObject *, S32, const char **); \
  845. static ConsoleConstructor g##name##obj(NULL,#name,c##name,"",minArgs,maxArgs, true);\
  846. static returnType c##name(SimObject *, S32 argc, const char **argv)
  847. # define ConsoleMethod(className,name,returnType,minArgs,maxArgs,usage1) \
  848. static inline returnType c##className##name(className *, S32, const char **argv); \
  849. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  850. conmethod_return_##returnType ) c##className##name(static_cast<className*>(object),argc,argv); \
  851. }; \
  852. static ConsoleConstructor \
  853. className##name##obj(#className,#name,c##className##name##caster,"",minArgs,maxArgs); \
  854. static inline returnType c##className##name(className *object, S32 argc, const char **argv)
  855. # define ConsoleStaticMethod(className,name,returnType,minArgs,maxArgs,usage1) \
  856. static inline returnType c##className##name(S32, const char **); \
  857. static returnType c##className##name##caster(SimObject *object, S32 argc, const char **argv) { \
  858. conmethod_return_##returnType ) c##className##name(argc,argv); \
  859. }; \
  860. static ConsoleConstructor \
  861. className##name##obj(#className,#name,c##className##name##caster,"",minArgs,maxArgs); \
  862. static inline returnType c##className##name(S32 argc, const char **argv)
  863. #define ConsoleDoc( text )
  864. #endif
  865. /// @}
  866. /// @}
  867. #endif // _CONSOLE_H_