doc_tools.cpp 43 KB

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