console.h 53 KB

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