doc_tools.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  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. Set<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. Variant::Type rt = Variant::get_operator_return_type(Variant::Operator(j), Variant::Type(i), Variant::Type(k));
  585. if (rt != Variant::NIL) { // Has operator.
  586. // Skip String % operator as it's registered separately for each Variant arg type,
  587. // we'll add it manually below.
  588. if (i == Variant::STRING && Variant::Operator(j) == Variant::OP_MODULE) {
  589. continue;
  590. }
  591. MethodInfo mi;
  592. mi.name = "operator " + Variant::get_operator_name(Variant::Operator(j));
  593. mi.return_val.type = rt;
  594. if (k != Variant::NIL) {
  595. PropertyInfo arg;
  596. arg.name = "right";
  597. arg.type = Variant::Type(k);
  598. mi.arguments.push_back(arg);
  599. }
  600. method_list.push_back(mi);
  601. }
  602. }
  603. }
  604. if (i == Variant::STRING) {
  605. // We skipped % operator above, and we register it manually once for Variant arg type here.
  606. MethodInfo mi;
  607. mi.name = "operator %";
  608. mi.return_val.type = Variant::STRING;
  609. PropertyInfo arg;
  610. arg.name = "right";
  611. arg.type = Variant::NIL;
  612. arg.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  613. mi.arguments.push_back(arg);
  614. method_list.push_back(mi);
  615. }
  616. if (Variant::is_keyed(Variant::Type(i))) {
  617. MethodInfo mi;
  618. mi.name = "operator []";
  619. mi.return_val.type = Variant::NIL;
  620. mi.return_val.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  621. PropertyInfo arg;
  622. arg.name = "key";
  623. arg.type = Variant::NIL;
  624. arg.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  625. mi.arguments.push_back(arg);
  626. method_list.push_back(mi);
  627. } else if (Variant::has_indexing(Variant::Type(i))) {
  628. MethodInfo mi;
  629. mi.name = "operator []";
  630. mi.return_val.type = Variant::get_indexed_element_type(Variant::Type(i));
  631. PropertyInfo arg;
  632. arg.name = "index";
  633. arg.type = Variant::INT;
  634. mi.arguments.push_back(arg);
  635. method_list.push_back(mi);
  636. }
  637. for (const MethodInfo &mi : method_list) {
  638. DocData::MethodDoc method;
  639. method.name = mi.name;
  640. for (int j = 0; j < mi.arguments.size(); j++) {
  641. PropertyInfo arginfo = mi.arguments[j];
  642. DocData::ArgumentDoc ad;
  643. DocData::argument_doc_from_arginfo(ad, mi.arguments[j]);
  644. ad.name = arginfo.name;
  645. int darg_idx = mi.default_arguments.size() - mi.arguments.size() + j;
  646. if (darg_idx >= 0) {
  647. Variant default_arg = mi.default_arguments[darg_idx];
  648. ad.default_value = default_arg.get_construct_string().replace("\n", " ");
  649. }
  650. method.arguments.push_back(ad);
  651. }
  652. DocData::return_doc_from_retinfo(method, mi.return_val);
  653. if (mi.flags & METHOD_FLAG_VARARG) {
  654. if (!method.qualifiers.is_empty()) {
  655. method.qualifiers += " ";
  656. }
  657. method.qualifiers += "vararg";
  658. }
  659. if (mi.flags & METHOD_FLAG_CONST) {
  660. if (!method.qualifiers.is_empty()) {
  661. method.qualifiers += " ";
  662. }
  663. method.qualifiers += "const";
  664. }
  665. if (mi.flags & METHOD_FLAG_STATIC) {
  666. if (!method.qualifiers.is_empty()) {
  667. method.qualifiers += " ";
  668. }
  669. method.qualifiers += "static";
  670. }
  671. if (method.name == cname) {
  672. c.constructors.push_back(method);
  673. } else if (method.name.begins_with("operator")) {
  674. c.operators.push_back(method);
  675. } else {
  676. c.methods.push_back(method);
  677. }
  678. }
  679. List<PropertyInfo> properties;
  680. v.get_property_list(&properties);
  681. for (const PropertyInfo &pi : properties) {
  682. DocData::PropertyDoc property;
  683. property.name = pi.name;
  684. property.type = Variant::get_type_name(pi.type);
  685. property.default_value = v.get(pi.name).get_construct_string().replace("\n", " ");
  686. c.properties.push_back(property);
  687. }
  688. List<StringName> constants;
  689. Variant::get_constants_for_type(Variant::Type(i), &constants);
  690. for (const StringName &E : constants) {
  691. DocData::ConstantDoc constant;
  692. constant.name = E;
  693. Variant value = Variant::get_constant_value(Variant::Type(i), E);
  694. constant.value = value.get_type() == Variant::INT ? itos(value) : value.get_construct_string().replace("\n", " ");
  695. constant.is_value_valid = true;
  696. c.constants.push_back(constant);
  697. }
  698. }
  699. //built in constants and functions
  700. {
  701. String cname = "@GlobalScope";
  702. class_list[cname] = DocData::ClassDoc();
  703. DocData::ClassDoc &c = class_list[cname];
  704. c.name = cname;
  705. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  706. DocData::ConstantDoc cd;
  707. cd.name = CoreConstants::get_global_constant_name(i);
  708. if (!CoreConstants::get_ignore_value_in_docs(i)) {
  709. cd.value = itos(CoreConstants::get_global_constant_value(i));
  710. cd.is_value_valid = true;
  711. } else {
  712. cd.is_value_valid = false;
  713. }
  714. cd.enumeration = CoreConstants::get_global_constant_enum(i);
  715. c.constants.push_back(cd);
  716. }
  717. List<Engine::Singleton> singletons;
  718. Engine::get_singleton()->get_singletons(&singletons);
  719. //servers (this is kind of hackish)
  720. for (const Engine::Singleton &s : singletons) {
  721. DocData::PropertyDoc pd;
  722. if (!s.ptr) {
  723. continue;
  724. }
  725. pd.name = s.name;
  726. pd.type = s.ptr->get_class();
  727. while (String(ClassDB::get_parent_class(pd.type)) != "Object") {
  728. pd.type = ClassDB::get_parent_class(pd.type);
  729. }
  730. c.properties.push_back(pd);
  731. }
  732. List<StringName> utility_functions;
  733. Variant::get_utility_function_list(&utility_functions);
  734. utility_functions.sort_custom<StringName::AlphCompare>();
  735. for (const StringName &E : utility_functions) {
  736. DocData::MethodDoc md;
  737. md.name = E;
  738. //return
  739. if (Variant::has_utility_function_return_value(E)) {
  740. PropertyInfo pi;
  741. pi.type = Variant::get_utility_function_return_type(E);
  742. if (pi.type == Variant::NIL) {
  743. pi.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  744. }
  745. DocData::ArgumentDoc ad;
  746. DocData::argument_doc_from_arginfo(ad, pi);
  747. md.return_type = ad.type;
  748. }
  749. if (Variant::is_utility_function_vararg(E)) {
  750. md.qualifiers = "vararg";
  751. } else {
  752. for (int i = 0; i < Variant::get_utility_function_argument_count(E); i++) {
  753. PropertyInfo pi;
  754. pi.type = Variant::get_utility_function_argument_type(E, i);
  755. pi.name = Variant::get_utility_function_argument_name(E, i);
  756. if (pi.type == Variant::NIL) {
  757. pi.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  758. }
  759. DocData::ArgumentDoc ad;
  760. DocData::argument_doc_from_arginfo(ad, pi);
  761. md.arguments.push_back(ad);
  762. }
  763. }
  764. c.methods.push_back(md);
  765. }
  766. }
  767. // Built-in script reference.
  768. // We only add a doc entry for languages which actually define any built-in
  769. // methods or constants.
  770. {
  771. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  772. ScriptLanguage *lang = ScriptServer::get_language(i);
  773. String cname = "@" + lang->get_name();
  774. DocData::ClassDoc c;
  775. c.name = cname;
  776. // Get functions.
  777. List<MethodInfo> minfo;
  778. lang->get_public_functions(&minfo);
  779. for (const MethodInfo &mi : minfo) {
  780. DocData::MethodDoc md;
  781. md.name = mi.name;
  782. if (mi.flags & METHOD_FLAG_VARARG) {
  783. if (!md.qualifiers.is_empty()) {
  784. md.qualifiers += " ";
  785. }
  786. md.qualifiers += "vararg";
  787. }
  788. DocData::return_doc_from_retinfo(md, mi.return_val);
  789. for (int j = 0; j < mi.arguments.size(); j++) {
  790. DocData::ArgumentDoc ad;
  791. DocData::argument_doc_from_arginfo(ad, mi.arguments[j]);
  792. int darg_idx = j - (mi.arguments.size() - mi.default_arguments.size());
  793. if (darg_idx >= 0) {
  794. Variant default_arg = mi.default_arguments[darg_idx];
  795. ad.default_value = default_arg.get_construct_string().replace("\n", " ");
  796. }
  797. md.arguments.push_back(ad);
  798. }
  799. c.methods.push_back(md);
  800. }
  801. // Get constants.
  802. List<Pair<String, Variant>> cinfo;
  803. lang->get_public_constants(&cinfo);
  804. for (const Pair<String, Variant> &E : cinfo) {
  805. DocData::ConstantDoc cd;
  806. cd.name = E.first;
  807. cd.value = E.second;
  808. cd.is_value_valid = true;
  809. c.constants.push_back(cd);
  810. }
  811. // Skip adding the lang if it doesn't expose anything (e.g. C#).
  812. if (c.methods.is_empty() && c.constants.is_empty()) {
  813. continue;
  814. }
  815. class_list[cname] = c;
  816. }
  817. }
  818. }
  819. static Error _parse_methods(Ref<XMLParser> &parser, Vector<DocData::MethodDoc> &methods) {
  820. String section = parser->get_node_name();
  821. String element = section.substr(0, section.length() - 1);
  822. while (parser->read() == OK) {
  823. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  824. if (parser->get_node_name() == element) {
  825. DocData::MethodDoc method;
  826. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  827. method.name = parser->get_attribute_value("name");
  828. if (parser->has_attribute("qualifiers")) {
  829. method.qualifiers = parser->get_attribute_value("qualifiers");
  830. }
  831. while (parser->read() == OK) {
  832. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  833. String name = parser->get_node_name();
  834. if (name == "return") {
  835. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  836. method.return_type = parser->get_attribute_value("type");
  837. if (parser->has_attribute("enum")) {
  838. method.return_enum = parser->get_attribute_value("enum");
  839. }
  840. } else if (name == "returns_error") {
  841. ERR_FAIL_COND_V(!parser->has_attribute("number"), ERR_FILE_CORRUPT);
  842. method.errors_returned.push_back(parser->get_attribute_value("number").to_int());
  843. } else if (name == "argument") {
  844. DocData::ArgumentDoc argument;
  845. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  846. argument.name = parser->get_attribute_value("name");
  847. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  848. argument.type = parser->get_attribute_value("type");
  849. if (parser->has_attribute("enum")) {
  850. argument.enumeration = parser->get_attribute_value("enum");
  851. }
  852. method.arguments.push_back(argument);
  853. } else if (name == "description") {
  854. parser->read();
  855. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  856. method.description = parser->get_node_data();
  857. }
  858. }
  859. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == element) {
  860. break;
  861. }
  862. }
  863. methods.push_back(method);
  864. } else {
  865. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + parser->get_node_name() + ", expected " + element + ".");
  866. }
  867. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == section) {
  868. break;
  869. }
  870. }
  871. return OK;
  872. }
  873. Error DocTools::load_classes(const String &p_dir) {
  874. Error err;
  875. DirAccessRef da = DirAccess::open(p_dir, &err);
  876. if (!da) {
  877. return err;
  878. }
  879. da->list_dir_begin();
  880. String path;
  881. path = da->get_next();
  882. while (!path.is_empty()) {
  883. if (!da->current_is_dir() && path.ends_with("xml")) {
  884. Ref<XMLParser> parser = memnew(XMLParser);
  885. Error err2 = parser->open(p_dir.plus_file(path));
  886. if (err2) {
  887. return err2;
  888. }
  889. _load(parser);
  890. }
  891. path = da->get_next();
  892. }
  893. da->list_dir_end();
  894. return OK;
  895. }
  896. Error DocTools::erase_classes(const String &p_dir) {
  897. Error err;
  898. DirAccessRef da = DirAccess::open(p_dir, &err);
  899. if (!da) {
  900. return err;
  901. }
  902. List<String> to_erase;
  903. da->list_dir_begin();
  904. String path;
  905. path = da->get_next();
  906. while (!path.is_empty()) {
  907. if (!da->current_is_dir() && path.ends_with("xml")) {
  908. to_erase.push_back(path);
  909. }
  910. path = da->get_next();
  911. }
  912. da->list_dir_end();
  913. while (to_erase.size()) {
  914. da->remove(to_erase.front()->get());
  915. to_erase.pop_front();
  916. }
  917. return OK;
  918. }
  919. Error DocTools::_load(Ref<XMLParser> parser) {
  920. Error err = OK;
  921. while ((err = parser->read()) == OK) {
  922. if (parser->get_node_type() == XMLParser::NODE_ELEMENT && parser->get_node_name() == "?xml") {
  923. parser->skip_section();
  924. }
  925. if (parser->get_node_type() != XMLParser::NODE_ELEMENT) {
  926. continue; //no idea what this may be, but skipping anyway
  927. }
  928. ERR_FAIL_COND_V(parser->get_node_name() != "class", ERR_FILE_CORRUPT);
  929. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  930. String name = parser->get_attribute_value("name");
  931. class_list[name] = DocData::ClassDoc();
  932. DocData::ClassDoc &c = class_list[name];
  933. c.name = name;
  934. if (parser->has_attribute("inherits")) {
  935. c.inherits = parser->get_attribute_value("inherits");
  936. }
  937. while (parser->read() == OK) {
  938. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  939. String name2 = parser->get_node_name();
  940. if (name2 == "brief_description") {
  941. parser->read();
  942. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  943. c.brief_description = parser->get_node_data();
  944. }
  945. } else if (name2 == "description") {
  946. parser->read();
  947. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  948. c.description = parser->get_node_data();
  949. }
  950. } else if (name2 == "tutorials") {
  951. while (parser->read() == OK) {
  952. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  953. String name3 = parser->get_node_name();
  954. if (name3 == "link") {
  955. DocData::TutorialDoc tutorial;
  956. if (parser->has_attribute("title")) {
  957. tutorial.title = parser->get_attribute_value("title");
  958. }
  959. parser->read();
  960. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  961. tutorial.link = parser->get_node_data().strip_edges();
  962. c.tutorials.push_back(tutorial);
  963. }
  964. } else {
  965. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  966. }
  967. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "tutorials") {
  968. break; // End of <tutorials>.
  969. }
  970. }
  971. } else if (name2 == "constructors") {
  972. Error err2 = _parse_methods(parser, c.constructors);
  973. ERR_FAIL_COND_V(err2, err2);
  974. } else if (name2 == "methods") {
  975. Error err2 = _parse_methods(parser, c.methods);
  976. ERR_FAIL_COND_V(err2, err2);
  977. } else if (name2 == "operators") {
  978. Error err2 = _parse_methods(parser, c.operators);
  979. ERR_FAIL_COND_V(err2, err2);
  980. } else if (name2 == "signals") {
  981. Error err2 = _parse_methods(parser, c.signals);
  982. ERR_FAIL_COND_V(err2, err2);
  983. } else if (name2 == "members") {
  984. while (parser->read() == OK) {
  985. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  986. String name3 = parser->get_node_name();
  987. if (name3 == "member") {
  988. DocData::PropertyDoc prop2;
  989. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  990. prop2.name = parser->get_attribute_value("name");
  991. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  992. prop2.type = parser->get_attribute_value("type");
  993. if (parser->has_attribute("setter")) {
  994. prop2.setter = parser->get_attribute_value("setter");
  995. }
  996. if (parser->has_attribute("getter")) {
  997. prop2.getter = parser->get_attribute_value("getter");
  998. }
  999. if (parser->has_attribute("enum")) {
  1000. prop2.enumeration = parser->get_attribute_value("enum");
  1001. }
  1002. if (!parser->is_empty()) {
  1003. parser->read();
  1004. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1005. prop2.description = parser->get_node_data();
  1006. }
  1007. }
  1008. c.properties.push_back(prop2);
  1009. } else {
  1010. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1011. }
  1012. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "members") {
  1013. break; // End of <members>.
  1014. }
  1015. }
  1016. } else if (name2 == "theme_items") {
  1017. while (parser->read() == OK) {
  1018. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1019. String name3 = parser->get_node_name();
  1020. if (name3 == "theme_item") {
  1021. DocData::ThemeItemDoc prop2;
  1022. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  1023. prop2.name = parser->get_attribute_value("name");
  1024. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  1025. prop2.type = parser->get_attribute_value("type");
  1026. ERR_FAIL_COND_V(!parser->has_attribute("data_type"), ERR_FILE_CORRUPT);
  1027. prop2.data_type = parser->get_attribute_value("data_type");
  1028. if (!parser->is_empty()) {
  1029. parser->read();
  1030. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1031. prop2.description = parser->get_node_data();
  1032. }
  1033. }
  1034. c.theme_properties.push_back(prop2);
  1035. } else {
  1036. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1037. }
  1038. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "theme_items") {
  1039. break; // End of <theme_items>.
  1040. }
  1041. }
  1042. } else if (name2 == "constants") {
  1043. while (parser->read() == OK) {
  1044. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1045. String name3 = parser->get_node_name();
  1046. if (name3 == "constant") {
  1047. DocData::ConstantDoc constant2;
  1048. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  1049. constant2.name = parser->get_attribute_value("name");
  1050. ERR_FAIL_COND_V(!parser->has_attribute("value"), ERR_FILE_CORRUPT);
  1051. constant2.value = parser->get_attribute_value("value");
  1052. constant2.is_value_valid = true;
  1053. if (parser->has_attribute("enum")) {
  1054. constant2.enumeration = parser->get_attribute_value("enum");
  1055. }
  1056. if (!parser->is_empty()) {
  1057. parser->read();
  1058. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1059. constant2.description = parser->get_node_data();
  1060. }
  1061. }
  1062. c.constants.push_back(constant2);
  1063. } else {
  1064. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1065. }
  1066. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "constants") {
  1067. break; // End of <constants>.
  1068. }
  1069. }
  1070. } else {
  1071. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name2 + ".");
  1072. }
  1073. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "class") {
  1074. break; // End of <class>.
  1075. }
  1076. }
  1077. }
  1078. return OK;
  1079. }
  1080. static void _write_string(FileAccess *f, int p_tablevel, const String &p_string) {
  1081. if (p_string.is_empty()) {
  1082. return;
  1083. }
  1084. String tab;
  1085. for (int i = 0; i < p_tablevel; i++) {
  1086. tab += "\t";
  1087. }
  1088. f->store_string(tab + p_string + "\n");
  1089. }
  1090. static void _write_method_doc(FileAccess *f, const String &p_name, Vector<DocData::MethodDoc> &p_method_docs) {
  1091. if (!p_method_docs.is_empty()) {
  1092. p_method_docs.sort();
  1093. _write_string(f, 1, "<" + p_name + "s>");
  1094. for (int i = 0; i < p_method_docs.size(); i++) {
  1095. const DocData::MethodDoc &m = p_method_docs[i];
  1096. String qualifiers;
  1097. if (!m.qualifiers.is_empty()) {
  1098. qualifiers += " qualifiers=\"" + m.qualifiers.xml_escape() + "\"";
  1099. }
  1100. _write_string(f, 2, "<" + p_name + " name=\"" + m.name.xml_escape() + "\"" + qualifiers + ">");
  1101. if (!m.return_type.is_empty()) {
  1102. String enum_text;
  1103. if (!m.return_enum.is_empty()) {
  1104. enum_text = " enum=\"" + m.return_enum + "\"";
  1105. }
  1106. _write_string(f, 3, "<return type=\"" + m.return_type + "\"" + enum_text + " />");
  1107. }
  1108. if (m.errors_returned.size() > 0) {
  1109. for (int j = 0; j < m.errors_returned.size(); j++) {
  1110. _write_string(f, 3, "<returns_error number=\"" + itos(m.errors_returned[j]) + "\"/>");
  1111. }
  1112. }
  1113. for (int j = 0; j < m.arguments.size(); j++) {
  1114. const DocData::ArgumentDoc &a = m.arguments[j];
  1115. String enum_text;
  1116. if (!a.enumeration.is_empty()) {
  1117. enum_text = " enum=\"" + a.enumeration + "\"";
  1118. }
  1119. if (!a.default_value.is_empty()) {
  1120. _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) + "\" />");
  1121. } else {
  1122. _write_string(f, 3, "<argument index=\"" + itos(j) + "\" name=\"" + a.name.xml_escape() + "\" type=\"" + a.type.xml_escape() + "\"" + enum_text + " />");
  1123. }
  1124. }
  1125. _write_string(f, 3, "<description>");
  1126. _write_string(f, 4, _translate_doc_string(m.description).strip_edges().xml_escape());
  1127. _write_string(f, 3, "</description>");
  1128. _write_string(f, 2, "</" + p_name + ">");
  1129. }
  1130. _write_string(f, 1, "</" + p_name + "s>");
  1131. }
  1132. }
  1133. Error DocTools::save_classes(const String &p_default_path, const Map<String, String> &p_class_path) {
  1134. for (KeyValue<String, DocData::ClassDoc> &E : class_list) {
  1135. DocData::ClassDoc &c = E.value;
  1136. String save_path;
  1137. if (p_class_path.has(c.name)) {
  1138. save_path = p_class_path[c.name];
  1139. } else {
  1140. save_path = p_default_path;
  1141. }
  1142. Error err;
  1143. String save_file = save_path.plus_file(c.name + ".xml");
  1144. FileAccessRef f = FileAccess::open(save_file, FileAccess::WRITE, &err);
  1145. ERR_CONTINUE_MSG(err != OK, "Can't write doc file: " + save_file + ".");
  1146. _write_string(f, 0, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
  1147. String header = "<class name=\"" + c.name + "\"";
  1148. if (!c.inherits.is_empty()) {
  1149. header += " inherits=\"" + c.inherits + "\"";
  1150. }
  1151. header += String(" version=\"") + VERSION_BRANCH + "\"";
  1152. // Reference the XML schema so editors can provide error checking.
  1153. // Modules are nested deep, so change the path to reference the same schema everywhere.
  1154. const String schema_path = save_path.find("modules/") != -1 ? "../../../doc/class.xsd" : "../class.xsd";
  1155. header += vformat(
  1156. R"( xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="%s">)",
  1157. schema_path);
  1158. _write_string(f, 0, header);
  1159. _write_string(f, 1, "<brief_description>");
  1160. _write_string(f, 2, _translate_doc_string(c.brief_description).strip_edges().xml_escape());
  1161. _write_string(f, 1, "</brief_description>");
  1162. _write_string(f, 1, "<description>");
  1163. _write_string(f, 2, _translate_doc_string(c.description).strip_edges().xml_escape());
  1164. _write_string(f, 1, "</description>");
  1165. _write_string(f, 1, "<tutorials>");
  1166. for (int i = 0; i < c.tutorials.size(); i++) {
  1167. DocData::TutorialDoc tutorial = c.tutorials.get(i);
  1168. String title_attribute = (!tutorial.title.is_empty()) ? " title=\"" + _translate_doc_string(tutorial.title).xml_escape() + "\"" : "";
  1169. _write_string(f, 2, "<link" + title_attribute + ">" + tutorial.link.xml_escape() + "</link>");
  1170. }
  1171. _write_string(f, 1, "</tutorials>");
  1172. _write_method_doc(f, "constructor", c.constructors);
  1173. _write_method_doc(f, "method", c.methods);
  1174. if (!c.properties.is_empty()) {
  1175. _write_string(f, 1, "<members>");
  1176. c.properties.sort();
  1177. for (int i = 0; i < c.properties.size(); i++) {
  1178. String additional_attributes;
  1179. if (!c.properties[i].enumeration.is_empty()) {
  1180. additional_attributes += " enum=\"" + c.properties[i].enumeration + "\"";
  1181. }
  1182. if (!c.properties[i].default_value.is_empty()) {
  1183. additional_attributes += " default=\"" + c.properties[i].default_value.xml_escape(true) + "\"";
  1184. }
  1185. const DocData::PropertyDoc &p = c.properties[i];
  1186. if (c.properties[i].overridden) {
  1187. _write_string(f, 2, "<member name=\"" + p.name + "\" type=\"" + p.type + "\" setter=\"" + p.setter + "\" getter=\"" + p.getter + "\" overrides=\"" + p.overrides + "\"" + additional_attributes + " />");
  1188. } else {
  1189. _write_string(f, 2, "<member name=\"" + p.name + "\" type=\"" + p.type + "\" setter=\"" + p.setter + "\" getter=\"" + p.getter + "\"" + additional_attributes + ">");
  1190. _write_string(f, 3, _translate_doc_string(p.description).strip_edges().xml_escape());
  1191. _write_string(f, 2, "</member>");
  1192. }
  1193. }
  1194. _write_string(f, 1, "</members>");
  1195. }
  1196. _write_method_doc(f, "signal", c.signals);
  1197. if (!c.constants.is_empty()) {
  1198. _write_string(f, 1, "<constants>");
  1199. for (int i = 0; i < c.constants.size(); i++) {
  1200. const DocData::ConstantDoc &k = c.constants[i];
  1201. if (k.is_value_valid) {
  1202. if (!k.enumeration.is_empty()) {
  1203. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"" + k.value + "\" enum=\"" + k.enumeration + "\">");
  1204. } else {
  1205. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"" + k.value + "\">");
  1206. }
  1207. } else {
  1208. if (!k.enumeration.is_empty()) {
  1209. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"platform-dependent\" enum=\"" + k.enumeration + "\">");
  1210. } else {
  1211. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"platform-dependent\">");
  1212. }
  1213. }
  1214. _write_string(f, 3, _translate_doc_string(k.description).strip_edges().xml_escape());
  1215. _write_string(f, 2, "</constant>");
  1216. }
  1217. _write_string(f, 1, "</constants>");
  1218. }
  1219. if (!c.theme_properties.is_empty()) {
  1220. c.theme_properties.sort();
  1221. _write_string(f, 1, "<theme_items>");
  1222. for (int i = 0; i < c.theme_properties.size(); i++) {
  1223. const DocData::ThemeItemDoc &ti = c.theme_properties[i];
  1224. if (!ti.default_value.is_empty()) {
  1225. _write_string(f, 2, "<theme_item name=\"" + ti.name + "\" data_type=\"" + ti.data_type + "\" type=\"" + ti.type + "\" default=\"" + ti.default_value.xml_escape(true) + "\">");
  1226. } else {
  1227. _write_string(f, 2, "<theme_item name=\"" + ti.name + "\" data_type=\"" + ti.data_type + "\" type=\"" + ti.type + "\">");
  1228. }
  1229. _write_string(f, 3, _translate_doc_string(ti.description).strip_edges().xml_escape());
  1230. _write_string(f, 2, "</theme_item>");
  1231. }
  1232. _write_string(f, 1, "</theme_items>");
  1233. }
  1234. _write_method_doc(f, "operator", c.operators);
  1235. _write_string(f, 0, "</class>");
  1236. }
  1237. return OK;
  1238. }
  1239. Error DocTools::load_compressed(const uint8_t *p_data, int p_compressed_size, int p_uncompressed_size) {
  1240. Vector<uint8_t> data;
  1241. data.resize(p_uncompressed_size);
  1242. Compression::decompress(data.ptrw(), p_uncompressed_size, p_data, p_compressed_size, Compression::MODE_DEFLATE);
  1243. class_list.clear();
  1244. Ref<XMLParser> parser = memnew(XMLParser);
  1245. Error err = parser->open_buffer(data);
  1246. if (err) {
  1247. return err;
  1248. }
  1249. _load(parser);
  1250. return OK;
  1251. }