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) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 = DocData::get_default_value_string(default_value);
  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 = DocData::get_default_value_string(ThemeDB::get_singleton()->get_default_theme()->get_color(E, cname));
  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 || i == Variant::STRING_NAME) && 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 || i == Variant::STRING_NAME) {
  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. mi.return_val.usage = Variant::get_indexed_element_usage(Variant::Type(i));
  646. PropertyInfo arg;
  647. arg.name = "index";
  648. arg.type = Variant::INT;
  649. mi.arguments.push_back(arg);
  650. method_list.push_back(mi);
  651. }
  652. for (const MethodInfo &mi : method_list) {
  653. DocData::MethodDoc method;
  654. method.name = mi.name;
  655. for (int j = 0; j < mi.arguments.size(); j++) {
  656. PropertyInfo arginfo = mi.arguments[j];
  657. DocData::ArgumentDoc ad;
  658. DocData::argument_doc_from_arginfo(ad, mi.arguments[j]);
  659. ad.name = arginfo.name;
  660. int darg_idx = mi.default_arguments.size() - mi.arguments.size() + j;
  661. if (darg_idx >= 0) {
  662. ad.default_value = DocData::get_default_value_string(mi.default_arguments[darg_idx]);
  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 = DocData::get_default_value_string(v.get(pi.name));
  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. cd.is_bitfield = CoreConstants::is_global_constant_bitfield(i);
  724. if (!CoreConstants::get_ignore_value_in_docs(i)) {
  725. cd.value = itos(CoreConstants::get_global_constant_value(i));
  726. cd.is_value_valid = true;
  727. } else {
  728. cd.is_value_valid = false;
  729. }
  730. cd.enumeration = CoreConstants::get_global_constant_enum(i);
  731. c.constants.push_back(cd);
  732. }
  733. // Servers/engine singletons.
  734. List<Engine::Singleton> singletons;
  735. Engine::get_singleton()->get_singletons(&singletons);
  736. // FIXME: this is kind of hackish...
  737. for (const Engine::Singleton &s : singletons) {
  738. DocData::PropertyDoc pd;
  739. if (!s.ptr) {
  740. continue;
  741. }
  742. pd.name = s.name;
  743. pd.type = s.ptr->get_class();
  744. while (String(ClassDB::get_parent_class(pd.type)) != "Object") {
  745. pd.type = ClassDB::get_parent_class(pd.type);
  746. }
  747. c.properties.push_back(pd);
  748. }
  749. // Variant utility functions.
  750. List<StringName> utility_functions;
  751. Variant::get_utility_function_list(&utility_functions);
  752. utility_functions.sort_custom<StringName::AlphCompare>();
  753. for (const StringName &E : utility_functions) {
  754. DocData::MethodDoc md;
  755. md.name = E;
  756. // Utility function's return type.
  757. if (Variant::has_utility_function_return_value(E)) {
  758. PropertyInfo pi;
  759. pi.type = Variant::get_utility_function_return_type(E);
  760. if (pi.type == Variant::NIL) {
  761. pi.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  762. }
  763. DocData::ArgumentDoc ad;
  764. DocData::argument_doc_from_arginfo(ad, pi);
  765. md.return_type = ad.type;
  766. }
  767. // Utility function's arguments.
  768. if (Variant::is_utility_function_vararg(E)) {
  769. md.qualifiers = "vararg";
  770. } else {
  771. for (int i = 0; i < Variant::get_utility_function_argument_count(E); i++) {
  772. PropertyInfo pi;
  773. pi.type = Variant::get_utility_function_argument_type(E, i);
  774. pi.name = Variant::get_utility_function_argument_name(E, i);
  775. if (pi.type == Variant::NIL) {
  776. pi.usage = PROPERTY_USAGE_NIL_IS_VARIANT;
  777. }
  778. DocData::ArgumentDoc ad;
  779. DocData::argument_doc_from_arginfo(ad, pi);
  780. md.arguments.push_back(ad);
  781. }
  782. }
  783. c.methods.push_back(md);
  784. }
  785. }
  786. // Add scripting language built-ins.
  787. {
  788. // We only add a doc entry for languages which actually define any built-in
  789. // methods, constants, or annotations.
  790. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  791. ScriptLanguage *lang = ScriptServer::get_language(i);
  792. String cname = "@" + lang->get_name();
  793. DocData::ClassDoc c;
  794. c.name = cname;
  795. // Get functions.
  796. List<MethodInfo> minfo;
  797. lang->get_public_functions(&minfo);
  798. for (const MethodInfo &mi : minfo) {
  799. DocData::MethodDoc md;
  800. md.name = mi.name;
  801. if (mi.flags & METHOD_FLAG_VARARG) {
  802. if (!md.qualifiers.is_empty()) {
  803. md.qualifiers += " ";
  804. }
  805. md.qualifiers += "vararg";
  806. }
  807. DocData::return_doc_from_retinfo(md, mi.return_val);
  808. for (int j = 0; j < mi.arguments.size(); j++) {
  809. DocData::ArgumentDoc ad;
  810. DocData::argument_doc_from_arginfo(ad, mi.arguments[j]);
  811. int darg_idx = j - (mi.arguments.size() - mi.default_arguments.size());
  812. if (darg_idx >= 0) {
  813. ad.default_value = DocData::get_default_value_string(mi.default_arguments[darg_idx]);
  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. ad.default_value = DocData::get_default_value_string(ai.default_arguments[darg_idx]);
  848. }
  849. atd.arguments.push_back(ad);
  850. }
  851. c.annotations.push_back(atd);
  852. }
  853. // Skip adding the lang if it doesn't expose anything (e.g. C#).
  854. if (c.methods.is_empty() && c.constants.is_empty() && c.annotations.is_empty()) {
  855. continue;
  856. }
  857. class_list[cname] = c;
  858. }
  859. }
  860. }
  861. static Error _parse_methods(Ref<XMLParser> &parser, Vector<DocData::MethodDoc> &methods) {
  862. String section = parser->get_node_name();
  863. String element = section.substr(0, section.length() - 1);
  864. while (parser->read() == OK) {
  865. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  866. if (parser->get_node_name() == element) {
  867. DocData::MethodDoc method;
  868. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  869. method.name = parser->get_named_attribute_value("name");
  870. if (parser->has_attribute("qualifiers")) {
  871. method.qualifiers = parser->get_named_attribute_value("qualifiers");
  872. }
  873. if (parser->has_attribute("is_deprecated")) {
  874. method.is_deprecated = parser->get_named_attribute_value("is_deprecated").to_lower() == "true";
  875. }
  876. if (parser->has_attribute("is_experimental")) {
  877. method.is_experimental = parser->get_named_attribute_value("is_experimental").to_lower() == "true";
  878. }
  879. while (parser->read() == OK) {
  880. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  881. String name = parser->get_node_name();
  882. if (name == "return") {
  883. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  884. method.return_type = parser->get_named_attribute_value("type");
  885. if (parser->has_attribute("enum")) {
  886. method.return_enum = parser->get_named_attribute_value("enum");
  887. }
  888. } else if (name == "returns_error") {
  889. ERR_FAIL_COND_V(!parser->has_attribute("number"), ERR_FILE_CORRUPT);
  890. method.errors_returned.push_back(parser->get_named_attribute_value("number").to_int());
  891. } else if (name == "param") {
  892. DocData::ArgumentDoc argument;
  893. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  894. argument.name = parser->get_named_attribute_value("name");
  895. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  896. argument.type = parser->get_named_attribute_value("type");
  897. if (parser->has_attribute("enum")) {
  898. argument.enumeration = parser->get_named_attribute_value("enum");
  899. }
  900. method.arguments.push_back(argument);
  901. } else if (name == "description") {
  902. parser->read();
  903. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  904. method.description = parser->get_node_data();
  905. }
  906. }
  907. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == element) {
  908. break;
  909. }
  910. }
  911. methods.push_back(method);
  912. } else {
  913. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + parser->get_node_name() + ", expected " + element + ".");
  914. }
  915. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == section) {
  916. break;
  917. }
  918. }
  919. return OK;
  920. }
  921. Error DocTools::load_classes(const String &p_dir) {
  922. Error err;
  923. Ref<DirAccess> da = DirAccess::open(p_dir, &err);
  924. if (da.is_null()) {
  925. return err;
  926. }
  927. da->list_dir_begin();
  928. String path;
  929. path = da->get_next();
  930. while (!path.is_empty()) {
  931. if (!da->current_is_dir() && path.ends_with("xml")) {
  932. Ref<XMLParser> parser = memnew(XMLParser);
  933. Error err2 = parser->open(p_dir.path_join(path));
  934. if (err2) {
  935. return err2;
  936. }
  937. _load(parser);
  938. }
  939. path = da->get_next();
  940. }
  941. da->list_dir_end();
  942. return OK;
  943. }
  944. Error DocTools::erase_classes(const String &p_dir) {
  945. Error err;
  946. Ref<DirAccess> da = DirAccess::open(p_dir, &err);
  947. if (da.is_null()) {
  948. return err;
  949. }
  950. List<String> to_erase;
  951. da->list_dir_begin();
  952. String path;
  953. path = da->get_next();
  954. while (!path.is_empty()) {
  955. if (!da->current_is_dir() && path.ends_with("xml")) {
  956. to_erase.push_back(path);
  957. }
  958. path = da->get_next();
  959. }
  960. da->list_dir_end();
  961. while (to_erase.size()) {
  962. da->remove(to_erase.front()->get());
  963. to_erase.pop_front();
  964. }
  965. return OK;
  966. }
  967. Error DocTools::_load(Ref<XMLParser> parser) {
  968. Error err = OK;
  969. while ((err = parser->read()) == OK) {
  970. if (parser->get_node_type() == XMLParser::NODE_ELEMENT && parser->get_node_name() == "?xml") {
  971. parser->skip_section();
  972. }
  973. if (parser->get_node_type() != XMLParser::NODE_ELEMENT) {
  974. continue; //no idea what this may be, but skipping anyway
  975. }
  976. ERR_FAIL_COND_V(parser->get_node_name() != "class", ERR_FILE_CORRUPT);
  977. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  978. String name = parser->get_named_attribute_value("name");
  979. class_list[name] = DocData::ClassDoc();
  980. DocData::ClassDoc &c = class_list[name];
  981. c.name = name;
  982. if (parser->has_attribute("inherits")) {
  983. c.inherits = parser->get_named_attribute_value("inherits");
  984. }
  985. if (parser->has_attribute("is_deprecated")) {
  986. c.is_deprecated = parser->get_named_attribute_value("is_deprecated").to_lower() == "true";
  987. }
  988. if (parser->has_attribute("is_experimental")) {
  989. c.is_experimental = parser->get_named_attribute_value("is_experimental").to_lower() == "true";
  990. }
  991. while (parser->read() == OK) {
  992. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  993. String name2 = parser->get_node_name();
  994. if (name2 == "brief_description") {
  995. parser->read();
  996. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  997. c.brief_description = parser->get_node_data();
  998. }
  999. } else if (name2 == "description") {
  1000. parser->read();
  1001. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1002. c.description = parser->get_node_data();
  1003. }
  1004. } else if (name2 == "tutorials") {
  1005. while (parser->read() == OK) {
  1006. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1007. String name3 = parser->get_node_name();
  1008. if (name3 == "link") {
  1009. DocData::TutorialDoc tutorial;
  1010. if (parser->has_attribute("title")) {
  1011. tutorial.title = parser->get_named_attribute_value("title");
  1012. }
  1013. parser->read();
  1014. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1015. tutorial.link = parser->get_node_data().strip_edges();
  1016. c.tutorials.push_back(tutorial);
  1017. }
  1018. } else {
  1019. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1020. }
  1021. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "tutorials") {
  1022. break; // End of <tutorials>.
  1023. }
  1024. }
  1025. } else if (name2 == "constructors") {
  1026. Error err2 = _parse_methods(parser, c.constructors);
  1027. ERR_FAIL_COND_V(err2, err2);
  1028. } else if (name2 == "methods") {
  1029. Error err2 = _parse_methods(parser, c.methods);
  1030. ERR_FAIL_COND_V(err2, err2);
  1031. } else if (name2 == "operators") {
  1032. Error err2 = _parse_methods(parser, c.operators);
  1033. ERR_FAIL_COND_V(err2, err2);
  1034. } else if (name2 == "signals") {
  1035. Error err2 = _parse_methods(parser, c.signals);
  1036. ERR_FAIL_COND_V(err2, err2);
  1037. } else if (name2 == "annotations") {
  1038. Error err2 = _parse_methods(parser, c.annotations);
  1039. ERR_FAIL_COND_V(err2, err2);
  1040. } else if (name2 == "members") {
  1041. while (parser->read() == OK) {
  1042. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1043. String name3 = parser->get_node_name();
  1044. if (name3 == "member") {
  1045. DocData::PropertyDoc prop2;
  1046. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  1047. prop2.name = parser->get_named_attribute_value("name");
  1048. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  1049. prop2.type = parser->get_named_attribute_value("type");
  1050. if (parser->has_attribute("setter")) {
  1051. prop2.setter = parser->get_named_attribute_value("setter");
  1052. }
  1053. if (parser->has_attribute("getter")) {
  1054. prop2.getter = parser->get_named_attribute_value("getter");
  1055. }
  1056. if (parser->has_attribute("enum")) {
  1057. prop2.enumeration = parser->get_named_attribute_value("enum");
  1058. }
  1059. if (parser->has_attribute("is_deprecated")) {
  1060. prop2.is_deprecated = parser->get_named_attribute_value("is_deprecated").to_lower() == "true";
  1061. }
  1062. if (parser->has_attribute("is_experimental")) {
  1063. prop2.is_experimental = parser->get_named_attribute_value("is_experimental").to_lower() == "true";
  1064. }
  1065. if (!parser->is_empty()) {
  1066. parser->read();
  1067. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1068. prop2.description = parser->get_node_data();
  1069. }
  1070. }
  1071. c.properties.push_back(prop2);
  1072. } else {
  1073. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1074. }
  1075. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "members") {
  1076. break; // End of <members>.
  1077. }
  1078. }
  1079. } else if (name2 == "theme_items") {
  1080. while (parser->read() == OK) {
  1081. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1082. String name3 = parser->get_node_name();
  1083. if (name3 == "theme_item") {
  1084. DocData::ThemeItemDoc prop2;
  1085. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  1086. prop2.name = parser->get_named_attribute_value("name");
  1087. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  1088. prop2.type = parser->get_named_attribute_value("type");
  1089. ERR_FAIL_COND_V(!parser->has_attribute("data_type"), ERR_FILE_CORRUPT);
  1090. prop2.data_type = parser->get_named_attribute_value("data_type");
  1091. if (!parser->is_empty()) {
  1092. parser->read();
  1093. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1094. prop2.description = parser->get_node_data();
  1095. }
  1096. }
  1097. c.theme_properties.push_back(prop2);
  1098. } else {
  1099. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1100. }
  1101. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "theme_items") {
  1102. break; // End of <theme_items>.
  1103. }
  1104. }
  1105. } else if (name2 == "constants") {
  1106. while (parser->read() == OK) {
  1107. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  1108. String name3 = parser->get_node_name();
  1109. if (name3 == "constant") {
  1110. DocData::ConstantDoc constant2;
  1111. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  1112. constant2.name = parser->get_named_attribute_value("name");
  1113. ERR_FAIL_COND_V(!parser->has_attribute("value"), ERR_FILE_CORRUPT);
  1114. constant2.value = parser->get_named_attribute_value("value");
  1115. constant2.is_value_valid = true;
  1116. if (parser->has_attribute("enum")) {
  1117. constant2.enumeration = parser->get_named_attribute_value("enum");
  1118. }
  1119. if (parser->has_attribute("is_bitfield")) {
  1120. constant2.is_bitfield = parser->get_named_attribute_value("is_bitfield").to_lower() == "true";
  1121. }
  1122. if (parser->has_attribute("is_deprecated")) {
  1123. constant2.is_deprecated = parser->get_named_attribute_value("is_deprecated").to_lower() == "true";
  1124. }
  1125. if (parser->has_attribute("is_experimental")) {
  1126. constant2.is_experimental = parser->get_named_attribute_value("is_experimental").to_lower() == "true";
  1127. }
  1128. if (!parser->is_empty()) {
  1129. parser->read();
  1130. if (parser->get_node_type() == XMLParser::NODE_TEXT) {
  1131. constant2.description = parser->get_node_data();
  1132. }
  1133. }
  1134. c.constants.push_back(constant2);
  1135. } else {
  1136. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  1137. }
  1138. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "constants") {
  1139. break; // End of <constants>.
  1140. }
  1141. }
  1142. } else {
  1143. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name2 + ".");
  1144. }
  1145. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "class") {
  1146. break; // End of <class>.
  1147. }
  1148. }
  1149. }
  1150. return OK;
  1151. }
  1152. static void _write_string(Ref<FileAccess> f, int p_tablevel, const String &p_string) {
  1153. if (p_string.is_empty()) {
  1154. return;
  1155. }
  1156. String tab;
  1157. for (int i = 0; i < p_tablevel; i++) {
  1158. tab += "\t";
  1159. }
  1160. f->store_string(tab + p_string + "\n");
  1161. }
  1162. static void _write_method_doc(Ref<FileAccess> f, const String &p_name, Vector<DocData::MethodDoc> &p_method_docs) {
  1163. if (!p_method_docs.is_empty()) {
  1164. p_method_docs.sort();
  1165. _write_string(f, 1, "<" + p_name + "s>");
  1166. for (int i = 0; i < p_method_docs.size(); i++) {
  1167. const DocData::MethodDoc &m = p_method_docs[i];
  1168. String qualifiers;
  1169. if (!m.qualifiers.is_empty()) {
  1170. qualifiers += " qualifiers=\"" + m.qualifiers.xml_escape() + "\"";
  1171. }
  1172. String additional_attributes;
  1173. if (m.is_deprecated) {
  1174. additional_attributes += " is_deprecated=\"true\"";
  1175. }
  1176. if (m.is_experimental) {
  1177. additional_attributes += " is_experimental=\"true\"";
  1178. }
  1179. _write_string(f, 2, "<" + p_name + " name=\"" + m.name.xml_escape() + "\"" + qualifiers + additional_attributes + ">");
  1180. if (!m.return_type.is_empty()) {
  1181. String enum_text;
  1182. if (!m.return_enum.is_empty()) {
  1183. enum_text = " enum=\"" + m.return_enum + "\"";
  1184. }
  1185. _write_string(f, 3, "<return type=\"" + m.return_type + "\"" + enum_text + " />");
  1186. }
  1187. if (m.errors_returned.size() > 0) {
  1188. for (int j = 0; j < m.errors_returned.size(); j++) {
  1189. _write_string(f, 3, "<returns_error number=\"" + itos(m.errors_returned[j]) + "\"/>");
  1190. }
  1191. }
  1192. for (int j = 0; j < m.arguments.size(); j++) {
  1193. const DocData::ArgumentDoc &a = m.arguments[j];
  1194. String enum_text;
  1195. if (!a.enumeration.is_empty()) {
  1196. enum_text = " enum=\"" + a.enumeration + "\"";
  1197. }
  1198. if (!a.default_value.is_empty()) {
  1199. _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) + "\" />");
  1200. } else {
  1201. _write_string(f, 3, "<param index=\"" + itos(j) + "\" name=\"" + a.name.xml_escape() + "\" type=\"" + a.type.xml_escape() + "\"" + enum_text + " />");
  1202. }
  1203. }
  1204. _write_string(f, 3, "<description>");
  1205. _write_string(f, 4, _translate_doc_string(m.description).strip_edges().xml_escape());
  1206. _write_string(f, 3, "</description>");
  1207. _write_string(f, 2, "</" + p_name + ">");
  1208. }
  1209. _write_string(f, 1, "</" + p_name + "s>");
  1210. }
  1211. }
  1212. Error DocTools::save_classes(const String &p_default_path, const HashMap<String, String> &p_class_path) {
  1213. for (KeyValue<String, DocData::ClassDoc> &E : class_list) {
  1214. DocData::ClassDoc &c = E.value;
  1215. String save_path;
  1216. if (p_class_path.has(c.name)) {
  1217. save_path = p_class_path[c.name];
  1218. } else {
  1219. save_path = p_default_path;
  1220. }
  1221. Error err;
  1222. String save_file = save_path.path_join(c.name + ".xml");
  1223. Ref<FileAccess> f = FileAccess::open(save_file, FileAccess::WRITE, &err);
  1224. ERR_CONTINUE_MSG(err != OK, "Can't write doc file: " + save_file + ".");
  1225. _write_string(f, 0, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
  1226. String header = "<class name=\"" + c.name + "\"";
  1227. if (!c.inherits.is_empty()) {
  1228. header += " inherits=\"" + c.inherits + "\"";
  1229. if (c.is_deprecated) {
  1230. header += " is_deprecated=\"true\"";
  1231. }
  1232. if (c.is_experimental) {
  1233. header += " is_experimental=\"true\"";
  1234. }
  1235. }
  1236. header += String(" version=\"") + VERSION_BRANCH + "\"";
  1237. // Reference the XML schema so editors can provide error checking.
  1238. // Modules are nested deep, so change the path to reference the same schema everywhere.
  1239. const String schema_path = save_path.find("modules/") != -1 ? "../../../doc/class.xsd" : "../class.xsd";
  1240. header += vformat(
  1241. R"( xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="%s">)",
  1242. schema_path);
  1243. _write_string(f, 0, header);
  1244. _write_string(f, 1, "<brief_description>");
  1245. _write_string(f, 2, _translate_doc_string(c.brief_description).strip_edges().xml_escape());
  1246. _write_string(f, 1, "</brief_description>");
  1247. _write_string(f, 1, "<description>");
  1248. _write_string(f, 2, _translate_doc_string(c.description).strip_edges().xml_escape());
  1249. _write_string(f, 1, "</description>");
  1250. _write_string(f, 1, "<tutorials>");
  1251. for (int i = 0; i < c.tutorials.size(); i++) {
  1252. DocData::TutorialDoc tutorial = c.tutorials.get(i);
  1253. String title_attribute = (!tutorial.title.is_empty()) ? " title=\"" + _translate_doc_string(tutorial.title).xml_escape() + "\"" : "";
  1254. _write_string(f, 2, "<link" + title_attribute + ">" + tutorial.link.xml_escape() + "</link>");
  1255. }
  1256. _write_string(f, 1, "</tutorials>");
  1257. _write_method_doc(f, "constructor", c.constructors);
  1258. _write_method_doc(f, "method", c.methods);
  1259. if (!c.properties.is_empty()) {
  1260. _write_string(f, 1, "<members>");
  1261. c.properties.sort();
  1262. for (int i = 0; i < c.properties.size(); i++) {
  1263. String additional_attributes;
  1264. if (!c.properties[i].enumeration.is_empty()) {
  1265. additional_attributes += " enum=\"" + c.properties[i].enumeration + "\"";
  1266. }
  1267. if (!c.properties[i].default_value.is_empty()) {
  1268. additional_attributes += " default=\"" + c.properties[i].default_value.xml_escape(true) + "\"";
  1269. }
  1270. if (c.properties[i].is_deprecated) {
  1271. additional_attributes += " is_deprecated=\"true\"";
  1272. }
  1273. if (c.properties[i].is_experimental) {
  1274. additional_attributes += " is_experimental=\"true\"";
  1275. }
  1276. const DocData::PropertyDoc &p = c.properties[i];
  1277. if (c.properties[i].overridden) {
  1278. _write_string(f, 2, "<member name=\"" + p.name + "\" type=\"" + p.type + "\" setter=\"" + p.setter + "\" getter=\"" + p.getter + "\" overrides=\"" + p.overrides + "\"" + additional_attributes + " />");
  1279. } else {
  1280. _write_string(f, 2, "<member name=\"" + p.name + "\" type=\"" + p.type + "\" setter=\"" + p.setter + "\" getter=\"" + p.getter + "\"" + additional_attributes + ">");
  1281. _write_string(f, 3, _translate_doc_string(p.description).strip_edges().xml_escape());
  1282. _write_string(f, 2, "</member>");
  1283. }
  1284. }
  1285. _write_string(f, 1, "</members>");
  1286. }
  1287. _write_method_doc(f, "signal", c.signals);
  1288. if (!c.constants.is_empty()) {
  1289. _write_string(f, 1, "<constants>");
  1290. for (int i = 0; i < c.constants.size(); i++) {
  1291. const DocData::ConstantDoc &k = c.constants[i];
  1292. String additional_attributes;
  1293. if (c.constants[i].is_deprecated) {
  1294. additional_attributes += " is_deprecated=\"true\"";
  1295. }
  1296. if (c.constants[i].is_experimental) {
  1297. additional_attributes += " is_experimental=\"true\"";
  1298. }
  1299. if (k.is_value_valid) {
  1300. if (!k.enumeration.is_empty()) {
  1301. if (k.is_bitfield) {
  1302. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"" + k.value + "\" enum=\"" + k.enumeration + "\" is_bitfield=\"true\"" + additional_attributes + ">");
  1303. } else {
  1304. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"" + k.value + "\" enum=\"" + k.enumeration + "\"" + additional_attributes + ">");
  1305. }
  1306. } else {
  1307. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"" + k.value + "\"" + additional_attributes + ">");
  1308. }
  1309. } else {
  1310. if (!k.enumeration.is_empty()) {
  1311. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"platform-dependent\" enum=\"" + k.enumeration + "\"" + additional_attributes + ">");
  1312. } else {
  1313. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"platform-dependent\"" + additional_attributes + ">");
  1314. }
  1315. }
  1316. _write_string(f, 3, _translate_doc_string(k.description).strip_edges().xml_escape());
  1317. _write_string(f, 2, "</constant>");
  1318. }
  1319. _write_string(f, 1, "</constants>");
  1320. }
  1321. _write_method_doc(f, "annotation", c.annotations);
  1322. if (!c.theme_properties.is_empty()) {
  1323. c.theme_properties.sort();
  1324. _write_string(f, 1, "<theme_items>");
  1325. for (int i = 0; i < c.theme_properties.size(); i++) {
  1326. const DocData::ThemeItemDoc &ti = c.theme_properties[i];
  1327. if (!ti.default_value.is_empty()) {
  1328. _write_string(f, 2, "<theme_item name=\"" + ti.name + "\" data_type=\"" + ti.data_type + "\" type=\"" + ti.type + "\" default=\"" + ti.default_value.xml_escape(true) + "\">");
  1329. } else {
  1330. _write_string(f, 2, "<theme_item name=\"" + ti.name + "\" data_type=\"" + ti.data_type + "\" type=\"" + ti.type + "\">");
  1331. }
  1332. _write_string(f, 3, _translate_doc_string(ti.description).strip_edges().xml_escape());
  1333. _write_string(f, 2, "</theme_item>");
  1334. }
  1335. _write_string(f, 1, "</theme_items>");
  1336. }
  1337. _write_method_doc(f, "operator", c.operators);
  1338. _write_string(f, 0, "</class>");
  1339. }
  1340. return OK;
  1341. }
  1342. Error DocTools::load_compressed(const uint8_t *p_data, int p_compressed_size, int p_uncompressed_size) {
  1343. Vector<uint8_t> data;
  1344. data.resize(p_uncompressed_size);
  1345. int ret = Compression::decompress(data.ptrw(), p_uncompressed_size, p_data, p_compressed_size, Compression::MODE_DEFLATE);
  1346. ERR_FAIL_COND_V_MSG(ret == -1, ERR_FILE_CORRUPT, "Compressed file is corrupt.");
  1347. class_list.clear();
  1348. Ref<XMLParser> parser = memnew(XMLParser);
  1349. Error err = parser->open_buffer(data);
  1350. if (err) {
  1351. return err;
  1352. }
  1353. _load(parser);
  1354. return OK;
  1355. }