doc_tools.cpp 55 KB

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