console.h 44 KB

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