doc_tools.cpp 46 KB

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