console.h 50 KB

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