doc_tools.cpp 63 KB

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