doc_tools.cpp 63 KB

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