engineXMLExport.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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 idx)
  51. {
  52. return *(const T*)(defaultArgs->mFirst + defaultArgs->mOffsets[idx]);
  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) || *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 getValueForType(const EngineTypeInfo* type, void* addr)
  135. {
  136. String value;
  137. #define ADDRESS_TO_TYPE(tp) *(const tp*)(addr);
  138. switch (type->getTypeKind())
  139. {
  140. case EngineTypeKindPrimitive:
  141. {
  142. #define PRIMTYPE( tp ) \
  143. if( TYPE< tp >() == type ) \
  144. { \
  145. tp val = ADDRESS_TO_TYPE(tp); \
  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< String >() == type || TYPE< const UTF8* >() == type)
  157. {
  158. const UTF8* val = *((const UTF8**)(addr));
  159. value = val;
  160. }
  161. #undef PRIMTYPE
  162. break;
  163. }
  164. case EngineTypeKindEnum:
  165. {
  166. S32 val = ADDRESS_TO_TYPE(S32);
  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 = ADDRESS_TO_TYPE(S32);
  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. AssertFatal(type->getFieldTable(), "engineXMLExport - Struct type without table!");
  198. const EngineFieldTable* fieldTable = type->getFieldTable();
  199. U32 numFields = fieldTable->getNumFields();
  200. for (int i = 0; i < numFields; ++i)
  201. {
  202. const EngineTypeInfo* fieldType = (*fieldTable)[i].getType();
  203. U32 fieldOffset = (*fieldTable)[i].getOffset();
  204. U32 numElements = (*fieldTable)[i].getNumElements();
  205. for (int j = 0; j < numElements; ++j)
  206. {
  207. if (i == 0 && j == 0) {
  208. value = getValueForType(fieldType, (void*)((size_t)addr + fieldOffset));
  209. }
  210. else {
  211. value += " " + getValueForType(fieldType, (void*)((size_t)addr + (size_t)fieldOffset * ((size_t)j * fieldType->getInstanceSize())));
  212. }
  213. }
  214. }
  215. break;
  216. }
  217. case EngineTypeKindClass:
  218. case EngineTypeKindFunction:
  219. {
  220. // For these two kinds, we support "null" as the only valid
  221. // default value.
  222. const void* ptr = ADDRESS_TO_TYPE(void*);
  223. if (!ptr)
  224. value = "null";
  225. break;
  226. }
  227. default:
  228. break;
  229. }
  230. #undef ADDRESS_TO_TYPE
  231. return value;
  232. }
  233. //-----------------------------------------------------------------------------
  234. static String getDefaultArgumentValue(const EngineFunctionInfo* function, const EngineTypeInfo* type, U32 idx)
  235. {
  236. const EngineFunctionDefaultArguments* defaultArgs = function->getDefaultArguments();
  237. return getValueForType(type, (void*)(defaultArgs->mFirst + defaultArgs->mOffsets[idx]));
  238. }
  239. //-----------------------------------------------------------------------------
  240. static void exportFunction(const EngineFunctionInfo* function, SimXMLDocument* xml)
  241. {
  242. if (isExportFiltered(function))
  243. return;
  244. xml->pushNewElement("EngineFunction");
  245. xml->setAttribute("name", function->getExportName());
  246. xml->setAttribute("returnType", getTypeName(function->getReturnType()));
  247. xml->setAttribute("symbol", function->getBindingName());
  248. xml->setAttribute("isCallback", function->isCallout() ? "1" : "0");
  249. xml->setAttribute("isVariadic", function->getFunctionType()->isVariadic() ? "1" : "0");
  250. xml->setAttribute("docs", getDocString(function));
  251. xml->pushNewElement("arguments");
  252. const U32 numArguments = function->getNumArguments();
  253. const U32 numDefaultArguments = (function->getDefaultArguments() ? function->getDefaultArguments()->mNumDefaultArgs : 0);
  254. const U32 firstDefaultArg = numArguments - numDefaultArguments;
  255. Vector< String > argumentNames = parseFunctionArgumentNames(function);
  256. const U32 numArgumentNames = argumentNames.size();
  257. for (U32 i = 0; i < numArguments; ++i)
  258. {
  259. xml->pushNewElement("EngineFunctionArgument");
  260. const EngineTypeInfo* type = function->getArgumentType(i);
  261. AssertFatal(type != NULL, "exportFunction - Argument cannot have type void!");
  262. String argName;
  263. if (i < numArgumentNames)
  264. argName = argumentNames[i];
  265. xml->setAttribute("name", argName);
  266. xml->setAttribute("type", getTypeName(type));
  267. if (i >= firstDefaultArg)
  268. {
  269. String defaultValue = getDefaultArgumentValue(function, type, i);
  270. xml->setAttribute("defaultValue", defaultValue);
  271. }
  272. // A bit hacky, default arguments have all offsets.
  273. if (function->getDefaultArguments() != NULL)
  274. {
  275. xml->setAttribute("offset", String::ToString(function->getDefaultArguments()->mOffsets[i]));
  276. }
  277. xml->popElement();
  278. }
  279. xml->popElement();
  280. xml->popElement();
  281. }
  282. //=============================================================================
  283. // Types.
  284. //=============================================================================
  285. // MARK: ---- Types ----
  286. //-----------------------------------------------------------------------------
  287. static void exportType(const EngineTypeInfo* type, SimXMLDocument* xml)
  288. {
  289. // Don't export anonymous types.
  290. if (!type->getTypeName()[0])
  291. return;
  292. if (isExportFiltered(type))
  293. return;
  294. const char* nodeName = NULL;
  295. switch (type->getTypeKind())
  296. {
  297. case EngineTypeKindPrimitive:
  298. nodeName = "EnginePrimitiveType";
  299. break;
  300. case EngineTypeKindEnum:
  301. nodeName = "EngineEnumType";
  302. break;
  303. case EngineTypeKindBitfield:
  304. nodeName = "EngineBitfieldType";
  305. break;
  306. case EngineTypeKindStruct:
  307. nodeName = "EngineStructType";
  308. break;
  309. case EngineTypeKindClass:
  310. nodeName = "EngineClassType";
  311. break;
  312. default:
  313. return;
  314. }
  315. xml->pushNewElement(nodeName);
  316. xml->setAttribute("name", type->getTypeName());
  317. xml->setAttribute("size", String::ToString(type->getInstanceSize()));
  318. xml->setAttribute("isAbstract", type->isAbstract() ? "1" : "0");
  319. xml->setAttribute("isInstantiable", type->isInstantiable() ? "1" : "0");
  320. xml->setAttribute("isDisposable", type->isDisposable() ? "1" : "0");
  321. xml->setAttribute("isSingleton", type->isSingleton() ? "1" : "0");
  322. xml->setAttribute("docs", getDocString(type));
  323. if (type->getSuperType())
  324. xml->setAttribute("superType", getTypeName(type->getSuperType()));
  325. if (type->getEnumTable())
  326. {
  327. xml->pushNewElement("enums");
  328. const EngineEnumTable& table = *(type->getEnumTable());
  329. const U32 numValues = table.getNumValues();
  330. for (U32 i = 0; i < numValues; ++i)
  331. {
  332. xml->pushNewElement("EngineEnum");
  333. xml->setAttribute("name", table[i].getName());
  334. xml->setAttribute("value", String::ToString(table[i].getInt()));
  335. xml->setAttribute("docs", table[i].getDocString() ? table[i].getDocString() : "");
  336. xml->popElement();
  337. }
  338. xml->popElement();
  339. }
  340. else if (type->getFieldTable())
  341. {
  342. xml->pushNewElement("fields");
  343. const EngineFieldTable& table = *(type->getFieldTable());
  344. const U32 numFields = table.getNumFields();
  345. for (U32 i = 0; i < numFields; ++i)
  346. {
  347. const EngineFieldTable::Field& field = table[i];
  348. xml->pushNewElement("EngineField");
  349. xml->setAttribute("name", field.getName());
  350. xml->setAttribute("type", getTypeName(field.getType()));
  351. xml->setAttribute("offset", String::ToString(field.getOffset()));
  352. xml->setAttribute("indexedSize", String::ToString(field.getNumElements()));
  353. xml->setAttribute("docs", field.getDocString() ? field.getDocString() : "");
  354. xml->popElement();
  355. }
  356. xml->popElement();
  357. }
  358. else if (type->getPropertyTable())
  359. {
  360. xml->pushNewElement("properties");
  361. const EnginePropertyTable& table = *(type->getPropertyTable());
  362. const U32 numProperties = table.getNumProperties();
  363. U32 groupNestingDepth = 0;
  364. for (U32 i = 0; i < numProperties; ++i)
  365. {
  366. const EnginePropertyTable::Property& property = table[i];
  367. if (property.isGroupBegin())
  368. {
  369. groupNestingDepth++;
  370. xml->pushNewElement("EnginePropertyGroup");
  371. xml->setAttribute("name", property.getName());
  372. xml->setAttribute("indexedSize", String::ToString(property.getNumElements()));
  373. xml->setAttribute("docs", property.getDocString() ? property.getDocString() : "");
  374. xml->pushNewElement("properties");
  375. }
  376. else if (property.isGroupEnd())
  377. {
  378. groupNestingDepth--;
  379. xml->popElement();
  380. xml->popElement();
  381. }
  382. else
  383. {
  384. if (property.getType() == AbstractClassRep::StartArrayFieldType
  385. || property.getType() == AbstractClassRep::EndArrayFieldType) {
  386. continue;
  387. }
  388. xml->pushNewElement("EngineProperty");
  389. xml->setAttribute("name", property.getName());
  390. xml->setAttribute("indexedSize", String::ToString(property.getNumElements()));
  391. xml->setAttribute("isConstant", property.isConstant() ? "1" : "0");
  392. xml->setAttribute("isTransient", property.isTransient() ? "1" : "0");
  393. xml->setAttribute("isVisible", property.hideInInspectors() ? "0" : "1");
  394. xml->setAttribute("docs", property.getDocString() ? property.getDocString() : "");
  395. const bool isDeprecated = (property.getType() == AbstractClassRep::DeprecatedFieldType);
  396. if (isDeprecated)
  397. {
  398. xml->setAttribute("type", "deprecated");
  399. }
  400. else
  401. {
  402. ConsoleBaseType *cbt = ConsoleBaseType::getType(property.getType());
  403. if (cbt != NULL)
  404. {
  405. if (cbt->getTypeInfo() != NULL) {
  406. xml->setAttribute("type", cbt->getTypeInfo()->getTypeName());
  407. }
  408. else {
  409. xml->setAttribute("type", cbt->getTypeClassName());
  410. }
  411. }
  412. else
  413. {
  414. xml->setAttribute("type", "unknown");
  415. }
  416. }
  417. xml->popElement();
  418. }
  419. }
  420. AssertFatal(!groupNestingDepth, "exportType - Property group nesting mismatch!");
  421. xml->popElement();
  422. }
  423. exportScope(type, xml);
  424. xml->popElement();
  425. }
  426. //=============================================================================
  427. // Scopes.
  428. //=============================================================================
  429. // MARK: ---- Scopes ----
  430. //-----------------------------------------------------------------------------
  431. static void exportScope(const EngineExportScope* scope, SimXMLDocument* xml, bool addNode)
  432. {
  433. if (addNode)
  434. {
  435. if (isExportFiltered(scope))
  436. return;
  437. xml->pushNewElement("EngineExportScope");
  438. xml->setAttribute("name", scope->getExportName());
  439. xml->setAttribute("docs", getDocString(scope));
  440. }
  441. // Dump all contained exports.
  442. xml->pushNewElement("exports");
  443. for (const EngineExport* exportInfo = scope->getExports(); exportInfo != NULL; exportInfo = exportInfo->getNextExport())
  444. {
  445. switch (exportInfo->getExportKind())
  446. {
  447. case EngineExportKindScope:
  448. exportScope(static_cast< const EngineExportScope* >(exportInfo), xml, true);
  449. break;
  450. case EngineExportKindFunction:
  451. exportFunction(static_cast< const EngineFunctionInfo* >(exportInfo), xml);
  452. break;
  453. case EngineExportKindType:
  454. exportType(static_cast< const EngineTypeInfo* >(exportInfo), xml);
  455. break;
  456. default:
  457. AssertFatal(true, "Unknown EngineExportKind: " + exportInfo->getExportKind());
  458. break;
  459. }
  460. }
  461. xml->popElement();
  462. if (addNode)
  463. xml->popElement();
  464. }
  465. //-----------------------------------------------------------------------------
  466. DefineEngineFunction(exportEngineAPIToXML, SimXMLDocument*, (), ,
  467. "Create a XML document containing a dump of the entire exported engine API.\n\n"
  468. "@return A SimXMLDocument containing a dump of the engine's export information or NULL if the operation failed.\n\n"
  469. "@ingroup Console")
  470. {
  471. SimXMLDocument* xml = new SimXMLDocument;
  472. xml->registerObject();
  473. Sim::getRootGroup()->addObject(xml);
  474. xml->addHeader();
  475. exportScope(EngineExportScope::getGlobalScope(), xml, true);
  476. return xml;
  477. }