doc_data.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. /*************************************************************************/
  2. /* doc_data.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_data.h"
  31. #include "core/engine.h"
  32. #include "core/global_constants.h"
  33. #include "core/io/compression.h"
  34. #include "core/io/marshalls.h"
  35. #include "core/os/dir_access.h"
  36. #include "core/project_settings.h"
  37. #include "core/script_language.h"
  38. #include "core/version.h"
  39. #include "scene/resources/theme.h"
  40. void DocData::merge_from(const DocData &p_data) {
  41. for (Map<String, ClassDoc>::Element *E = class_list.front(); E; E = E->next()) {
  42. ClassDoc &c = E->get();
  43. if (!p_data.class_list.has(c.name))
  44. continue;
  45. const ClassDoc &cf = p_data.class_list[c.name];
  46. c.description = cf.description;
  47. c.brief_description = cf.brief_description;
  48. c.tutorials = cf.tutorials;
  49. for (int i = 0; i < c.methods.size(); i++) {
  50. MethodDoc &m = c.methods.write[i];
  51. for (int j = 0; j < cf.methods.size(); j++) {
  52. if (cf.methods[j].name != m.name)
  53. continue;
  54. if (cf.methods[j].arguments.size() != m.arguments.size())
  55. continue;
  56. // since polymorphic functions are allowed we need to check the type of
  57. // the arguments so we make sure they are different.
  58. int arg_count = cf.methods[j].arguments.size();
  59. Vector<bool> arg_used;
  60. arg_used.resize(arg_count);
  61. for (int l = 0; l < arg_count; ++l)
  62. arg_used.write[l] = false;
  63. // also there is no guarantee that argument ordering will match, so we
  64. // have to check one by one so we make sure we have an exact match
  65. for (int k = 0; k < arg_count; ++k) {
  66. for (int l = 0; l < arg_count; ++l)
  67. if (cf.methods[j].arguments[k].type == m.arguments[l].type && !arg_used[l]) {
  68. arg_used.write[l] = true;
  69. break;
  70. }
  71. }
  72. bool not_the_same = false;
  73. for (int l = 0; l < arg_count; ++l)
  74. if (!arg_used[l]) // at least one of the arguments was different
  75. not_the_same = true;
  76. if (not_the_same)
  77. continue;
  78. const MethodDoc &mf = cf.methods[j];
  79. m.description = mf.description;
  80. break;
  81. }
  82. }
  83. for (int i = 0; i < c.signals.size(); i++) {
  84. MethodDoc &m = c.signals.write[i];
  85. for (int j = 0; j < cf.signals.size(); j++) {
  86. if (cf.signals[j].name != m.name)
  87. continue;
  88. const MethodDoc &mf = cf.signals[j];
  89. m.description = mf.description;
  90. break;
  91. }
  92. }
  93. for (int i = 0; i < c.constants.size(); i++) {
  94. ConstantDoc &m = c.constants.write[i];
  95. for (int j = 0; j < cf.constants.size(); j++) {
  96. if (cf.constants[j].name != m.name)
  97. continue;
  98. const ConstantDoc &mf = cf.constants[j];
  99. m.description = mf.description;
  100. break;
  101. }
  102. }
  103. for (int i = 0; i < c.properties.size(); i++) {
  104. PropertyDoc &p = c.properties.write[i];
  105. for (int j = 0; j < cf.properties.size(); j++) {
  106. if (cf.properties[j].name != p.name)
  107. continue;
  108. const PropertyDoc &pf = cf.properties[j];
  109. p.description = pf.description;
  110. break;
  111. }
  112. }
  113. for (int i = 0; i < c.theme_properties.size(); i++) {
  114. PropertyDoc &p = c.theme_properties.write[i];
  115. for (int j = 0; j < cf.theme_properties.size(); j++) {
  116. if (cf.theme_properties[j].name != p.name)
  117. continue;
  118. const PropertyDoc &pf = cf.theme_properties[j];
  119. p.description = pf.description;
  120. break;
  121. }
  122. }
  123. }
  124. }
  125. void DocData::remove_from(const DocData &p_data) {
  126. for (Map<String, ClassDoc>::Element *E = p_data.class_list.front(); E; E = E->next()) {
  127. if (class_list.has(E->key()))
  128. class_list.erase(E->key());
  129. }
  130. }
  131. static void return_doc_from_retinfo(DocData::MethodDoc &p_method, const PropertyInfo &p_retinfo) {
  132. if (p_retinfo.type == Variant::INT && p_retinfo.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  133. p_method.return_enum = p_retinfo.class_name;
  134. if (p_method.return_enum.begins_with("_")) //proxy class
  135. p_method.return_enum = p_method.return_enum.substr(1, p_method.return_enum.length());
  136. p_method.return_type = "int";
  137. } else if (p_retinfo.class_name != StringName()) {
  138. p_method.return_type = p_retinfo.class_name;
  139. } else if (p_retinfo.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  140. p_method.return_type = p_retinfo.hint_string;
  141. } else if (p_retinfo.type == Variant::NIL && p_retinfo.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
  142. p_method.return_type = "Variant";
  143. } else if (p_retinfo.type == Variant::NIL) {
  144. p_method.return_type = "void";
  145. } else {
  146. p_method.return_type = Variant::get_type_name(p_retinfo.type);
  147. }
  148. }
  149. static void argument_doc_from_arginfo(DocData::ArgumentDoc &p_argument, const PropertyInfo &p_arginfo) {
  150. p_argument.name = p_arginfo.name;
  151. if (p_arginfo.type == Variant::INT && p_arginfo.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  152. p_argument.enumeration = p_arginfo.class_name;
  153. if (p_argument.enumeration.begins_with("_")) //proxy class
  154. p_argument.enumeration = p_argument.enumeration.substr(1, p_argument.enumeration.length());
  155. p_argument.type = "int";
  156. } else if (p_arginfo.class_name != StringName()) {
  157. p_argument.type = p_arginfo.class_name;
  158. } else if (p_arginfo.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  159. p_argument.type = p_arginfo.hint_string;
  160. } else if (p_arginfo.type == Variant::NIL) {
  161. // Parameters cannot be void, so PROPERTY_USAGE_NIL_IS_VARIANT is not necessary
  162. p_argument.type = "Variant";
  163. } else {
  164. p_argument.type = Variant::get_type_name(p_arginfo.type);
  165. }
  166. }
  167. static Variant get_documentation_default_value(const StringName &p_class_name, const StringName &p_property_name, bool &r_default_value_valid) {
  168. Variant default_value = Variant();
  169. r_default_value_valid = false;
  170. if (ClassDB::can_instance(p_class_name)) {
  171. default_value = ClassDB::class_get_default_property_value(p_class_name, p_property_name, &r_default_value_valid);
  172. } else {
  173. // Cannot get default value of classes that can't be instanced
  174. List<StringName> inheriting_classes;
  175. ClassDB::get_direct_inheriters_from_class(p_class_name, &inheriting_classes);
  176. for (List<StringName>::Element *E2 = inheriting_classes.front(); E2; E2 = E2->next()) {
  177. if (ClassDB::can_instance(E2->get())) {
  178. default_value = ClassDB::class_get_default_property_value(E2->get(), p_property_name, &r_default_value_valid);
  179. if (r_default_value_valid)
  180. break;
  181. }
  182. }
  183. }
  184. return default_value;
  185. }
  186. void DocData::generate(bool p_basic_types) {
  187. List<StringName> classes;
  188. ClassDB::get_class_list(&classes);
  189. classes.sort_custom<StringName::AlphCompare>();
  190. // Move ProjectSettings, so that other classes can register properties there.
  191. classes.move_to_back(classes.find("ProjectSettings"));
  192. bool skip_setter_getter_methods = true;
  193. while (classes.size()) {
  194. Set<StringName> setters_getters;
  195. String name = classes.front()->get();
  196. String cname = name;
  197. if (cname.begins_with("_")) //proxy class
  198. cname = cname.substr(1, name.length());
  199. class_list[cname] = ClassDoc();
  200. ClassDoc &c = class_list[cname];
  201. c.name = cname;
  202. c.inherits = ClassDB::get_parent_class(name);
  203. List<PropertyInfo> properties;
  204. List<PropertyInfo> own_properties;
  205. if (name == "ProjectSettings") {
  206. //special case for project settings, so settings can be documented
  207. ProjectSettings::get_singleton()->get_property_list(&properties);
  208. own_properties = properties;
  209. } else {
  210. ClassDB::get_property_list(name, &properties);
  211. ClassDB::get_property_list(name, &own_properties, true);
  212. }
  213. List<PropertyInfo>::Element *EO = own_properties.front();
  214. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  215. bool inherited = EO == NULL;
  216. if (EO && EO->get() == E->get()) {
  217. inherited = false;
  218. EO = EO->next();
  219. }
  220. if (E->get().usage & PROPERTY_USAGE_GROUP || E->get().usage & PROPERTY_USAGE_CATEGORY || E->get().usage & PROPERTY_USAGE_INTERNAL)
  221. continue;
  222. PropertyDoc prop;
  223. prop.name = E->get().name;
  224. prop.overridden = inherited;
  225. bool default_value_valid = false;
  226. Variant default_value;
  227. if (name == "ProjectSettings") {
  228. // Special case for project settings, so that settings are not taken from the current project's settings
  229. if (E->get().name == "script" ||
  230. ProjectSettings::get_singleton()->get_order(E->get().name) >= ProjectSettings::NO_BUILTIN_ORDER_BASE) {
  231. continue;
  232. }
  233. if (E->get().usage & PROPERTY_USAGE_EDITOR) {
  234. default_value = ProjectSettings::get_singleton()->property_get_revert(E->get().name);
  235. default_value_valid = true;
  236. }
  237. } else {
  238. default_value = get_documentation_default_value(name, E->get().name, default_value_valid);
  239. if (inherited) {
  240. bool base_default_value_valid = false;
  241. Variant base_default_value = get_documentation_default_value(ClassDB::get_parent_class(name), E->get().name, base_default_value_valid);
  242. if (!default_value_valid || !base_default_value_valid || default_value == base_default_value)
  243. continue;
  244. }
  245. }
  246. //used to track uninitialized values using valgrind
  247. //print_line("getting default value for " + String(name) + "." + String(E->get().name));
  248. if (default_value_valid && default_value.get_type() != Variant::OBJECT) {
  249. prop.default_value = default_value.get_construct_string().replace("\n", "");
  250. }
  251. StringName setter = ClassDB::get_property_setter(name, E->get().name);
  252. StringName getter = ClassDB::get_property_getter(name, E->get().name);
  253. prop.setter = setter;
  254. prop.getter = getter;
  255. bool found_type = false;
  256. if (getter != StringName()) {
  257. MethodBind *mb = ClassDB::get_method(name, getter);
  258. if (mb) {
  259. PropertyInfo retinfo = mb->get_return_info();
  260. found_type = true;
  261. if (retinfo.type == Variant::INT && retinfo.usage & PROPERTY_USAGE_CLASS_IS_ENUM) {
  262. prop.enumeration = retinfo.class_name;
  263. prop.type = "int";
  264. } else if (retinfo.class_name != StringName()) {
  265. prop.type = retinfo.class_name;
  266. } else if (retinfo.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  267. prop.type = retinfo.hint_string;
  268. } else if (retinfo.type == Variant::NIL && retinfo.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
  269. prop.type = "Variant";
  270. } else if (retinfo.type == Variant::NIL) {
  271. prop.type = "void";
  272. } else {
  273. prop.type = Variant::get_type_name(retinfo.type);
  274. }
  275. }
  276. setters_getters.insert(getter);
  277. }
  278. if (setter != StringName()) {
  279. setters_getters.insert(setter);
  280. }
  281. if (!found_type) {
  282. if (E->get().type == Variant::OBJECT && E->get().hint == PROPERTY_HINT_RESOURCE_TYPE)
  283. prop.type = E->get().hint_string;
  284. else
  285. prop.type = Variant::get_type_name(E->get().type);
  286. }
  287. c.properties.push_back(prop);
  288. }
  289. List<MethodInfo> method_list;
  290. ClassDB::get_method_list(name, &method_list, true);
  291. method_list.sort();
  292. for (List<MethodInfo>::Element *E = method_list.front(); E; E = E->next()) {
  293. if (E->get().name == "" || (E->get().name[0] == '_' && !(E->get().flags & METHOD_FLAG_VIRTUAL)))
  294. continue; //hidden, don't count
  295. if (skip_setter_getter_methods && setters_getters.has(E->get().name)) {
  296. // Don't skip parametric setters and getters, i.e. method which require
  297. // one or more parameters to define what property should be set or retrieved.
  298. // E.g. CPUParticles::set_param(Parameter param, float value).
  299. if (E->get().arguments.size() == 0 /* getter */ || (E->get().arguments.size() == 1 && E->get().return_val.type == Variant::NIL /* setter */)) {
  300. continue;
  301. }
  302. }
  303. MethodDoc method;
  304. method.name = E->get().name;
  305. if (E->get().flags & METHOD_FLAG_VIRTUAL)
  306. method.qualifiers = "virtual";
  307. if (E->get().flags & METHOD_FLAG_CONST) {
  308. if (method.qualifiers != "")
  309. method.qualifiers += " ";
  310. method.qualifiers += "const";
  311. } else if (E->get().flags & METHOD_FLAG_VARARG) {
  312. if (method.qualifiers != "")
  313. method.qualifiers += " ";
  314. method.qualifiers += "vararg";
  315. }
  316. for (int i = -1; i < E->get().arguments.size(); i++) {
  317. if (i == -1) {
  318. #ifdef DEBUG_METHODS_ENABLED
  319. return_doc_from_retinfo(method, E->get().return_val);
  320. #endif
  321. } else {
  322. const PropertyInfo &arginfo = E->get().arguments[i];
  323. ArgumentDoc argument;
  324. argument_doc_from_arginfo(argument, arginfo);
  325. int darg_idx = i - (E->get().arguments.size() - E->get().default_arguments.size());
  326. if (darg_idx >= 0) {
  327. Variant default_arg = E->get().default_arguments[darg_idx];
  328. argument.default_value = default_arg.get_construct_string();
  329. }
  330. method.arguments.push_back(argument);
  331. }
  332. }
  333. c.methods.push_back(method);
  334. }
  335. List<MethodInfo> signal_list;
  336. ClassDB::get_signal_list(name, &signal_list, true);
  337. if (signal_list.size()) {
  338. for (List<MethodInfo>::Element *EV = signal_list.front(); EV; EV = EV->next()) {
  339. MethodDoc signal;
  340. signal.name = EV->get().name;
  341. for (int i = 0; i < EV->get().arguments.size(); i++) {
  342. const PropertyInfo &arginfo = EV->get().arguments[i];
  343. ArgumentDoc argument;
  344. argument_doc_from_arginfo(argument, arginfo);
  345. signal.arguments.push_back(argument);
  346. }
  347. c.signals.push_back(signal);
  348. }
  349. }
  350. List<String> constant_list;
  351. ClassDB::get_integer_constant_list(name, &constant_list, true);
  352. for (List<String>::Element *E = constant_list.front(); E; E = E->next()) {
  353. ConstantDoc constant;
  354. constant.name = E->get();
  355. constant.value = itos(ClassDB::get_integer_constant(name, E->get()));
  356. constant.enumeration = ClassDB::get_integer_constant_enum(name, E->get());
  357. c.constants.push_back(constant);
  358. }
  359. //theme stuff
  360. {
  361. List<StringName> l;
  362. Theme::get_default()->get_constant_list(cname, &l);
  363. for (List<StringName>::Element *E = l.front(); E; E = E->next()) {
  364. PropertyDoc pd;
  365. pd.name = E->get();
  366. pd.type = "int";
  367. pd.default_value = itos(Theme::get_default()->get_constant(E->get(), cname));
  368. c.theme_properties.push_back(pd);
  369. }
  370. l.clear();
  371. Theme::get_default()->get_color_list(cname, &l);
  372. for (List<StringName>::Element *E = l.front(); E; E = E->next()) {
  373. PropertyDoc pd;
  374. pd.name = E->get();
  375. pd.type = "Color";
  376. pd.default_value = Variant(Theme::get_default()->get_color(E->get(), cname)).get_construct_string();
  377. c.theme_properties.push_back(pd);
  378. }
  379. l.clear();
  380. Theme::get_default()->get_icon_list(cname, &l);
  381. for (List<StringName>::Element *E = l.front(); E; E = E->next()) {
  382. PropertyDoc pd;
  383. pd.name = E->get();
  384. pd.type = "Texture2D";
  385. c.theme_properties.push_back(pd);
  386. }
  387. l.clear();
  388. Theme::get_default()->get_font_list(cname, &l);
  389. for (List<StringName>::Element *E = l.front(); E; E = E->next()) {
  390. PropertyDoc pd;
  391. pd.name = E->get();
  392. pd.type = "Font";
  393. c.theme_properties.push_back(pd);
  394. }
  395. l.clear();
  396. Theme::get_default()->get_stylebox_list(cname, &l);
  397. for (List<StringName>::Element *E = l.front(); E; E = E->next()) {
  398. PropertyDoc pd;
  399. pd.name = E->get();
  400. pd.type = "StyleBox";
  401. c.theme_properties.push_back(pd);
  402. }
  403. }
  404. classes.pop_front();
  405. }
  406. {
  407. // So we can document the concept of Variant even if it's not a usable class per se.
  408. class_list["Variant"] = ClassDoc();
  409. class_list["Variant"].name = "Variant";
  410. }
  411. if (!p_basic_types)
  412. return;
  413. // Add Variant types.
  414. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  415. if (i == Variant::NIL)
  416. continue; // Not exposed outside of 'null', should not be in class list.
  417. if (i == Variant::OBJECT)
  418. continue; // Use the core type instead.
  419. String cname = Variant::get_type_name(Variant::Type(i));
  420. class_list[cname] = ClassDoc();
  421. ClassDoc &c = class_list[cname];
  422. c.name = cname;
  423. Variant::CallError cerror;
  424. Variant v = Variant::construct(Variant::Type(i), NULL, 0, cerror);
  425. List<MethodInfo> method_list;
  426. v.get_method_list(&method_list);
  427. method_list.sort();
  428. Variant::get_constructor_list(Variant::Type(i), &method_list);
  429. for (List<MethodInfo>::Element *E = method_list.front(); E; E = E->next()) {
  430. MethodInfo &mi = E->get();
  431. MethodDoc method;
  432. method.name = mi.name;
  433. for (int j = 0; j < mi.arguments.size(); j++) {
  434. PropertyInfo arginfo = mi.arguments[j];
  435. ArgumentDoc ad;
  436. argument_doc_from_arginfo(ad, mi.arguments[j]);
  437. ad.name = arginfo.name;
  438. int defarg = mi.default_arguments.size() - mi.arguments.size() + j;
  439. if (defarg >= 0)
  440. ad.default_value = mi.default_arguments[defarg];
  441. method.arguments.push_back(ad);
  442. }
  443. if (mi.return_val.type == Variant::NIL) {
  444. if (mi.return_val.name != "")
  445. method.return_type = "Variant";
  446. } else {
  447. method.return_type = Variant::get_type_name(mi.return_val.type);
  448. }
  449. c.methods.push_back(method);
  450. }
  451. List<PropertyInfo> properties;
  452. v.get_property_list(&properties);
  453. for (List<PropertyInfo>::Element *E = properties.front(); E; E = E->next()) {
  454. PropertyInfo pi = E->get();
  455. PropertyDoc property;
  456. property.name = pi.name;
  457. property.type = Variant::get_type_name(pi.type);
  458. property.default_value = v.get(pi.name).get_construct_string();
  459. c.properties.push_back(property);
  460. }
  461. List<StringName> constants;
  462. Variant::get_constants_for_type(Variant::Type(i), &constants);
  463. for (List<StringName>::Element *E = constants.front(); E; E = E->next()) {
  464. ConstantDoc constant;
  465. constant.name = E->get();
  466. Variant value = Variant::get_constant_value(Variant::Type(i), E->get());
  467. constant.value = value.get_type() == Variant::INT ? itos(value) : value.get_construct_string();
  468. c.constants.push_back(constant);
  469. }
  470. }
  471. //built in constants and functions
  472. {
  473. String cname = "@GlobalScope";
  474. class_list[cname] = ClassDoc();
  475. ClassDoc &c = class_list[cname];
  476. c.name = cname;
  477. for (int i = 0; i < GlobalConstants::get_global_constant_count(); i++) {
  478. ConstantDoc cd;
  479. cd.name = GlobalConstants::get_global_constant_name(i);
  480. cd.value = itos(GlobalConstants::get_global_constant_value(i));
  481. cd.enumeration = GlobalConstants::get_global_constant_enum(i);
  482. c.constants.push_back(cd);
  483. }
  484. List<Engine::Singleton> singletons;
  485. Engine::get_singleton()->get_singletons(&singletons);
  486. //servers (this is kind of hackish)
  487. for (List<Engine::Singleton>::Element *E = singletons.front(); E; E = E->next()) {
  488. PropertyDoc pd;
  489. Engine::Singleton &s = E->get();
  490. if (!s.ptr) {
  491. continue;
  492. }
  493. pd.name = s.name;
  494. pd.type = s.ptr->get_class();
  495. while (String(ClassDB::get_parent_class(pd.type)) != "Object")
  496. pd.type = ClassDB::get_parent_class(pd.type);
  497. if (pd.type.begins_with("_"))
  498. pd.type = pd.type.substr(1, pd.type.length());
  499. c.properties.push_back(pd);
  500. }
  501. }
  502. //built in script reference
  503. {
  504. for (int i = 0; i < ScriptServer::get_language_count(); i++) {
  505. ScriptLanguage *lang = ScriptServer::get_language(i);
  506. String cname = "@" + lang->get_name();
  507. class_list[cname] = ClassDoc();
  508. ClassDoc &c = class_list[cname];
  509. c.name = cname;
  510. List<MethodInfo> minfo;
  511. lang->get_public_functions(&minfo);
  512. for (List<MethodInfo>::Element *E = minfo.front(); E; E = E->next()) {
  513. MethodInfo &mi = E->get();
  514. MethodDoc md;
  515. md.name = mi.name;
  516. if (mi.flags & METHOD_FLAG_VARARG) {
  517. if (md.qualifiers != "")
  518. md.qualifiers += " ";
  519. md.qualifiers += "vararg";
  520. }
  521. return_doc_from_retinfo(md, mi.return_val);
  522. for (int j = 0; j < mi.arguments.size(); j++) {
  523. ArgumentDoc ad;
  524. argument_doc_from_arginfo(ad, mi.arguments[j]);
  525. int darg_idx = j - (mi.arguments.size() - mi.default_arguments.size());
  526. if (darg_idx >= 0) {
  527. Variant default_arg = E->get().default_arguments[darg_idx];
  528. ad.default_value = default_arg.get_construct_string();
  529. }
  530. md.arguments.push_back(ad);
  531. }
  532. c.methods.push_back(md);
  533. }
  534. List<Pair<String, Variant> > cinfo;
  535. lang->get_public_constants(&cinfo);
  536. for (List<Pair<String, Variant> >::Element *E = cinfo.front(); E; E = E->next()) {
  537. ConstantDoc cd;
  538. cd.name = E->get().first;
  539. cd.value = E->get().second;
  540. c.constants.push_back(cd);
  541. }
  542. }
  543. }
  544. }
  545. static Error _parse_methods(Ref<XMLParser> &parser, Vector<DocData::MethodDoc> &methods) {
  546. String section = parser->get_node_name();
  547. String element = section.substr(0, section.length() - 1);
  548. while (parser->read() == OK) {
  549. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  550. if (parser->get_node_name() == element) {
  551. DocData::MethodDoc method;
  552. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  553. method.name = parser->get_attribute_value("name");
  554. if (parser->has_attribute("qualifiers"))
  555. method.qualifiers = parser->get_attribute_value("qualifiers");
  556. while (parser->read() == OK) {
  557. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  558. String name = parser->get_node_name();
  559. if (name == "return") {
  560. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  561. method.return_type = parser->get_attribute_value("type");
  562. if (parser->has_attribute("enum")) {
  563. method.return_enum = parser->get_attribute_value("enum");
  564. }
  565. } else if (name == "argument") {
  566. DocData::ArgumentDoc argument;
  567. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  568. argument.name = parser->get_attribute_value("name");
  569. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  570. argument.type = parser->get_attribute_value("type");
  571. if (parser->has_attribute("enum")) {
  572. argument.enumeration = parser->get_attribute_value("enum");
  573. }
  574. method.arguments.push_back(argument);
  575. } else if (name == "description") {
  576. parser->read();
  577. if (parser->get_node_type() == XMLParser::NODE_TEXT)
  578. method.description = parser->get_node_data();
  579. }
  580. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == element)
  581. break;
  582. }
  583. methods.push_back(method);
  584. } else {
  585. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + parser->get_node_name() + ".");
  586. }
  587. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == section)
  588. break;
  589. }
  590. return OK;
  591. }
  592. Error DocData::load_classes(const String &p_dir) {
  593. Error err;
  594. DirAccessRef da = DirAccess::open(p_dir, &err);
  595. if (!da) {
  596. return err;
  597. }
  598. da->list_dir_begin();
  599. String path;
  600. path = da->get_next();
  601. while (path != String()) {
  602. if (!da->current_is_dir() && path.ends_with("xml")) {
  603. Ref<XMLParser> parser = memnew(XMLParser);
  604. Error err2 = parser->open(p_dir.plus_file(path));
  605. if (err2)
  606. return err2;
  607. _load(parser);
  608. }
  609. path = da->get_next();
  610. }
  611. da->list_dir_end();
  612. return OK;
  613. }
  614. Error DocData::erase_classes(const String &p_dir) {
  615. Error err;
  616. DirAccessRef da = DirAccess::open(p_dir, &err);
  617. if (!da) {
  618. return err;
  619. }
  620. List<String> to_erase;
  621. da->list_dir_begin();
  622. String path;
  623. path = da->get_next();
  624. while (path != String()) {
  625. if (!da->current_is_dir() && path.ends_with("xml")) {
  626. to_erase.push_back(path);
  627. }
  628. path = da->get_next();
  629. }
  630. da->list_dir_end();
  631. while (to_erase.size()) {
  632. da->remove(to_erase.front()->get());
  633. to_erase.pop_front();
  634. }
  635. return OK;
  636. }
  637. Error DocData::_load(Ref<XMLParser> parser) {
  638. Error err = OK;
  639. while ((err = parser->read()) == OK) {
  640. if (parser->get_node_type() == XMLParser::NODE_ELEMENT && parser->get_node_name() == "?xml") {
  641. parser->skip_section();
  642. }
  643. if (parser->get_node_type() != XMLParser::NODE_ELEMENT)
  644. continue; //no idea what this may be, but skipping anyway
  645. ERR_FAIL_COND_V(parser->get_node_name() != "class", ERR_FILE_CORRUPT);
  646. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  647. String name = parser->get_attribute_value("name");
  648. class_list[name] = ClassDoc();
  649. ClassDoc &c = class_list[name];
  650. c.name = name;
  651. if (parser->has_attribute("inherits"))
  652. c.inherits = parser->get_attribute_value("inherits");
  653. while (parser->read() == OK) {
  654. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  655. String name2 = parser->get_node_name();
  656. if (name2 == "brief_description") {
  657. parser->read();
  658. if (parser->get_node_type() == XMLParser::NODE_TEXT)
  659. c.brief_description = parser->get_node_data();
  660. } else if (name2 == "description") {
  661. parser->read();
  662. if (parser->get_node_type() == XMLParser::NODE_TEXT)
  663. c.description = parser->get_node_data();
  664. } else if (name2 == "tutorials") {
  665. while (parser->read() == OK) {
  666. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  667. String name3 = parser->get_node_name();
  668. if (name3 == "link") {
  669. parser->read();
  670. if (parser->get_node_type() == XMLParser::NODE_TEXT)
  671. c.tutorials.push_back(parser->get_node_data().strip_edges());
  672. } else {
  673. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  674. }
  675. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "tutorials")
  676. break; // End of <tutorials>.
  677. }
  678. } else if (name2 == "methods") {
  679. Error err2 = _parse_methods(parser, c.methods);
  680. ERR_FAIL_COND_V(err2, err2);
  681. } else if (name2 == "signals") {
  682. Error err2 = _parse_methods(parser, c.signals);
  683. ERR_FAIL_COND_V(err2, err2);
  684. } else if (name2 == "members") {
  685. while (parser->read() == OK) {
  686. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  687. String name3 = parser->get_node_name();
  688. if (name3 == "member") {
  689. PropertyDoc prop2;
  690. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  691. prop2.name = parser->get_attribute_value("name");
  692. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  693. prop2.type = parser->get_attribute_value("type");
  694. if (parser->has_attribute("setter"))
  695. prop2.setter = parser->get_attribute_value("setter");
  696. if (parser->has_attribute("getter"))
  697. prop2.getter = parser->get_attribute_value("getter");
  698. if (parser->has_attribute("enum"))
  699. prop2.enumeration = parser->get_attribute_value("enum");
  700. if (!parser->is_empty()) {
  701. parser->read();
  702. if (parser->get_node_type() == XMLParser::NODE_TEXT)
  703. prop2.description = parser->get_node_data();
  704. }
  705. c.properties.push_back(prop2);
  706. } else {
  707. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  708. }
  709. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "members")
  710. break; // End of <members>.
  711. }
  712. } else if (name2 == "theme_items") {
  713. while (parser->read() == OK) {
  714. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  715. String name3 = parser->get_node_name();
  716. if (name3 == "theme_item") {
  717. PropertyDoc prop2;
  718. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  719. prop2.name = parser->get_attribute_value("name");
  720. ERR_FAIL_COND_V(!parser->has_attribute("type"), ERR_FILE_CORRUPT);
  721. prop2.type = parser->get_attribute_value("type");
  722. if (!parser->is_empty()) {
  723. parser->read();
  724. if (parser->get_node_type() == XMLParser::NODE_TEXT)
  725. prop2.description = parser->get_node_data();
  726. }
  727. c.theme_properties.push_back(prop2);
  728. } else {
  729. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  730. }
  731. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "theme_items")
  732. break; // End of <theme_items>.
  733. }
  734. } else if (name2 == "constants") {
  735. while (parser->read() == OK) {
  736. if (parser->get_node_type() == XMLParser::NODE_ELEMENT) {
  737. String name3 = parser->get_node_name();
  738. if (name3 == "constant") {
  739. ConstantDoc constant2;
  740. ERR_FAIL_COND_V(!parser->has_attribute("name"), ERR_FILE_CORRUPT);
  741. constant2.name = parser->get_attribute_value("name");
  742. ERR_FAIL_COND_V(!parser->has_attribute("value"), ERR_FILE_CORRUPT);
  743. constant2.value = parser->get_attribute_value("value");
  744. if (parser->has_attribute("enum")) {
  745. constant2.enumeration = parser->get_attribute_value("enum");
  746. }
  747. if (!parser->is_empty()) {
  748. parser->read();
  749. if (parser->get_node_type() == XMLParser::NODE_TEXT)
  750. constant2.description = parser->get_node_data();
  751. }
  752. c.constants.push_back(constant2);
  753. } else {
  754. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name3 + ".");
  755. }
  756. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "constants")
  757. break; // End of <constants>.
  758. }
  759. } else {
  760. ERR_FAIL_V_MSG(ERR_FILE_CORRUPT, "Invalid tag in doc file: " + name2 + ".");
  761. }
  762. } else if (parser->get_node_type() == XMLParser::NODE_ELEMENT_END && parser->get_node_name() == "class")
  763. break; // End of <class>.
  764. }
  765. }
  766. return OK;
  767. }
  768. static void _write_string(FileAccess *f, int p_tablevel, const String &p_string) {
  769. if (p_string == "")
  770. return;
  771. String tab;
  772. for (int i = 0; i < p_tablevel; i++)
  773. tab += "\t";
  774. f->store_string(tab + p_string + "\n");
  775. }
  776. Error DocData::save_classes(const String &p_default_path, const Map<String, String> &p_class_path) {
  777. for (Map<String, ClassDoc>::Element *E = class_list.front(); E; E = E->next()) {
  778. ClassDoc &c = E->get();
  779. String save_path;
  780. if (p_class_path.has(c.name)) {
  781. save_path = p_class_path[c.name];
  782. } else {
  783. save_path = p_default_path;
  784. }
  785. Error err;
  786. String save_file = save_path.plus_file(c.name + ".xml");
  787. FileAccessRef f = FileAccess::open(save_file, FileAccess::WRITE, &err);
  788. ERR_CONTINUE_MSG(err != OK, "Can't write doc file: " + save_file + ".");
  789. _write_string(f, 0, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
  790. String header = "<class name=\"" + c.name + "\"";
  791. if (c.inherits != "")
  792. header += " inherits=\"" + c.inherits + "\"";
  793. header += String(" version=\"") + VERSION_NUMBER + "\"";
  794. header += ">";
  795. _write_string(f, 0, header);
  796. _write_string(f, 1, "<brief_description>");
  797. _write_string(f, 2, c.brief_description.strip_edges().xml_escape());
  798. _write_string(f, 1, "</brief_description>");
  799. _write_string(f, 1, "<description>");
  800. _write_string(f, 2, c.description.strip_edges().xml_escape());
  801. _write_string(f, 1, "</description>");
  802. _write_string(f, 1, "<tutorials>");
  803. for (int i = 0; i < c.tutorials.size(); i++) {
  804. _write_string(f, 2, "<link>" + c.tutorials.get(i).xml_escape() + "</link>");
  805. }
  806. _write_string(f, 1, "</tutorials>");
  807. _write_string(f, 1, "<methods>");
  808. c.methods.sort();
  809. for (int i = 0; i < c.methods.size(); i++) {
  810. const MethodDoc &m = c.methods[i];
  811. String qualifiers;
  812. if (m.qualifiers != "")
  813. qualifiers += " qualifiers=\"" + m.qualifiers.xml_escape() + "\"";
  814. _write_string(f, 2, "<method name=\"" + m.name + "\"" + qualifiers + ">");
  815. if (m.return_type != "") {
  816. String enum_text;
  817. if (m.return_enum != String()) {
  818. enum_text = " enum=\"" + m.return_enum + "\"";
  819. }
  820. _write_string(f, 3, "<return type=\"" + m.return_type + "\"" + enum_text + ">");
  821. _write_string(f, 3, "</return>");
  822. }
  823. for (int j = 0; j < m.arguments.size(); j++) {
  824. const ArgumentDoc &a = m.arguments[j];
  825. String enum_text;
  826. if (a.enumeration != String()) {
  827. enum_text = " enum=\"" + a.enumeration + "\"";
  828. }
  829. if (a.default_value != "")
  830. _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) + "\">");
  831. else
  832. _write_string(f, 3, "<argument index=\"" + itos(j) + "\" name=\"" + a.name.xml_escape() + "\" type=\"" + a.type.xml_escape() + "\"" + enum_text + ">");
  833. _write_string(f, 3, "</argument>");
  834. }
  835. _write_string(f, 3, "<description>");
  836. _write_string(f, 4, m.description.strip_edges().xml_escape());
  837. _write_string(f, 3, "</description>");
  838. _write_string(f, 2, "</method>");
  839. }
  840. _write_string(f, 1, "</methods>");
  841. if (c.properties.size()) {
  842. _write_string(f, 1, "<members>");
  843. c.properties.sort();
  844. for (int i = 0; i < c.properties.size(); i++) {
  845. String additional_attributes;
  846. if (c.properties[i].enumeration != String()) {
  847. additional_attributes += " enum=\"" + c.properties[i].enumeration + "\"";
  848. }
  849. if (c.properties[i].default_value != String()) {
  850. additional_attributes += " default=\"" + c.properties[i].default_value.xml_escape(true) + "\"";
  851. }
  852. const PropertyDoc &p = c.properties[i];
  853. if (c.properties[i].overridden) {
  854. _write_string(f, 2, "<member name=\"" + p.name + "\" type=\"" + p.type + "\" setter=\"" + p.setter + "\" getter=\"" + p.getter + "\" override=\"true\"" + additional_attributes + " />");
  855. } else {
  856. _write_string(f, 2, "<member name=\"" + p.name + "\" type=\"" + p.type + "\" setter=\"" + p.setter + "\" getter=\"" + p.getter + "\"" + additional_attributes + ">");
  857. _write_string(f, 3, p.description.strip_edges().xml_escape());
  858. _write_string(f, 2, "</member>");
  859. }
  860. }
  861. _write_string(f, 1, "</members>");
  862. }
  863. if (c.signals.size()) {
  864. c.signals.sort();
  865. _write_string(f, 1, "<signals>");
  866. for (int i = 0; i < c.signals.size(); i++) {
  867. const MethodDoc &m = c.signals[i];
  868. _write_string(f, 2, "<signal name=\"" + m.name + "\">");
  869. for (int j = 0; j < m.arguments.size(); j++) {
  870. const ArgumentDoc &a = m.arguments[j];
  871. _write_string(f, 3, "<argument index=\"" + itos(j) + "\" name=\"" + a.name.xml_escape() + "\" type=\"" + a.type.xml_escape() + "\">");
  872. _write_string(f, 3, "</argument>");
  873. }
  874. _write_string(f, 3, "<description>");
  875. _write_string(f, 4, m.description.strip_edges().xml_escape());
  876. _write_string(f, 3, "</description>");
  877. _write_string(f, 2, "</signal>");
  878. }
  879. _write_string(f, 1, "</signals>");
  880. }
  881. _write_string(f, 1, "<constants>");
  882. for (int i = 0; i < c.constants.size(); i++) {
  883. const ConstantDoc &k = c.constants[i];
  884. if (k.enumeration != String()) {
  885. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"" + k.value + "\" enum=\"" + k.enumeration + "\">");
  886. } else {
  887. _write_string(f, 2, "<constant name=\"" + k.name + "\" value=\"" + k.value + "\">");
  888. }
  889. _write_string(f, 3, k.description.strip_edges().xml_escape());
  890. _write_string(f, 2, "</constant>");
  891. }
  892. _write_string(f, 1, "</constants>");
  893. if (c.theme_properties.size()) {
  894. c.theme_properties.sort();
  895. _write_string(f, 1, "<theme_items>");
  896. for (int i = 0; i < c.theme_properties.size(); i++) {
  897. const PropertyDoc &p = c.theme_properties[i];
  898. if (p.default_value != "")
  899. _write_string(f, 2, "<theme_item name=\"" + p.name + "\" type=\"" + p.type + "\" default=\"" + p.default_value.xml_escape(true) + "\">");
  900. else
  901. _write_string(f, 2, "<theme_item name=\"" + p.name + "\" type=\"" + p.type + "\">");
  902. _write_string(f, 3, p.description.strip_edges().xml_escape());
  903. _write_string(f, 2, "</theme_item>");
  904. }
  905. _write_string(f, 1, "</theme_items>");
  906. }
  907. _write_string(f, 0, "</class>");
  908. }
  909. return OK;
  910. }
  911. Error DocData::load_compressed(const uint8_t *p_data, int p_compressed_size, int p_uncompressed_size) {
  912. Vector<uint8_t> data;
  913. data.resize(p_uncompressed_size);
  914. Compression::decompress(data.ptrw(), p_uncompressed_size, p_data, p_compressed_size, Compression::MODE_DEFLATE);
  915. class_list.clear();
  916. Ref<XMLParser> parser = memnew(XMLParser);
  917. Error err = parser->open_buffer(data);
  918. if (err)
  919. return err;
  920. _load(parser);
  921. return OK;
  922. }