engineXMLExport.cpp 18 KB

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