doc_tools.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  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/dir_access.h"
  36. #include "core/io/marshalls.h"
  37. #include "core/object/script_language.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_instantiate(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 instantiated
  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_instantiate(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 (const PropertyInfo &E : properties) {
  231. bool inherited = EO == nullptr;
  232. if (EO && EO->get() == E) {
  233. inherited = false;
  234. EO = EO->next();
  235. }
  236. if (E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP || E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_INTERNAL) {
  237. continue;
  238. }
  239. DocData::PropertyDoc prop;
  240. prop.name = E.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.name == "script" || !ProjectSettings::get_singleton()->is_builtin_setting(E.name)) {
  247. continue;
  248. }
  249. if (E.usage & PROPERTY_USAGE_EDITOR) {
  250. if (!ProjectSettings::get_singleton()->get_ignore_value_in_docs(E.name)) {
  251. default_value = ProjectSettings::get_singleton()->property_get_revert(E.name);
  252. default_value_valid = true;
  253. }
  254. }
  255. } else {
  256. default_value = get_documentation_default_value(name, E.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.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.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.name);
  271. StringName getter = ClassDB::get_property_getter(name, E.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.type == Variant::OBJECT && E.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  304. prop.type = E.hint_string;
  305. } else {
  306. prop.type = Variant::get_type_name(E.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 (const MethodInfo &E : method_list) {
  315. if (E.name == "" || (E.name[0] == '_' && !(E.flags & METHOD_FLAG_VIRTUAL))) {
  316. continue; //hidden, don't count
  317. }
  318. if (skip_setter_getter_methods && setters_getters.has(E.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.arguments.size() == 0 /* getter */ || (E.arguments.size() == 1 && E.return_val.type == Variant::NIL /* setter */)) {
  323. continue;
  324. }
  325. }
  326. DocData::MethodDoc method;
  327. method.name = E.name;
  328. if (E.flags & METHOD_FLAG_VIRTUAL) {
  329. method.qualifiers = "virtual";
  330. }
  331. if (E.flags & METHOD_FLAG_CONST) {
  332. if (method.qualifiers != "") {
  333. method.qualifiers += " ";
  334. }
  335. method.qualifiers += "const";
  336. }
  337. if (E.flags & METHOD_FLAG_VARARG) {
  338. if (method.qualifiers != "") {
  339. method.qualifiers += " ";
  340. }
  341. method.qualifiers += "vararg";
  342. }
  343. if (E.flags & METHOD_FLAG_STATIC) {
  344. if (method.qualifiers != "") {
  345. method.qualifiers += " ";
  346. }
  347. method.qualifiers += "static";
  348. }
  349. for (int i = -1; i < E.arguments.size(); i++) {
  350. if (i == -1) {
  351. #ifdef DEBUG_METHODS_ENABLED
  352. DocData::return_doc_from_retinfo(method, E.return_val);
  353. #endif
  354. } else {
  355. const PropertyInfo &arginfo = E.arguments[i];
  356. DocData::ArgumentDoc argument;
  357. DocData::argument_doc_from_arginfo(argument, arginfo);
  358. int darg_idx = i - (E.arguments.size() - E.default_arguments.size());
  359. if (darg_idx >= 0) {
  360. Variant default_arg = E.default_arguments[darg_idx];
  361. argument.default_value = default_arg.get_construct_string();
  362. }
  363. method.arguments.push_back(argument);
  364. }
  365. }
  366. c.methods.push_back(method);
  367. }
  368. List<MethodInfo> signal_list;
  369. ClassDB::get_signal_list(name, &signal_list, true);
  370. if (signal_list.size()) {
  371. for (List<MethodInfo>::Element *EV = signal_list.front(); EV; EV = EV->next()) {
  372. DocData::MethodDoc signal;
  373. signal.name = EV->get().name;
  374. for (int i = 0; i < EV->get().arguments.size(); i++) {
  375. const PropertyInfo &arginfo = EV->get().arguments[i];
  376. DocData::ArgumentDoc argument;
  377. DocData::argument_doc_from_arginfo(argument, arginfo);
  378. signal.arguments.push_back(argument);
  379. }
  380. c.signals.push_back(signal);
  381. }
  382. }
  383. List<String> constant_list;
  384. ClassDB::get_integer_constant_list(name, &constant_list, true);
  385. for (const String &E : constant_list) {
  386. DocData::ConstantDoc constant;
  387. constant.name = E;
  388. constant.value = itos(ClassDB::get_integer_constant(name, E));
  389. constant.is_value_valid = true;
  390. constant.enumeration = ClassDB::get_integer_constant_enum(name, E);
  391. c.constants.push_back(constant);
  392. }
  393. //theme stuff
  394. {
  395. List<StringName> l;
  396. Theme::get_default()->get_constant_list(cname, &l);
  397. for (const StringName &E : l) {
  398. DocData::PropertyDoc pd;
  399. pd.name = E;
  400. pd.type = "int";
  401. pd.default_value = itos(Theme::get_default()->get_constant(E, cname));
  402. c.theme_properties.push_back(pd);
  403. }
  404. l.clear();
  405. Theme::get_default()->get_color_list(cname, &l);
  406. for (const StringName &E : l) {
  407. DocData::PropertyDoc pd;
  408. pd.name = E;
  409. pd.type = "Color";
  410. pd.default_value = Variant(Theme::get_default()->get_color(E, cname)).get_construct_string();
  411. c.theme_properties.push_back(pd);
  412. }
  413. l.clear();
  414. Theme::get_default()->get_icon_list(cname, &l);
  415. for (const StringName &E : l) {
  416. DocData::PropertyDoc pd;
  417. pd.name = E;
  418. pd.type = "Texture2D";
  419. c.theme_properties.push_back(pd);
  420. }
  421. l.clear();
  422. Theme::get_default()->get_font_list(cname, &l);
  423. for (const StringName &E : l) {
  424. DocData::PropertyDoc pd;
  425. pd.name = E;
  426. pd.type = "Font";
  427. c.theme_properties.push_back(pd);
  428. }
  429. l.clear();
  430. Theme::get_default()->get_font_size_list(cname, &l);
  431. for (const StringName &E : l) {
  432. DocData::PropertyDoc pd;
  433. pd.name = E;
  434. pd.type = "int";
  435. c.theme_properties.push_back(pd);
  436. }
  437. l.clear();
  438. Theme::get_default()->get_stylebox_list(cname, &l);
  439. for (const StringName &E : l) {
  440. DocData::PropertyDoc pd;
  441. pd.name = E;
  442. pd.type = "StyleBox";
  443. c.theme_properties.push_back(pd);
  444. }
  445. }
  446. classes.pop_front();
  447. }
  448. {
  449. // So we can document the concept of Variant even if it's not a usable class per se.
  450. class_list["Variant"] = DocData::ClassDoc();
  451. class_list["Variant"].name = "Variant";
  452. }
  453. if (!p_basic_types) {
  454. return;
  455. }
  456. // Add Variant types.
  457. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  458. if (i == Variant::NIL) {
  459. continue; // Not exposed outside of 'null', should not be in class list.
  460. }
  461. if (i == Variant::OBJECT) {
  462. continue; // Use the core type instead.
  463. }
  464. String cname = Variant::get_type_name(Variant::Type(i));
  465. class_list[cname] = DocData::ClassDoc();
  466. DocData::ClassDoc &c = class_list[cname];
  467. c.name = cname;
  468. Callable::CallError cerror;
  469. Variant v;
  470. Variant::construct(Variant::Type(i), v, nullptr, 0, cerror);
  471. List<MethodInfo> method_list;
  472. v.get_method_list(&method_list);
  473. method_list.sort();
  474. Variant::get_constructor_list(Variant::Type(i), &method_list);
  475. for (int j = 0; j < Variant::OP_AND; j++) { // Showing above 'and' is pretty confusing and there are a lot of variations.
  476. for (int k = 0; k < Variant::VARIANT_MAX; k++) {
  477. Variant::Type rt = Variant::get_operator_return_type(Variant::Operator(j), Variant::Type(i), Variant::Type(k));
  478. if (rt != Variant::NIL) { // Has operator.
  479. // Skip String % operator as it's registered separately for each Variant arg type,
  480. // we'll add it manually below.
  481. if (i == Variant::STRING && Variant::Operator(j) == Variant::OP_MODULE) {
  482. continue;
  483. }
  484. MethodInfo mi;
  485. mi.name = "operator " + Variant::get_operator_name(Variant::Operator(j));
  486. mi.return_val.type = rt;
  487. if (k != Variant::NIL) {
  488. PropertyInfo arg;
  489. arg.name = "right";
  490. arg.type = Variant::Type(k);
  491. mi.arguments.push_back(arg);
  492. }
  493. method_list.push_back(mi);
  494. }
  495. }
  496. }
  497. if (i == Variant::STRING) {
  498. // We skipped % operator above, and we register it manually once for Variant arg type here.
  499. MethodInfo mi;
  500. mi.name = "operator %";
  501. mi.return_val.type = Variant::STRING;
  502. PropertyInfo arg;
  503. arg.name = "right";
  504. arg.type = Variant::NIL;
  505. arg.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  506. mi.arguments.push_back(arg);
  507. method_list.push_back(mi);
  508. }
  509. if (Variant::is_keyed(Variant::Type(i))) {
  510. MethodInfo mi;
  511. mi.name = "operator []";
  512. mi.return_val.type = Variant::NIL;
  513. mi.return_val.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  514. PropertyInfo arg;
  515. arg.name = "key";
  516. arg.type = Variant::NIL;
  517. arg.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  518. mi.arguments.push_back(arg);
  519. method_list.push_back(mi);
  520. } else if (Variant::has_indexing(Variant::Type(i))) {
  521. MethodInfo mi;
  522. mi.name = "operator []";
  523. mi.return_val.type = Variant::get_indexed_element_type(Variant::Type(i));
  524. PropertyInfo arg;
  525. arg.name = "index";
  526. arg.type = Variant::INT;
  527. mi.arguments.push_back(arg);
  528. method_list.push_back(mi);
  529. }
  530. for (const MethodInfo &mi : method_list) {
  531. DocData::MethodDoc method;
  532. method.name = mi.name;
  533. if (method.name == cname) {
  534. method.qualifiers = "constructor";
  535. } else if (method.name.begins_with("operator")) {
  536. method.qualifiers = "operator";
  537. }
  538. for (int j = 0; j < mi.arguments.size(); j++) {
  539. PropertyInfo arginfo = mi.arguments[j];
  540. DocData::ArgumentDoc ad;
  541. DocData::argument_doc_from_arginfo(ad, mi.arguments[j]);
  542. ad.name = arginfo.name;
  543. int darg_idx = mi.default_arguments.size() - mi.arguments.size() + j;
  544. if (darg_idx >= 0) {
  545. Variant default_arg = mi.default_arguments[darg_idx];
  546. ad.default_value = default_arg.get_construct_string();
  547. }
  548. method.arguments.push_back(ad);
  549. }
  550. DocData::return_doc_from_retinfo(method, mi.return_val);
  551. if (mi.flags & METHOD_FLAG_VARARG) {
  552. if (method.qualifiers != "") {
  553. method.qualifiers += " ";
  554. }
  555. method.qualifiers += "vararg";
  556. }
  557. if (mi.flags & METHOD_FLAG_CONST) {
  558. if (method.qualifiers != "") {
  559. method.qualifiers += " ";
  560. }
  561. method.qualifiers += "const";
  562. }
  563. if (mi.flags & METHOD_FLAG_STATIC) {
  564. if (method.qualifiers != "") {
  565. method.qualifiers += " ";
  566. }
  567. method.qualifiers += "static";
  568. }
  569. c.methods.push_back(method);
  570. }
  571. List<PropertyInfo> properties;
  572. v.get_property_list(&properties);
  573. for (const PropertyInfo &pi : properties) {
  574. DocData::PropertyDoc property;
  575. property.name = pi.name;
  576. property.type = Variant::get_type_name(pi.type);
  577. property.default_value = v.get(pi.name).get_construct_string();
  578. c.properties.push_back(property);
  579. }
  580. List<StringName> constants;
  581. Variant::get_constants_for_type(Variant::Type(i), &constants);
  582. for (const StringName &E : constants) {
  583. DocData::ConstantDoc constant;
  584. constant.name = E;
  585. Variant value = Variant::get_constant_value(Variant::Type(i), E);
  586. constant.value = value.get_type() == Variant::INT ? itos(value) : value.get_construct_string();
  587. constant.is_value_valid = true;
  588. c.constants.push_back(constant);
  589. }
  590. }
  591. //built in constants and functions
  592. {
  593. String cname = "@GlobalScope";
  594. class_list[cname] = DocData::ClassDoc();
  595. DocData::ClassDoc &c = class_list[cname];
  596. c.name = cname;
  597. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  598. DocData::ConstantDoc cd;
  599. cd.name = CoreConstants::get_global_constant_name(i);
  600. if (!CoreConstants::get_ignore_value_in_docs(i)) {
  601. cd.value = itos(CoreConstants::get_global_constant_value(i));
  602. cd.is_value_valid = true;
  603. } else {
  604. cd.is_value_valid = false;
  605. }
  606. cd.enumeration = CoreConstants::get_global_constant_enum(i);
  607. c.constants.push_back(cd);
  608. }
  609. List<Engine::Singleton> singletons;
  610. Engine::get_singleton()->get_singletons(&singletons);
  611. //servers (this is kind of hackish)
  612. for (const Engine::Singleton &s : singletons) {
  613. DocData::PropertyDoc pd;
  614. if (!s.ptr) {
  615. continue;
  616. }
  617. pd.name = s.name;
  618. pd.type = s.ptr->get_class();
  619. while (String(ClassDB::get_parent_class(pd.type)) != "Object") {
  620. pd.type = ClassDB::get_parent_class(pd.type);
  621. }
  622. if (pd.type.begins_with("_")) {
  623. pd.type = pd.type.substr(1, pd.type.length());
  624. }
  625. c.properties.push_back(pd);
  626. }
  627. List<StringName> utility_functions;
  628. Variant::get_utility_function_list(&utility_functions);
  629. utility_functions.sort_custom<StringName::AlphCompare>();
  630. for (const StringName &E : utility_functions) {
  631. DocData::MethodDoc md;
  632. md.name = E;
  633. //return
  634. if (Variant::has_utility_function_return_value(E)) {
  635. PropertyInfo pi;
  636. pi.type = Variant::get_utility_function_return_type(E);
  637. if (pi.type == Variant::NIL) {
  638. pi.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  639. }
  640. DocData::ArgumentDoc ad;
  641. DocData::argument_doc_from_arginfo(ad, pi);
  642. md.return_type = ad.type;
  643. }
  644. if (Variant::is_utility_function_vararg(E)) {
  645. md.qualifiers = "vararg";
  646. } else {
  647. for (int i = 0; i < Variant::get_utility_function_argument_count(E); i++) {
  648. PropertyInfo pi;
  649. pi.type = Variant::get_utility_function_argument_type(E, i);
  650. pi.name = Variant::get_utility_function_argument_name(E, i);
  651. if (pi.type == Variant::NIL) {
  652. pi.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  653. }
  654. DocData::ArgumentDoc ad;
  655. DocData::argument_doc_from_arginfo(ad, pi);
  656. md.arguments.push_back(ad);
  657. }
  658. }
  659. c.methods.push_back(md);
  660. }
  661. }
  662. // Built-in script reference.
  663. // We only add a doc entry for languages which actually define any built-in
  664. // methods or constants.
  665. {
  666. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  667. ScriptLanguage *lang = ScriptServer::get_language(i);
  668. String cname = "@" + lang->get_name();
  669. DocData::ClassDoc c;
  670. c.name = cname;
  671. // Get functions.
  672. List<MethodInfo> minfo;
  673. lang->get_public_functions(&minfo);
  674. for (const MethodInfo &mi : minfo) {
  675. DocData::MethodDoc md;
  676. md.name = mi.name;
  677. if (mi.flags & METHOD_FLAG_VARARG) {
  678. if (md.qualifiers != "") {
  679. md.qualifiers += " ";
  680. }
  681. md.qualifiers += "vararg";
  682. }
  683. DocData::return_doc_from_retinfo(md, mi.return_val);
  684. for (int j = 0; j < mi.arguments.size(); j++) {
  685. DocData::ArgumentDoc ad;
  686. DocData::argument_doc_from_arginfo(ad, mi.arguments[j]);
  687. int darg_idx = j - (mi.arguments.size() - mi.default_arguments.size());
  688. if (darg_idx >= 0) {
  689. Variant default_arg = mi.default_arguments[darg_idx];
  690. ad.default_value = default_arg.get_construct_string();
  691. }
  692. md.arguments.push_back(ad);
  693. }
  694. c.methods.push_back(md);
  695. }
  696. // Get constants.
  697. List<Pair<String, Variant>> cinfo;
  698. lang->get_public_constants(&cinfo);
  699. for (const Pair<String, Variant> &E : cinfo) {
  700. DocData::ConstantDoc cd;
  701. cd.name = E.first;
  702. cd.value = E.second;
  703. cd.is_value_valid = true;
  704. c.constants.push_back(cd);
  705. }
  706. // Skip adding the lang if it doesn't expose anything (e.g. C#).
  707. if (c.methods.is_empty() && c.constants.is_empty()) {
  708. continue;
  709. }
  710. class_list[cname] = c;
  711. }
  712. }
  713. }
  714. static Error _parse_methods(Ref<XMLParser> &parser, Vector<DocData::MethodDoc> &methods) {
  715. String section = parser->get_node_name();
  716. String element = section.substr(0, section.length() - 1);
  717. while (parser->read() == OK) {
  718. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  719. if (parser->get_node_name() == element) {
  720. DocData::MethodDoc method;
  721. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  722. method.name = parser->get_attribute_value("name");
  723. if (parser->has_attribute("qualifiers")) {
  724. method.qualifiers = parser->get_attribute_value("qualifiers");
  725. }
  726. while (parser->read() == OK) {
  727. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  728. String name = parser->get_node_name();
  729. if (name == "return") {
  730. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  731. method.return_type = parser->get_attribute_value("type");
  732. if (parser->has_attribute("enum")) {
  733. method.return_enum = parser->get_attribute_value("enum");
  734. }
  735. } else if (name == "argument") {
  736. DocData::ArgumentDoc argument;
  737. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  738. argument.name = parser->get_attribute_value("name");
  739. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  740. argument.type = parser->get_attribute_value("type");
  741. if (parser->has_attribute("enum")) {
  742. argument.enumeration = parser->get_attribute_value("enum");
  743. }
  744. method.arguments.push_back(argument);
  745. } else if (name == "description") {
  746. parser->read();
  747. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  748. method.description = parser->get_node_data();
  749. }
  750. }
  751. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == element) {
  752. break;
  753. }
  754. }
  755. methods.push_back(method);
  756. } else {
  757. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + parser->get_node_name() + ".");
  758. }
  759. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == section) {
  760. break;
  761. }
  762. }
  763. return OK;
  764. }
  765. Error DocTools::load_classes(const String &p_dir) {
  766. Error err;
  767. DirAccessRef da = DirAccess::open(p_dir, &err);
  768. if (!da) {
  769. return err;
  770. }
  771. da->list_dir_begin();
  772. String path;
  773. path = da->get_next();
  774. while (path != String()) {
  775. if (!da->current_is_dir() && path.ends_with("xml")) {
  776. Ref<XMLParser> parser = memnew(XMLParser);
  777. Error err2 = parser->open(p_dir.plus_file(path));
  778. if (err2) {
  779. return err2;
  780. }
  781. _load(parser);
  782. }
  783. path = da->get_next();
  784. }
  785. da->list_dir_end();
  786. return OK;
  787. }
  788. Error DocTools::erase_classes(const String &p_dir) {
  789. Error err;
  790. DirAccessRef da = DirAccess::open(p_dir, &err);
  791. if (!da) {
  792. return err;
  793. }
  794. List<String> to_erase;
  795. da->list_dir_begin();
  796. String path;
  797. path = da->get_next();
  798. while (path != String()) {
  799. if (!da->current_is_dir() && path.ends_with("xml")) {
  800. to_erase.push_back(path);
  801. }
  802. path = da->get_next();
  803. }
  804. da->list_dir_end();
  805. while (to_erase.size()) {
  806. da->remove(to_erase.front()->get());
  807. to_erase.pop_front();
  808. }
  809. return OK;
  810. }
  811. Error DocTools::_load(Ref<XMLParser> parser) {
  812. Error err = OK;
  813. while ((err = parser->read()) == OK) {
  814. if (parser->get_node_type() == XMLParser::NODE_ELEMENT && parser->get_node_name() == "?xml") {
  815. parser->skip_section();
  816. }
  817. if (parser->get_node_type() != XMLParser::NODE_ELEMENT) {
  818. continue; //no idea what this may be, but skipping anyway
  819. }
  820. ERR_FAIL_COND_V(parser->get_node_name() != "class", ERR_FILE_CORRUPT);
  821. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  822. String name = parser->get_attribute_value("name");
  823. class_list[name] = DocData::ClassDoc();
  824. DocData::ClassDoc &c = class_list[name];
  825. c.name = name;
  826. if (parser->has_attribute("inherits")) {
  827. c.inherits = parser->get_attribute_value("inherits");
  828. }
  829. while (parser->read() == OK) {
  830. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  831. String name2 = parser->get_node_name();
  832. if (name2 == "brief_description") {
  833. parser->read();
  834. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  835. c.brief_description = parser->get_node_data();
  836. }
  837. } else if (name2 == "description") {
  838. parser->read();
  839. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  840. c.description = parser->get_node_data();
  841. }
  842. } else if (name2 == "tutorials") {
  843. while (parser->read() == OK) {
  844. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  845. String name3 = parser->get_node_name();
  846. if (name3 == "link") {
  847. DocData::TutorialDoc tutorial;
  848. if (parser->has_attribute("title")) {
  849. tutorial.title = parser->get_attribute_value("title");
  850. }
  851. parser->read();
  852. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  853. tutorial.link = parser->get_node_data().strip_edges();
  854. c.tutorials.push_back(tutorial);
  855. }
  856. } else {
  857. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  858. }
  859. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "tutorials") {
  860. break; // End of <tutorials>.
  861. }
  862. }
  863. } else if (name2 == "methods") {
  864. Error err2 = _parse_methods(parser, c.methods);
  865. ERR_FAIL_COND_V(err2, err2);
  866. } else if (name2 == "signals") {
  867. Error err2 = _parse_methods(parser, c.signals);
  868. ERR_FAIL_COND_V(err2, err2);
  869. } else if (name2 == "members") {
  870. while (parser->read() == OK) {
  871. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  872. String name3 = parser->get_node_name();
  873. if (name3 == "member") {
  874. DocData::PropertyDoc prop2;
  875. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  876. prop2.name = parser->get_attribute_value("name");
  877. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  878. prop2.type = parser->get_attribute_value("type");
  879. if (parser->has_attribute("setter")) {
  880. prop2.setter = parser->get_attribute_value("setter");
  881. }
  882. if (parser->has_attribute("getter")) {
  883. prop2.getter = parser->get_attribute_value("getter");
  884. }
  885. if (parser->has_attribute("enum")) {
  886. prop2.enumeration = parser->get_attribute_value("enum");
  887. }
  888. if (!parser->is_empty()) {
  889. parser->read();
  890. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  891. prop2.description = parser->get_node_data();
  892. }
  893. }
  894. c.properties.push_back(prop2);
  895. } else {
  896. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  897. }
  898. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "members") {
  899. break; // End of <members>.
  900. }
  901. }
  902. } else if (name2 == "theme_items") {
  903. while (parser->read() == OK) {
  904. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  905. String name3 = parser->get_node_name();
  906. if (name3 == "theme_item") {
  907. DocData::PropertyDoc prop2;
  908. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  909. prop2.name = parser->get_attribute_value("name");
  910. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  911. prop2.type = parser->get_attribute_value("type");
  912. if (!parser->is_empty()) {
  913. parser->read();
  914. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  915. prop2.description = parser->get_node_data();
  916. }
  917. }
  918. c.theme_properties.push_back(prop2);
  919. } else {
  920. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  921. }
  922. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "theme_items") {
  923. break; // End of <theme_items>.
  924. }
  925. }
  926. } else if (name2 == "constants") {
  927. while (parser->read() == OK) {
  928. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  929. String name3 = parser->get_node_name();
  930. if (name3 == "constant") {
  931. DocData::ConstantDoc constant2;
  932. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  933. constant2.name = parser->get_attribute_value("name");
  934. ERR_FAIL_COND_V(!parser->has_attribute("value"), ERR_FILE_CORRUPT);
  935. constant2.value = parser->get_attribute_value("value");
  936. constant2.is_value_valid = true;
  937. if (parser->has_attribute("enum")) {
  938. constant2.enumeration = parser->get_attribute_value("enum");
  939. }
  940. if (!parser->is_empty()) {
  941. parser->read();
  942. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  943. constant2.description = parser->get_node_data();
  944. }
  945. }
  946. c.constants.push_back(constant2);
  947. } else {
  948. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  949. }
  950. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "constants") {
  951. break; // End of <constants>.
  952. }
  953. }
  954. } else {
  955. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name2 + ".");
  956. }
  957. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "class") {
  958. break; // End of <class>.
  959. }
  960. }
  961. }
  962. return OK;
  963. }
  964. static void _write_string(FileAccess *f, int p_tablevel, const String &p_string) {
  965. if (p_string == "") {
  966. return;
  967. }
  968. String tab;
  969. for (int i = 0; i < p_tablevel; i++) {
  970. tab += "\t";
  971. }
  972. f->store_string(tab + p_string + "\n");
  973. }
  974. Error DocTools::save_classes(const String &p_default_path, const Map<String, String> &p_class_path) {
  975. for (Map<String, DocData::ClassDoc>::Element *E = class_list.front(); E; E = E->next()) {
  976. DocData::ClassDoc &c = E->get();
  977. String save_path;
  978. if (p_class_path.has(c.name)) {
  979. save_path = p_class_path[c.name];
  980. } else {
  981. save_path = p_default_path;
  982. }
  983. Error err;
  984. String save_file = save_path.plus_file(c.name + ".xml");
  985. FileAccessRef f = FileAccess::open(save_file, FileAccess::WRITE, &err);
  986. ERR_CONTINUE_MSG(err != OK, "Can't write doc file: " + save_file + ".");
  987. _write_string(f, 0, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
  988. String header = "<class name=\"" + c.name + "\"";
  989. if (c.inherits != "") {
  990. header += " inherits=\"" + c.inherits + "\"";
  991. }
  992. header += String(" version=\"") + VERSION_BRANCH + "\"";
  993. header += ">";
  994. _write_string(f, 0, header);
  995. _write_string(f, 1, "<brief_description>");
  996. _write_string(f, 2, c.brief_description.strip_edges().xml_escape());
  997. _write_string(f, 1, "</brief_description>");
  998. _write_string(f, 1, "<description>");
  999. _write_string(f, 2, c.description.strip_edges().xml_escape());
  1000. _write_string(f, 1, "</description>");
  1001. _write_string(f, 1, "<tutorials>");
  1002. for (int i = 0; i < c.tutorials.size(); i++) {
  1003. DocData::TutorialDoc tutorial = c.tutorials.get(i);
  1004. String title_attribute = (!tutorial.title.is_empty()) ? " title=\"" + tutorial.title.xml_escape() + "\"" : "";
  1005. _write_string(f, 2, "<link" + title_attribute + ">" + tutorial.link.xml_escape() + "</link>");
  1006. }
  1007. _write_string(f, 1, "</tutorials>");
  1008. _write_string(f, 1, "<methods>");
  1009. c.methods.sort();
  1010. for (int i = 0; i < c.methods.size(); i++) {
  1011. const DocData::MethodDoc &m = c.methods[i];
  1012. String qualifiers;
  1013. if (m.qualifiers != "") {
  1014. qualifiers += " qualifiers=\"" + m.qualifiers.xml_escape() + "\"";
  1015. }
  1016. _write_string(f, 2, "<method name=\"" + m.name.xml_escape() + "\"" + qualifiers + ">");
  1017. if (m.return_type != "") {
  1018. String enum_text;
  1019. if (m.return_enum != String()) {
  1020. enum_text = " enum=\"" + m.return_enum + "\"";
  1021. }
  1022. _write_string(f, 3, "<return type=\"" + m.return_type + "\"" + enum_text + ">");
  1023. _write_string(f, 3, "</return>");
  1024. }
  1025. for (int j = 0; j < m.arguments.size(); j++) {
  1026. const DocData::ArgumentDoc &a = m.arguments[j];
  1027. String enum_text;
  1028. if (a.enumeration != String()) {
  1029. enum_text = " enum=\"" + a.enumeration + "\"";
  1030. }
  1031. if (a.default_value != "") {
  1032. _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) + "\">");
  1033. } else {
  1034. _write_string(f, 3, "<argument index=\"" + itos(j) + "\" name=\"" + a.name.xml_escape() + "\" type=\"" + a.type.xml_escape() + "\"" + enum_text + ">");
  1035. }
  1036. _write_string(f, 3, "</argument>");
  1037. }
  1038. _write_string(f, 3, "<description>");
  1039. _write_string(f, 4, m.description.strip_edges().xml_escape());
  1040. _write_string(f, 3, "</description>");
  1041. _write_string(f, 2, "</method>");
  1042. }
  1043. _write_string(f, 1, "</methods>");
  1044. if (c.properties.size()) {
  1045. _write_string(f, 1, "<members>");
  1046. c.properties.sort();
  1047. for (int i = 0; i < c.properties.size(); i++) {
  1048. String additional_attributes;
  1049. if (c.properties[i].enumeration != String()) {
  1050. additional_attributes += " enum=\"" + c.properties[i].enumeration + "\"";
  1051. }
  1052. if (c.properties[i].default_value != String()) {
  1053. additional_attributes += " default=\"" + c.properties[i].default_value.xml_escape(true) + "\"";
  1054. }
  1055. const DocData::PropertyDoc &p = c.properties[i];
  1056. if (c.properties[i].overridden) {
  1057. _write_string(f, 2, "<member name=\"" + p.name + "\" type=\"" + p.type + "\" setter=\"" + p.setter + "\" getter=\"" + p.getter + "\" override=\"true\"" + additional_attributes + " />");
  1058. } else {
  1059. _write_string(f, 2, "<member name=\"" + p.name + "\" type=\"" + p.type + "\" setter=\"" + p.setter + "\" getter=\"" + p.getter + "\"" + additional_attributes + ">");
  1060. _write_string(f, 3, p.description.strip_edges().xml_escape());
  1061. _write_string(f, 2, "</member>");
  1062. }
  1063. }
  1064. _write_string(f, 1, "</members>");
  1065. }
  1066. if (c.signals.size()) {
  1067. c.signals.sort();
  1068. _write_string(f, 1, "<signals>");
  1069. for (int i = 0; i < c.signals.size(); i++) {
  1070. const DocData::MethodDoc &m = c.signals[i];
  1071. _write_string(f, 2, "<signal name=\"" + m.name + "\">");
  1072. for (int j = 0; j < m.arguments.size(); j++) {
  1073. const DocData::ArgumentDoc &a = m.arguments[j];
  1074. _write_string(f, 3, "<argument index=\"" + itos(j) + "\" name=\"" + a.name.xml_escape() + "\" type=\"" + a.type.xml_escape() + "\">");
  1075. _write_string(f, 3, "</argument>");
  1076. }
  1077. _write_string(f, 3, "<description>");
  1078. _write_string(f, 4, m.description.strip_edges().xml_escape());
  1079. _write_string(f, 3, "</description>");
  1080. _write_string(f, 2, "</signal>");
  1081. }
  1082. _write_string(f, 1, "</signals>");
  1083. }
  1084. _write_string(f, 1, "<constants>");
  1085. for (int i = 0; i < c.constants.size(); i++) {
  1086. const DocData::ConstantDoc &k = c.constants[i];
  1087. if (k.is_value_valid) {
  1088. if (k.enumeration != String()) {
  1089. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"" + k.value + "\" enum=\"" + k.enumeration + "\">");
  1090. } else {
  1091. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"" + k.value + "\">");
  1092. }
  1093. } else {
  1094. if (k.enumeration != String()) {
  1095. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"platform-dependent\" enum=\"" + k.enumeration + "\">");
  1096. } else {
  1097. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"platform-dependent\">");
  1098. }
  1099. }
  1100. _write_string(f, 3, k.description.strip_edges().xml_escape());
  1101. _write_string(f, 2, "</constant>");
  1102. }
  1103. _write_string(f, 1, "</constants>");
  1104. if (c.theme_properties.size()) {
  1105. c.theme_properties.sort();
  1106. _write_string(f, 1, "<theme_items>");
  1107. for (int i = 0; i < c.theme_properties.size(); i++) {
  1108. const DocData::PropertyDoc &p = c.theme_properties[i];
  1109. if (p.default_value != "") {
  1110. _write_string(f, 2, "<theme_item name=\"" + p.name + "\" type=\"" + p.type + "\" default=\"" + p.default_value.xml_escape(true) + "\">");
  1111. } else {
  1112. _write_string(f, 2, "<theme_item name=\"" + p.name + "\" type=\"" + p.type + "\">");
  1113. }
  1114. _write_string(f, 3, p.description.strip_edges().xml_escape());
  1115. _write_string(f, 2, "</theme_item>");
  1116. }
  1117. _write_string(f, 1, "</theme_items>");
  1118. }
  1119. _write_string(f, 0, "</class>");
  1120. }
  1121. return OK;
  1122. }
  1123. Error DocTools::load_compressed(const uint8_t *p_data, int p_compressed_size, int p_uncompressed_size) {
  1124. Vector<uint8_t> data;
  1125. data.resize(p_uncompressed_size);
  1126. Compression::decompress(data.ptrw(), p_uncompressed_size, p_data, p_compressed_size, Compression::MODE_DEFLATE);
  1127. class_list.clear();
  1128. Ref<XMLParser> parser = memnew(XMLParser);
  1129. Error err = parser->open_buffer(data);
  1130. if (err) {
  1131. return err;
  1132. }
  1133. _load(parser);
  1134. return OK;
  1135. }