doc_tools.cpp 45 KB

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