doc_tools.cpp 62 KB

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