doc_tools.cpp 52 KB

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