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