doc_tools.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. /**************************************************************************/
  2. /* doc_tools.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "doc_tools.h"
  31. #include "core/config/engine.h"
  32. #include "core/config/project_settings.h"
  33. #include "core/core_constants.h"
  34. #include "core/io/compression.h"
  35. #include "core/io/dir_access.h"
  36. #include "core/io/resource_importer.h"
  37. #include "core/object/script_language.h"
  38. #include "core/string/translation_server.h"
  39. #include "editor/editor_settings.h"
  40. #include "editor/export/editor_export_platform.h"
  41. #include "scene/resources/theme.h"
  42. #include "scene/theme/theme_db.h"
  43. // Used for a hack preserving Mono properties on non-Mono builds.
  44. #include "modules/modules_enabled.gen.h" // For mono.
  45. static String _get_indent(const String &p_text) {
  46. String indent;
  47. bool has_text = false;
  48. int line_start = 0;
  49. for (int i = 0; i < p_text.length(); i++) {
  50. const char32_t c = p_text[i];
  51. if (c == '\n') {
  52. line_start = i + 1;
  53. } else if (c > 32) {
  54. has_text = true;
  55. indent = p_text.substr(line_start, i - line_start);
  56. break; // Indentation of the first line that has text.
  57. }
  58. }
  59. if (!has_text) {
  60. return p_text;
  61. }
  62. return indent;
  63. }
  64. static String _translate_doc_string(const String &p_text) {
  65. const String indent = _get_indent(p_text);
  66. const String message = p_text.dedent().strip_edges();
  67. const String translated = TranslationServer::get_singleton()->doc_translate(message, "");
  68. // No need to restore stripped edges because they'll be stripped again later.
  69. return translated.indent(indent);
  70. }
  71. // Comparator for constructors, based on `MethodDoc` operator.
  72. struct ConstructorCompare {
  73. _FORCE_INLINE_ bool operator()(const DocData::MethodDoc &p_lhs, const DocData::MethodDoc &p_rhs) const {
  74. // Must be a constructor (i.e. assume named for the class)
  75. // We want this arbitrary order for a class "Foo":
  76. // - 1. Default constructor: Foo()
  77. // - 2. Copy constructor: Foo(Foo)
  78. // - 3+. Other constructors Foo(Bar, ...) based on first argument's name
  79. if (p_lhs.arguments.is_empty() || p_rhs.arguments.is_empty()) { // 1.
  80. return p_lhs.arguments.size() < p_rhs.arguments.size();
  81. }
  82. if (p_lhs.arguments[0].type == p_lhs.return_type || p_rhs.arguments[0].type == p_lhs.return_type) { // 2.
  83. return (p_lhs.arguments[0].type == p_lhs.return_type) || (p_rhs.arguments[0].type != p_lhs.return_type);
  84. }
  85. return p_lhs.arguments[0] < p_rhs.arguments[0];
  86. }
  87. };
  88. // Comparator for operators, compares on name and type.
  89. struct OperatorCompare {
  90. _FORCE_INLINE_ bool operator()(const DocData::MethodDoc &p_lhs, const DocData::MethodDoc &p_rhs) const {
  91. if (p_lhs.name == p_rhs.name) {
  92. if (p_lhs.arguments.size() == p_rhs.arguments.size()) {
  93. if (p_lhs.arguments.is_empty()) {
  94. return false;
  95. }
  96. return p_lhs.arguments[0].type < p_rhs.arguments[0].type;
  97. }
  98. return p_lhs.arguments.size() < p_rhs.arguments.size();
  99. }
  100. return p_lhs.name.naturalcasecmp_to(p_rhs.name) < 0;
  101. }
  102. };
  103. // Comparator for methods, compares on names.
  104. struct MethodCompare {
  105. _FORCE_INLINE_ bool operator()(const DocData::MethodDoc &p_lhs, const DocData::MethodDoc &p_rhs) const {
  106. return p_lhs.name.naturalcasecmp_to(p_rhs.name) < 0;
  107. }
  108. };
  109. static void merge_constructors(Vector<DocData::MethodDoc> &p_to, const Vector<DocData::MethodDoc> &p_from) {
  110. // Get data from `p_from`, to avoid mutation checks.
  111. const DocData::MethodDoc *from_ptr = p_from.ptr();
  112. int64_t from_size = p_from.size();
  113. // TODO: Improve constructor merging.
  114. for (DocData::MethodDoc &to : p_to) {
  115. for (int64_t from_i = 0; from_i < from_size; ++from_i) {
  116. const DocData::MethodDoc &from = from_ptr[from_i];
  117. // Compare argument count first.
  118. if (from.arguments.size() != to.arguments.size()) {
  119. continue;
  120. }
  121. if (from.name != to.name) {
  122. continue;
  123. }
  124. {
  125. // Since constructors can repeat, we need to check the type of
  126. // the arguments so we make sure they are different.
  127. int64_t arg_count = from.arguments.size();
  128. Vector<bool> arg_used;
  129. arg_used.resize_zeroed(arg_count);
  130. // Also there is no guarantee that argument ordering will match,
  131. // so we have to check one by one so we make sure we have an exact match.
  132. for (int64_t arg_i = 0; arg_i < arg_count; ++arg_i) {
  133. for (int64_t arg_j = 0; arg_j < arg_count; ++arg_j) {
  134. if (from.arguments[arg_i].type == to.arguments[arg_j].type && !arg_used[arg_j]) {
  135. arg_used.write[arg_j] = true;
  136. break;
  137. }
  138. }
  139. }
  140. bool not_the_same = false;
  141. for (int64_t arg_i = 0; arg_i < arg_count; ++arg_i) {
  142. if (!arg_used[arg_i]) { // At least one of the arguments was different.
  143. not_the_same = true;
  144. break;
  145. }
  146. }
  147. if (not_the_same) {
  148. continue;
  149. }
  150. }
  151. to.description = from.description;
  152. to.is_deprecated = from.is_deprecated;
  153. to.deprecated_message = from.deprecated_message;
  154. to.is_experimental = from.is_experimental;
  155. to.experimental_message = from.experimental_message;
  156. break;
  157. }
  158. }
  159. }
  160. static void merge_methods(Vector<DocData::MethodDoc> &p_to, const Vector<DocData::MethodDoc> &p_from) {
  161. // Get data from `p_to`, to avoid mutation checks. Searching will be done in the sorted `p_to` from the (potentially) unsorted `p_from`.
  162. DocData::MethodDoc *to_ptrw = p_to.ptrw();
  163. int64_t to_size = p_to.size();
  164. for (const DocData::MethodDoc &from : p_from) {
  165. int64_t found = p_to.span().bisect<MethodCompare>(from, true);
  166. if (found >= to_size) {
  167. continue;
  168. }
  169. DocData::MethodDoc &to = to_ptrw[found];
  170. // Check found entry on name.
  171. if (to.name == from.name) {
  172. to.description = from.description;
  173. to.is_deprecated = from.is_deprecated;
  174. to.deprecated_message = from.deprecated_message;
  175. to.is_experimental = from.is_experimental;
  176. to.experimental_message = from.experimental_message;
  177. to.keywords = from.keywords;
  178. }
  179. }
  180. }
  181. static void merge_constants(Vector<DocData::ConstantDoc> &p_to, const Vector<DocData::ConstantDoc> &p_from) {
  182. // Get data from `p_from`, to avoid mutation checks. Searching will be done in the sorted `p_from` from the unsorted `p_to`.
  183. const DocData::ConstantDoc *from_ptr = p_from.ptr();
  184. int64_t from_size = p_from.size();
  185. for (DocData::ConstantDoc &to : p_to) {
  186. int64_t found = p_from.span().bisect(to, true);
  187. if (found >= from_size) {
  188. continue;
  189. }
  190. // Check found entry on name.
  191. const DocData::ConstantDoc &from = from_ptr[found];
  192. if (from.name == to.name) {
  193. to.description = from.description;
  194. to.is_deprecated = from.is_deprecated;
  195. to.deprecated_message = from.deprecated_message;
  196. to.is_experimental = from.is_experimental;
  197. to.experimental_message = from.experimental_message;
  198. to.keywords = from.keywords;
  199. }
  200. }
  201. }
  202. static void merge_properties(Vector<DocData::PropertyDoc> &p_to, const Vector<DocData::PropertyDoc> &p_from) {
  203. // Get data from `p_to`, to avoid mutation checks. Searching will be done in the sorted `p_to` from the (potentially) unsorted `p_from`.
  204. DocData::PropertyDoc *to_ptrw = p_to.ptrw();
  205. int64_t to_size = p_to.size();
  206. for (const DocData::PropertyDoc &from : p_from) {
  207. int64_t found = p_to.span().bisect(from, true);
  208. if (found >= to_size) {
  209. continue;
  210. }
  211. DocData::PropertyDoc &to = to_ptrw[found];
  212. // Check found entry on name.
  213. if (to.name == from.name) {
  214. to.description = from.description;
  215. to.is_deprecated = from.is_deprecated;
  216. to.deprecated_message = from.deprecated_message;
  217. to.is_experimental = from.is_experimental;
  218. to.experimental_message = from.experimental_message;
  219. to.keywords = from.keywords;
  220. }
  221. }
  222. }
  223. static void merge_theme_properties(Vector<DocData::ThemeItemDoc> &p_to, const Vector<DocData::ThemeItemDoc> &p_from) {
  224. // Get data from `p_to`, to avoid mutation checks. Searching will be done in the sorted `p_to` from the (potentially) unsorted `p_from`.
  225. DocData::ThemeItemDoc *to_ptrw = p_to.ptrw();
  226. int64_t to_size = p_to.size();
  227. for (const DocData::ThemeItemDoc &from : p_from) {
  228. int64_t found = p_to.span().bisect(from, true);
  229. if (found >= to_size) {
  230. continue;
  231. }
  232. DocData::ThemeItemDoc &to = to_ptrw[found];
  233. // Check found entry on name and data type.
  234. if (to.name == from.name && to.data_type == from.data_type) {
  235. to.description = from.description;
  236. to.is_deprecated = from.is_deprecated;
  237. to.deprecated_message = from.deprecated_message;
  238. to.is_experimental = from.is_experimental;
  239. to.experimental_message = from.experimental_message;
  240. to.keywords = from.keywords;
  241. }
  242. }
  243. }
  244. static void merge_operators(Vector<DocData::MethodDoc> &p_to, const Vector<DocData::MethodDoc> &p_from) {
  245. // Get data from `p_to`, to avoid mutation checks. Searching will be done in the sorted `p_to` from the (potentially) unsorted `p_from`.
  246. DocData::MethodDoc *to_ptrw = p_to.ptrw();
  247. int64_t to_size = p_to.size();
  248. for (const DocData::MethodDoc &from : p_from) {
  249. int64_t found = p_to.span().bisect(from, true);
  250. if (found >= to_size) {
  251. continue;
  252. }
  253. DocData::MethodDoc &to = to_ptrw[found];
  254. // Check found entry on name and argument.
  255. if (to.name == from.name && to.arguments.size() == from.arguments.size() && (to.arguments.is_empty() || to.arguments[0].type == from.arguments[0].type)) {
  256. to.description = from.description;
  257. to.is_deprecated = from.is_deprecated;
  258. to.deprecated_message = from.deprecated_message;
  259. to.is_experimental = from.is_experimental;
  260. to.experimental_message = from.experimental_message;
  261. }
  262. }
  263. }
  264. void DocTools::merge_from(const DocTools &p_data) {
  265. for (KeyValue<String, DocData::ClassDoc> &E : class_list) {
  266. DocData::ClassDoc &c = E.value;
  267. if (!p_data.class_list.has(c.name)) {
  268. continue;
  269. }
  270. const DocData::ClassDoc &cf = p_data.class_list[c.name];
  271. c.is_deprecated = cf.is_deprecated;
  272. c.deprecated_message = cf.deprecated_message;
  273. c.is_experimental = cf.is_experimental;
  274. c.experimental_message = cf.experimental_message;
  275. c.keywords = cf.keywords;
  276. c.description = cf.description;
  277. c.brief_description = cf.brief_description;
  278. c.tutorials = cf.tutorials;
  279. merge_constructors(c.constructors, cf.constructors);
  280. merge_methods(c.methods, cf.methods);
  281. merge_methods(c.signals, cf.signals);
  282. merge_constants(c.constants, cf.constants);
  283. merge_methods(c.annotations, cf.annotations);
  284. merge_properties(c.properties, cf.properties);
  285. merge_theme_properties(c.theme_properties, cf.theme_properties);
  286. merge_operators(c.operators, cf.operators);
  287. }
  288. }
  289. void DocTools::add_doc(const DocData::ClassDoc &p_class_doc) {
  290. ERR_FAIL_COND(p_class_doc.name.is_empty());
  291. class_list[p_class_doc.name] = p_class_doc;
  292. inheriting[p_class_doc.inherits].insert(p_class_doc.name);
  293. }
  294. void DocTools::remove_doc(const String &p_class_name) {
  295. ERR_FAIL_COND(p_class_name.is_empty() || !class_list.has(p_class_name));
  296. const String &inherits = class_list[p_class_name].inherits;
  297. if (inheriting.has(inherits)) {
  298. inheriting[inherits].erase(p_class_name);
  299. if (inheriting[inherits].is_empty()) {
  300. inheriting.erase(inherits);
  301. }
  302. }
  303. class_list.erase(p_class_name);
  304. }
  305. void DocTools::remove_script_doc_by_path(const String &p_path) {
  306. for (KeyValue<String, DocData::ClassDoc> &E : class_list) {
  307. if (E.value.is_script_doc && E.value.script_path == p_path) {
  308. remove_doc(E.key);
  309. return;
  310. }
  311. }
  312. }
  313. bool DocTools::has_doc(const String &p_class_name) {
  314. if (p_class_name.is_empty()) {
  315. return false;
  316. }
  317. return class_list.has(p_class_name);
  318. }
  319. static Variant get_documentation_default_value(const StringName &p_class_name, const StringName &p_property_name, bool &r_default_value_valid) {
  320. Variant default_value = Variant();
  321. r_default_value_valid = false;
  322. if (ClassDB::can_instantiate(p_class_name) && !ClassDB::is_virtual(p_class_name)) { // Keep this condition in sync with ClassDB::class_get_default_property_value.
  323. default_value = ClassDB::class_get_default_property_value(p_class_name, p_property_name, &r_default_value_valid);
  324. } else {
  325. // Cannot get default value of classes that can't be instantiated
  326. List<StringName> inheriting_classes;
  327. ClassDB::get_direct_inheriters_from_class(p_class_name, &inheriting_classes);
  328. for (const StringName &class_name : inheriting_classes) {
  329. if (ClassDB::can_instantiate(class_name)) {
  330. default_value = ClassDB::class_get_default_property_value(class_name, p_property_name, &r_default_value_valid);
  331. if (r_default_value_valid) {
  332. break;
  333. }
  334. }
  335. }
  336. }
  337. return default_value;
  338. }
  339. void DocTools::generate(BitField<GenerateFlags> p_flags) {
  340. // This may involve instantiating classes that are only usable from the main thread
  341. // (which is in fact the case of the core API).
  342. ERR_FAIL_COND(!Thread::is_main_thread());
  343. // Add ClassDB-exposed classes.
  344. {
  345. List<StringName> classes;
  346. if (p_flags.has_flag(GENERATE_FLAG_EXTENSION_CLASSES_ONLY)) {
  347. ClassDB::get_extensions_class_list(&classes);
  348. } else {
  349. ClassDB::get_class_list(&classes);
  350. // Move ProjectSettings, so that other classes can register properties there.
  351. classes.move_to_back(classes.find("ProjectSettings"));
  352. }
  353. bool skip_setter_getter_methods = true;
  354. // Populate documentation data for each exposed class.
  355. while (classes.size()) {
  356. const String &name = classes.front()->get();
  357. if (!ClassDB::is_class_exposed(name)) {
  358. print_verbose(vformat("Class '%s' is not exposed, skipping.", name));
  359. classes.pop_front();
  360. continue;
  361. }
  362. const String &cname = name;
  363. // Property setters and getters do not get exposed as individual methods.
  364. HashSet<StringName> setters_getters;
  365. class_list[cname] = DocData::ClassDoc();
  366. DocData::ClassDoc &c = class_list[cname];
  367. c.name = cname;
  368. c.inherits = ClassDB::get_parent_class(name);
  369. inheriting[c.inherits].insert(cname);
  370. List<PropertyInfo> properties;
  371. List<PropertyInfo> own_properties;
  372. // Special cases for editor/project settings, and ResourceImporter classes,
  373. // we have to rely on Object's property list to get settings and import options.
  374. // Otherwise we just use ClassDB's property list (pure registered properties).
  375. bool properties_from_instance = true; // To skip `script`, etc.
  376. bool import_option = false; // Special case for default value.
  377. HashMap<StringName, Variant> import_options_default;
  378. if (name == "EditorSettings") {
  379. // We don't create the full blown EditorSettings (+ config file) with `create()`,
  380. // instead we just make a local instance to get default values.
  381. Ref<EditorSettings> edset = memnew(EditorSettings);
  382. edset->get_property_list(&properties);
  383. own_properties = properties;
  384. } else if (name == "ProjectSettings") {
  385. ProjectSettings::get_singleton()->get_property_list(&properties);
  386. own_properties = properties;
  387. } else if (ClassDB::is_parent_class(name, "ResourceImporter") && name != "EditorImportPlugin" && ClassDB::can_instantiate(name)) {
  388. import_option = true;
  389. ResourceImporter *resimp = Object::cast_to<ResourceImporter>(ClassDB::instantiate(name));
  390. List<ResourceImporter::ImportOption> options;
  391. resimp->get_import_options("", &options);
  392. for (const ResourceImporter::ImportOption &option : options) {
  393. const PropertyInfo &prop = option.option;
  394. properties.push_back(prop);
  395. import_options_default[prop.name] = option.default_value;
  396. }
  397. own_properties = properties;
  398. memdelete(resimp);
  399. } else if (name.begins_with("EditorExportPlatform") && ClassDB::can_instantiate(name)) {
  400. properties_from_instance = false;
  401. Ref<EditorExportPlatform> platform = Object::cast_to<EditorExportPlatform>(ClassDB::instantiate(name));
  402. if (platform.is_valid()) {
  403. List<EditorExportPlatform::ExportOption> options;
  404. platform->get_export_options(&options);
  405. for (const EditorExportPlatform::ExportOption &E : options) {
  406. properties.push_back(E.option);
  407. }
  408. own_properties = properties;
  409. }
  410. } else {
  411. properties_from_instance = false;
  412. ClassDB::get_property_list(name, &properties);
  413. ClassDB::get_property_list(name, &own_properties, true);
  414. }
  415. // Sort is still needed here to handle inherited properties, even though it is done below, do not remove.
  416. properties.sort();
  417. own_properties.sort();
  418. List<PropertyInfo>::Element *EO = own_properties.front();
  419. for (const PropertyInfo &E : properties) {
  420. bool inherited = true;
  421. if (EO && EO->get() == E) {
  422. inherited = false;
  423. EO = EO->next();
  424. }
  425. if (properties_from_instance) {
  426. if (E.name == "resource_local_to_scene" || E.name == "resource_name" || E.name == "resource_path" || E.name == "script" || E.name == "resource_scene_unique_id") {
  427. // Don't include spurious properties from Object property list.
  428. continue;
  429. }
  430. }
  431. if (E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP || E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_INTERNAL || (E.type == Variant::NIL && E.usage & PROPERTY_USAGE_ARRAY)) {
  432. continue;
  433. }
  434. DocData::PropertyDoc prop;
  435. prop.name = E.name;
  436. prop.overridden = inherited;
  437. if (inherited) {
  438. String parent = ClassDB::get_parent_class(c.name);
  439. while (!ClassDB::has_property(parent, prop.name, true)) {
  440. parent = ClassDB::get_parent_class(parent);
  441. }
  442. prop.overrides = parent;
  443. }
  444. bool default_value_valid = false;
  445. Variant default_value;
  446. if (name == "ProjectSettings") {
  447. // Special case for project settings, so that settings are not taken from the current project's settings
  448. if (!ProjectSettings::get_singleton()->is_builtin_setting(E.name)) {
  449. continue;
  450. }
  451. if (E.usage & PROPERTY_USAGE_EDITOR) {
  452. if (!ProjectSettings::get_singleton()->get_ignore_value_in_docs(E.name)) {
  453. default_value = ProjectSettings::get_singleton()->property_get_revert(E.name);
  454. default_value_valid = true;
  455. }
  456. }
  457. } else if (name == "EditorSettings") {
  458. // Special case for editor settings, to prevent hardware or OS specific settings to affect the result.
  459. } else if (import_option) {
  460. default_value = import_options_default[E.name];
  461. default_value_valid = true;
  462. } else {
  463. default_value = get_documentation_default_value(name, E.name, default_value_valid);
  464. if (inherited) {
  465. bool base_default_value_valid = false;
  466. Variant base_default_value = get_documentation_default_value(ClassDB::get_parent_class(name), E.name, base_default_value_valid);
  467. if (!default_value_valid || !base_default_value_valid || default_value == base_default_value) {
  468. continue;
  469. }
  470. }
  471. }
  472. if (default_value_valid && default_value.get_type() != Variant::OBJECT) {
  473. prop.default_value = DocData::get_default_value_string(default_value);
  474. }
  475. StringName setter = ClassDB::get_property_setter(name, E.name);
  476. StringName getter = ClassDB::get_property_getter(name, E.name);
  477. prop.setter = setter;
  478. prop.getter = getter;
  479. bool found_type = false;
  480. if (getter != StringName()) {
  481. MethodBind *mb = ClassDB::get_method(name, getter);
  482. if (mb) {
  483. PropertyInfo retinfo = mb->get_return_info();
  484. found_type = true;
  485. if (retinfo.type == Variant::INT && retinfo.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
  486. prop.enumeration = retinfo.class_name;
  487. prop.is_bitfield = retinfo.usage & PROPERTY_USAGE_CLASS_IS_BITFIELD;
  488. prop.type = "int";
  489. } else if (retinfo.class_name != StringName()) {
  490. prop.type = retinfo.class_name;
  491. } else if (retinfo.type == Variant::ARRAY && retinfo.hint == PROPERTY_HINT_ARRAY_TYPE) {
  492. prop.type = retinfo.hint_string + "[]";
  493. } else if (retinfo.type == Variant::DICTIONARY && retinfo.hint == PROPERTY_HINT_DICTIONARY_TYPE) {
  494. prop.type = "Dictionary[" + retinfo.hint_string.replace(";", ", ") + "]";
  495. } else if (retinfo.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  496. prop.type = retinfo.hint_string;
  497. } else if (retinfo.type == Variant::NIL && retinfo.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
  498. prop.type = "Variant";
  499. } else if (retinfo.type == Variant::NIL) {
  500. prop.type = "void";
  501. } else {
  502. prop.type = Variant::get_type_name(retinfo.type);
  503. }
  504. }
  505. setters_getters.insert(getter);
  506. }
  507. if (setter != StringName()) {
  508. setters_getters.insert(setter);
  509. }
  510. if (!found_type) {
  511. if (E.type == Variant::OBJECT && E.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  512. prop.type = E.hint_string;
  513. } else {
  514. prop.type = Variant::get_type_name(E.type);
  515. }
  516. }
  517. c.properties.push_back(prop);
  518. }
  519. c.properties.sort();
  520. List<MethodInfo> method_list;
  521. ClassDB::get_method_list(name, &method_list, true);
  522. for (const MethodInfo &E : method_list) {
  523. if (E.name.is_empty() || (E.name[0] == '_' && !(E.flags & METHOD_FLAG_VIRTUAL))) {
  524. continue; //hidden, don't count
  525. }
  526. if (skip_setter_getter_methods && setters_getters.has(E.name)) {
  527. // Don't skip parametric setters and getters, i.e. method which require
  528. // one or more parameters to define what property should be set or retrieved.
  529. // E.g. CPUParticles3D::set_param(Parameter param, float value).
  530. if (E.arguments.is_empty() /* getter */ || (E.arguments.size() == 1 && E.return_val.type == Variant::NIL /* setter */)) {
  531. continue;
  532. }
  533. }
  534. DocData::MethodDoc method;
  535. DocData::method_doc_from_methodinfo(method, E, "");
  536. Vector<Error> errs = ClassDB::get_method_error_return_values(name, E.name);
  537. if (errs.size()) {
  538. if (!errs.has(OK)) {
  539. errs.insert(0, OK);
  540. }
  541. for (int i = 0; i < errs.size(); i++) {
  542. if (!method.errors_returned.has(errs[i])) {
  543. method.errors_returned.push_back(errs[i]);
  544. }
  545. }
  546. }
  547. c.methods.push_back(method);
  548. }
  549. c.methods.sort_custom<MethodCompare>();
  550. List<MethodInfo> signal_list;
  551. ClassDB::get_signal_list(name, &signal_list, true);
  552. if (signal_list.size()) {
  553. for (const MethodInfo &mi : signal_list) {
  554. DocData::MethodDoc signal;
  555. signal.name = mi.name;
  556. for (const PropertyInfo &arginfo : mi.arguments) {
  557. DocData::ArgumentDoc argument;
  558. DocData::argument_doc_from_arginfo(argument, arginfo);
  559. signal.arguments.push_back(argument);
  560. }
  561. c.signals.push_back(signal);
  562. }
  563. c.signals.sort_custom<MethodCompare>();
  564. }
  565. List<String> constant_list;
  566. ClassDB::get_integer_constant_list(name, &constant_list, true);
  567. for (const String &E : constant_list) {
  568. DocData::ConstantDoc constant;
  569. constant.name = E;
  570. constant.value = itos(ClassDB::get_integer_constant(name, E));
  571. constant.is_value_valid = true;
  572. constant.type = "int";
  573. constant.enumeration = ClassDB::get_integer_constant_enum(name, E);
  574. constant.is_bitfield = ClassDB::is_enum_bitfield(name, constant.enumeration);
  575. c.constants.push_back(constant);
  576. }
  577. // Theme items.
  578. {
  579. List<ThemeDB::ThemeItemBind> theme_items;
  580. ThemeDB::get_singleton()->get_class_items(cname, &theme_items);
  581. Ref<Theme> default_theme = ThemeDB::get_singleton()->get_default_theme();
  582. for (const ThemeDB::ThemeItemBind &theme_item : theme_items) {
  583. DocData::ThemeItemDoc tid;
  584. tid.name = theme_item.item_name;
  585. switch (theme_item.data_type) {
  586. case Theme::DATA_TYPE_COLOR:
  587. tid.type = "Color";
  588. tid.data_type = "color";
  589. break;
  590. case Theme::DATA_TYPE_CONSTANT:
  591. tid.type = "int";
  592. tid.data_type = "constant";
  593. break;
  594. case Theme::DATA_TYPE_FONT:
  595. tid.type = "Font";
  596. tid.data_type = "font";
  597. break;
  598. case Theme::DATA_TYPE_FONT_SIZE:
  599. tid.type = "int";
  600. tid.data_type = "font_size";
  601. break;
  602. case Theme::DATA_TYPE_ICON:
  603. tid.type = "Texture2D";
  604. tid.data_type = "icon";
  605. break;
  606. case Theme::DATA_TYPE_STYLEBOX:
  607. tid.type = "StyleBox";
  608. tid.data_type = "style";
  609. break;
  610. case Theme::DATA_TYPE_MAX:
  611. break; // Can't happen, but silences warning.
  612. }
  613. if (theme_item.data_type == Theme::DATA_TYPE_COLOR || theme_item.data_type == Theme::DATA_TYPE_CONSTANT) {
  614. tid.default_value = DocData::get_default_value_string(default_theme->get_theme_item(theme_item.data_type, theme_item.item_name, cname));
  615. }
  616. c.theme_properties.push_back(tid);
  617. }
  618. c.theme_properties.sort();
  619. }
  620. classes.pop_front();
  621. }
  622. }
  623. if (p_flags.has_flag(GENERATE_FLAG_SKIP_BASIC_TYPES)) {
  624. return;
  625. }
  626. // Add a dummy Variant entry.
  627. {
  628. // This allows us to document the concept of Variant even though
  629. // it's not a ClassDB-exposed class.
  630. class_list["Variant"] = DocData::ClassDoc();
  631. class_list["Variant"].name = "Variant";
  632. inheriting[""].insert("Variant");
  633. }
  634. // Add Variant data types.
  635. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  636. if (i == Variant::NIL) {
  637. continue; // Not exposed outside of 'null', should not be in class list.
  638. }
  639. if (i == Variant::OBJECT) {
  640. continue; // Use the core type instead.
  641. }
  642. String cname = Variant::get_type_name(Variant::Type(i));
  643. class_list[cname] = DocData::ClassDoc();
  644. DocData::ClassDoc &c = class_list[cname];
  645. c.name = cname;
  646. inheriting[""].insert(cname);
  647. Callable::CallError cerror;
  648. Variant v;
  649. Variant::construct(Variant::Type(i), v, nullptr, 0, cerror);
  650. List<MethodInfo> method_list;
  651. v.get_method_list(&method_list);
  652. Variant::get_constructor_list(Variant::Type(i), &method_list);
  653. for (int j = 0; j < Variant::OP_AND; j++) { // Showing above 'and' is pretty confusing and there are a lot of variations.
  654. for (int k = 0; k < Variant::VARIANT_MAX; k++) {
  655. // Prevent generating for comparison with null.
  656. if (Variant::Type(k) == Variant::NIL && (Variant::Operator(j) == Variant::OP_EQUAL || Variant::Operator(j) == Variant::OP_NOT_EQUAL)) {
  657. continue;
  658. }
  659. Variant::Type rt = Variant::get_operator_return_type(Variant::Operator(j), Variant::Type(i), Variant::Type(k));
  660. if (rt != Variant::NIL) { // Has operator.
  661. // Skip String % operator as it's registered separately for each Variant arg type,
  662. // we'll add it manually below.
  663. if ((i == Variant::STRING || i == Variant::STRING_NAME) && Variant::Operator(j) == Variant::OP_MODULE) {
  664. continue;
  665. }
  666. MethodInfo mi;
  667. mi.name = "operator " + Variant::get_operator_name(Variant::Operator(j));
  668. mi.return_val.type = rt;
  669. if (k != Variant::NIL) {
  670. PropertyInfo arg;
  671. arg.name = "right";
  672. arg.type = Variant::Type(k);
  673. mi.arguments.push_back(arg);
  674. }
  675. method_list.push_back(mi);
  676. }
  677. }
  678. }
  679. if (i == Variant::STRING || i == Variant::STRING_NAME) {
  680. // We skipped % operator above, and we register it manually once for Variant arg type here.
  681. MethodInfo mi;
  682. mi.name = "operator %";
  683. mi.return_val.type = Variant::STRING;
  684. PropertyInfo arg;
  685. arg.name = "right";
  686. arg.type = Variant::NIL;
  687. arg.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  688. mi.arguments.push_back(arg);
  689. method_list.push_back(mi);
  690. }
  691. if (Variant::is_keyed(Variant::Type(i))) {
  692. MethodInfo mi;
  693. mi.name = "operator []";
  694. mi.return_val.type = Variant::NIL;
  695. mi.return_val.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  696. PropertyInfo arg;
  697. arg.name = "key";
  698. arg.type = Variant::NIL;
  699. arg.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  700. mi.arguments.push_back(arg);
  701. method_list.push_back(mi);
  702. } else if (Variant::has_indexing(Variant::Type(i))) {
  703. MethodInfo mi;
  704. mi.name = "operator []";
  705. mi.return_val.type = Variant::get_indexed_element_type(Variant::Type(i));
  706. mi.return_val.usage = Variant::get_indexed_element_usage(Variant::Type(i));
  707. PropertyInfo arg;
  708. arg.name = "index";
  709. arg.type = Variant::INT;
  710. mi.arguments.push_back(arg);
  711. method_list.push_back(mi);
  712. }
  713. for (const MethodInfo &mi : method_list) {
  714. DocData::MethodDoc method;
  715. method.name = mi.name;
  716. for (int64_t j = 0; j < mi.arguments.size(); ++j) {
  717. const PropertyInfo &arginfo = mi.arguments[j];
  718. DocData::ArgumentDoc ad;
  719. DocData::argument_doc_from_arginfo(ad, arginfo);
  720. ad.name = arginfo.name;
  721. int darg_idx = mi.default_arguments.size() - mi.arguments.size() + j;
  722. if (darg_idx >= 0) {
  723. ad.default_value = DocData::get_default_value_string(mi.default_arguments[darg_idx]);
  724. }
  725. method.arguments.push_back(ad);
  726. }
  727. DocData::return_doc_from_retinfo(method, mi.return_val);
  728. if (mi.flags & METHOD_FLAG_VARARG) {
  729. if (!method.qualifiers.is_empty()) {
  730. method.qualifiers += " ";
  731. }
  732. method.qualifiers += "vararg";
  733. }
  734. if (mi.flags & METHOD_FLAG_CONST) {
  735. if (!method.qualifiers.is_empty()) {
  736. method.qualifiers += " ";
  737. }
  738. method.qualifiers += "const";
  739. }
  740. if (mi.flags & METHOD_FLAG_STATIC) {
  741. if (!method.qualifiers.is_empty()) {
  742. method.qualifiers += " ";
  743. }
  744. method.qualifiers += "static";
  745. }
  746. if (method.name == cname) {
  747. c.constructors.push_back(method);
  748. } else if (method.name.begins_with("operator")) {
  749. c.operators.push_back(method);
  750. } else {
  751. c.methods.push_back(method);
  752. }
  753. }
  754. c.constructors.sort_custom<ConstructorCompare>();
  755. c.operators.sort_custom<OperatorCompare>();
  756. c.methods.sort_custom<MethodCompare>();
  757. List<PropertyInfo> properties;
  758. v.get_property_list(&properties);
  759. for (const PropertyInfo &pi : properties) {
  760. DocData::PropertyDoc property;
  761. property.name = pi.name;
  762. property.type = Variant::get_type_name(pi.type);
  763. property.default_value = DocData::get_default_value_string(v.get(pi.name));
  764. c.properties.push_back(property);
  765. }
  766. c.properties.sort();
  767. List<StringName> enums;
  768. Variant::get_enums_for_type(Variant::Type(i), &enums);
  769. for (const StringName &E : enums) {
  770. List<StringName> enumerations;
  771. Variant::get_enumerations_for_enum(Variant::Type(i), E, &enumerations);
  772. for (const StringName &F : enumerations) {
  773. DocData::ConstantDoc constant;
  774. constant.name = F;
  775. constant.value = itos(Variant::get_enum_value(Variant::Type(i), E, F));
  776. constant.is_value_valid = true;
  777. constant.type = "int";
  778. constant.enumeration = E;
  779. c.constants.push_back(constant);
  780. }
  781. }
  782. List<StringName> constants;
  783. Variant::get_constants_for_type(Variant::Type(i), &constants);
  784. for (const StringName &E : constants) {
  785. DocData::ConstantDoc constant;
  786. constant.name = E;
  787. Variant value = Variant::get_constant_value(Variant::Type(i), E);
  788. constant.value = value.get_type() == Variant::INT ? itos(value) : value.get_construct_string().replace_char('\n', ' ');
  789. constant.is_value_valid = true;
  790. constant.type = Variant::get_type_name(value.get_type());
  791. c.constants.push_back(constant);
  792. }
  793. }
  794. // Add global API (servers, engine singletons, global constants) and Variant utility functions.
  795. {
  796. String cname = "@GlobalScope";
  797. class_list[cname] = DocData::ClassDoc();
  798. DocData::ClassDoc &c = class_list[cname];
  799. c.name = cname;
  800. inheriting[""].insert(cname);
  801. // Global constants.
  802. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  803. DocData::ConstantDoc cd;
  804. cd.name = CoreConstants::get_global_constant_name(i);
  805. cd.type = "int";
  806. cd.enumeration = CoreConstants::get_global_constant_enum(i);
  807. cd.is_bitfield = CoreConstants::is_global_constant_bitfield(i);
  808. if (!CoreConstants::get_ignore_value_in_docs(i)) {
  809. cd.value = itos(CoreConstants::get_global_constant_value(i));
  810. cd.is_value_valid = true;
  811. } else {
  812. cd.is_value_valid = false;
  813. }
  814. c.constants.push_back(cd);
  815. }
  816. // Servers/engine singletons.
  817. List<Engine::Singleton> singletons;
  818. Engine::get_singleton()->get_singletons(&singletons);
  819. // FIXME: this is kind of hackish...
  820. for (const Engine::Singleton &s : singletons) {
  821. DocData::PropertyDoc pd;
  822. if (!s.ptr) {
  823. continue;
  824. }
  825. pd.name = s.name;
  826. pd.type = s.ptr->get_class();
  827. while (String(ClassDB::get_parent_class(pd.type)) != "Object") {
  828. pd.type = ClassDB::get_parent_class(pd.type);
  829. }
  830. c.properties.push_back(pd);
  831. }
  832. c.properties.sort();
  833. // Variant utility functions.
  834. List<StringName> utility_functions;
  835. Variant::get_utility_function_list(&utility_functions);
  836. for (const StringName &E : utility_functions) {
  837. DocData::MethodDoc md;
  838. md.name = E;
  839. // Utility function's return type.
  840. if (Variant::has_utility_function_return_value(E)) {
  841. PropertyInfo pi;
  842. pi.type = Variant::get_utility_function_return_type(E);
  843. if (pi.type == Variant::NIL) {
  844. pi.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  845. }
  846. DocData::ArgumentDoc ad;
  847. DocData::argument_doc_from_arginfo(ad, pi);
  848. md.return_type = ad.type;
  849. } else {
  850. md.return_type = "void";
  851. }
  852. // Utility function's arguments.
  853. if (Variant::is_utility_function_vararg(E)) {
  854. md.qualifiers = "vararg";
  855. } else {
  856. for (int i = 0; i < Variant::get_utility_function_argument_count(E); i++) {
  857. PropertyInfo pi;
  858. pi.type = Variant::get_utility_function_argument_type(E, i);
  859. pi.name = Variant::get_utility_function_argument_name(E, i);
  860. if (pi.type == Variant::NIL) {
  861. pi.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  862. }
  863. DocData::ArgumentDoc ad;
  864. DocData::argument_doc_from_arginfo(ad, pi);
  865. md.arguments.push_back(ad);
  866. }
  867. }
  868. c.methods.push_back(md);
  869. }
  870. c.methods.sort_custom<MethodCompare>();
  871. }
  872. // Add scripting language built-ins.
  873. {
  874. // We only add a doc entry for languages which actually define any built-in
  875. // methods, constants, or annotations.
  876. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  877. ScriptLanguage *lang = ScriptServer::get_language(i);
  878. String cname = "@" + lang->get_name();
  879. DocData::ClassDoc c;
  880. c.name = cname;
  881. inheriting[""].insert(cname);
  882. // Get functions.
  883. List<MethodInfo> minfo;
  884. lang->get_public_functions(&minfo);
  885. for (const MethodInfo &mi : minfo) {
  886. DocData::MethodDoc md;
  887. md.name = mi.name;
  888. if (mi.flags & METHOD_FLAG_VARARG) {
  889. if (!md.qualifiers.is_empty()) {
  890. md.qualifiers += " ";
  891. }
  892. md.qualifiers += "vararg";
  893. }
  894. DocData::return_doc_from_retinfo(md, mi.return_val);
  895. for (int64_t j = 0; j < mi.arguments.size(); ++j) {
  896. DocData::ArgumentDoc ad;
  897. DocData::argument_doc_from_arginfo(ad, mi.arguments[j]);
  898. int darg_idx = j - (mi.arguments.size() - mi.default_arguments.size());
  899. if (darg_idx >= 0) {
  900. ad.default_value = DocData::get_default_value_string(mi.default_arguments[darg_idx]);
  901. }
  902. md.arguments.push_back(ad);
  903. }
  904. c.methods.push_back(md);
  905. }
  906. // Get constants.
  907. List<Pair<String, Variant>> cinfo;
  908. lang->get_public_constants(&cinfo);
  909. for (const Pair<String, Variant> &E : cinfo) {
  910. DocData::ConstantDoc cd;
  911. cd.name = E.first;
  912. cd.value = E.second;
  913. cd.is_value_valid = true;
  914. cd.type = Variant::get_type_name(E.second.get_type());
  915. c.constants.push_back(cd);
  916. }
  917. // Get annotations.
  918. List<MethodInfo> ainfo;
  919. lang->get_public_annotations(&ainfo);
  920. for (const MethodInfo &ai : ainfo) {
  921. DocData::MethodDoc atd;
  922. atd.name = ai.name;
  923. if (ai.flags & METHOD_FLAG_VARARG) {
  924. if (!atd.qualifiers.is_empty()) {
  925. atd.qualifiers += " ";
  926. }
  927. atd.qualifiers += "vararg";
  928. }
  929. DocData::return_doc_from_retinfo(atd, ai.return_val);
  930. for (int64_t j = 0; j < ai.arguments.size(); ++j) {
  931. DocData::ArgumentDoc ad;
  932. DocData::argument_doc_from_arginfo(ad, ai.arguments[j]);
  933. int64_t darg_idx = j - (ai.arguments.size() - ai.default_arguments.size());
  934. if (darg_idx >= 0) {
  935. ad.default_value = DocData::get_default_value_string(ai.default_arguments[darg_idx]);
  936. }
  937. atd.arguments.push_back(ad);
  938. }
  939. c.annotations.push_back(atd);
  940. }
  941. // Skip adding the lang if it doesn't expose anything (e.g. C#).
  942. if (c.methods.is_empty() && c.constants.is_empty() && c.annotations.is_empty()) {
  943. continue;
  944. }
  945. c.methods.sort_custom<MethodCompare>();
  946. c.annotations.sort_custom<MethodCompare>();
  947. class_list[cname] = c;
  948. }
  949. }
  950. }
  951. static Error _parse_methods(Ref<XMLParser> &parser, Vector<DocData::MethodDoc> &methods) {
  952. String section = parser->get_node_name();
  953. String element = section.substr(0, section.length() - 1);
  954. while (parser->read() == OK) {
  955. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  956. if (parser->get_node_name() == element) {
  957. DocData::MethodDoc method;
  958. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  959. method.name = parser->get_named_attribute_value("name");
  960. if (parser->has_attribute("qualifiers")) {
  961. method.qualifiers = parser->get_named_attribute_value("qualifiers");
  962. }
  963. #ifndef DISABLE_DEPRECATED
  964. if (parser->has_attribute("is_deprecated")) {
  965. method.is_deprecated = parser->get_named_attribute_value("is_deprecated").to_lower() == "true";
  966. }
  967. if (parser->has_attribute("is_experimental")) {
  968. method.is_experimental = parser->get_named_attribute_value("is_experimental").to_lower() == "true";
  969. }
  970. #endif
  971. if (parser->has_attribute("deprecated")) {
  972. method.is_deprecated = true;
  973. method.deprecated_message = parser->get_named_attribute_value("deprecated");
  974. }
  975. if (parser->has_attribute("experimental")) {
  976. method.is_experimental = true;
  977. method.experimental_message = parser->get_named_attribute_value("experimental");
  978. }
  979. if (parser->has_attribute("keywords")) {
  980. method.keywords = parser->get_named_attribute_value("keywords");
  981. }
  982. while (parser->read() == OK) {
  983. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  984. String name = parser->get_node_name();
  985. if (name == "return") {
  986. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  987. method.return_type = parser->get_named_attribute_value("type");
  988. if (parser->has_attribute("enum")) {
  989. method.return_enum = parser->get_named_attribute_value("enum");
  990. if (parser->has_attribute("is_bitfield")) {
  991. method.return_is_bitfield = parser->get_named_attribute_value("is_bitfield").to_lower() == "true";
  992. }
  993. }
  994. } else if (name == "returns_error") {
  995. ERR_FAIL_COND_V(!parser->has_attribute("number"), ERR_FILE_CORRUPT);
  996. method.errors_returned.push_back(parser->get_named_attribute_value("number").to_int());
  997. } else if (name == "param") {
  998. DocData::ArgumentDoc argument;
  999. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  1000. argument.name = parser->get_named_attribute_value("name");
  1001. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  1002. argument.type = parser->get_named_attribute_value("type");
  1003. if (parser->has_attribute("enum")) {
  1004. argument.enumeration = parser->get_named_attribute_value("enum");
  1005. if (parser->has_attribute("is_bitfield")) {
  1006. argument.is_bitfield = parser->get_named_attribute_value("is_bitfield").to_lower() == "true";
  1007. }
  1008. }
  1009. method.arguments.push_back(argument);
  1010. } else if (name == "description") {
  1011. parser->read();
  1012. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1013. method.description = parser->get_node_data();
  1014. }
  1015. }
  1016. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == element) {
  1017. break;
  1018. }
  1019. }
  1020. methods.push_back(method);
  1021. } else {
  1022. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + parser->get_node_name() + ", expected " + element + ".");
  1023. }
  1024. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == section) {
  1025. break;
  1026. }
  1027. }
  1028. return OK;
  1029. }
  1030. Error DocTools::load_classes(const String &p_dir) {
  1031. Error err;
  1032. Ref<DirAccess> da = DirAccess::open(p_dir, &err);
  1033. if (da.is_null()) {
  1034. return err;
  1035. }
  1036. da->list_dir_begin();
  1037. String path;
  1038. path = da->get_next();
  1039. while (!path.is_empty()) {
  1040. if (!da->current_is_dir() && path.ends_with("xml")) {
  1041. Ref<XMLParser> parser = memnew(XMLParser);
  1042. Error err2 = parser->open(p_dir.path_join(path));
  1043. if (err2) {
  1044. return err2;
  1045. }
  1046. _load(parser);
  1047. }
  1048. path = da->get_next();
  1049. }
  1050. da->list_dir_end();
  1051. return OK;
  1052. }
  1053. Error DocTools::erase_classes(const String &p_dir) {
  1054. Error err;
  1055. Ref<DirAccess> da = DirAccess::open(p_dir, &err);
  1056. if (da.is_null()) {
  1057. return err;
  1058. }
  1059. List<String> to_erase;
  1060. da->list_dir_begin();
  1061. String path;
  1062. path = da->get_next();
  1063. while (!path.is_empty()) {
  1064. if (!da->current_is_dir() && path.ends_with("xml")) {
  1065. to_erase.push_back(path);
  1066. }
  1067. path = da->get_next();
  1068. }
  1069. da->list_dir_end();
  1070. while (to_erase.size()) {
  1071. da->remove(to_erase.front()->get());
  1072. to_erase.pop_front();
  1073. }
  1074. return OK;
  1075. }
  1076. Error DocTools::_load(Ref<XMLParser> parser) {
  1077. Error err = OK;
  1078. while ((err = parser->read()) == OK) {
  1079. if (parser->get_node_type() == XMLParser::NODE_ELEMENT && parser->get_node_name() == "?xml") {
  1080. parser->skip_section();
  1081. }
  1082. if (parser->get_node_type() != XMLParser::NODE_ELEMENT) {
  1083. continue; //no idea what this may be, but skipping anyway
  1084. }
  1085. ERR_FAIL_COND_V(parser->get_node_name() != "class", ERR_FILE_CORRUPT);
  1086. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  1087. String name = parser->get_named_attribute_value("name");
  1088. class_list[name] = DocData::ClassDoc();
  1089. DocData::ClassDoc &c = class_list[name];
  1090. c.name = name;
  1091. if (parser->has_attribute("inherits")) {
  1092. c.inherits = parser->get_named_attribute_value("inherits");
  1093. }
  1094. inheriting[c.inherits].insert(name);
  1095. #ifndef DISABLE_DEPRECATED
  1096. if (parser->has_attribute("is_deprecated")) {
  1097. c.is_deprecated = parser->get_named_attribute_value("is_deprecated").to_lower() == "true";
  1098. }
  1099. if (parser->has_attribute("is_experimental")) {
  1100. c.is_experimental = parser->get_named_attribute_value("is_experimental").to_lower() == "true";
  1101. }
  1102. #endif
  1103. if (parser->has_attribute("deprecated")) {
  1104. c.is_deprecated = true;
  1105. c.deprecated_message = parser->get_named_attribute_value("deprecated");
  1106. }
  1107. if (parser->has_attribute("experimental")) {
  1108. c.is_experimental = true;
  1109. c.experimental_message = parser->get_named_attribute_value("experimental");
  1110. }
  1111. if (parser->has_attribute("keywords")) {
  1112. c.keywords = parser->get_named_attribute_value("keywords");
  1113. }
  1114. while (parser->read() == OK) {
  1115. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1116. String name2 = parser->get_node_name();
  1117. if (name2 == "brief_description") {
  1118. parser->read();
  1119. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1120. c.brief_description = parser->get_node_data();
  1121. }
  1122. } else if (name2 == "description") {
  1123. parser->read();
  1124. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1125. c.description = parser->get_node_data();
  1126. }
  1127. } else if (name2 == "tutorials") {
  1128. while (parser->read() == OK) {
  1129. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1130. String name3 = parser->get_node_name();
  1131. if (name3 == "link") {
  1132. DocData::TutorialDoc tutorial;
  1133. if (parser->has_attribute("title")) {
  1134. tutorial.title = parser->get_named_attribute_value("title");
  1135. }
  1136. parser->read();
  1137. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1138. tutorial.link = parser->get_node_data().strip_edges();
  1139. c.tutorials.push_back(tutorial);
  1140. }
  1141. } else {
  1142. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1143. }
  1144. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "tutorials") {
  1145. break; // End of <tutorials>.
  1146. }
  1147. }
  1148. } else if (name2 == "constructors") {
  1149. Error err2 = _parse_methods(parser, c.constructors);
  1150. ERR_FAIL_COND_V(err2, err2);
  1151. } else if (name2 == "methods") {
  1152. Error err2 = _parse_methods(parser, c.methods);
  1153. ERR_FAIL_COND_V(err2, err2);
  1154. } else if (name2 == "operators") {
  1155. Error err2 = _parse_methods(parser, c.operators);
  1156. ERR_FAIL_COND_V(err2, err2);
  1157. } else if (name2 == "signals") {
  1158. Error err2 = _parse_methods(parser, c.signals);
  1159. ERR_FAIL_COND_V(err2, err2);
  1160. } else if (name2 == "annotations") {
  1161. Error err2 = _parse_methods(parser, c.annotations);
  1162. ERR_FAIL_COND_V(err2, err2);
  1163. } else if (name2 == "members") {
  1164. while (parser->read() == OK) {
  1165. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1166. String name3 = parser->get_node_name();
  1167. if (name3 == "member") {
  1168. DocData::PropertyDoc prop2;
  1169. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  1170. prop2.name = parser->get_named_attribute_value("name");
  1171. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  1172. prop2.type = parser->get_named_attribute_value("type");
  1173. if (parser->has_attribute("setter")) {
  1174. prop2.setter = parser->get_named_attribute_value("setter");
  1175. }
  1176. if (parser->has_attribute("getter")) {
  1177. prop2.getter = parser->get_named_attribute_value("getter");
  1178. }
  1179. if (parser->has_attribute("enum")) {
  1180. prop2.enumeration = parser->get_named_attribute_value("enum");
  1181. if (parser->has_attribute("is_bitfield")) {
  1182. prop2.is_bitfield = parser->get_named_attribute_value("is_bitfield").to_lower() == "true";
  1183. }
  1184. }
  1185. #ifndef DISABLE_DEPRECATED
  1186. if (parser->has_attribute("is_deprecated")) {
  1187. prop2.is_deprecated = parser->get_named_attribute_value("is_deprecated").to_lower() == "true";
  1188. }
  1189. if (parser->has_attribute("is_experimental")) {
  1190. prop2.is_experimental = parser->get_named_attribute_value("is_experimental").to_lower() == "true";
  1191. }
  1192. #endif
  1193. if (parser->has_attribute("deprecated")) {
  1194. prop2.is_deprecated = true;
  1195. prop2.deprecated_message = parser->get_named_attribute_value("deprecated");
  1196. }
  1197. if (parser->has_attribute("experimental")) {
  1198. prop2.is_experimental = true;
  1199. prop2.experimental_message = parser->get_named_attribute_value("experimental");
  1200. }
  1201. if (parser->has_attribute("keywords")) {
  1202. prop2.keywords = parser->get_named_attribute_value("keywords");
  1203. }
  1204. if (!parser->is_empty()) {
  1205. parser->read();
  1206. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1207. prop2.description = parser->get_node_data();
  1208. }
  1209. }
  1210. c.properties.push_back(prop2);
  1211. } else {
  1212. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1213. }
  1214. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "members") {
  1215. break; // End of <members>.
  1216. }
  1217. }
  1218. } else if (name2 == "theme_items") {
  1219. while (parser->read() == OK) {
  1220. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1221. String name3 = parser->get_node_name();
  1222. if (name3 == "theme_item") {
  1223. DocData::ThemeItemDoc prop2;
  1224. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  1225. prop2.name = parser->get_named_attribute_value("name");
  1226. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  1227. prop2.type = parser->get_named_attribute_value("type");
  1228. ERR_FAIL_COND_V(!parser->has_attribute("data_type"), ERR_FILE_CORRUPT);
  1229. prop2.data_type = parser->get_named_attribute_value("data_type");
  1230. if (parser->has_attribute("deprecated")) {
  1231. prop2.is_deprecated = true;
  1232. prop2.deprecated_message = parser->get_named_attribute_value("deprecated");
  1233. }
  1234. if (parser->has_attribute("experimental")) {
  1235. prop2.is_experimental = true;
  1236. prop2.experimental_message = parser->get_named_attribute_value("experimental");
  1237. }
  1238. if (parser->has_attribute("keywords")) {
  1239. prop2.keywords = parser->get_named_attribute_value("keywords");
  1240. }
  1241. if (!parser->is_empty()) {
  1242. parser->read();
  1243. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1244. prop2.description = parser->get_node_data();
  1245. }
  1246. }
  1247. c.theme_properties.push_back(prop2);
  1248. } else {
  1249. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1250. }
  1251. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "theme_items") {
  1252. break; // End of <theme_items>.
  1253. }
  1254. }
  1255. } else if (name2 == "constants") {
  1256. while (parser->read() == OK) {
  1257. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1258. String name3 = parser->get_node_name();
  1259. if (name3 == "constant") {
  1260. DocData::ConstantDoc constant2;
  1261. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  1262. constant2.name = parser->get_named_attribute_value("name");
  1263. ERR_FAIL_COND_V(!parser->has_attribute("value"), ERR_FILE_CORRUPT);
  1264. constant2.value = parser->get_named_attribute_value("value");
  1265. constant2.is_value_valid = true;
  1266. if (parser->has_attribute("enum")) {
  1267. constant2.enumeration = parser->get_named_attribute_value("enum");
  1268. if (parser->has_attribute("is_bitfield")) {
  1269. constant2.is_bitfield = parser->get_named_attribute_value("is_bitfield").to_lower() == "true";
  1270. }
  1271. }
  1272. #ifndef DISABLE_DEPRECATED
  1273. if (parser->has_attribute("is_deprecated")) {
  1274. constant2.is_deprecated = parser->get_named_attribute_value("is_deprecated").to_lower() == "true";
  1275. }
  1276. if (parser->has_attribute("is_experimental")) {
  1277. constant2.is_experimental = parser->get_named_attribute_value("is_experimental").to_lower() == "true";
  1278. }
  1279. #endif
  1280. if (parser->has_attribute("deprecated")) {
  1281. constant2.is_deprecated = true;
  1282. constant2.deprecated_message = parser->get_named_attribute_value("deprecated");
  1283. }
  1284. if (parser->has_attribute("experimental")) {
  1285. constant2.is_experimental = true;
  1286. constant2.experimental_message = parser->get_named_attribute_value("experimental");
  1287. }
  1288. if (parser->has_attribute("keywords")) {
  1289. constant2.keywords = parser->get_named_attribute_value("keywords");
  1290. }
  1291. if (!parser->is_empty()) {
  1292. parser->read();
  1293. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1294. constant2.description = parser->get_node_data();
  1295. }
  1296. }
  1297. c.constants.push_back(constant2);
  1298. } else {
  1299. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1300. }
  1301. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "constants") {
  1302. break; // End of <constants>.
  1303. }
  1304. }
  1305. } else {
  1306. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name2 + ".");
  1307. }
  1308. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "class") {
  1309. break; // End of <class>.
  1310. }
  1311. }
  1312. // Sort loaded constants for merging.
  1313. c.constants.sort();
  1314. }
  1315. return OK;
  1316. }
  1317. static void _write_string(Ref<FileAccess> f, int p_tablevel, const String &p_string) {
  1318. if (p_string.is_empty()) {
  1319. return;
  1320. }
  1321. String tab = String("\t").repeat(p_tablevel);
  1322. f->store_string(tab + p_string + "\n");
  1323. }
  1324. static void _write_method_doc(Ref<FileAccess> f, const String &p_name, Vector<DocData::MethodDoc> &p_method_docs) {
  1325. if (!p_method_docs.is_empty()) {
  1326. _write_string(f, 1, "<" + p_name + "s>");
  1327. for (int i = 0; i < p_method_docs.size(); i++) {
  1328. const DocData::MethodDoc &m = p_method_docs[i];
  1329. String additional_attributes;
  1330. if (!m.qualifiers.is_empty()) {
  1331. additional_attributes += " qualifiers=\"" + m.qualifiers.xml_escape(true) + "\"";
  1332. }
  1333. if (m.is_deprecated) {
  1334. additional_attributes += " deprecated=\"" + m.deprecated_message.xml_escape(true) + "\"";
  1335. }
  1336. if (m.is_experimental) {
  1337. additional_attributes += " experimental=\"" + m.experimental_message.xml_escape(true) + "\"";
  1338. }
  1339. if (!m.keywords.is_empty()) {
  1340. additional_attributes += String(" keywords=\"") + m.keywords.xml_escape(true) + "\"";
  1341. }
  1342. _write_string(f, 2, "<" + p_name + " name=\"" + m.name.xml_escape(true) + "\"" + additional_attributes + ">");
  1343. if (!m.return_type.is_empty()) {
  1344. String enum_text;
  1345. if (!m.return_enum.is_empty()) {
  1346. enum_text = " enum=\"" + m.return_enum.xml_escape(true) + "\"";
  1347. if (m.return_is_bitfield) {
  1348. enum_text += " is_bitfield=\"true\"";
  1349. }
  1350. }
  1351. _write_string(f, 3, "<return type=\"" + m.return_type.xml_escape(true) + "\"" + enum_text + " />");
  1352. }
  1353. if (m.errors_returned.size() > 0) {
  1354. for (int j = 0; j < m.errors_returned.size(); j++) {
  1355. _write_string(f, 3, "<returns_error number=\"" + itos(m.errors_returned[j]) + "\"/>");
  1356. }
  1357. }
  1358. for (int j = 0; j < m.arguments.size(); j++) {
  1359. const DocData::ArgumentDoc &a = m.arguments[j];
  1360. String enum_text;
  1361. if (!a.enumeration.is_empty()) {
  1362. enum_text = " enum=\"" + a.enumeration.xml_escape(true) + "\"";
  1363. if (a.is_bitfield) {
  1364. enum_text += " is_bitfield=\"true\"";
  1365. }
  1366. }
  1367. if (!a.default_value.is_empty()) {
  1368. _write_string(f, 3, "<param index=\"" + itos(j) + "\" name=\"" + a.name.xml_escape(true) + "\" type=\"" + a.type.xml_escape(true) + "\"" + enum_text + " default=\"" + a.default_value.xml_escape(true) + "\" />");
  1369. } else {
  1370. _write_string(f, 3, "<param index=\"" + itos(j) + "\" name=\"" + a.name.xml_escape(true) + "\" type=\"" + a.type.xml_escape(true) + "\"" + enum_text + " />");
  1371. }
  1372. }
  1373. _write_string(f, 3, "<description>");
  1374. _write_string(f, 4, _translate_doc_string(m.description).strip_edges().xml_escape());
  1375. _write_string(f, 3, "</description>");
  1376. _write_string(f, 2, "</" + p_name + ">");
  1377. }
  1378. _write_string(f, 1, "</" + p_name + "s>");
  1379. }
  1380. }
  1381. Error DocTools::save_classes(const String &p_default_path, const HashMap<String, String> &p_class_path, bool p_use_relative_schema) {
  1382. for (KeyValue<String, DocData::ClassDoc> &E : class_list) {
  1383. DocData::ClassDoc &c = E.value;
  1384. String save_path;
  1385. if (p_class_path.has(c.name)) {
  1386. save_path = p_class_path[c.name];
  1387. } else {
  1388. save_path = p_default_path;
  1389. }
  1390. Error err;
  1391. String save_file = save_path.path_join(c.name.remove_char('\"').replace("/", "--") + ".xml");
  1392. Ref<FileAccess> f = FileAccess::open(save_file, FileAccess::WRITE, &err);
  1393. ERR_CONTINUE_MSG(err != OK, "Can't write doc file: " + save_file + ".");
  1394. _write_string(f, 0, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
  1395. String header = "<class name=\"" + c.name.xml_escape(true) + "\"";
  1396. if (!c.inherits.is_empty()) {
  1397. header += " inherits=\"" + c.inherits.xml_escape(true) + "\"";
  1398. if (c.is_deprecated) {
  1399. header += " deprecated=\"" + c.deprecated_message.xml_escape(true) + "\"";
  1400. }
  1401. if (c.is_experimental) {
  1402. header += " experimental=\"" + c.experimental_message.xml_escape(true) + "\"";
  1403. }
  1404. }
  1405. if (!c.keywords.is_empty()) {
  1406. header += String(" keywords=\"") + c.keywords.xml_escape(true) + "\"";
  1407. }
  1408. // Reference the XML schema so editors can provide error checking.
  1409. String schema_path;
  1410. if (p_use_relative_schema) {
  1411. // Modules are nested deep, so change the path to reference the same schema everywhere.
  1412. schema_path = save_path.contains("modules/") ? "../../../doc/class.xsd" : "../class.xsd";
  1413. } else {
  1414. schema_path = "https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd";
  1415. }
  1416. header += vformat(
  1417. R"( xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="%s">)",
  1418. schema_path);
  1419. _write_string(f, 0, header);
  1420. _write_string(f, 1, "<brief_description>");
  1421. _write_string(f, 2, _translate_doc_string(c.brief_description).strip_edges().xml_escape());
  1422. _write_string(f, 1, "</brief_description>");
  1423. _write_string(f, 1, "<description>");
  1424. _write_string(f, 2, _translate_doc_string(c.description).strip_edges().xml_escape());
  1425. _write_string(f, 1, "</description>");
  1426. _write_string(f, 1, "<tutorials>");
  1427. for (int i = 0; i < c.tutorials.size(); i++) {
  1428. DocData::TutorialDoc tutorial = c.tutorials.get(i);
  1429. String title_attribute = (!tutorial.title.is_empty()) ? " title=\"" + _translate_doc_string(tutorial.title).xml_escape(true) + "\"" : "";
  1430. _write_string(f, 2, "<link" + title_attribute + ">" + tutorial.link.xml_escape() + "</link>");
  1431. }
  1432. _write_string(f, 1, "</tutorials>");
  1433. _write_method_doc(f, "constructor", c.constructors);
  1434. _write_method_doc(f, "method", c.methods);
  1435. if (!c.properties.is_empty()) {
  1436. _write_string(f, 1, "<members>");
  1437. for (int i = 0; i < c.properties.size(); i++) {
  1438. String additional_attributes;
  1439. if (!c.properties[i].enumeration.is_empty()) {
  1440. additional_attributes += " enum=\"" + c.properties[i].enumeration.xml_escape(true) + "\"";
  1441. if (c.properties[i].is_bitfield) {
  1442. additional_attributes += " is_bitfield=\"true\"";
  1443. }
  1444. }
  1445. if (!c.properties[i].default_value.is_empty()) {
  1446. additional_attributes += " default=\"" + c.properties[i].default_value.xml_escape(true) + "\"";
  1447. }
  1448. if (c.properties[i].is_deprecated) {
  1449. additional_attributes += " deprecated=\"" + c.properties[i].deprecated_message.xml_escape(true) + "\"";
  1450. }
  1451. if (c.properties[i].is_experimental) {
  1452. additional_attributes += " experimental=\"" + c.properties[i].experimental_message.xml_escape(true) + "\"";
  1453. }
  1454. if (!c.properties[i].keywords.is_empty()) {
  1455. additional_attributes += String(" keywords=\"") + c.properties[i].keywords.xml_escape(true) + "\"";
  1456. }
  1457. const DocData::PropertyDoc &p = c.properties[i];
  1458. if (c.properties[i].overridden) {
  1459. _write_string(f, 2, "<member name=\"" + p.name.xml_escape(true) + "\" type=\"" + p.type.xml_escape(true) + "\" setter=\"" + p.setter.xml_escape(true) + "\" getter=\"" + p.getter.xml_escape(true) + "\" overrides=\"" + p.overrides.xml_escape(true) + "\"" + additional_attributes + " />");
  1460. } else {
  1461. _write_string(f, 2, "<member name=\"" + p.name.xml_escape(true) + "\" type=\"" + p.type.xml_escape(true) + "\" setter=\"" + p.setter.xml_escape(true) + "\" getter=\"" + p.getter.xml_escape(true) + "\"" + additional_attributes + ">");
  1462. _write_string(f, 3, _translate_doc_string(p.description).strip_edges().xml_escape());
  1463. _write_string(f, 2, "</member>");
  1464. }
  1465. }
  1466. _write_string(f, 1, "</members>");
  1467. }
  1468. _write_method_doc(f, "signal", c.signals);
  1469. if (!c.constants.is_empty()) {
  1470. _write_string(f, 1, "<constants>");
  1471. for (int i = 0; i < c.constants.size(); i++) {
  1472. const DocData::ConstantDoc &k = c.constants[i];
  1473. String additional_attributes;
  1474. if (c.constants[i].is_deprecated) {
  1475. additional_attributes += " deprecated=\"" + c.constants[i].deprecated_message.xml_escape(true) + "\"";
  1476. }
  1477. if (c.constants[i].is_experimental) {
  1478. additional_attributes += " experimental=\"" + c.constants[i].experimental_message.xml_escape(true) + "\"";
  1479. }
  1480. if (!c.constants[i].keywords.is_empty()) {
  1481. additional_attributes += String(" keywords=\"") + c.constants[i].keywords.xml_escape(true) + "\"";
  1482. }
  1483. if (k.is_value_valid) {
  1484. if (!k.enumeration.is_empty()) {
  1485. if (k.is_bitfield) {
  1486. _write_string(f, 2, "<constant name=\"" + k.name.xml_escape(true) + "\" value=\"" + k.value.xml_escape(true) + "\" enum=\"" + k.enumeration.xml_escape(true) + "\" is_bitfield=\"true\"" + additional_attributes + ">");
  1487. } else {
  1488. _write_string(f, 2, "<constant name=\"" + k.name.xml_escape(true) + "\" value=\"" + k.value.xml_escape(true) + "\" enum=\"" + k.enumeration.xml_escape(true) + "\"" + additional_attributes + ">");
  1489. }
  1490. } else {
  1491. _write_string(f, 2, "<constant name=\"" + k.name.xml_escape(true) + "\" value=\"" + k.value.xml_escape(true) + "\"" + additional_attributes + ">");
  1492. }
  1493. } else {
  1494. if (!k.enumeration.is_empty()) {
  1495. _write_string(f, 2, "<constant name=\"" + k.name.xml_escape(true) + "\" value=\"platform-dependent\" enum=\"" + k.enumeration.xml_escape(true) + "\"" + additional_attributes + ">");
  1496. } else {
  1497. _write_string(f, 2, "<constant name=\"" + k.name.xml_escape(true) + "\" value=\"platform-dependent\"" + additional_attributes + ">");
  1498. }
  1499. }
  1500. _write_string(f, 3, _translate_doc_string(k.description).strip_edges().xml_escape());
  1501. _write_string(f, 2, "</constant>");
  1502. }
  1503. _write_string(f, 1, "</constants>");
  1504. }
  1505. _write_method_doc(f, "annotation", c.annotations);
  1506. if (!c.theme_properties.is_empty()) {
  1507. _write_string(f, 1, "<theme_items>");
  1508. for (int i = 0; i < c.theme_properties.size(); i++) {
  1509. const DocData::ThemeItemDoc &ti = c.theme_properties[i];
  1510. String additional_attributes;
  1511. if (!ti.default_value.is_empty()) {
  1512. additional_attributes += String(" default=\"") + ti.default_value.xml_escape(true) + "\"";
  1513. }
  1514. if (ti.is_deprecated) {
  1515. additional_attributes += " deprecated=\"" + ti.deprecated_message.xml_escape(true) + "\"";
  1516. }
  1517. if (ti.is_experimental) {
  1518. additional_attributes += " experimental=\"" + ti.experimental_message.xml_escape(true) + "\"";
  1519. }
  1520. if (!ti.keywords.is_empty()) {
  1521. additional_attributes += String(" keywords=\"") + ti.keywords.xml_escape(true) + "\"";
  1522. }
  1523. _write_string(f, 2, "<theme_item name=\"" + ti.name.xml_escape(true) + "\" data_type=\"" + ti.data_type.xml_escape(true) + "\" type=\"" + ti.type.xml_escape(true) + "\"" + additional_attributes + ">");
  1524. _write_string(f, 3, _translate_doc_string(ti.description).strip_edges().xml_escape());
  1525. _write_string(f, 2, "</theme_item>");
  1526. }
  1527. _write_string(f, 1, "</theme_items>");
  1528. }
  1529. _write_method_doc(f, "operator", c.operators);
  1530. _write_string(f, 0, "</class>");
  1531. }
  1532. return OK;
  1533. }
  1534. Error DocTools::load_compressed(const uint8_t *p_data, int p_compressed_size, int p_uncompressed_size) {
  1535. Vector<uint8_t> data;
  1536. data.resize(p_uncompressed_size);
  1537. int ret = Compression::decompress(data.ptrw(), p_uncompressed_size, p_data, p_compressed_size, Compression::MODE_DEFLATE);
  1538. ERR_FAIL_COND_V_MSG(ret == -1, ERR_FILE_CORRUPT, "Compressed file is corrupt.");
  1539. class_list.clear();
  1540. Ref<XMLParser> parser = memnew(XMLParser);
  1541. Error err = parser->open_buffer(data);
  1542. if (err) {
  1543. return err;
  1544. }
  1545. _load(parser);
  1546. return OK;
  1547. }
  1548. Error DocTools::load_xml(const uint8_t *p_data, int p_size) {
  1549. Ref<XMLParser> parser = memnew(XMLParser);
  1550. Error err = parser->_open_buffer(p_data, p_size);
  1551. if (err) {
  1552. return err;
  1553. }
  1554. _load(parser);
  1555. return OK;
  1556. }