doc_tools.cpp 64 KB

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