console.h 54 KB

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