doc_tools.cpp 54 KB

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