doc_tools.cpp 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  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. properties.sort();
  360. own_properties.sort();
  361. List<PropertyInfo>::Element *EO = own_properties.front();
  362. for (const PropertyInfo &E : properties) {
  363. bool inherited = true;
  364. if (EO && EO->get() == E) {
  365. inherited = false;
  366. EO = EO->next();
  367. }
  368. 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)) {
  369. continue;
  370. }
  371. DocData::PropertyDoc prop;
  372. prop.name = E.name;
  373. prop.overridden = inherited;
  374. if (inherited) {
  375. String parent = ClassDB::get_parent_class(c.name);
  376. while (!ClassDB::has_property(parent, prop.name, true)) {
  377. parent = ClassDB::get_parent_class(parent);
  378. }
  379. prop.overrides = parent;
  380. }
  381. bool default_value_valid = false;
  382. Variant default_value;
  383. if (name == "EditorSettings") {
  384. if (E.name == "resource_local_to_scene" || E.name == "resource_name" || E.name == "resource_path" || E.name == "script") {
  385. // Don't include spurious properties in the generated EditorSettings class reference.
  386. continue;
  387. }
  388. }
  389. if (name.begins_with("EditorExportPlatform")) {
  390. if (E.name == "script") {
  391. continue;
  392. }
  393. }
  394. if (name == "ProjectSettings") {
  395. // Special case for project settings, so that settings are not taken from the current project's settings
  396. if (E.name == "script" || !ProjectSettings::get_singleton()->is_builtin_setting(E.name)) {
  397. continue;
  398. }
  399. if (E.usage & PROPERTY_USAGE_EDITOR) {
  400. if (!ProjectSettings::get_singleton()->get_ignore_value_in_docs(E.name)) {
  401. default_value = ProjectSettings::get_singleton()->property_get_revert(E.name);
  402. default_value_valid = true;
  403. }
  404. }
  405. } else {
  406. default_value = get_documentation_default_value(name, E.name, default_value_valid);
  407. if (inherited) {
  408. bool base_default_value_valid = false;
  409. Variant base_default_value = get_documentation_default_value(ClassDB::get_parent_class(name), E.name, base_default_value_valid);
  410. if (!default_value_valid || !base_default_value_valid || default_value == base_default_value) {
  411. continue;
  412. }
  413. }
  414. }
  415. if (default_value_valid && default_value.get_type() != Variant::OBJECT) {
  416. prop.default_value = DocData::get_default_value_string(default_value);
  417. }
  418. StringName setter = ClassDB::get_property_setter(name, E.name);
  419. StringName getter = ClassDB::get_property_getter(name, E.name);
  420. prop.setter = setter;
  421. prop.getter = getter;
  422. bool found_type = false;
  423. if (getter != StringName()) {
  424. MethodBind *mb = ClassDB::get_method(name, getter);
  425. if (mb) {
  426. PropertyInfo retinfo = mb->get_return_info();
  427. found_type = true;
  428. if (retinfo.type == Variant::INT && retinfo.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
  429. prop.enumeration = retinfo.class_name;
  430. prop.type = "int";
  431. } else if (retinfo.class_name != StringName()) {
  432. prop.type = retinfo.class_name;
  433. } else if (retinfo.type == Variant::ARRAY && retinfo.hint == PROPERTY_HINT_ARRAY_TYPE) {
  434. prop.type = retinfo.hint_string + "[]";
  435. } else if (retinfo.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  436. prop.type = retinfo.hint_string;
  437. } else if (retinfo.type == Variant::NIL && retinfo.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
  438. prop.type = "Variant";
  439. } else if (retinfo.type == Variant::NIL) {
  440. prop.type = "void";
  441. } else {
  442. prop.type = Variant::get_type_name(retinfo.type);
  443. }
  444. }
  445. setters_getters.insert(getter);
  446. }
  447. if (setter != StringName()) {
  448. setters_getters.insert(setter);
  449. }
  450. if (!found_type) {
  451. if (E.type == Variant::OBJECT && E.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  452. prop.type = E.hint_string;
  453. } else {
  454. prop.type = Variant::get_type_name(E.type);
  455. }
  456. }
  457. c.properties.push_back(prop);
  458. }
  459. List<MethodInfo> method_list;
  460. ClassDB::get_method_list(name, &method_list, true);
  461. method_list.sort();
  462. for (const MethodInfo &E : method_list) {
  463. if (E.name.is_empty() || (E.name[0] == '_' && !(E.flags & METHOD_FLAG_VIRTUAL))) {
  464. continue; //hidden, don't count
  465. }
  466. if (skip_setter_getter_methods && setters_getters.has(E.name)) {
  467. // Don't skip parametric setters and getters, i.e. method which require
  468. // one or more parameters to define what property should be set or retrieved.
  469. // E.g. CPUParticles3D::set_param(Parameter param, float value).
  470. if (E.arguments.size() == 0 /* getter */ || (E.arguments.size() == 1 && E.return_val.type == Variant::NIL /* setter */)) {
  471. continue;
  472. }
  473. }
  474. DocData::MethodDoc method;
  475. DocData::method_doc_from_methodinfo(method, E, "");
  476. Vector<Error> errs = ClassDB::get_method_error_return_values(name, E.name);
  477. if (errs.size()) {
  478. if (!errs.has(OK)) {
  479. errs.insert(0, OK);
  480. }
  481. for (int i = 0; i < errs.size(); i++) {
  482. if (!method.errors_returned.has(errs[i])) {
  483. method.errors_returned.push_back(errs[i]);
  484. }
  485. }
  486. }
  487. c.methods.push_back(method);
  488. }
  489. List<MethodInfo> signal_list;
  490. ClassDB::get_signal_list(name, &signal_list, true);
  491. if (signal_list.size()) {
  492. for (List<MethodInfo>::Element *EV = signal_list.front(); EV; EV = EV->next()) {
  493. DocData::MethodDoc signal;
  494. signal.name = EV->get().name;
  495. for (int i = 0; i < EV->get().arguments.size(); i++) {
  496. const PropertyInfo &arginfo = EV->get().arguments[i];
  497. DocData::ArgumentDoc argument;
  498. DocData::argument_doc_from_arginfo(argument, arginfo);
  499. signal.arguments.push_back(argument);
  500. }
  501. c.signals.push_back(signal);
  502. }
  503. }
  504. List<String> constant_list;
  505. ClassDB::get_integer_constant_list(name, &constant_list, true);
  506. for (const String &E : constant_list) {
  507. DocData::ConstantDoc constant;
  508. constant.name = E;
  509. constant.value = itos(ClassDB::get_integer_constant(name, E));
  510. constant.is_value_valid = true;
  511. constant.enumeration = ClassDB::get_integer_constant_enum(name, E);
  512. constant.is_bitfield = ClassDB::is_enum_bitfield(name, constant.enumeration);
  513. c.constants.push_back(constant);
  514. }
  515. // Theme items.
  516. {
  517. List<StringName> l;
  518. ThemeDB::get_singleton()->get_default_theme()->get_color_list(cname, &l);
  519. for (const StringName &E : l) {
  520. DocData::ThemeItemDoc tid;
  521. tid.name = E;
  522. tid.type = "Color";
  523. tid.data_type = "color";
  524. tid.default_value = DocData::get_default_value_string(ThemeDB::get_singleton()->get_default_theme()->get_color(E, cname));
  525. c.theme_properties.push_back(tid);
  526. }
  527. l.clear();
  528. ThemeDB::get_singleton()->get_default_theme()->get_constant_list(cname, &l);
  529. for (const StringName &E : l) {
  530. DocData::ThemeItemDoc tid;
  531. tid.name = E;
  532. tid.type = "int";
  533. tid.data_type = "constant";
  534. tid.default_value = itos(ThemeDB::get_singleton()->get_default_theme()->get_constant(E, cname));
  535. c.theme_properties.push_back(tid);
  536. }
  537. l.clear();
  538. ThemeDB::get_singleton()->get_default_theme()->get_font_list(cname, &l);
  539. for (const StringName &E : l) {
  540. DocData::ThemeItemDoc tid;
  541. tid.name = E;
  542. tid.type = "Font";
  543. tid.data_type = "font";
  544. c.theme_properties.push_back(tid);
  545. }
  546. l.clear();
  547. ThemeDB::get_singleton()->get_default_theme()->get_font_size_list(cname, &l);
  548. for (const StringName &E : l) {
  549. DocData::ThemeItemDoc tid;
  550. tid.name = E;
  551. tid.type = "int";
  552. tid.data_type = "font_size";
  553. c.theme_properties.push_back(tid);
  554. }
  555. l.clear();
  556. ThemeDB::get_singleton()->get_default_theme()->get_icon_list(cname, &l);
  557. for (const StringName &E : l) {
  558. DocData::ThemeItemDoc tid;
  559. tid.name = E;
  560. tid.type = "Texture2D";
  561. tid.data_type = "icon";
  562. c.theme_properties.push_back(tid);
  563. }
  564. l.clear();
  565. ThemeDB::get_singleton()->get_default_theme()->get_stylebox_list(cname, &l);
  566. for (const StringName &E : l) {
  567. DocData::ThemeItemDoc tid;
  568. tid.name = E;
  569. tid.type = "StyleBox";
  570. tid.data_type = "style";
  571. c.theme_properties.push_back(tid);
  572. }
  573. c.theme_properties.sort();
  574. }
  575. classes.pop_front();
  576. }
  577. }
  578. // Add a dummy Variant entry.
  579. {
  580. // This allows us to document the concept of Variant even though
  581. // it's not a ClassDB-exposed class.
  582. class_list["Variant"] = DocData::ClassDoc();
  583. class_list["Variant"].name = "Variant";
  584. }
  585. // If we don't want to populate basic types, break here.
  586. if (!p_basic_types) {
  587. return;
  588. }
  589. // Add Variant data types.
  590. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  591. if (i == Variant::NIL) {
  592. continue; // Not exposed outside of 'null', should not be in class list.
  593. }
  594. if (i == Variant::OBJECT) {
  595. continue; // Use the core type instead.
  596. }
  597. String cname = Variant::get_type_name(Variant::Type(i));
  598. class_list[cname] = DocData::ClassDoc();
  599. DocData::ClassDoc &c = class_list[cname];
  600. c.name = cname;
  601. Callable::CallError cerror;
  602. Variant v;
  603. Variant::construct(Variant::Type(i), v, nullptr, 0, cerror);
  604. List<MethodInfo> method_list;
  605. v.get_method_list(&method_list);
  606. method_list.sort();
  607. Variant::get_constructor_list(Variant::Type(i), &method_list);
  608. for (int j = 0; j < Variant::OP_AND; j++) { // Showing above 'and' is pretty confusing and there are a lot of variations.
  609. for (int k = 0; k < Variant::VARIANT_MAX; k++) {
  610. // Prevent generating for comparison with null.
  611. if (Variant::Type(k) == Variant::NIL && (Variant::Operator(j) == Variant::OP_EQUAL || Variant::Operator(j) == Variant::OP_NOT_EQUAL)) {
  612. continue;
  613. }
  614. Variant::Type rt = Variant::get_operator_return_type(Variant::Operator(j), Variant::Type(i), Variant::Type(k));
  615. if (rt != Variant::NIL) { // Has operator.
  616. // Skip String % operator as it's registered separately for each Variant arg type,
  617. // we'll add it manually below.
  618. if ((i == Variant::STRING || i == Variant::STRING_NAME) && Variant::Operator(j) == Variant::OP_MODULE) {
  619. continue;
  620. }
  621. MethodInfo mi;
  622. mi.name = "operator " + Variant::get_operator_name(Variant::Operator(j));
  623. mi.return_val.type = rt;
  624. if (k != Variant::NIL) {
  625. PropertyInfo arg;
  626. arg.name = "right";
  627. arg.type = Variant::Type(k);
  628. mi.arguments.push_back(arg);
  629. }
  630. method_list.push_back(mi);
  631. }
  632. }
  633. }
  634. if (i == Variant::STRING || i == Variant::STRING_NAME) {
  635. // We skipped % operator above, and we register it manually once for Variant arg type here.
  636. MethodInfo mi;
  637. mi.name = "operator %";
  638. mi.return_val.type = Variant::STRING;
  639. PropertyInfo arg;
  640. arg.name = "right";
  641. arg.type = Variant::NIL;
  642. arg.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  643. mi.arguments.push_back(arg);
  644. method_list.push_back(mi);
  645. }
  646. if (Variant::is_keyed(Variant::Type(i))) {
  647. MethodInfo mi;
  648. mi.name = "operator []";
  649. mi.return_val.type = Variant::NIL;
  650. mi.return_val.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  651. PropertyInfo arg;
  652. arg.name = "key";
  653. arg.type = Variant::NIL;
  654. arg.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  655. mi.arguments.push_back(arg);
  656. method_list.push_back(mi);
  657. } else if (Variant::has_indexing(Variant::Type(i))) {
  658. MethodInfo mi;
  659. mi.name = "operator []";
  660. mi.return_val.type = Variant::get_indexed_element_type(Variant::Type(i));
  661. mi.return_val.usage = Variant::get_indexed_element_usage(Variant::Type(i));
  662. PropertyInfo arg;
  663. arg.name = "index";
  664. arg.type = Variant::INT;
  665. mi.arguments.push_back(arg);
  666. method_list.push_back(mi);
  667. }
  668. for (const MethodInfo &mi : method_list) {
  669. DocData::MethodDoc method;
  670. method.name = mi.name;
  671. for (int j = 0; j < mi.arguments.size(); j++) {
  672. PropertyInfo arginfo = mi.arguments[j];
  673. DocData::ArgumentDoc ad;
  674. DocData::argument_doc_from_arginfo(ad, mi.arguments[j]);
  675. ad.name = arginfo.name;
  676. int darg_idx = mi.default_arguments.size() - mi.arguments.size() + j;
  677. if (darg_idx >= 0) {
  678. ad.default_value = DocData::get_default_value_string(mi.default_arguments[darg_idx]);
  679. }
  680. method.arguments.push_back(ad);
  681. }
  682. DocData::return_doc_from_retinfo(method, mi.return_val);
  683. if (mi.flags & METHOD_FLAG_VARARG) {
  684. if (!method.qualifiers.is_empty()) {
  685. method.qualifiers += " ";
  686. }
  687. method.qualifiers += "vararg";
  688. }
  689. if (mi.flags & METHOD_FLAG_CONST) {
  690. if (!method.qualifiers.is_empty()) {
  691. method.qualifiers += " ";
  692. }
  693. method.qualifiers += "const";
  694. }
  695. if (mi.flags & METHOD_FLAG_STATIC) {
  696. if (!method.qualifiers.is_empty()) {
  697. method.qualifiers += " ";
  698. }
  699. method.qualifiers += "static";
  700. }
  701. if (method.name == cname) {
  702. c.constructors.push_back(method);
  703. } else if (method.name.begins_with("operator")) {
  704. c.operators.push_back(method);
  705. } else {
  706. c.methods.push_back(method);
  707. }
  708. }
  709. List<PropertyInfo> properties;
  710. v.get_property_list(&properties);
  711. for (const PropertyInfo &pi : properties) {
  712. DocData::PropertyDoc property;
  713. property.name = pi.name;
  714. property.type = Variant::get_type_name(pi.type);
  715. property.default_value = DocData::get_default_value_string(v.get(pi.name));
  716. c.properties.push_back(property);
  717. }
  718. List<StringName> constants;
  719. Variant::get_constants_for_type(Variant::Type(i), &constants);
  720. for (const StringName &E : constants) {
  721. DocData::ConstantDoc constant;
  722. constant.name = E;
  723. Variant value = Variant::get_constant_value(Variant::Type(i), E);
  724. constant.value = value.get_type() == Variant::INT ? itos(value) : value.get_construct_string().replace("\n", " ");
  725. constant.is_value_valid = true;
  726. c.constants.push_back(constant);
  727. }
  728. }
  729. // Add global API (servers, engine singletons, global constants) and Variant utility functions.
  730. {
  731. String cname = "@GlobalScope";
  732. class_list[cname] = DocData::ClassDoc();
  733. DocData::ClassDoc &c = class_list[cname];
  734. c.name = cname;
  735. // Global constants.
  736. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  737. DocData::ConstantDoc cd;
  738. cd.name = CoreConstants::get_global_constant_name(i);
  739. cd.is_bitfield = CoreConstants::is_global_constant_bitfield(i);
  740. if (!CoreConstants::get_ignore_value_in_docs(i)) {
  741. cd.value = itos(CoreConstants::get_global_constant_value(i));
  742. cd.is_value_valid = true;
  743. } else {
  744. cd.is_value_valid = false;
  745. }
  746. cd.enumeration = CoreConstants::get_global_constant_enum(i);
  747. c.constants.push_back(cd);
  748. }
  749. // Servers/engine singletons.
  750. List<Engine::Singleton> singletons;
  751. Engine::get_singleton()->get_singletons(&singletons);
  752. // FIXME: this is kind of hackish...
  753. for (const Engine::Singleton &s : singletons) {
  754. DocData::PropertyDoc pd;
  755. if (!s.ptr) {
  756. continue;
  757. }
  758. pd.name = s.name;
  759. pd.type = s.ptr->get_class();
  760. while (String(ClassDB::get_parent_class(pd.type)) != "Object") {
  761. pd.type = ClassDB::get_parent_class(pd.type);
  762. }
  763. c.properties.push_back(pd);
  764. }
  765. // Variant utility functions.
  766. List<StringName> utility_functions;
  767. Variant::get_utility_function_list(&utility_functions);
  768. utility_functions.sort_custom<StringName::AlphCompare>();
  769. for (const StringName &E : utility_functions) {
  770. DocData::MethodDoc md;
  771. md.name = E;
  772. // Utility function's return type.
  773. if (Variant::has_utility_function_return_value(E)) {
  774. PropertyInfo pi;
  775. pi.type = Variant::get_utility_function_return_type(E);
  776. if (pi.type == Variant::NIL) {
  777. pi.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  778. }
  779. DocData::ArgumentDoc ad;
  780. DocData::argument_doc_from_arginfo(ad, pi);
  781. md.return_type = ad.type;
  782. }
  783. // Utility function's arguments.
  784. if (Variant::is_utility_function_vararg(E)) {
  785. md.qualifiers = "vararg";
  786. } else {
  787. for (int i = 0; i < Variant::get_utility_function_argument_count(E); i++) {
  788. PropertyInfo pi;
  789. pi.type = Variant::get_utility_function_argument_type(E, i);
  790. pi.name = Variant::get_utility_function_argument_name(E, i);
  791. if (pi.type == Variant::NIL) {
  792. pi.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  793. }
  794. DocData::ArgumentDoc ad;
  795. DocData::argument_doc_from_arginfo(ad, pi);
  796. md.arguments.push_back(ad);
  797. }
  798. }
  799. c.methods.push_back(md);
  800. }
  801. }
  802. // Add scripting language built-ins.
  803. {
  804. // We only add a doc entry for languages which actually define any built-in
  805. // methods, constants, or annotations.
  806. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  807. ScriptLanguage *lang = ScriptServer::get_language(i);
  808. String cname = "@" + lang->get_name();
  809. DocData::ClassDoc c;
  810. c.name = cname;
  811. // Get functions.
  812. List<MethodInfo> minfo;
  813. lang->get_public_functions(&minfo);
  814. for (const MethodInfo &mi : minfo) {
  815. DocData::MethodDoc md;
  816. md.name = mi.name;
  817. if (mi.flags & METHOD_FLAG_VARARG) {
  818. if (!md.qualifiers.is_empty()) {
  819. md.qualifiers += " ";
  820. }
  821. md.qualifiers += "vararg";
  822. }
  823. DocData::return_doc_from_retinfo(md, mi.return_val);
  824. for (int j = 0; j < mi.arguments.size(); j++) {
  825. DocData::ArgumentDoc ad;
  826. DocData::argument_doc_from_arginfo(ad, mi.arguments[j]);
  827. int darg_idx = j - (mi.arguments.size() - mi.default_arguments.size());
  828. if (darg_idx >= 0) {
  829. ad.default_value = DocData::get_default_value_string(mi.default_arguments[darg_idx]);
  830. }
  831. md.arguments.push_back(ad);
  832. }
  833. c.methods.push_back(md);
  834. }
  835. // Get constants.
  836. List<Pair<String, Variant>> cinfo;
  837. lang->get_public_constants(&cinfo);
  838. for (const Pair<String, Variant> &E : cinfo) {
  839. DocData::ConstantDoc cd;
  840. cd.name = E.first;
  841. cd.value = E.second;
  842. cd.is_value_valid = true;
  843. c.constants.push_back(cd);
  844. }
  845. // Get annotations.
  846. List<MethodInfo> ainfo;
  847. lang->get_public_annotations(&ainfo);
  848. for (const MethodInfo &ai : ainfo) {
  849. DocData::MethodDoc atd;
  850. atd.name = ai.name;
  851. if (ai.flags & METHOD_FLAG_VARARG) {
  852. if (!atd.qualifiers.is_empty()) {
  853. atd.qualifiers += " ";
  854. }
  855. atd.qualifiers += "vararg";
  856. }
  857. DocData::return_doc_from_retinfo(atd, ai.return_val);
  858. for (int j = 0; j < ai.arguments.size(); j++) {
  859. DocData::ArgumentDoc ad;
  860. DocData::argument_doc_from_arginfo(ad, ai.arguments[j]);
  861. int darg_idx = j - (ai.arguments.size() - ai.default_arguments.size());
  862. if (darg_idx >= 0) {
  863. ad.default_value = DocData::get_default_value_string(ai.default_arguments[darg_idx]);
  864. }
  865. atd.arguments.push_back(ad);
  866. }
  867. c.annotations.push_back(atd);
  868. }
  869. // Skip adding the lang if it doesn't expose anything (e.g. C#).
  870. if (c.methods.is_empty() && c.constants.is_empty() && c.annotations.is_empty()) {
  871. continue;
  872. }
  873. class_list[cname] = c;
  874. }
  875. }
  876. }
  877. static Error _parse_methods(Ref<XMLParser> &parser, Vector<DocData::MethodDoc> &methods) {
  878. String section = parser->get_node_name();
  879. String element = section.substr(0, section.length() - 1);
  880. while (parser->read() == OK) {
  881. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  882. if (parser->get_node_name() == element) {
  883. DocData::MethodDoc method;
  884. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  885. method.name = parser->get_named_attribute_value("name");
  886. if (parser->has_attribute("qualifiers")) {
  887. method.qualifiers = parser->get_named_attribute_value("qualifiers");
  888. }
  889. if (parser->has_attribute("is_deprecated")) {
  890. method.is_deprecated = parser->get_named_attribute_value("is_deprecated").to_lower() == "true";
  891. }
  892. if (parser->has_attribute("is_experimental")) {
  893. method.is_experimental = parser->get_named_attribute_value("is_experimental").to_lower() == "true";
  894. }
  895. while (parser->read() == OK) {
  896. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  897. String name = parser->get_node_name();
  898. if (name == "return") {
  899. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  900. method.return_type = parser->get_named_attribute_value("type");
  901. if (parser->has_attribute("enum")) {
  902. method.return_enum = parser->get_named_attribute_value("enum");
  903. }
  904. } else if (name == "returns_error") {
  905. ERR_FAIL_COND_V(!parser->has_attribute("number"), ERR_FILE_CORRUPT);
  906. method.errors_returned.push_back(parser->get_named_attribute_value("number").to_int());
  907. } else if (name == "param") {
  908. DocData::ArgumentDoc argument;
  909. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  910. argument.name = parser->get_named_attribute_value("name");
  911. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  912. argument.type = parser->get_named_attribute_value("type");
  913. if (parser->has_attribute("enum")) {
  914. argument.enumeration = parser->get_named_attribute_value("enum");
  915. }
  916. method.arguments.push_back(argument);
  917. } else if (name == "description") {
  918. parser->read();
  919. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  920. method.description = parser->get_node_data();
  921. }
  922. }
  923. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == element) {
  924. break;
  925. }
  926. }
  927. methods.push_back(method);
  928. } else {
  929. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + parser->get_node_name() + ", expected " + element + ".");
  930. }
  931. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == section) {
  932. break;
  933. }
  934. }
  935. return OK;
  936. }
  937. Error DocTools::load_classes(const String &p_dir) {
  938. Error err;
  939. Ref<DirAccess> da = DirAccess::open(p_dir, &err);
  940. if (da.is_null()) {
  941. return err;
  942. }
  943. da->list_dir_begin();
  944. String path;
  945. path = da->get_next();
  946. while (!path.is_empty()) {
  947. if (!da->current_is_dir() && path.ends_with("xml")) {
  948. Ref<XMLParser> parser = memnew(XMLParser);
  949. Error err2 = parser->open(p_dir.path_join(path));
  950. if (err2) {
  951. return err2;
  952. }
  953. _load(parser);
  954. }
  955. path = da->get_next();
  956. }
  957. da->list_dir_end();
  958. return OK;
  959. }
  960. Error DocTools::erase_classes(const String &p_dir) {
  961. Error err;
  962. Ref<DirAccess> da = DirAccess::open(p_dir, &err);
  963. if (da.is_null()) {
  964. return err;
  965. }
  966. List<String> to_erase;
  967. da->list_dir_begin();
  968. String path;
  969. path = da->get_next();
  970. while (!path.is_empty()) {
  971. if (!da->current_is_dir() && path.ends_with("xml")) {
  972. to_erase.push_back(path);
  973. }
  974. path = da->get_next();
  975. }
  976. da->list_dir_end();
  977. while (to_erase.size()) {
  978. da->remove(to_erase.front()->get());
  979. to_erase.pop_front();
  980. }
  981. return OK;
  982. }
  983. Error DocTools::_load(Ref<XMLParser> parser) {
  984. Error err = OK;
  985. while ((err = parser->read()) == OK) {
  986. if (parser->get_node_type() == XMLParser::NODE_ELEMENT && parser->get_node_name() == "?xml") {
  987. parser->skip_section();
  988. }
  989. if (parser->get_node_type() != XMLParser::NODE_ELEMENT) {
  990. continue; //no idea what this may be, but skipping anyway
  991. }
  992. ERR_FAIL_COND_V(parser->get_node_name() != "class", ERR_FILE_CORRUPT);
  993. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  994. String name = parser->get_named_attribute_value("name");
  995. class_list[name] = DocData::ClassDoc();
  996. DocData::ClassDoc &c = class_list[name];
  997. c.name = name;
  998. if (parser->has_attribute("inherits")) {
  999. c.inherits = parser->get_named_attribute_value("inherits");
  1000. }
  1001. if (parser->has_attribute("is_deprecated")) {
  1002. c.is_deprecated = parser->get_named_attribute_value("is_deprecated").to_lower() == "true";
  1003. }
  1004. if (parser->has_attribute("is_experimental")) {
  1005. c.is_experimental = parser->get_named_attribute_value("is_experimental").to_lower() == "true";
  1006. }
  1007. while (parser->read() == OK) {
  1008. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1009. String name2 = parser->get_node_name();
  1010. if (name2 == "brief_description") {
  1011. parser->read();
  1012. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1013. c.brief_description = parser->get_node_data();
  1014. }
  1015. } else if (name2 == "description") {
  1016. parser->read();
  1017. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1018. c.description = parser->get_node_data();
  1019. }
  1020. } else if (name2 == "tutorials") {
  1021. while (parser->read() == OK) {
  1022. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1023. String name3 = parser->get_node_name();
  1024. if (name3 == "link") {
  1025. DocData::TutorialDoc tutorial;
  1026. if (parser->has_attribute("title")) {
  1027. tutorial.title = parser->get_named_attribute_value("title");
  1028. }
  1029. parser->read();
  1030. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1031. tutorial.link = parser->get_node_data().strip_edges();
  1032. c.tutorials.push_back(tutorial);
  1033. }
  1034. } else {
  1035. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1036. }
  1037. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "tutorials") {
  1038. break; // End of <tutorials>.
  1039. }
  1040. }
  1041. } else if (name2 == "constructors") {
  1042. Error err2 = _parse_methods(parser, c.constructors);
  1043. ERR_FAIL_COND_V(err2, err2);
  1044. } else if (name2 == "methods") {
  1045. Error err2 = _parse_methods(parser, c.methods);
  1046. ERR_FAIL_COND_V(err2, err2);
  1047. } else if (name2 == "operators") {
  1048. Error err2 = _parse_methods(parser, c.operators);
  1049. ERR_FAIL_COND_V(err2, err2);
  1050. } else if (name2 == "signals") {
  1051. Error err2 = _parse_methods(parser, c.signals);
  1052. ERR_FAIL_COND_V(err2, err2);
  1053. } else if (name2 == "annotations") {
  1054. Error err2 = _parse_methods(parser, c.annotations);
  1055. ERR_FAIL_COND_V(err2, err2);
  1056. } else if (name2 == "members") {
  1057. while (parser->read() == OK) {
  1058. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1059. String name3 = parser->get_node_name();
  1060. if (name3 == "member") {
  1061. DocData::PropertyDoc prop2;
  1062. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  1063. prop2.name = parser->get_named_attribute_value("name");
  1064. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  1065. prop2.type = parser->get_named_attribute_value("type");
  1066. if (parser->has_attribute("setter")) {
  1067. prop2.setter = parser->get_named_attribute_value("setter");
  1068. }
  1069. if (parser->has_attribute("getter")) {
  1070. prop2.getter = parser->get_named_attribute_value("getter");
  1071. }
  1072. if (parser->has_attribute("enum")) {
  1073. prop2.enumeration = parser->get_named_attribute_value("enum");
  1074. }
  1075. if (parser->has_attribute("is_deprecated")) {
  1076. prop2.is_deprecated = parser->get_named_attribute_value("is_deprecated").to_lower() == "true";
  1077. }
  1078. if (parser->has_attribute("is_experimental")) {
  1079. prop2.is_experimental = parser->get_named_attribute_value("is_experimental").to_lower() == "true";
  1080. }
  1081. if (!parser->is_empty()) {
  1082. parser->read();
  1083. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1084. prop2.description = parser->get_node_data();
  1085. }
  1086. }
  1087. c.properties.push_back(prop2);
  1088. } else {
  1089. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1090. }
  1091. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "members") {
  1092. break; // End of <members>.
  1093. }
  1094. }
  1095. } else if (name2 == "theme_items") {
  1096. while (parser->read() == OK) {
  1097. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1098. String name3 = parser->get_node_name();
  1099. if (name3 == "theme_item") {
  1100. DocData::ThemeItemDoc prop2;
  1101. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  1102. prop2.name = parser->get_named_attribute_value("name");
  1103. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  1104. prop2.type = parser->get_named_attribute_value("type");
  1105. ERR_FAIL_COND_V(!parser->has_attribute("data_type"), ERR_FILE_CORRUPT);
  1106. prop2.data_type = parser->get_named_attribute_value("data_type");
  1107. if (!parser->is_empty()) {
  1108. parser->read();
  1109. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1110. prop2.description = parser->get_node_data();
  1111. }
  1112. }
  1113. c.theme_properties.push_back(prop2);
  1114. } else {
  1115. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1116. }
  1117. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "theme_items") {
  1118. break; // End of <theme_items>.
  1119. }
  1120. }
  1121. } else if (name2 == "constants") {
  1122. while (parser->read() == OK) {
  1123. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1124. String name3 = parser->get_node_name();
  1125. if (name3 == "constant") {
  1126. DocData::ConstantDoc constant2;
  1127. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  1128. constant2.name = parser->get_named_attribute_value("name");
  1129. ERR_FAIL_COND_V(!parser->has_attribute("value"), ERR_FILE_CORRUPT);
  1130. constant2.value = parser->get_named_attribute_value("value");
  1131. constant2.is_value_valid = true;
  1132. if (parser->has_attribute("enum")) {
  1133. constant2.enumeration = parser->get_named_attribute_value("enum");
  1134. }
  1135. if (parser->has_attribute("is_bitfield")) {
  1136. constant2.is_bitfield = parser->get_named_attribute_value("is_bitfield").to_lower() == "true";
  1137. }
  1138. if (parser->has_attribute("is_deprecated")) {
  1139. constant2.is_deprecated = parser->get_named_attribute_value("is_deprecated").to_lower() == "true";
  1140. }
  1141. if (parser->has_attribute("is_experimental")) {
  1142. constant2.is_experimental = parser->get_named_attribute_value("is_experimental").to_lower() == "true";
  1143. }
  1144. if (!parser->is_empty()) {
  1145. parser->read();
  1146. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1147. constant2.description = parser->get_node_data();
  1148. }
  1149. }
  1150. c.constants.push_back(constant2);
  1151. } else {
  1152. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1153. }
  1154. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "constants") {
  1155. break; // End of <constants>.
  1156. }
  1157. }
  1158. } else {
  1159. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name2 + ".");
  1160. }
  1161. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "class") {
  1162. break; // End of <class>.
  1163. }
  1164. }
  1165. }
  1166. return OK;
  1167. }
  1168. static void _write_string(Ref<FileAccess> f, int p_tablevel, const String &p_string) {
  1169. if (p_string.is_empty()) {
  1170. return;
  1171. }
  1172. String tab;
  1173. for (int i = 0; i < p_tablevel; i++) {
  1174. tab += "\t";
  1175. }
  1176. f->store_string(tab + p_string + "\n");
  1177. }
  1178. static void _write_method_doc(Ref<FileAccess> f, const String &p_name, Vector<DocData::MethodDoc> &p_method_docs) {
  1179. if (!p_method_docs.is_empty()) {
  1180. p_method_docs.sort();
  1181. _write_string(f, 1, "<" + p_name + "s>");
  1182. for (int i = 0; i < p_method_docs.size(); i++) {
  1183. const DocData::MethodDoc &m = p_method_docs[i];
  1184. String qualifiers;
  1185. if (!m.qualifiers.is_empty()) {
  1186. qualifiers += " qualifiers=\"" + m.qualifiers.xml_escape() + "\"";
  1187. }
  1188. String additional_attributes;
  1189. if (m.is_deprecated) {
  1190. additional_attributes += " is_deprecated=\"true\"";
  1191. }
  1192. if (m.is_experimental) {
  1193. additional_attributes += " is_experimental=\"true\"";
  1194. }
  1195. _write_string(f, 2, "<" + p_name + " name=\"" + m.name.xml_escape() + "\"" + qualifiers + additional_attributes + ">");
  1196. if (!m.return_type.is_empty()) {
  1197. String enum_text;
  1198. if (!m.return_enum.is_empty()) {
  1199. enum_text = " enum=\"" + m.return_enum + "\"";
  1200. }
  1201. _write_string(f, 3, "<return type=\"" + m.return_type + "\"" + enum_text + " />");
  1202. }
  1203. if (m.errors_returned.size() > 0) {
  1204. for (int j = 0; j < m.errors_returned.size(); j++) {
  1205. _write_string(f, 3, "<returns_error number=\"" + itos(m.errors_returned[j]) + "\"/>");
  1206. }
  1207. }
  1208. for (int j = 0; j < m.arguments.size(); j++) {
  1209. const DocData::ArgumentDoc &a = m.arguments[j];
  1210. String enum_text;
  1211. if (!a.enumeration.is_empty()) {
  1212. enum_text = " enum=\"" + a.enumeration + "\"";
  1213. }
  1214. if (!a.default_value.is_empty()) {
  1215. _write_string(f, 3, "<param index=\"" + itos(j) + "\" name=\"" + a.name.xml_escape() + "\" type=\"" + a.type.xml_escape() + "\"" + enum_text + " default=\"" + a.default_value.xml_escape(true) + "\" />");
  1216. } else {
  1217. _write_string(f, 3, "<param index=\"" + itos(j) + "\" name=\"" + a.name.xml_escape() + "\" type=\"" + a.type.xml_escape() + "\"" + enum_text + " />");
  1218. }
  1219. }
  1220. _write_string(f, 3, "<description>");
  1221. _write_string(f, 4, _translate_doc_string(m.description).strip_edges().xml_escape());
  1222. _write_string(f, 3, "</description>");
  1223. _write_string(f, 2, "</" + p_name + ">");
  1224. }
  1225. _write_string(f, 1, "</" + p_name + "s>");
  1226. }
  1227. }
  1228. Error DocTools::save_classes(const String &p_default_path, const HashMap<String, String> &p_class_path) {
  1229. for (KeyValue<String, DocData::ClassDoc> &E : class_list) {
  1230. DocData::ClassDoc &c = E.value;
  1231. String save_path;
  1232. if (p_class_path.has(c.name)) {
  1233. save_path = p_class_path[c.name];
  1234. } else {
  1235. save_path = p_default_path;
  1236. }
  1237. Error err;
  1238. String save_file = save_path.path_join(c.name + ".xml");
  1239. Ref<FileAccess> f = FileAccess::open(save_file, FileAccess::WRITE, &err);
  1240. ERR_CONTINUE_MSG(err != OK, "Can't write doc file: " + save_file + ".");
  1241. _write_string(f, 0, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
  1242. String header = "<class name=\"" + c.name + "\"";
  1243. if (!c.inherits.is_empty()) {
  1244. header += " inherits=\"" + c.inherits + "\"";
  1245. if (c.is_deprecated) {
  1246. header += " is_deprecated=\"true\"";
  1247. }
  1248. if (c.is_experimental) {
  1249. header += " is_experimental=\"true\"";
  1250. }
  1251. }
  1252. header += String(" version=\"") + VERSION_BRANCH + "\"";
  1253. // Reference the XML schema so editors can provide error checking.
  1254. // Modules are nested deep, so change the path to reference the same schema everywhere.
  1255. const String schema_path = save_path.find("modules/") != -1 ? "../../../doc/class.xsd" : "../class.xsd";
  1256. header += vformat(
  1257. R"( xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="%s">)",
  1258. schema_path);
  1259. _write_string(f, 0, header);
  1260. _write_string(f, 1, "<brief_description>");
  1261. _write_string(f, 2, _translate_doc_string(c.brief_description).strip_edges().xml_escape());
  1262. _write_string(f, 1, "</brief_description>");
  1263. _write_string(f, 1, "<description>");
  1264. _write_string(f, 2, _translate_doc_string(c.description).strip_edges().xml_escape());
  1265. _write_string(f, 1, "</description>");
  1266. _write_string(f, 1, "<tutorials>");
  1267. for (int i = 0; i < c.tutorials.size(); i++) {
  1268. DocData::TutorialDoc tutorial = c.tutorials.get(i);
  1269. String title_attribute = (!tutorial.title.is_empty()) ? " title=\"" + _translate_doc_string(tutorial.title).xml_escape() + "\"" : "";
  1270. _write_string(f, 2, "<link" + title_attribute + ">" + tutorial.link.xml_escape() + "</link>");
  1271. }
  1272. _write_string(f, 1, "</tutorials>");
  1273. _write_method_doc(f, "constructor", c.constructors);
  1274. _write_method_doc(f, "method", c.methods);
  1275. if (!c.properties.is_empty()) {
  1276. _write_string(f, 1, "<members>");
  1277. c.properties.sort();
  1278. for (int i = 0; i < c.properties.size(); i++) {
  1279. String additional_attributes;
  1280. if (!c.properties[i].enumeration.is_empty()) {
  1281. additional_attributes += " enum=\"" + c.properties[i].enumeration + "\"";
  1282. }
  1283. if (!c.properties[i].default_value.is_empty()) {
  1284. additional_attributes += " default=\"" + c.properties[i].default_value.xml_escape(true) + "\"";
  1285. }
  1286. if (c.properties[i].is_deprecated) {
  1287. additional_attributes += " is_deprecated=\"true\"";
  1288. }
  1289. if (c.properties[i].is_experimental) {
  1290. additional_attributes += " is_experimental=\"true\"";
  1291. }
  1292. const DocData::PropertyDoc &p = c.properties[i];
  1293. if (c.properties[i].overridden) {
  1294. _write_string(f, 2, "<member name=\"" + p.name + "\" type=\"" + p.type + "\" setter=\"" + p.setter + "\" getter=\"" + p.getter + "\" overrides=\"" + p.overrides + "\"" + additional_attributes + " />");
  1295. } else {
  1296. _write_string(f, 2, "<member name=\"" + p.name + "\" type=\"" + p.type + "\" setter=\"" + p.setter + "\" getter=\"" + p.getter + "\"" + additional_attributes + ">");
  1297. _write_string(f, 3, _translate_doc_string(p.description).strip_edges().xml_escape());
  1298. _write_string(f, 2, "</member>");
  1299. }
  1300. }
  1301. _write_string(f, 1, "</members>");
  1302. }
  1303. _write_method_doc(f, "signal", c.signals);
  1304. if (!c.constants.is_empty()) {
  1305. _write_string(f, 1, "<constants>");
  1306. for (int i = 0; i < c.constants.size(); i++) {
  1307. const DocData::ConstantDoc &k = c.constants[i];
  1308. String additional_attributes;
  1309. if (c.constants[i].is_deprecated) {
  1310. additional_attributes += " is_deprecated=\"true\"";
  1311. }
  1312. if (c.constants[i].is_experimental) {
  1313. additional_attributes += " is_experimental=\"true\"";
  1314. }
  1315. if (k.is_value_valid) {
  1316. if (!k.enumeration.is_empty()) {
  1317. if (k.is_bitfield) {
  1318. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"" + k.value + "\" enum=\"" + k.enumeration + "\" is_bitfield=\"true\"" + additional_attributes + ">");
  1319. } else {
  1320. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"" + k.value + "\" enum=\"" + k.enumeration + "\"" + additional_attributes + ">");
  1321. }
  1322. } else {
  1323. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"" + k.value + "\"" + additional_attributes + ">");
  1324. }
  1325. } else {
  1326. if (!k.enumeration.is_empty()) {
  1327. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"platform-dependent\" enum=\"" + k.enumeration + "\"" + additional_attributes + ">");
  1328. } else {
  1329. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"platform-dependent\"" + additional_attributes + ">");
  1330. }
  1331. }
  1332. _write_string(f, 3, _translate_doc_string(k.description).strip_edges().xml_escape());
  1333. _write_string(f, 2, "</constant>");
  1334. }
  1335. _write_string(f, 1, "</constants>");
  1336. }
  1337. _write_method_doc(f, "annotation", c.annotations);
  1338. if (!c.theme_properties.is_empty()) {
  1339. c.theme_properties.sort();
  1340. _write_string(f, 1, "<theme_items>");
  1341. for (int i = 0; i < c.theme_properties.size(); i++) {
  1342. const DocData::ThemeItemDoc &ti = c.theme_properties[i];
  1343. if (!ti.default_value.is_empty()) {
  1344. _write_string(f, 2, "<theme_item name=\"" + ti.name + "\" data_type=\"" + ti.data_type + "\" type=\"" + ti.type + "\" default=\"" + ti.default_value.xml_escape(true) + "\">");
  1345. } else {
  1346. _write_string(f, 2, "<theme_item name=\"" + ti.name + "\" data_type=\"" + ti.data_type + "\" type=\"" + ti.type + "\">");
  1347. }
  1348. _write_string(f, 3, _translate_doc_string(ti.description).strip_edges().xml_escape());
  1349. _write_string(f, 2, "</theme_item>");
  1350. }
  1351. _write_string(f, 1, "</theme_items>");
  1352. }
  1353. _write_method_doc(f, "operator", c.operators);
  1354. _write_string(f, 0, "</class>");
  1355. }
  1356. return OK;
  1357. }
  1358. Error DocTools::load_compressed(const uint8_t *p_data, int p_compressed_size, int p_uncompressed_size) {
  1359. Vector<uint8_t> data;
  1360. data.resize(p_uncompressed_size);
  1361. int ret = Compression::decompress(data.ptrw(), p_uncompressed_size, p_data, p_compressed_size, Compression::MODE_DEFLATE);
  1362. ERR_FAIL_COND_V_MSG(ret == -1, ERR_FILE_CORRUPT, "Compressed file is corrupt.");
  1363. class_list.clear();
  1364. Ref<XMLParser> parser = memnew(XMLParser);
  1365. Error err = parser->open_buffer(data);
  1366. if (err) {
  1367. return err;
  1368. }
  1369. _load(parser);
  1370. return OK;
  1371. }