doc_data.cpp 36 KB

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