doc_tools.cpp 52 KB

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