ScriptAPIDump.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../AngelScript/Script.h"
  5. #include "../Core/Context.h"
  6. #include "../IO/File.h"
  7. #include "../IO/FileSystem.h"
  8. #include "../IO/Log.h"
  9. #include <AngelScript/angelscript.h>
  10. #include "../DebugNew.h"
  11. namespace Urho3D
  12. {
  13. /// %Object property info for scripting API dump.
  14. struct PropertyInfo
  15. {
  16. /// Construct.
  17. PropertyInfo() :
  18. read_(false),
  19. write_(false),
  20. indexed_(false)
  21. {
  22. }
  23. /// Property name.
  24. String name_;
  25. /// Property data type.
  26. String type_;
  27. /// Reading supported flag.
  28. bool read_;
  29. /// Writing supported flag.
  30. bool write_;
  31. /// Indexed flag.
  32. bool indexed_;
  33. };
  34. /// Header information for dumping events.
  35. struct HeaderFile
  36. {
  37. /// Full path to header file.
  38. String fileName;
  39. /// Event section name.
  40. String sectionName;
  41. };
  42. bool CompareHeaderFiles(const HeaderFile& lhs, const HeaderFile& rhs)
  43. {
  44. return lhs.sectionName < rhs.sectionName;
  45. }
  46. void ExtractPropertyInfo(const String& functionName, const String& declaration, Vector<PropertyInfo>& propertyInfos)
  47. {
  48. String propertyName = functionName.Substring(4);
  49. PropertyInfo* info = nullptr;
  50. for (unsigned k = 0; k < propertyInfos.Size(); ++k)
  51. {
  52. if (propertyInfos[k].name_ == propertyName)
  53. {
  54. info = &propertyInfos[k];
  55. break;
  56. }
  57. }
  58. if (!info)
  59. {
  60. propertyInfos.Resize(propertyInfos.Size() + 1);
  61. info = &propertyInfos.Back();
  62. info->name_ = propertyName;
  63. }
  64. if (functionName.Contains("get_"))
  65. {
  66. info->read_ = true;
  67. // Extract type from the return value
  68. Vector<String> parts = declaration.Split(' ');
  69. if (parts.Size())
  70. {
  71. if (parts[0] != "const")
  72. info->type_ = parts[0];
  73. else if (parts.Size() > 1)
  74. info->type_ = parts[1];
  75. }
  76. // If get method has parameters, it is indexed
  77. if (!declaration.Contains("()"))
  78. {
  79. info->indexed_ = true;
  80. info->type_ += "[]";
  81. }
  82. // Sanitate the reference operator away
  83. info->type_.Replace("&", "");
  84. }
  85. if (functionName.Contains("set_"))
  86. {
  87. info->write_ = true;
  88. if (info->type_.Empty())
  89. {
  90. // Extract type from parameters
  91. i32 begin = declaration.Find(',');
  92. if (begin == String::NPOS)
  93. begin = declaration.Find('(');
  94. else
  95. info->indexed_ = true;
  96. if (begin != String::NPOS)
  97. {
  98. ++begin;
  99. i32 end = declaration.Find(')');
  100. if (end != String::NPOS)
  101. {
  102. info->type_ = declaration.Substring(begin, end - begin);
  103. // Sanitate const & reference operator away
  104. info->type_.Replace("const ", "");
  105. info->type_.Replace("&in", "");
  106. info->type_.Replace("&", "");
  107. }
  108. }
  109. }
  110. }
  111. }
  112. bool ComparePropertyStrings(const String& lhs, const String& rhs)
  113. {
  114. i32 spaceLhs = lhs.Find(' ');
  115. i32 spaceRhs = rhs.Find(' ');
  116. if (spaceLhs != String::NPOS && spaceRhs != String::NPOS)
  117. return String::Compare(lhs.CString() + spaceLhs, rhs.CString() + spaceRhs, true) < 0;
  118. else
  119. return String::Compare(lhs.CString(), rhs.CString(), true) < 0;
  120. }
  121. bool ComparePropertyInfos(const PropertyInfo& lhs, const PropertyInfo& rhs)
  122. {
  123. return String::Compare(lhs.name_.CString(), rhs.name_.CString(), true) < 0;
  124. }
  125. void Script::OutputAPIRow(DumpMode mode, const String& row, bool removeReference, const String& separator)
  126. {
  127. String out(row.Trimmed());
  128. out.Replace("&in", "&");
  129. out.Replace("&out", "&");
  130. if (removeReference)
  131. out.Replace("&", "");
  132. if (mode == DOXYGEN)
  133. Log::WriteRaw("- " + out + "\n");
  134. else if (mode == C_HEADER)
  135. {
  136. out.Replace("@", "");
  137. out.Replace("?&", "void*");
  138. // s/(\w+)\[\]/Array<\1>/g
  139. i32 posBegin = String::NPOS;
  140. while (true) // Loop to cater for array of array of T
  141. {
  142. i32 posEnd = out.Find("[]");
  143. if (posEnd == String::NPOS)
  144. break;
  145. if (posBegin == String::NPOS)
  146. posBegin = posEnd - 1;
  147. while (posBegin >= 0 && isalnum(out[posBegin]))
  148. --posBegin;
  149. ++posBegin;
  150. out.Replace(posBegin, posEnd - posBegin + 2, "Array<" + out.Substring(posBegin, posEnd - posBegin) + ">");
  151. }
  152. // "opIndex" early was replaced to "operator[ ]", so not replaced here to Array<operator>
  153. out.Replace("[ ]", "[]");
  154. Log::WriteRaw(out + separator + "\n");
  155. }
  156. }
  157. void Script::DumpAPI(DumpMode mode, const String& sourceTree)
  158. {
  159. // Does not use URHO3D_LOGRAW macro here to ensure the messages are always dumped regardless of URHO3D_LOGGING compiler directive
  160. // and of Log subsystem availability
  161. // Dump event descriptions and attribute definitions in Doxygen mode. For events, this means going through the header files,
  162. // as the information is not available otherwise.
  163. /// \todo Dump events + attributes before the actual script API because the remarks (readonly / writeonly) seem to throw off
  164. // Doxygen parsing and the following page definition(s) may not be properly recognized
  165. if (mode == DOXYGEN)
  166. {
  167. Log::WriteRaw("namespace Urho3D\n{\n\n/**\n");
  168. auto* fileSystem = GetSubsystem<FileSystem>();
  169. Vector<String> headerFileNames;
  170. String path = AddTrailingSlash(sourceTree);
  171. if (!path.Empty())
  172. path.Append("Source/Urho3D/");
  173. fileSystem->ScanDir(headerFileNames, path, "*.h", SCAN_FILES, true);
  174. /// \hack Rename any Events2D to 2DEvents to work with the event category creation correctly (currently PhysicsEvents2D)
  175. Vector<HeaderFile> headerFiles;
  176. for (unsigned i = 0; i < headerFileNames.Size(); ++i)
  177. {
  178. HeaderFile entry;
  179. entry.fileName = headerFileNames[i];
  180. entry.sectionName = GetFileNameAndExtension(entry.fileName).Replaced("Events2D", "2DEvents");
  181. if (entry.sectionName.EndsWith("Events.h"))
  182. headerFiles.Push(entry);
  183. }
  184. if (!headerFiles.Empty())
  185. {
  186. Log::WriteRaw("\n\\page EventList Event list\n");
  187. Sort(headerFiles.Begin(), headerFiles.End(), CompareHeaderFiles);
  188. for (unsigned i = 0; i < headerFiles.Size(); ++i)
  189. {
  190. SharedPtr<File> file(new File(context_, path + headerFiles[i].fileName, FILE_READ));
  191. if (!file->IsOpen())
  192. continue;
  193. const String& sectionName = headerFiles[i].sectionName;
  194. i32 start = sectionName.Find('/') + 1;
  195. i32 end = sectionName.Find("Events.h");
  196. Log::WriteRaw("\n## %" + sectionName.Substring(start, end - start) + " events\n");
  197. while (!file->IsEof())
  198. {
  199. String line = file->ReadLine();
  200. if (line.StartsWith("URHO3D_EVENT"))
  201. {
  202. Vector<String> parts = line.Split(',');
  203. if (parts.Size() == 2)
  204. Log::WriteRaw("\n### " + parts[1].Substring(0, parts[1].Length() - 1).Trimmed() + "\n");
  205. }
  206. if (line.Contains("URHO3D_PARAM"))
  207. {
  208. Vector<String> parts = line.Split(',');
  209. if (parts.Size() == 2)
  210. {
  211. String paramName = parts[1].Substring(0, parts[1].Find(')')).Trimmed();
  212. String paramType = parts[1].Substring(parts[1].Find("// ") + 3);
  213. if (!paramName.Empty() && !paramType.Empty())
  214. Log::WriteRaw("- %" + paramName + " : " + paramType + "\n");
  215. }
  216. }
  217. }
  218. }
  219. Log::WriteRaw("\n");
  220. }
  221. Log::WriteRaw("\n\\page AttributeList Attribute list\n");
  222. const HashMap<StringHash, Vector<AttributeInfo>>& attributes = context_->GetAllAttributes();
  223. Vector<String> objectTypes;
  224. for (HashMap<StringHash, Vector<AttributeInfo>>::ConstIterator i = attributes.Begin(); i != attributes.End(); ++i)
  225. objectTypes.Push(context_->GetTypeName(i->first_));
  226. Sort(objectTypes.Begin(), objectTypes.End());
  227. for (unsigned i = 0; i < objectTypes.Size(); ++i)
  228. {
  229. const Vector<AttributeInfo>& attrs = attributes.Find(objectTypes[i])->second_;
  230. unsigned usableAttrs = 0;
  231. for (unsigned j = 0; j < attrs.Size(); ++j)
  232. {
  233. // Attributes that are not shown in the editor are typically internal and not usable for eg. attribute
  234. // animation
  235. if (attrs[j].mode_ & AM_NOEDIT)
  236. continue;
  237. ++usableAttrs;
  238. }
  239. if (!usableAttrs)
  240. continue;
  241. Log::WriteRaw("\n### " + objectTypes[i] + "\n");
  242. for (unsigned j = 0; j < attrs.Size(); ++j)
  243. {
  244. if (attrs[j].mode_ & AM_NOEDIT)
  245. continue;
  246. // Prepend each word in the attribute name with % to prevent unintended links
  247. Vector<String> nameParts = attrs[j].name_.Split(' ');
  248. for (unsigned k = 0; k < nameParts.Size(); ++k)
  249. {
  250. if (nameParts[k].Length() > 1 && IsAlpha((unsigned)nameParts[k][0]))
  251. nameParts[k] = "%" + nameParts[k];
  252. }
  253. String name;
  254. name.Join(nameParts, " ");
  255. String type = Variant::GetTypeName(attrs[j].type_);
  256. // Variant typenames are all uppercase. Convert primitive types to the proper lowercase form for the documentation
  257. if (type == "Int" || type == "Bool" || type == "Float")
  258. type[0] = (char)ToLower((unsigned)type[0]);
  259. Log::WriteRaw("- " + name + " : " + type + "\n");
  260. }
  261. }
  262. Log::WriteRaw("\n");
  263. }
  264. if (mode == DOXYGEN)
  265. Log::WriteRaw("\n\\page ScriptAPI Scripting API\n\n");
  266. else if (mode == C_HEADER)
  267. Log::WriteRaw(
  268. "// Script API header intended to be 'force included' in IDE for AngelScript content assist / code completion\n\n"
  269. "#define int8 signed char\n"
  270. "#define int16 signed short\n"
  271. "#define int64 long\n"
  272. "#define uint unsigned\n"
  273. "#define uint8 unsigned char\n"
  274. "#define uint16 unsigned short\n"
  275. "#define uint64 unsigned long\n"
  276. "#define null 0\n"
  277. "#define in\n"
  278. "#define out\n"
  279. "#define inout\n"
  280. "#define is ==\n"
  281. "#define interface struct\n"
  282. "#define class struct\n"
  283. "#define cast reinterpret_cast\n"
  284. "#define mixin\n"
  285. "#define funcdef\n"
  286. "#define protected\n"
  287. "#define private\n"
  288. );
  289. unsigned types = scriptEngine_->GetObjectTypeCount();
  290. Vector<Pair<String, unsigned>> sortedTypes;
  291. for (unsigned i = 0; i < types; ++i)
  292. {
  293. asITypeInfo* type = scriptEngine_->GetObjectTypeByIndex(i);
  294. if (type)
  295. {
  296. String typeName(type->GetName());
  297. sortedTypes.Push(MakePair(typeName, i));
  298. }
  299. }
  300. Sort(sortedTypes.Begin(), sortedTypes.End());
  301. // Get global constants by namespace
  302. HashMap<String, Vector<String>> globalConstants;
  303. unsigned properties = scriptEngine_->GetGlobalPropertyCount();
  304. for (unsigned i = 0; i < properties; ++i)
  305. {
  306. const char* propertyName;
  307. const char* propertyDeclaration;
  308. const char* propertyNameSpace;
  309. int typeId;
  310. scriptEngine_->GetGlobalPropertyByIndex(i, &propertyName, &propertyNameSpace, &typeId);
  311. propertyDeclaration = scriptEngine_->GetTypeDeclaration(typeId);
  312. String type(propertyDeclaration);
  313. globalConstants[String(propertyNameSpace)].Push(type + " " + String(propertyName));
  314. }
  315. for (HashMap<String, Vector<String>>::Iterator i = globalConstants.Begin(); i != globalConstants.End(); ++i)
  316. Sort(i->second_.Begin(), i->second_.End(), ComparePropertyStrings);
  317. if (mode == DOXYGEN)
  318. {
  319. Log::WriteRaw("\\section ScriptAPI_TableOfContents Table of contents\n"
  320. "\\ref ScriptAPI_ClassList \"Class list\"<br>\n"
  321. "\\ref ScriptAPI_Classes \"Classes\"<br>\n"
  322. "\\ref ScriptAPI_Enums \"Enumerations\"<br>\n"
  323. "\\ref ScriptAPI_GlobalFunctions \"Global functions\"<br>\n"
  324. "\\ref ScriptAPI_GlobalProperties \"Global properties\"<br>\n"
  325. "\\ref ScriptAPI_GlobalConstants \"Global constants\"<br>\n\n");
  326. Log::WriteRaw("\\section ScriptAPI_ClassList Class list\n\n");
  327. for (unsigned i = 0; i < sortedTypes.Size(); ++i)
  328. {
  329. asITypeInfo* type = scriptEngine_->GetObjectTypeByIndex(sortedTypes[i].second_);
  330. if (type)
  331. {
  332. String typeName(type->GetName());
  333. Log::WriteRaw("<a href=\"#Class_" + typeName + "\"><b>" + typeName + "</b></a>\n");
  334. }
  335. }
  336. Log::WriteRaw("\n\\section ScriptAPI_Classes Classes\n");
  337. }
  338. else if (mode == C_HEADER)
  339. Log::WriteRaw("\n// Classes\n");
  340. for (unsigned i = 0; i < sortedTypes.Size(); ++i)
  341. {
  342. asITypeInfo* type = scriptEngine_->GetObjectTypeByIndex(sortedTypes[i].second_);
  343. if (type)
  344. {
  345. String typeName(type->GetName());
  346. Vector<String> methodDeclarations;
  347. Vector<PropertyInfo> propertyInfos;
  348. if (mode == DOXYGEN)
  349. {
  350. Log::WriteRaw("<a name=\"Class_" + typeName + "\"></a>\n");
  351. Log::WriteRaw("\n### " + typeName + "\n");
  352. }
  353. else if (mode == C_HEADER)
  354. {
  355. if (type->GetFlags() & asOBJ_TEMPLATE) {
  356. String str = "\ntemplate <";
  357. for (asUINT tt = 0, ttm = type->GetSubTypeCount(); tt < ttm; tt++) {
  358. asITypeInfo* pSubType = type->GetSubType(tt);
  359. str += String("typename ") + pSubType->GetName() + (tt < ttm - 1 ? ", " : ">");
  360. }
  361. Log::WriteRaw(str);
  362. }
  363. Log::WriteRaw("\nclass " + typeName + "\n{\npublic:\n");
  364. for (asUINT m = 0, mc = type->GetBehaviourCount(); m < mc; m++) {
  365. asEBehaviours bh;
  366. asIScriptFunction* pM = type->GetBehaviourByIndex(m, &bh);
  367. if (bh == asBEHAVE_CONSTRUCT || bh == asBEHAVE_DESTRUCT)
  368. Log::WriteRaw(String(pM->GetDeclaration(false, false, true)) + ";\n");
  369. }
  370. for (asUINT m = 0, mc = type->GetFactoryCount(); m < mc; m++) {
  371. asIScriptFunction* pM = type->GetFactoryByIndex(m);
  372. String declaration(pM->GetDeclaration(false, false, true));
  373. declaration = declaration.Substring(declaration.Find(' ') + 1);
  374. declaration.Replace("@", "&");
  375. Log::WriteRaw(declaration + ";\n");
  376. }
  377. if (typeName == "String")
  378. Log::WriteRaw("String(const char*);\n");
  379. }
  380. unsigned methods = type->GetMethodCount();
  381. for (unsigned j = 0; j < methods; ++j)
  382. {
  383. asIScriptFunction* method = type->GetMethodByIndex(j);
  384. String methodName(method->GetName());
  385. String declaration(method->GetDeclaration(true, false, true));
  386. // Recreate tab escape sequences
  387. declaration.Replace("\t", "\\t");
  388. if (methodName.Contains("get_") || methodName.Contains("set_"))
  389. ExtractPropertyInfo(methodName, declaration, propertyInfos);
  390. else
  391. {
  392. // Sanitate the method name. For some operators fix name
  393. if (declaration.Contains("::op")) {
  394. declaration.Replace("::opEquals(", ":: operator==(");
  395. declaration.Replace("::opAssign(", ":: operator=(");
  396. declaration.Replace("::opAddAssign(", ":: operator+=(");
  397. declaration.Replace("::opAdd(", ":: operator+(");
  398. declaration.Replace("::opCmp(", ":: operator<(");
  399. declaration.Replace("::opPreInc(", ":: operator++(");
  400. declaration.Replace("::opPostInc()", ":: operator++(int)");
  401. declaration.Replace("::opIndex(", ":: operator[ ](");
  402. if (declaration.Contains("::opImplCast()") || declaration.Contains("::opImplConv()")) {
  403. int sp = declaration.Find(' ');
  404. String retType = declaration.Substring(0, sp);
  405. if (retType == "const")
  406. {
  407. sp = declaration.Find(' ', sp + 1);
  408. retType = declaration.Substring(0, sp);
  409. }
  410. declaration = "operator " + retType + "() const";
  411. }
  412. }
  413. if (!declaration.Contains("::op"))
  414. {
  415. String prefix(typeName + "::");
  416. declaration.Replace(prefix, "");
  417. ///\todo Is there a better way to mark deprecated API bindings for AngelScript?
  418. i32 posBegin = declaration.FindLast("const String&in = \"deprecated:");
  419. if (posBegin != String::NPOS)
  420. {
  421. // Assume this 'mark' is added as the last parameter
  422. i32 posEnd = declaration.Find(')', posBegin);
  423. if (posEnd != String::NPOS)
  424. {
  425. declaration.Replace(posBegin, posEnd - posBegin, "");
  426. posBegin = declaration.Find(", ", posBegin - 2);
  427. if (posBegin != String::NPOS)
  428. declaration.Replace(posBegin, 2, "");
  429. if (mode == DOXYGEN)
  430. declaration += " // deprecated";
  431. else if (mode == C_HEADER)
  432. declaration = "/* deprecated */\n" + declaration;
  433. }
  434. }
  435. methodDeclarations.Push(declaration);
  436. }
  437. }
  438. }
  439. // Assume that the same property is never both an accessor property, and a direct one
  440. unsigned properties = type->GetPropertyCount();
  441. for (unsigned j = 0; j < properties; ++j)
  442. {
  443. const char* propertyName;
  444. const char* propertyDeclaration;
  445. int typeId;
  446. type->GetProperty(j, &propertyName, &typeId);
  447. propertyDeclaration = scriptEngine_->GetTypeDeclaration(typeId);
  448. PropertyInfo newInfo;
  449. newInfo.name_ = String(propertyName);
  450. newInfo.type_ = String(propertyDeclaration);
  451. newInfo.read_ = newInfo.write_ = true;
  452. propertyInfos.Push(newInfo);
  453. }
  454. Sort(methodDeclarations.Begin(), methodDeclarations.End(), ComparePropertyStrings);
  455. Sort(propertyInfos.Begin(), propertyInfos.End(), ComparePropertyInfos);
  456. if (!methodDeclarations.Empty())
  457. {
  458. if (mode == DOXYGEN)
  459. Log::WriteRaw("\nMethods:\n\n");
  460. else if (mode == C_HEADER)
  461. Log::WriteRaw("// Methods:\n");
  462. for (unsigned j = 0; j < methodDeclarations.Size(); ++j)
  463. OutputAPIRow(mode, methodDeclarations[j]);
  464. }
  465. if (!propertyInfos.Empty())
  466. {
  467. if (mode == DOXYGEN)
  468. Log::WriteRaw("\nProperties:\n\n");
  469. else if (mode == C_HEADER)
  470. Log::WriteRaw("\n// Properties:\n");
  471. for (unsigned j = 0; j < propertyInfos.Size(); ++j)
  472. {
  473. String remark;
  474. String cppdoc;
  475. if (!propertyInfos[j].write_)
  476. remark = "readonly";
  477. else if (!propertyInfos[j].read_)
  478. remark = "writeonly";
  479. if (!remark.Empty())
  480. {
  481. if (mode == DOXYGEN)
  482. {
  483. remark = " // " + remark;
  484. }
  485. else if (mode == C_HEADER)
  486. {
  487. cppdoc = "/* " + remark + " */\n";
  488. remark.Clear();
  489. }
  490. }
  491. OutputAPIRow(mode, cppdoc + propertyInfos[j].type_ + " " + propertyInfos[j].name_ + remark);
  492. }
  493. }
  494. // Check for namespaced constants to be included in the class documentation
  495. HashMap<String, Vector<String>>::ConstIterator gcIt = globalConstants.Find(typeName);
  496. if (gcIt != globalConstants.End())
  497. {
  498. String prefix;
  499. if (mode == DOXYGEN)
  500. {
  501. Log::WriteRaw("\nConstants:\n\n");
  502. }
  503. else if (mode == C_HEADER)
  504. {
  505. Log::WriteRaw("\n// Constants:\n");
  506. prefix = "static const ";
  507. }
  508. const Vector<String>& constants = gcIt->second_;
  509. for (unsigned j = 0; j < constants.Size(); ++j)
  510. OutputAPIRow(mode, prefix + constants[j]);
  511. }
  512. if (mode == DOXYGEN)
  513. Log::WriteRaw("\n");
  514. else if (mode == C_HEADER)
  515. Log::WriteRaw("};\n");
  516. }
  517. }
  518. Vector<PropertyInfo> globalPropertyInfos;
  519. Vector<String> globalFunctions;
  520. unsigned functions = scriptEngine_->GetGlobalFunctionCount();
  521. for (unsigned i = 0; i < functions; ++i)
  522. {
  523. asIScriptFunction* function = scriptEngine_->GetGlobalFunctionByIndex(i);
  524. String functionName(function->GetName());
  525. String declaration(function->GetDeclaration(true, false, true));
  526. // Recreate tab escape sequences
  527. declaration.Replace("\t", "\\t");
  528. if (functionName.Contains("set_") || functionName.Contains("get_"))
  529. ExtractPropertyInfo(functionName, declaration, globalPropertyInfos);
  530. else
  531. globalFunctions.Push(declaration);
  532. }
  533. Sort(globalFunctions.Begin(), globalFunctions.End(), ComparePropertyStrings);
  534. Sort(globalPropertyInfos.Begin(), globalPropertyInfos.End(), ComparePropertyInfos);
  535. if (mode == DOXYGEN)
  536. Log::WriteRaw("\\section ScriptAPI_Enums Enumerations\n");
  537. else if (mode == C_HEADER)
  538. Log::WriteRaw("\n// Enumerations\n");
  539. unsigned enums = scriptEngine_->GetEnumCount();
  540. Vector<Pair<String, unsigned>> sortedEnums;
  541. for (unsigned i = 0; i < enums; ++i)
  542. sortedEnums.Push(MakePair(String(scriptEngine_->GetEnumByIndex(i)->GetName()), i));
  543. Sort(sortedEnums.Begin(), sortedEnums.End());
  544. for (unsigned i = 0; i < sortedEnums.Size(); ++i)
  545. {
  546. asITypeInfo* enumType = scriptEngine_->GetEnumByIndex(sortedEnums[i].second_);
  547. int typeId = enumType->GetTypeId();
  548. if (mode == DOXYGEN)
  549. Log::WriteRaw("\n### " + String(enumType->GetName()) + "\n\n");
  550. else if (mode == C_HEADER)
  551. Log::WriteRaw("\nenum " + String(enumType->GetName()) + "\n{\n");
  552. for (unsigned j = 0; j < enumType->GetEnumValueCount(); ++j)
  553. {
  554. int value = 0;
  555. const char* name = enumType->GetEnumValueByIndex(j, &value);
  556. OutputAPIRow(mode, String(name), false, ",");
  557. }
  558. if (mode == DOXYGEN)
  559. Log::WriteRaw("\n");
  560. else if (mode == C_HEADER)
  561. Log::WriteRaw("};\n");
  562. }
  563. if (mode == DOXYGEN)
  564. Log::WriteRaw("\\section ScriptAPI_GlobalFunctions Global functions\n");
  565. else if (mode == C_HEADER)
  566. Log::WriteRaw("\n// Global functions\n");
  567. for (unsigned i = 0; i < globalFunctions.Size(); ++i)
  568. OutputAPIRow(mode, globalFunctions[i]);
  569. if (mode == DOXYGEN)
  570. Log::WriteRaw("\\section ScriptAPI_GlobalProperties Global properties\n");
  571. else if (mode == C_HEADER)
  572. Log::WriteRaw("\n// Global properties\n");
  573. for (unsigned i = 0; i < globalPropertyInfos.Size(); ++i)
  574. OutputAPIRow(mode, globalPropertyInfos[i].type_ + " " + globalPropertyInfos[i].name_, true);
  575. if (mode == DOXYGEN)
  576. Log::WriteRaw("\\section ScriptAPI_GlobalConstants Global constants\n");
  577. else if (mode == C_HEADER)
  578. Log::WriteRaw("\n// Global constants\n");
  579. const Vector<String>& noNameSpaceConstants = globalConstants[String()];
  580. for (unsigned i = 0; i < noNameSpaceConstants.Size(); ++i)
  581. OutputAPIRow(mode, noNameSpaceConstants[i], true);
  582. if (mode == DOXYGEN)
  583. Log::WriteRaw("*/\n\n}\n");
  584. }
  585. }