doc_tools.cpp 55 KB

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