doc_tools.cpp 47 KB

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