2
0

console.h 53 KB

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