doc_data.cpp 35 KB

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