2
0

ScriptAPIDump.cpp 27 KB

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