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