doc_tools.cpp 64 KB

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