doc_tools.cpp 54 KB

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