doc_tools.cpp 47 KB

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