doc_tools.cpp 49 KB

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