doc_data.cpp 34 KB

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