engineXMLExport.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. #include "console/engineExports.h"
  23. #include "console/engineAPI.h"
  24. #include "console/engineTypes.h"
  25. #include "console/engineFunctions.h"
  26. #include "console/SimXMLDocument.h"
  27. /// @file
  28. /// A generator that will dump all export structures contained in an engine
  29. /// DLL to an XML file which may then be used by wrapper generators to create a
  30. /// language-specific binding for the engine API. Using XML as an intermediary
  31. /// format allows the generators to use all of the export structures without
  32. /// actually having to access them directly in the DLL as native entities.
  33. static void exportScope( const EngineExportScope* scope, SimXMLDocument* xml, bool addNode = false );
  34. static String getTypeName( const EngineTypeInfo* type )
  35. {
  36. if( !type )
  37. {
  38. static String sVoid( "void" );
  39. return sVoid;
  40. }
  41. return type->getFullyQualifiedExportName();
  42. }
  43. static const char* getDocString( const EngineExport* exportInfo )
  44. {
  45. if( !exportInfo->getDocString() )
  46. return "";
  47. return exportInfo->getDocString();
  48. }
  49. template< typename T >
  50. inline T getArgValue( const EngineFunctionDefaultArguments* defaultArgs, U32 offset )
  51. {
  52. return *reinterpret_cast< const T* >( defaultArgs->getArgs() + offset );
  53. }
  54. // List of exports that we want filtered out. This will only be needed as long
  55. // as the console system is still around.
  56. static const char* sExportFilterList[] =
  57. {
  58. "Console", // Console namespace
  59. };
  60. static bool isExportFiltered( const EngineExport* exportInfo )
  61. {
  62. String qualifiedName = exportInfo->getFullyQualifiedExportName();
  63. for( U32 i = 0; i < ( sizeof( sExportFilterList ) / sizeof( sExportFilterList[ 0 ] ) ); ++ i )
  64. if( qualifiedName.compare( sExportFilterList[ i ] ) == 0 )
  65. return true;
  66. return false;
  67. }
  68. //=============================================================================
  69. // Functions.
  70. //=============================================================================
  71. // MARK: ---- Functions ----
  72. //-----------------------------------------------------------------------------
  73. /// Helper to parse argument names out of a prototype string.
  74. static Vector< String > parseFunctionArgumentNames( const EngineFunctionInfo* function )
  75. {
  76. Vector< String > argNames;
  77. const char* prototype = function->getPrototypeString();
  78. if( !prototype )
  79. return argNames;
  80. const U32 prototypeLength = dStrlen( prototype );
  81. const char* prototypeEnd = &prototype[ prototypeLength ];
  82. const char* ptr = prototypeEnd - 1;
  83. // Search for right parenthesis.
  84. while( ptr >= prototype && *ptr != ')' )
  85. ptr --;
  86. if( ptr < prototype )
  87. return argNames;
  88. ptr --;
  89. while( ptr >= prototype && *ptr != '(' )
  90. {
  91. // Skip back over spaces.
  92. while( ptr >= prototype && dIsspace( *ptr ) )
  93. ptr --;
  94. if( ptr < prototype )
  95. return argNames;
  96. // Parse out name.
  97. const char* end = ptr + 1;
  98. while( ptr > prototype && dIsalnum( *ptr ) )
  99. ptr --;
  100. const char* start = ptr + 1;
  101. // Skip back over spaces.
  102. while( ptr >= prototype && dIsspace( *ptr ) )
  103. ptr --;
  104. // If we're sure we don't have just a type name without an
  105. // argument name, copy out the argument name name.
  106. if( ptr >= prototype && *ptr != ',' && *ptr != '(' && end > start )
  107. argNames.push_front( String( start, end - start ) );
  108. else
  109. argNames.push_front( "" );
  110. // Skip back to comma or opening parenthesis.
  111. U32 parenNestingCount = 0;
  112. while( ptr >= prototype )
  113. {
  114. if( *ptr == ')' )
  115. parenNestingCount ++;
  116. else if( *ptr == '(' )
  117. parenNestingCount --;
  118. else if( *ptr == ',' && parenNestingCount == 0 )
  119. {
  120. ptr --;
  121. break;
  122. }
  123. else if( *ptr == '(' && parenNestingCount == 0 )
  124. break;
  125. ptr --;
  126. }
  127. }
  128. // Add 'this' parameter if this is a method.
  129. if( dStrncmp( prototype, "virtual ", sizeof( "virtual " ) - 1 ) == 0 )
  130. argNames.push_front( "this" );
  131. return argNames;
  132. }
  133. //-----------------------------------------------------------------------------
  134. static String getDefaultArgumentValue( const EngineFunctionInfo* function, const EngineTypeInfo* type, U32 offset )
  135. {
  136. String value;
  137. const EngineFunctionDefaultArguments* defaultArgs = function->getDefaultArguments();
  138. switch( type->getTypeKind() )
  139. {
  140. case EngineTypeKindPrimitive:
  141. {
  142. #define PRIMTYPE( tp ) \
  143. if( TYPE< tp >() == type ) \
  144. { \
  145. tp val = getArgValue< tp >( defaultArgs, offset ); \
  146. value = String::ToString( val ); \
  147. }
  148. PRIMTYPE( bool );
  149. PRIMTYPE( S8 );
  150. PRIMTYPE( U8 );
  151. PRIMTYPE( S32 );
  152. PRIMTYPE( U32 );
  153. PRIMTYPE( F32 );
  154. PRIMTYPE( F64 );
  155. //TODO: for now we store string literals in ASCII; needs to be sorted out
  156. if( TYPE< const char* >() == type )
  157. {
  158. const char* val = getArgValue< const char* >( defaultArgs, offset );
  159. value = val;
  160. }
  161. #undef PRIMTYPE
  162. break;
  163. }
  164. case EngineTypeKindEnum:
  165. {
  166. S32 val = getArgValue< S32 >( defaultArgs, offset );
  167. AssertFatal( type->getEnumTable(), "engineXMLExport - Enum type without table!" );
  168. const EngineEnumTable& table = *( type->getEnumTable() );
  169. const U32 numValues = table.getNumValues();
  170. for( U32 i = 0; i < numValues; ++ i )
  171. if( table[ i ].getInt() == val )
  172. {
  173. value = table[ i ].getName();
  174. break;
  175. }
  176. break;
  177. }
  178. case EngineTypeKindBitfield:
  179. {
  180. S32 val = getArgValue< S32 >( defaultArgs, offset );
  181. AssertFatal( type->getEnumTable(), "engineXMLExport - Bitfield type without table!" );
  182. const EngineEnumTable& table = *( type->getEnumTable() );
  183. const U32 numValues = table.getNumValues();
  184. bool isFirst = true;
  185. for( U32 i = 0; i < numValues; ++ i )
  186. if( table[ i ].getInt() & val )
  187. {
  188. if( !isFirst )
  189. value += '|';
  190. value = table[ i ].getName();
  191. isFirst = false;
  192. }
  193. break;
  194. }
  195. case EngineTypeKindStruct:
  196. {
  197. //TODO: struct type default argument values
  198. break;
  199. }
  200. case EngineTypeKindClass:
  201. case EngineTypeKindFunction:
  202. {
  203. // For these two kinds, we support "null" as the only valid
  204. // default value.
  205. const void* ptr = getArgValue< const void* >( defaultArgs, offset );
  206. if( !ptr )
  207. value = "null";
  208. break;
  209. }
  210. default:
  211. break;
  212. }
  213. return value;
  214. }
  215. //-----------------------------------------------------------------------------
  216. static void exportFunction( const EngineFunctionInfo* function, SimXMLDocument* xml )
  217. {
  218. if( isExportFiltered( function ) )
  219. return;
  220. xml->pushNewElement( "EngineFunction" );
  221. xml->setAttribute( "name", function->getExportName() );
  222. xml->setAttribute( "returnType", getTypeName( function->getReturnType() ) );
  223. xml->setAttribute( "symbol", function->getBindingName() );
  224. xml->setAttribute( "isCallback", function->isCallout() ? "1" : "0" );
  225. xml->setAttribute( "isVariadic", function->getFunctionType()->isVariadic() ? "1" : "0" );
  226. xml->setAttribute( "docs", getDocString( function ) );
  227. xml->pushNewElement( "arguments" );
  228. const U32 numArguments = function->getNumArguments();
  229. const U32 numDefaultArguments = ( function->getDefaultArguments() ? function->getDefaultArguments()->mNumDefaultArgs : 0 );
  230. const U32 firstDefaultArg = numArguments - numDefaultArguments;
  231. Vector< String > argumentNames = parseFunctionArgumentNames( function );
  232. const U32 numArgumentNames = argumentNames.size();
  233. // Accumulated offset in function argument frame vector.
  234. U32 argFrameOffset = 0;
  235. for( U32 i = 0; i < numArguments; ++ i )
  236. {
  237. xml->pushNewElement( "EngineFunctionArgument" );
  238. const EngineTypeInfo* type = function->getArgumentType( i );
  239. AssertFatal( type != NULL, "exportFunction - Argument cannot have type void!" );
  240. String argName;
  241. if( i < numArgumentNames )
  242. argName = argumentNames[ i ];
  243. xml->setAttribute( "name", argName );
  244. xml->setAttribute( "type", getTypeName( type ) );
  245. if( i >= firstDefaultArg )
  246. {
  247. String defaultValue = getDefaultArgumentValue( function, type, argFrameOffset );
  248. xml->setAttribute( "defaultValue", defaultValue );
  249. }
  250. xml->popElement();
  251. if( type->getTypeKind() == EngineTypeKindStruct )
  252. argFrameOffset += type->getInstanceSize();
  253. else
  254. argFrameOffset += type->getValueSize();
  255. #ifdef _PACK_BUG_WORKAROUNDS
  256. if( argFrameOffset % 4 > 0 )
  257. argFrameOffset += 4 - ( argFrameOffset % 4 );
  258. #endif
  259. }
  260. xml->popElement();
  261. xml->popElement();
  262. }
  263. //=============================================================================
  264. // Types.
  265. //=============================================================================
  266. // MARK: ---- Types ----
  267. //-----------------------------------------------------------------------------
  268. static void exportType( const EngineTypeInfo* type, SimXMLDocument* xml )
  269. {
  270. // Don't export anonymous types.
  271. if( !type->getTypeName()[ 0 ] )
  272. return;
  273. if( isExportFiltered( type ) )
  274. return;
  275. const char* nodeName = NULL;
  276. switch( type->getTypeKind() )
  277. {
  278. case EngineTypeKindPrimitive:
  279. nodeName = "EnginePrimitiveType";
  280. break;
  281. case EngineTypeKindEnum:
  282. nodeName = "EngineEnumType";
  283. break;
  284. case EngineTypeKindBitfield:
  285. nodeName = "EngineBitfieldType";
  286. break;
  287. case EngineTypeKindStruct:
  288. nodeName = "EngineStructType";
  289. break;
  290. case EngineTypeKindClass:
  291. nodeName = "EngineClassType";
  292. break;
  293. default:
  294. return;
  295. }
  296. xml->pushNewElement( nodeName );
  297. xml->setAttribute( "name", type->getTypeName() );
  298. xml->setAttribute( "size", String::ToString( type->getInstanceSize() ) );
  299. xml->setAttribute( "isAbstract", type->isAbstract() ? "1" : "0" );
  300. xml->setAttribute( "isInstantiable", type->isInstantiable() ? "1" : "0" );
  301. xml->setAttribute( "isDisposable", type->isDisposable() ? "1" : "0" );
  302. xml->setAttribute( "isSingleton", type->isSingleton() ? "1" : "0" );
  303. xml->setAttribute( "docs", getDocString( type ) );
  304. if( type->getSuperType() )
  305. xml->setAttribute( "superType", getTypeName( type->getSuperType() ) );
  306. if( type->getEnumTable() )
  307. {
  308. xml->pushNewElement( "enums" );
  309. const EngineEnumTable& table = *( type->getEnumTable() );
  310. const U32 numValues = table.getNumValues();
  311. for( U32 i = 0; i < numValues; ++ i )
  312. {
  313. xml->pushNewElement( "EngineEnum" );
  314. xml->setAttribute( "name", table[ i ].getName() );
  315. xml->setAttribute( "value", String::ToString( table[ i ].getInt() ) );
  316. xml->setAttribute( "docs", table[ i ].getDocString() ? table[ i ].getDocString() : "" );
  317. xml->popElement();
  318. }
  319. xml->popElement();
  320. }
  321. else if( type->getFieldTable() )
  322. {
  323. xml->pushNewElement( "fields" );
  324. const EngineFieldTable& table = *( type->getFieldTable() );
  325. const U32 numFields = table.getNumFields();
  326. for( U32 i = 0; i < numFields; ++ i )
  327. {
  328. const EngineFieldTable::Field& field = table[ i ];
  329. xml->pushNewElement( "EngineField" );
  330. xml->setAttribute( "name", field.getName() );
  331. xml->setAttribute( "type", getTypeName( field.getType() ) );
  332. xml->setAttribute( "offset", String::ToString( field.getOffset() ) );
  333. xml->setAttribute( "indexedSize", String::ToString( field.getNumElements() ) );
  334. xml->setAttribute( "docs", field.getDocString() ? field.getDocString() : "" );
  335. xml->popElement();
  336. }
  337. xml->popElement();
  338. }
  339. else if( type->getPropertyTable() )
  340. {
  341. xml->pushNewElement( "properties" );
  342. const EnginePropertyTable& table = *( type->getPropertyTable() );
  343. const U32 numProperties = table.getNumProperties();
  344. U32 groupNestingDepth = 0;
  345. for( U32 i = 0; i < numProperties; ++ i )
  346. {
  347. const EnginePropertyTable::Property& property = table[ i ];
  348. if( property.isGroupBegin() )
  349. {
  350. groupNestingDepth ++;
  351. xml->pushNewElement( "EnginePropertyGroup" );
  352. xml->setAttribute( "name", property.getName() );
  353. xml->setAttribute( "indexedSize", String::ToString( property.getNumElements() ) );
  354. xml->setAttribute( "docs", property.getDocString() ? property.getDocString() : "" );
  355. xml->pushNewElement( "properties" );
  356. }
  357. else if( property.isGroupEnd() )
  358. {
  359. groupNestingDepth --;
  360. xml->popElement();
  361. xml->popElement();
  362. }
  363. else
  364. {
  365. xml->pushNewElement( "EngineProperty" );
  366. xml->setAttribute( "name", property.getName() );
  367. xml->setAttribute( "indexedSize", String::ToString( property.getNumElements() ) );
  368. xml->setAttribute( "isConstant", property.isConstant() ? "1" : "0" );
  369. xml->setAttribute( "isTransient", property.isTransient() ? "1" : "0" );
  370. xml->setAttribute( "isVisible", property.hideInInspectors() ? "0" : "1" );
  371. xml->setAttribute( "docs", property.getDocString() ? property.getDocString() : "" );
  372. xml->popElement();
  373. }
  374. }
  375. AssertFatal( !groupNestingDepth, "exportType - Property group nesting mismatch!" );
  376. xml->popElement();
  377. }
  378. exportScope( type, xml );
  379. xml->popElement();
  380. }
  381. //=============================================================================
  382. // Scopes.
  383. //=============================================================================
  384. // MARK: ---- Scopes ----
  385. //-----------------------------------------------------------------------------
  386. static void exportScope( const EngineExportScope* scope, SimXMLDocument* xml, bool addNode )
  387. {
  388. if( addNode )
  389. {
  390. if( isExportFiltered( scope ) )
  391. return;
  392. xml->pushNewElement( "EngineExportScope" );
  393. xml->setAttribute( "name", scope->getExportName() );
  394. xml->setAttribute( "docs", getDocString( scope ) );
  395. }
  396. // Dump all contained exports.
  397. xml->pushNewElement( "exports" );
  398. for( const EngineExport* exportInfo = scope->getExports(); exportInfo != NULL; exportInfo = exportInfo->getNextExport() )
  399. {
  400. switch( exportInfo->getExportKind() )
  401. {
  402. case EngineExportKindScope:
  403. exportScope( static_cast< const EngineExportScope* >( exportInfo ), xml, true );
  404. break;
  405. case EngineExportKindFunction:
  406. exportFunction( static_cast< const EngineFunctionInfo* >( exportInfo ), xml );
  407. break;
  408. case EngineExportKindType:
  409. exportType( static_cast< const EngineTypeInfo* >( exportInfo ), xml );
  410. break;
  411. default:
  412. break;
  413. }
  414. }
  415. xml->popElement();
  416. if( addNode )
  417. xml->popElement();
  418. }
  419. //-----------------------------------------------------------------------------
  420. DefineEngineFunction( exportEngineAPIToXML, SimXMLDocument*, (),,
  421. "Create a XML document containing a dump of the entire exported engine API.\n\n"
  422. "@return A SimXMLDocument containing a dump of the engine's export information or NULL if the operation failed.\n\n"
  423. "@ingroup Console" )
  424. {
  425. SimXMLDocument* xml = new SimXMLDocument;
  426. xml->registerObject();
  427. Sim::getRootGroup()->addObject( xml );
  428. xml->addHeader();
  429. exportScope( EngineExportScope::getGlobalScope(), xml, true );
  430. return xml;
  431. }