console.h 49 KB

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