console.h 54 KB

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