doc_tools.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  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. LocalVector<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.erase("ProjectSettings");
  353. classes.push_back("ProjectSettings");
  354. }
  355. bool skip_setter_getter_methods = true;
  356. // Populate documentation data for each exposed class.
  357. for (uint32_t classes_idx = 0; classes_idx < classes.size(); classes_idx++) {
  358. const String &name = classes[classes_idx];
  359. if (!ClassDB::is_class_exposed(name)) {
  360. print_verbose(vformat("Class '%s' is not exposed, skipping.", name));
  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. }
  622. }
  623. if (p_flags.has_flag(GENERATE_FLAG_SKIP_BASIC_TYPES)) {
  624. return;
  625. }
  626. // Add a dummy Variant entry.
  627. {
  628. // This allows us to document the concept of Variant even though
  629. // it's not a ClassDB-exposed class.
  630. class_list["Variant"] = DocData::ClassDoc();
  631. class_list["Variant"].name = "Variant";
  632. inheriting[""].insert("Variant");
  633. }
  634. // Add Variant data types.
  635. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  636. if (i == Variant::NIL) {
  637. continue; // Not exposed outside of 'null', should not be in class list.
  638. }
  639. if (i == Variant::OBJECT) {
  640. continue; // Use the core type instead.
  641. }
  642. String cname = Variant::get_type_name(Variant::Type(i));
  643. class_list[cname] = DocData::ClassDoc();
  644. DocData::ClassDoc &c = class_list[cname];
  645. c.name = cname;
  646. inheriting[""].insert(cname);
  647. Callable::CallError cerror;
  648. Variant v;
  649. Variant::construct(Variant::Type(i), v, nullptr, 0, cerror);
  650. List<MethodInfo> method_list;
  651. v.get_method_list(&method_list);
  652. Variant::get_constructor_list(Variant::Type(i), &method_list);
  653. for (int j = 0; j < Variant::OP_AND; j++) { // Showing above 'and' is pretty confusing and there are a lot of variations.
  654. for (int k = 0; k < Variant::VARIANT_MAX; k++) {
  655. // Prevent generating for comparison with null.
  656. if (Variant::Type(k) == Variant::NIL && (Variant::Operator(j) == Variant::OP_EQUAL || Variant::Operator(j) == Variant::OP_NOT_EQUAL)) {
  657. continue;
  658. }
  659. Variant::Type rt = Variant::get_operator_return_type(Variant::Operator(j), Variant::Type(i), Variant::Type(k));
  660. if (rt != Variant::NIL) { // Has operator.
  661. // Skip String % operator as it's registered separately for each Variant arg type,
  662. // we'll add it manually below.
  663. if ((i == Variant::STRING || i == Variant::STRING_NAME) && Variant::Operator(j) == Variant::OP_MODULE) {
  664. continue;
  665. }
  666. MethodInfo mi;
  667. mi.name = "operator " + Variant::get_operator_name(Variant::Operator(j));
  668. mi.return_val.type = rt;
  669. if (k != Variant::NIL) {
  670. PropertyInfo arg;
  671. arg.name = "right";
  672. arg.type = Variant::Type(k);
  673. mi.arguments.push_back(arg);
  674. }
  675. method_list.push_back(mi);
  676. }
  677. }
  678. }
  679. if (i == Variant::STRING || i == Variant::STRING_NAME) {
  680. // We skipped % operator above, and we register it manually once for Variant arg type here.
  681. MethodInfo mi;
  682. mi.name = "operator %";
  683. mi.return_val.type = Variant::STRING;
  684. PropertyInfo arg;
  685. arg.name = "right";
  686. arg.type = Variant::NIL;
  687. arg.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  688. mi.arguments.push_back(arg);
  689. method_list.push_back(mi);
  690. }
  691. if (Variant::is_keyed(Variant::Type(i))) {
  692. MethodInfo mi;
  693. mi.name = "operator []";
  694. mi.return_val.type = Variant::NIL;
  695. mi.return_val.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  696. PropertyInfo arg;
  697. arg.name = "key";
  698. arg.type = Variant::NIL;
  699. arg.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  700. mi.arguments.push_back(arg);
  701. method_list.push_back(mi);
  702. } else if (Variant::has_indexing(Variant::Type(i))) {
  703. MethodInfo mi;
  704. mi.name = "operator []";
  705. mi.return_val.type = Variant::get_indexed_element_type(Variant::Type(i));
  706. mi.return_val.usage = Variant::get_indexed_element_usage(Variant::Type(i));
  707. PropertyInfo arg;
  708. arg.name = "index";
  709. arg.type = Variant::INT;
  710. mi.arguments.push_back(arg);
  711. method_list.push_back(mi);
  712. }
  713. for (const MethodInfo &mi : method_list) {
  714. DocData::MethodDoc method;
  715. method.name = mi.name;
  716. for (int64_t j = 0; j < mi.arguments.size(); ++j) {
  717. const PropertyInfo &arginfo = mi.arguments[j];
  718. DocData::ArgumentDoc ad;
  719. DocData::argument_doc_from_arginfo(ad, arginfo);
  720. ad.name = arginfo.name;
  721. int darg_idx = mi.default_arguments.size() - mi.arguments.size() + j;
  722. if (darg_idx >= 0) {
  723. ad.default_value = DocData::get_default_value_string(mi.default_arguments[darg_idx]);
  724. }
  725. method.arguments.push_back(ad);
  726. }
  727. DocData::return_doc_from_retinfo(method, mi.return_val);
  728. if (mi.flags & METHOD_FLAG_VARARG) {
  729. if (!method.qualifiers.is_empty()) {
  730. method.qualifiers += " ";
  731. }
  732. method.qualifiers += "vararg";
  733. }
  734. if (mi.flags & METHOD_FLAG_CONST) {
  735. if (!method.qualifiers.is_empty()) {
  736. method.qualifiers += " ";
  737. }
  738. method.qualifiers += "const";
  739. }
  740. if (mi.flags & METHOD_FLAG_STATIC) {
  741. if (!method.qualifiers.is_empty()) {
  742. method.qualifiers += " ";
  743. }
  744. method.qualifiers += "static";
  745. }
  746. if (method.name == cname) {
  747. c.constructors.push_back(method);
  748. } else if (method.name.begins_with("operator")) {
  749. c.operators.push_back(method);
  750. } else {
  751. c.methods.push_back(method);
  752. }
  753. }
  754. c.constructors.sort_custom<ConstructorCompare>();
  755. c.operators.sort_custom<OperatorCompare>();
  756. c.methods.sort_custom<MethodCompare>();
  757. List<PropertyInfo> properties;
  758. v.get_property_list(&properties);
  759. for (const PropertyInfo &pi : properties) {
  760. DocData::PropertyDoc property;
  761. property.name = pi.name;
  762. property.type = Variant::get_type_name(pi.type);
  763. property.default_value = DocData::get_default_value_string(v.get(pi.name));
  764. c.properties.push_back(property);
  765. }
  766. c.properties.sort();
  767. List<StringName> enums;
  768. Variant::get_enums_for_type(Variant::Type(i), &enums);
  769. for (const StringName &E : enums) {
  770. List<StringName> enumerations;
  771. Variant::get_enumerations_for_enum(Variant::Type(i), E, &enumerations);
  772. for (const StringName &F : enumerations) {
  773. DocData::ConstantDoc constant;
  774. constant.name = F;
  775. constant.value = itos(Variant::get_enum_value(Variant::Type(i), E, F));
  776. constant.is_value_valid = true;
  777. constant.type = "int";
  778. constant.enumeration = E;
  779. c.constants.push_back(constant);
  780. }
  781. }
  782. List<StringName> constants;
  783. Variant::get_constants_for_type(Variant::Type(i), &constants);
  784. for (const StringName &E : constants) {
  785. DocData::ConstantDoc constant;
  786. constant.name = E;
  787. Variant value = Variant::get_constant_value(Variant::Type(i), E);
  788. constant.value = DocData::get_default_value_string(value);
  789. constant.is_value_valid = true;
  790. constant.type = Variant::get_type_name(value.get_type());
  791. c.constants.push_back(constant);
  792. }
  793. }
  794. // Add global API (servers, engine singletons, global constants) and Variant utility functions.
  795. {
  796. String cname = "@GlobalScope";
  797. class_list[cname] = DocData::ClassDoc();
  798. DocData::ClassDoc &c = class_list[cname];
  799. c.name = cname;
  800. inheriting[""].insert(cname);
  801. // Global constants.
  802. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  803. DocData::ConstantDoc cd;
  804. cd.name = CoreConstants::get_global_constant_name(i);
  805. cd.type = "int";
  806. cd.enumeration = CoreConstants::get_global_constant_enum(i);
  807. cd.is_bitfield = CoreConstants::is_global_constant_bitfield(i);
  808. if (!CoreConstants::get_ignore_value_in_docs(i)) {
  809. cd.value = itos(CoreConstants::get_global_constant_value(i));
  810. cd.is_value_valid = true;
  811. } else {
  812. cd.is_value_valid = false;
  813. }
  814. c.constants.push_back(cd);
  815. }
  816. // Servers/engine singletons.
  817. List<Engine::Singleton> singletons;
  818. Engine::get_singleton()->get_singletons(&singletons);
  819. // FIXME: this is kind of hackish...
  820. for (const Engine::Singleton &s : singletons) {
  821. DocData::PropertyDoc pd;
  822. if (!s.ptr) {
  823. continue;
  824. }
  825. pd.name = s.name;
  826. pd.type = s.ptr->get_class();
  827. while (String(ClassDB::get_parent_class(pd.type)) != "Object") {
  828. pd.type = ClassDB::get_parent_class(pd.type);
  829. }
  830. c.properties.push_back(pd);
  831. }
  832. c.properties.sort();
  833. // Variant utility functions.
  834. List<StringName> utility_functions;
  835. Variant::get_utility_function_list(&utility_functions);
  836. for (const StringName &E : utility_functions) {
  837. DocData::MethodDoc md;
  838. md.name = E;
  839. // Utility function's return type.
  840. if (Variant::has_utility_function_return_value(E)) {
  841. PropertyInfo pi;
  842. pi.type = Variant::get_utility_function_return_type(E);
  843. if (pi.type == Variant::NIL) {
  844. pi.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  845. }
  846. DocData::ArgumentDoc ad;
  847. DocData::argument_doc_from_arginfo(ad, pi);
  848. md.return_type = ad.type;
  849. } else {
  850. md.return_type = "void";
  851. }
  852. // Utility function's arguments.
  853. if (Variant::is_utility_function_vararg(E)) {
  854. md.qualifiers = "vararg";
  855. } else {
  856. for (int i = 0; i < Variant::get_utility_function_argument_count(E); i++) {
  857. PropertyInfo pi;
  858. pi.type = Variant::get_utility_function_argument_type(E, i);
  859. pi.name = Variant::get_utility_function_argument_name(E, i);
  860. if (pi.type == Variant::NIL) {
  861. pi.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  862. }
  863. DocData::ArgumentDoc ad;
  864. DocData::argument_doc_from_arginfo(ad, pi);
  865. md.arguments.push_back(ad);
  866. }
  867. }
  868. c.methods.push_back(md);
  869. }
  870. c.methods.sort_custom<MethodCompare>();
  871. }
  872. // Add scripting language built-ins.
  873. {
  874. // We only add a doc entry for languages which actually define any built-in
  875. // methods, constants, or annotations.
  876. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  877. ScriptLanguage *lang = ScriptServer::get_language(i);
  878. String cname = "@" + lang->get_name();
  879. DocData::ClassDoc c;
  880. c.name = cname;
  881. inheriting[""].insert(cname);
  882. // Get functions.
  883. List<MethodInfo> minfo;
  884. lang->get_public_functions(&minfo);
  885. for (const MethodInfo &mi : minfo) {
  886. DocData::MethodDoc md;
  887. md.name = mi.name;
  888. if (mi.flags & METHOD_FLAG_VARARG) {
  889. if (!md.qualifiers.is_empty()) {
  890. md.qualifiers += " ";
  891. }
  892. md.qualifiers += "vararg";
  893. }
  894. DocData::return_doc_from_retinfo(md, mi.return_val);
  895. for (int64_t j = 0; j < mi.arguments.size(); ++j) {
  896. DocData::ArgumentDoc ad;
  897. DocData::argument_doc_from_arginfo(ad, mi.arguments[j]);
  898. int darg_idx = j - (mi.arguments.size() - mi.default_arguments.size());
  899. if (darg_idx >= 0) {
  900. ad.default_value = DocData::get_default_value_string(mi.default_arguments[darg_idx]);
  901. }
  902. md.arguments.push_back(ad);
  903. }
  904. c.methods.push_back(md);
  905. }
  906. // Get constants.
  907. List<Pair<String, Variant>> cinfo;
  908. lang->get_public_constants(&cinfo);
  909. for (const Pair<String, Variant> &E : cinfo) {
  910. DocData::ConstantDoc cd;
  911. cd.name = E.first;
  912. cd.value = E.second;
  913. cd.is_value_valid = true;
  914. cd.type = Variant::get_type_name(E.second.get_type());
  915. c.constants.push_back(cd);
  916. }
  917. // Get annotations.
  918. List<MethodInfo> ainfo;
  919. lang->get_public_annotations(&ainfo);
  920. for (const MethodInfo &ai : ainfo) {
  921. DocData::MethodDoc atd;
  922. atd.name = ai.name;
  923. if (ai.flags & METHOD_FLAG_VARARG) {
  924. if (!atd.qualifiers.is_empty()) {
  925. atd.qualifiers += " ";
  926. }
  927. atd.qualifiers += "vararg";
  928. }
  929. DocData::return_doc_from_retinfo(atd, ai.return_val);
  930. for (int64_t j = 0; j < ai.arguments.size(); ++j) {
  931. DocData::ArgumentDoc ad;
  932. DocData::argument_doc_from_arginfo(ad, ai.arguments[j]);
  933. int64_t darg_idx = j - (ai.arguments.size() - ai.default_arguments.size());
  934. if (darg_idx >= 0) {
  935. ad.default_value = DocData::get_default_value_string(ai.default_arguments[darg_idx]);
  936. }
  937. atd.arguments.push_back(ad);
  938. }
  939. c.annotations.push_back(atd);
  940. }
  941. // Skip adding the lang if it doesn't expose anything (e.g. C#).
  942. if (c.methods.is_empty() && c.constants.is_empty() && c.annotations.is_empty()) {
  943. continue;
  944. }
  945. c.methods.sort_custom<MethodCompare>();
  946. c.annotations.sort_custom<MethodCompare>();
  947. class_list[cname] = c;
  948. }
  949. }
  950. }
  951. static Error _parse_methods(Ref<XMLParser> &parser, Vector<DocData::MethodDoc> &methods) {
  952. String section = parser->get_node_name();
  953. String element = section.substr(0, section.length() - 1);
  954. while (parser->read() == OK) {
  955. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  956. if (parser->get_node_name() == element) {
  957. DocData::MethodDoc method;
  958. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  959. method.name = parser->get_named_attribute_value("name");
  960. if (parser->has_attribute("qualifiers")) {
  961. method.qualifiers = parser->get_named_attribute_value("qualifiers");
  962. }
  963. #ifndef DISABLE_DEPRECATED
  964. if (parser->has_attribute("is_deprecated")) {
  965. method.is_deprecated = parser->get_named_attribute_value("is_deprecated").to_lower() == "true";
  966. }
  967. if (parser->has_attribute("is_experimental")) {
  968. method.is_experimental = parser->get_named_attribute_value("is_experimental").to_lower() == "true";
  969. }
  970. #endif
  971. if (parser->has_attribute("deprecated")) {
  972. method.is_deprecated = true;
  973. method.deprecated_message = parser->get_named_attribute_value("deprecated");
  974. }
  975. if (parser->has_attribute("experimental")) {
  976. method.is_experimental = true;
  977. method.experimental_message = parser->get_named_attribute_value("experimental");
  978. }
  979. if (parser->has_attribute("keywords")) {
  980. method.keywords = parser->get_named_attribute_value("keywords");
  981. }
  982. while (parser->read() == OK) {
  983. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  984. String name = parser->get_node_name();
  985. if (name == "return") {
  986. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  987. method.return_type = parser->get_named_attribute_value("type");
  988. if (parser->has_attribute("enum")) {
  989. method.return_enum = parser->get_named_attribute_value("enum");
  990. if (parser->has_attribute("is_bitfield")) {
  991. method.return_is_bitfield = parser->get_named_attribute_value("is_bitfield").to_lower() == "true";
  992. }
  993. }
  994. } else if (name == "returns_error") {
  995. ERR_FAIL_COND_V(!parser->has_attribute("number"), ERR_FILE_CORRUPT);
  996. method.errors_returned.push_back(parser->get_named_attribute_value("number").to_int());
  997. } else if (name == "param") {
  998. DocData::ArgumentDoc argument;
  999. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  1000. argument.name = parser->get_named_attribute_value("name");
  1001. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  1002. argument.type = parser->get_named_attribute_value("type");
  1003. if (parser->has_attribute("enum")) {
  1004. argument.enumeration = parser->get_named_attribute_value("enum");
  1005. if (parser->has_attribute("is_bitfield")) {
  1006. argument.is_bitfield = parser->get_named_attribute_value("is_bitfield").to_lower() == "true";
  1007. }
  1008. }
  1009. method.arguments.push_back(argument);
  1010. } else if (name == "description") {
  1011. parser->read();
  1012. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1013. method.description = parser->get_node_data();
  1014. }
  1015. }
  1016. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == element) {
  1017. break;
  1018. }
  1019. }
  1020. methods.push_back(method);
  1021. } else {
  1022. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + parser->get_node_name() + ", expected " + element + ".");
  1023. }
  1024. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == section) {
  1025. break;
  1026. }
  1027. }
  1028. return OK;
  1029. }
  1030. Error DocTools::load_classes(const String &p_dir) {
  1031. Error err;
  1032. Ref<DirAccess> da = DirAccess::open(p_dir, &err);
  1033. if (da.is_null()) {
  1034. return err;
  1035. }
  1036. da->list_dir_begin();
  1037. String path;
  1038. path = da->get_next();
  1039. while (!path.is_empty()) {
  1040. if (!da->current_is_dir() && path.ends_with("xml")) {
  1041. Ref<XMLParser> parser = memnew(XMLParser);
  1042. Error err2 = parser->open(p_dir.path_join(path));
  1043. if (err2) {
  1044. return err2;
  1045. }
  1046. _load(parser);
  1047. }
  1048. path = da->get_next();
  1049. }
  1050. da->list_dir_end();
  1051. return OK;
  1052. }
  1053. Error DocTools::erase_classes(const String &p_dir) {
  1054. Error err;
  1055. Ref<DirAccess> da = DirAccess::open(p_dir, &err);
  1056. if (da.is_null()) {
  1057. return err;
  1058. }
  1059. List<String> to_erase;
  1060. da->list_dir_begin();
  1061. String path;
  1062. path = da->get_next();
  1063. while (!path.is_empty()) {
  1064. if (!da->current_is_dir() && path.ends_with("xml")) {
  1065. to_erase.push_back(path);
  1066. }
  1067. path = da->get_next();
  1068. }
  1069. da->list_dir_end();
  1070. while (to_erase.size()) {
  1071. da->remove(to_erase.front()->get());
  1072. to_erase.pop_front();
  1073. }
  1074. return OK;
  1075. }
  1076. Error DocTools::_load(Ref<XMLParser> parser) {
  1077. Error err = OK;
  1078. while ((err = parser->read()) == OK) {
  1079. if (parser->get_node_type() == XMLParser::NODE_ELEMENT && parser->get_node_name() == "?xml") {
  1080. parser->skip_section();
  1081. }
  1082. if (parser->get_node_type() != XMLParser::NODE_ELEMENT) {
  1083. continue; //no idea what this may be, but skipping anyway
  1084. }
  1085. ERR_FAIL_COND_V(parser->get_node_name() != "class", ERR_FILE_CORRUPT);
  1086. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  1087. String name = parser->get_named_attribute_value("name");
  1088. class_list[name] = DocData::ClassDoc();
  1089. DocData::ClassDoc &c = class_list[name];
  1090. c.name = name;
  1091. if (parser->has_attribute("inherits")) {
  1092. c.inherits = parser->get_named_attribute_value("inherits");
  1093. }
  1094. inheriting[c.inherits].insert(name);
  1095. #ifndef DISABLE_DEPRECATED
  1096. if (parser->has_attribute("is_deprecated")) {
  1097. c.is_deprecated = parser->get_named_attribute_value("is_deprecated").to_lower() == "true";
  1098. }
  1099. if (parser->has_attribute("is_experimental")) {
  1100. c.is_experimental = parser->get_named_attribute_value("is_experimental").to_lower() == "true";
  1101. }
  1102. #endif
  1103. if (parser->has_attribute("deprecated")) {
  1104. c.is_deprecated = true;
  1105. c.deprecated_message = parser->get_named_attribute_value("deprecated");
  1106. }
  1107. if (parser->has_attribute("experimental")) {
  1108. c.is_experimental = true;
  1109. c.experimental_message = parser->get_named_attribute_value("experimental");
  1110. }
  1111. if (parser->has_attribute("keywords")) {
  1112. c.keywords = parser->get_named_attribute_value("keywords");
  1113. }
  1114. while (parser->read() == OK) {
  1115. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1116. String name2 = parser->get_node_name();
  1117. if (name2 == "brief_description") {
  1118. parser->read();
  1119. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1120. c.brief_description = parser->get_node_data();
  1121. }
  1122. } else if (name2 == "description") {
  1123. parser->read();
  1124. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1125. c.description = parser->get_node_data();
  1126. }
  1127. } else if (name2 == "tutorials") {
  1128. while (parser->read() == OK) {
  1129. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1130. String name3 = parser->get_node_name();
  1131. if (name3 == "link") {
  1132. DocData::TutorialDoc tutorial;
  1133. if (parser->has_attribute("title")) {
  1134. tutorial.title = parser->get_named_attribute_value("title");
  1135. }
  1136. parser->read();
  1137. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1138. tutorial.link = parser->get_node_data().strip_edges();
  1139. c.tutorials.push_back(tutorial);
  1140. }
  1141. } else {
  1142. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1143. }
  1144. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "tutorials") {
  1145. break; // End of <tutorials>.
  1146. }
  1147. }
  1148. } else if (name2 == "constructors") {
  1149. Error err2 = _parse_methods(parser, c.constructors);
  1150. ERR_FAIL_COND_V(err2, err2);
  1151. } else if (name2 == "methods") {
  1152. Error err2 = _parse_methods(parser, c.methods);
  1153. ERR_FAIL_COND_V(err2, err2);
  1154. } else if (name2 == "operators") {
  1155. Error err2 = _parse_methods(parser, c.operators);
  1156. ERR_FAIL_COND_V(err2, err2);
  1157. } else if (name2 == "signals") {
  1158. Error err2 = _parse_methods(parser, c.signals);
  1159. ERR_FAIL_COND_V(err2, err2);
  1160. } else if (name2 == "annotations") {
  1161. Error err2 = _parse_methods(parser, c.annotations);
  1162. ERR_FAIL_COND_V(err2, err2);
  1163. } else if (name2 == "members") {
  1164. while (parser->read() == OK) {
  1165. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1166. String name3 = parser->get_node_name();
  1167. if (name3 == "member") {
  1168. DocData::PropertyDoc prop2;
  1169. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  1170. prop2.name = parser->get_named_attribute_value("name");
  1171. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  1172. prop2.type = parser->get_named_attribute_value("type");
  1173. if (parser->has_attribute("setter")) {
  1174. prop2.setter = parser->get_named_attribute_value("setter");
  1175. }
  1176. if (parser->has_attribute("getter")) {
  1177. prop2.getter = parser->get_named_attribute_value("getter");
  1178. }
  1179. if (parser->has_attribute("enum")) {
  1180. prop2.enumeration = parser->get_named_attribute_value("enum");
  1181. if (parser->has_attribute("is_bitfield")) {
  1182. prop2.is_bitfield = parser->get_named_attribute_value("is_bitfield").to_lower() == "true";
  1183. }
  1184. }
  1185. #ifndef DISABLE_DEPRECATED
  1186. if (parser->has_attribute("is_deprecated")) {
  1187. prop2.is_deprecated = parser->get_named_attribute_value("is_deprecated").to_lower() == "true";
  1188. }
  1189. if (parser->has_attribute("is_experimental")) {
  1190. prop2.is_experimental = parser->get_named_attribute_value("is_experimental").to_lower() == "true";
  1191. }
  1192. #endif
  1193. if (parser->has_attribute("deprecated")) {
  1194. prop2.is_deprecated = true;
  1195. prop2.deprecated_message = parser->get_named_attribute_value("deprecated");
  1196. }
  1197. if (parser->has_attribute("experimental")) {
  1198. prop2.is_experimental = true;
  1199. prop2.experimental_message = parser->get_named_attribute_value("experimental");
  1200. }
  1201. if (parser->has_attribute("keywords")) {
  1202. prop2.keywords = parser->get_named_attribute_value("keywords");
  1203. }
  1204. if (!parser->is_empty()) {
  1205. parser->read();
  1206. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1207. prop2.description = parser->get_node_data();
  1208. }
  1209. }
  1210. c.properties.push_back(prop2);
  1211. } else {
  1212. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1213. }
  1214. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "members") {
  1215. break; // End of <members>.
  1216. }
  1217. }
  1218. } else if (name2 == "theme_items") {
  1219. while (parser->read() == OK) {
  1220. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1221. String name3 = parser->get_node_name();
  1222. if (name3 == "theme_item") {
  1223. DocData::ThemeItemDoc prop2;
  1224. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  1225. prop2.name = parser->get_named_attribute_value("name");
  1226. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  1227. prop2.type = parser->get_named_attribute_value("type");
  1228. ERR_FAIL_COND_V(!parser->has_attribute("data_type"), ERR_FILE_CORRUPT);
  1229. prop2.data_type = parser->get_named_attribute_value("data_type");
  1230. if (parser->has_attribute("deprecated")) {
  1231. prop2.is_deprecated = true;
  1232. prop2.deprecated_message = parser->get_named_attribute_value("deprecated");
  1233. }
  1234. if (parser->has_attribute("experimental")) {
  1235. prop2.is_experimental = true;
  1236. prop2.experimental_message = parser->get_named_attribute_value("experimental");
  1237. }
  1238. if (parser->has_attribute("keywords")) {
  1239. prop2.keywords = parser->get_named_attribute_value("keywords");
  1240. }
  1241. if (!parser->is_empty()) {
  1242. parser->read();
  1243. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1244. prop2.description = parser->get_node_data();
  1245. }
  1246. }
  1247. c.theme_properties.push_back(prop2);
  1248. } else {
  1249. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1250. }
  1251. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "theme_items") {
  1252. break; // End of <theme_items>.
  1253. }
  1254. }
  1255. } else if (name2 == "constants") {
  1256. while (parser->read() == OK) {
  1257. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1258. String name3 = parser->get_node_name();
  1259. if (name3 == "constant") {
  1260. DocData::ConstantDoc constant2;
  1261. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  1262. constant2.name = parser->get_named_attribute_value("name");
  1263. ERR_FAIL_COND_V(!parser->has_attribute("value"), ERR_FILE_CORRUPT);
  1264. constant2.value = parser->get_named_attribute_value("value");
  1265. constant2.is_value_valid = true;
  1266. if (parser->has_attribute("enum")) {
  1267. constant2.enumeration = parser->get_named_attribute_value("enum");
  1268. if (parser->has_attribute("is_bitfield")) {
  1269. constant2.is_bitfield = parser->get_named_attribute_value("is_bitfield").to_lower() == "true";
  1270. }
  1271. }
  1272. #ifndef DISABLE_DEPRECATED
  1273. if (parser->has_attribute("is_deprecated")) {
  1274. constant2.is_deprecated = parser->get_named_attribute_value("is_deprecated").to_lower() == "true";
  1275. }
  1276. if (parser->has_attribute("is_experimental")) {
  1277. constant2.is_experimental = parser->get_named_attribute_value("is_experimental").to_lower() == "true";
  1278. }
  1279. #endif
  1280. if (parser->has_attribute("deprecated")) {
  1281. constant2.is_deprecated = true;
  1282. constant2.deprecated_message = parser->get_named_attribute_value("deprecated");
  1283. }
  1284. if (parser->has_attribute("experimental")) {
  1285. constant2.is_experimental = true;
  1286. constant2.experimental_message = parser->get_named_attribute_value("experimental");
  1287. }
  1288. if (parser->has_attribute("keywords")) {
  1289. constant2.keywords = parser->get_named_attribute_value("keywords");
  1290. }
  1291. if (!parser->is_empty()) {
  1292. parser->read();
  1293. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1294. constant2.description = parser->get_node_data();
  1295. }
  1296. }
  1297. c.constants.push_back(constant2);
  1298. } else {
  1299. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1300. }
  1301. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "constants") {
  1302. break; // End of <constants>.
  1303. }
  1304. }
  1305. } else {
  1306. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name2 + ".");
  1307. }
  1308. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "class") {
  1309. break; // End of <class>.
  1310. }
  1311. }
  1312. // Sort loaded constants for merging.
  1313. c.constants.sort();
  1314. }
  1315. return OK;
  1316. }
  1317. static void _write_string(Ref<FileAccess> f, int p_tablevel, const String &p_string) {
  1318. if (p_string.is_empty()) {
  1319. return;
  1320. }
  1321. String tab = String("\t").repeat(p_tablevel);
  1322. f->store_string(tab + p_string + "\n");
  1323. }
  1324. static void _write_method_doc(Ref<FileAccess> f, const String &p_name, Vector<DocData::MethodDoc> &p_method_docs) {
  1325. if (!p_method_docs.is_empty()) {
  1326. _write_string(f, 1, "<" + p_name + "s>");
  1327. for (int i = 0; i < p_method_docs.size(); i++) {
  1328. const DocData::MethodDoc &m = p_method_docs[i];
  1329. String additional_attributes;
  1330. if (!m.qualifiers.is_empty()) {
  1331. additional_attributes += " qualifiers=\"" + m.qualifiers.xml_escape(true) + "\"";
  1332. }
  1333. if (m.is_deprecated) {
  1334. additional_attributes += " deprecated=\"" + m.deprecated_message.xml_escape(true) + "\"";
  1335. }
  1336. if (m.is_experimental) {
  1337. additional_attributes += " experimental=\"" + m.experimental_message.xml_escape(true) + "\"";
  1338. }
  1339. if (!m.keywords.is_empty()) {
  1340. additional_attributes += String(" keywords=\"") + m.keywords.xml_escape(true) + "\"";
  1341. }
  1342. _write_string(f, 2, "<" + p_name + " name=\"" + m.name.xml_escape(true) + "\"" + additional_attributes + ">");
  1343. if (!m.return_type.is_empty()) {
  1344. String enum_text;
  1345. if (!m.return_enum.is_empty()) {
  1346. enum_text = " enum=\"" + m.return_enum.xml_escape(true) + "\"";
  1347. if (m.return_is_bitfield) {
  1348. enum_text += " is_bitfield=\"true\"";
  1349. }
  1350. }
  1351. _write_string(f, 3, "<return type=\"" + m.return_type.xml_escape(true) + "\"" + enum_text + " />");
  1352. }
  1353. if (m.errors_returned.size() > 0) {
  1354. for (int j = 0; j < m.errors_returned.size(); j++) {
  1355. _write_string(f, 3, "<returns_error number=\"" + itos(m.errors_returned[j]) + "\"/>");
  1356. }
  1357. }
  1358. for (int j = 0; j < m.arguments.size(); j++) {
  1359. const DocData::ArgumentDoc &a = m.arguments[j];
  1360. String enum_text;
  1361. if (!a.enumeration.is_empty()) {
  1362. enum_text = " enum=\"" + a.enumeration.xml_escape(true) + "\"";
  1363. if (a.is_bitfield) {
  1364. enum_text += " is_bitfield=\"true\"";
  1365. }
  1366. }
  1367. if (!a.default_value.is_empty()) {
  1368. _write_string(f, 3, "<param index=\"" + itos(j) + "\" name=\"" + a.name.xml_escape(true) + "\" type=\"" + a.type.xml_escape(true) + "\"" + enum_text + " default=\"" + a.default_value.xml_escape(true) + "\" />");
  1369. } else {
  1370. _write_string(f, 3, "<param index=\"" + itos(j) + "\" name=\"" + a.name.xml_escape(true) + "\" type=\"" + a.type.xml_escape(true) + "\"" + enum_text + " />");
  1371. }
  1372. }
  1373. _write_string(f, 3, "<description>");
  1374. _write_string(f, 4, _translate_doc_string(m.description).strip_edges().xml_escape());
  1375. _write_string(f, 3, "</description>");
  1376. _write_string(f, 2, "</" + p_name + ">");
  1377. }
  1378. _write_string(f, 1, "</" + p_name + "s>");
  1379. }
  1380. }
  1381. Error DocTools::save_classes(const String &p_default_path, const HashMap<String, String> &p_class_path, bool p_use_relative_schema) {
  1382. for (KeyValue<String, DocData::ClassDoc> &E : class_list) {
  1383. DocData::ClassDoc &c = E.value;
  1384. String save_path;
  1385. if (p_class_path.has(c.name)) {
  1386. save_path = p_class_path[c.name];
  1387. } else {
  1388. save_path = p_default_path;
  1389. }
  1390. Error err;
  1391. String save_file = save_path.path_join(c.name.remove_char('\"').replace("/", "--") + ".xml");
  1392. Ref<FileAccess> f = FileAccess::open(save_file, FileAccess::WRITE, &err);
  1393. ERR_CONTINUE_MSG(err != OK, "Can't write doc file: " + save_file + ".");
  1394. _write_string(f, 0, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
  1395. String header = "<class name=\"" + c.name.xml_escape(true) + "\"";
  1396. if (!c.inherits.is_empty()) {
  1397. header += " inherits=\"" + c.inherits.xml_escape(true) + "\"";
  1398. if (c.is_deprecated) {
  1399. header += " deprecated=\"" + c.deprecated_message.xml_escape(true) + "\"";
  1400. }
  1401. if (c.is_experimental) {
  1402. header += " experimental=\"" + c.experimental_message.xml_escape(true) + "\"";
  1403. }
  1404. }
  1405. if (!c.keywords.is_empty()) {
  1406. header += String(" keywords=\"") + c.keywords.xml_escape(true) + "\"";
  1407. }
  1408. // Reference the XML schema so editors can provide error checking.
  1409. String schema_path;
  1410. if (p_use_relative_schema) {
  1411. // Modules are nested deep, so change the path to reference the same schema everywhere.
  1412. schema_path = save_path.contains("modules/") ? "../../../doc/class.xsd" : "../class.xsd";
  1413. } else {
  1414. schema_path = "https://raw.githubusercontent.com/godotengine/godot/master/doc/class.xsd";
  1415. }
  1416. header += vformat(
  1417. R"( xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="%s">)",
  1418. schema_path);
  1419. _write_string(f, 0, header);
  1420. _write_string(f, 1, "<brief_description>");
  1421. _write_string(f, 2, _translate_doc_string(c.brief_description).strip_edges().xml_escape());
  1422. _write_string(f, 1, "</brief_description>");
  1423. _write_string(f, 1, "<description>");
  1424. _write_string(f, 2, _translate_doc_string(c.description).strip_edges().xml_escape());
  1425. _write_string(f, 1, "</description>");
  1426. _write_string(f, 1, "<tutorials>");
  1427. for (int i = 0; i < c.tutorials.size(); i++) {
  1428. DocData::TutorialDoc tutorial = c.tutorials.get(i);
  1429. String title_attribute = (!tutorial.title.is_empty()) ? " title=\"" + _translate_doc_string(tutorial.title).xml_escape(true) + "\"" : "";
  1430. _write_string(f, 2, "<link" + title_attribute + ">" + tutorial.link.xml_escape() + "</link>");
  1431. }
  1432. _write_string(f, 1, "</tutorials>");
  1433. _write_method_doc(f, "constructor", c.constructors);
  1434. _write_method_doc(f, "method", c.methods);
  1435. if (!c.properties.is_empty()) {
  1436. _write_string(f, 1, "<members>");
  1437. for (int i = 0; i < c.properties.size(); i++) {
  1438. String additional_attributes;
  1439. if (!c.properties[i].enumeration.is_empty()) {
  1440. additional_attributes += " enum=\"" + c.properties[i].enumeration.xml_escape(true) + "\"";
  1441. if (c.properties[i].is_bitfield) {
  1442. additional_attributes += " is_bitfield=\"true\"";
  1443. }
  1444. }
  1445. if (!c.properties[i].default_value.is_empty()) {
  1446. additional_attributes += " default=\"" + c.properties[i].default_value.xml_escape(true) + "\"";
  1447. }
  1448. if (c.properties[i].is_deprecated) {
  1449. additional_attributes += " deprecated=\"" + c.properties[i].deprecated_message.xml_escape(true) + "\"";
  1450. }
  1451. if (c.properties[i].is_experimental) {
  1452. additional_attributes += " experimental=\"" + c.properties[i].experimental_message.xml_escape(true) + "\"";
  1453. }
  1454. if (!c.properties[i].keywords.is_empty()) {
  1455. additional_attributes += String(" keywords=\"") + c.properties[i].keywords.xml_escape(true) + "\"";
  1456. }
  1457. const DocData::PropertyDoc &p = c.properties[i];
  1458. if (c.properties[i].overridden) {
  1459. _write_string(f, 2, "<member name=\"" + p.name.xml_escape(true) + "\" type=\"" + p.type.xml_escape(true) + "\" setter=\"" + p.setter.xml_escape(true) + "\" getter=\"" + p.getter.xml_escape(true) + "\" overrides=\"" + p.overrides.xml_escape(true) + "\"" + additional_attributes + " />");
  1460. } else {
  1461. _write_string(f, 2, "<member name=\"" + p.name.xml_escape(true) + "\" type=\"" + p.type.xml_escape(true) + "\" setter=\"" + p.setter.xml_escape(true) + "\" getter=\"" + p.getter.xml_escape(true) + "\"" + additional_attributes + ">");
  1462. _write_string(f, 3, _translate_doc_string(p.description).strip_edges().xml_escape());
  1463. _write_string(f, 2, "</member>");
  1464. }
  1465. }
  1466. _write_string(f, 1, "</members>");
  1467. }
  1468. _write_method_doc(f, "signal", c.signals);
  1469. if (!c.constants.is_empty()) {
  1470. _write_string(f, 1, "<constants>");
  1471. for (int i = 0; i < c.constants.size(); i++) {
  1472. const DocData::ConstantDoc &k = c.constants[i];
  1473. String additional_attributes;
  1474. if (c.constants[i].is_deprecated) {
  1475. additional_attributes += " deprecated=\"" + c.constants[i].deprecated_message.xml_escape(true) + "\"";
  1476. }
  1477. if (c.constants[i].is_experimental) {
  1478. additional_attributes += " experimental=\"" + c.constants[i].experimental_message.xml_escape(true) + "\"";
  1479. }
  1480. if (!c.constants[i].keywords.is_empty()) {
  1481. additional_attributes += String(" keywords=\"") + c.constants[i].keywords.xml_escape(true) + "\"";
  1482. }
  1483. if (k.is_value_valid) {
  1484. if (!k.enumeration.is_empty()) {
  1485. if (k.is_bitfield) {
  1486. _write_string(f, 2, "<constant name=\"" + k.name.xml_escape(true) + "\" value=\"" + k.value.xml_escape(true) + "\" enum=\"" + k.enumeration.xml_escape(true) + "\" is_bitfield=\"true\"" + additional_attributes + ">");
  1487. } else {
  1488. _write_string(f, 2, "<constant name=\"" + k.name.xml_escape(true) + "\" value=\"" + k.value.xml_escape(true) + "\" enum=\"" + k.enumeration.xml_escape(true) + "\"" + additional_attributes + ">");
  1489. }
  1490. } else {
  1491. _write_string(f, 2, "<constant name=\"" + k.name.xml_escape(true) + "\" value=\"" + k.value.xml_escape(true) + "\"" + additional_attributes + ">");
  1492. }
  1493. } else {
  1494. if (!k.enumeration.is_empty()) {
  1495. _write_string(f, 2, "<constant name=\"" + k.name.xml_escape(true) + "\" value=\"platform-dependent\" enum=\"" + k.enumeration.xml_escape(true) + "\"" + additional_attributes + ">");
  1496. } else {
  1497. _write_string(f, 2, "<constant name=\"" + k.name.xml_escape(true) + "\" value=\"platform-dependent\"" + additional_attributes + ">");
  1498. }
  1499. }
  1500. _write_string(f, 3, _translate_doc_string(k.description).strip_edges().xml_escape());
  1501. _write_string(f, 2, "</constant>");
  1502. }
  1503. _write_string(f, 1, "</constants>");
  1504. }
  1505. _write_method_doc(f, "annotation", c.annotations);
  1506. if (!c.theme_properties.is_empty()) {
  1507. _write_string(f, 1, "<theme_items>");
  1508. for (int i = 0; i < c.theme_properties.size(); i++) {
  1509. const DocData::ThemeItemDoc &ti = c.theme_properties[i];
  1510. String additional_attributes;
  1511. if (!ti.default_value.is_empty()) {
  1512. additional_attributes += String(" default=\"") + ti.default_value.xml_escape(true) + "\"";
  1513. }
  1514. if (ti.is_deprecated) {
  1515. additional_attributes += " deprecated=\"" + ti.deprecated_message.xml_escape(true) + "\"";
  1516. }
  1517. if (ti.is_experimental) {
  1518. additional_attributes += " experimental=\"" + ti.experimental_message.xml_escape(true) + "\"";
  1519. }
  1520. if (!ti.keywords.is_empty()) {
  1521. additional_attributes += String(" keywords=\"") + ti.keywords.xml_escape(true) + "\"";
  1522. }
  1523. _write_string(f, 2, "<theme_item name=\"" + ti.name.xml_escape(true) + "\" data_type=\"" + ti.data_type.xml_escape(true) + "\" type=\"" + ti.type.xml_escape(true) + "\"" + additional_attributes + ">");
  1524. _write_string(f, 3, _translate_doc_string(ti.description).strip_edges().xml_escape());
  1525. _write_string(f, 2, "</theme_item>");
  1526. }
  1527. _write_string(f, 1, "</theme_items>");
  1528. }
  1529. _write_method_doc(f, "operator", c.operators);
  1530. _write_string(f, 0, "</class>");
  1531. }
  1532. return OK;
  1533. }
  1534. Error DocTools::load_compressed(const uint8_t *p_data, int64_t p_compressed_size, int64_t p_uncompressed_size) {
  1535. Vector<uint8_t> data;
  1536. data.resize(p_uncompressed_size);
  1537. const int64_t ret = Compression::decompress(data.ptrw(), p_uncompressed_size, p_data, p_compressed_size, Compression::MODE_DEFLATE);
  1538. ERR_FAIL_COND_V_MSG(ret == -1, ERR_FILE_CORRUPT, "Compressed file is corrupt.");
  1539. class_list.clear();
  1540. Ref<XMLParser> parser = memnew(XMLParser);
  1541. Error err = parser->open_buffer(data);
  1542. if (err) {
  1543. return err;
  1544. }
  1545. _load(parser);
  1546. return OK;
  1547. }
  1548. Error DocTools::load_xml(const uint8_t *p_data, int64_t p_size) {
  1549. Ref<XMLParser> parser = memnew(XMLParser);
  1550. Error err = parser->_open_buffer(p_data, p_size);
  1551. if (err) {
  1552. return err;
  1553. }
  1554. _load(parser);
  1555. return OK;
  1556. }