doc_dump.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*************************************************************************/
  2. /* doc_dump.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_dump.h"
  31. #include "core/os/file_access.h"
  32. #include "core/version.h"
  33. #include "scene/main/node.h"
  34. static void _write_string(FileAccess *f, int p_tablevel, const String &p_string) {
  35. String tab;
  36. for (int i = 0; i < p_tablevel; i++)
  37. tab += "\t";
  38. f->store_string(tab + p_string + "\n");
  39. }
  40. struct _ConstantSort {
  41. String name;
  42. int value;
  43. bool operator<(const _ConstantSort &p_c) const {
  44. String left_a = name.find("_") == -1 ? name : name.substr(0, name.find("_"));
  45. String left_b = p_c.name.find("_") == -1 ? p_c.name : p_c.name.substr(0, p_c.name.find("_"));
  46. if (left_a == left_b)
  47. return value < p_c.value;
  48. else
  49. return left_a < left_b;
  50. }
  51. };
  52. static String _escape_string(const String &p_str) {
  53. String ret = p_str;
  54. ret = ret.replace("&", "&amp;");
  55. ret = ret.replace("<", "&gt;");
  56. ret = ret.replace(">", "&lt;");
  57. ret = ret.replace("'", "&apos;");
  58. ret = ret.replace("\"", "&quot;");
  59. for (char i = 1; i < 32; i++) {
  60. char chr[2] = { i, 0 };
  61. ret = ret.replace(chr, "&#" + String::num(i) + ";");
  62. }
  63. ret = ret.utf8();
  64. return ret;
  65. }
  66. void DocDump::dump(const String &p_file) {
  67. List<StringName> class_list;
  68. ClassDB::get_class_list(&class_list);
  69. class_list.sort_custom<StringName::AlphCompare>();
  70. FileAccess *f = FileAccess::open(p_file, FileAccess::WRITE);
  71. _write_string(f, 0, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
  72. _write_string(f, 0, String("<doc version=\"") + VERSION_NUMBER + "\" name=\"Engine Types\">");
  73. while (class_list.size()) {
  74. String name = class_list.front()->get();
  75. String header = "<class name=\"" + name + "\"";
  76. String inherits = ClassDB::get_parent_class(name);
  77. if (inherits != "")
  78. header += " inherits=\"" + inherits + "\"";
  79. String category = ClassDB::get_category(name);
  80. if (category == "")
  81. category = "Core";
  82. header += " category=\"" + category + "\"";
  83. header += ">";
  84. _write_string(f, 0, header);
  85. _write_string(f, 1, "<brief_description>");
  86. _write_string(f, 1, "</brief_description>");
  87. _write_string(f, 1, "<description>");
  88. _write_string(f, 1, "</description>");
  89. _write_string(f, 1, "<methods>");
  90. List<MethodInfo> method_list;
  91. ClassDB::get_method_list(name, &method_list, true);
  92. method_list.sort();
  93. for (List<MethodInfo>::Element *E = method_list.front(); E; E = E->next()) {
  94. if (E->get().name == "" || E->get().name[0] == '_')
  95. continue; //hiden
  96. MethodBind *m = ClassDB::get_method(name, E->get().name);
  97. String qualifiers;
  98. if (E->get().flags & METHOD_FLAG_CONST)
  99. qualifiers += "qualifiers=\"const\"";
  100. _write_string(f, 2, "<method name=\"" + _escape_string(E->get().name) + "\" " + qualifiers + " >");
  101. for (int i = -1; i < E->get().arguments.size(); i++) {
  102. PropertyInfo arginfo;
  103. if (i == -1) {
  104. arginfo = E->get().return_val;
  105. String type_name = (arginfo.hint == PROPERTY_HINT_RESOURCE_TYPE) ? arginfo.hint_string : Variant::get_type_name(arginfo.type);
  106. if (arginfo.type == Variant::NIL)
  107. continue;
  108. _write_string(f, 3, "<return type=\"" + type_name + "\">");
  109. } else {
  110. arginfo = E->get().arguments[i];
  111. String type_name;
  112. if (arginfo.hint == PROPERTY_HINT_RESOURCE_TYPE)
  113. type_name = arginfo.hint_string;
  114. else if (arginfo.type == Variant::NIL)
  115. type_name = "Variant";
  116. else
  117. type_name = Variant::get_type_name(arginfo.type);
  118. if (m && m->has_default_argument(i)) {
  119. Variant default_arg = m->get_default_argument(i);
  120. String default_arg_text = String(_escape_string(m->get_default_argument(i)));
  121. switch (default_arg.get_type()) {
  122. case Variant::NIL:
  123. default_arg_text = "NULL";
  124. break;
  125. // atomic types
  126. case Variant::BOOL:
  127. if (bool(default_arg))
  128. default_arg_text = "true";
  129. else
  130. default_arg_text = "false";
  131. break;
  132. case Variant::INT:
  133. case Variant::REAL:
  134. //keep it
  135. break;
  136. case Variant::STRING:
  137. case Variant::NODE_PATH:
  138. default_arg_text = "\"" + default_arg_text + "\"";
  139. break;
  140. case Variant::TRANSFORM:
  141. if (default_arg.operator Transform() == Transform()) {
  142. default_arg_text = "";
  143. }
  144. default_arg_text = Variant::get_type_name(default_arg.get_type()) + "(" + default_arg_text + ")";
  145. break;
  146. case Variant::VECTOR2:
  147. case Variant::RECT2:
  148. case Variant::VECTOR3:
  149. case Variant::PLANE:
  150. case Variant::QUAT:
  151. case Variant::AABB:
  152. case Variant::BASIS:
  153. case Variant::COLOR:
  154. case Variant::POOL_BYTE_ARRAY:
  155. case Variant::POOL_INT_ARRAY:
  156. case Variant::POOL_REAL_ARRAY:
  157. case Variant::POOL_STRING_ARRAY:
  158. case Variant::POOL_VECTOR3_ARRAY:
  159. case Variant::POOL_COLOR_ARRAY:
  160. default_arg_text = Variant::get_type_name(default_arg.get_type()) + "(" + default_arg_text + ")";
  161. break;
  162. case Variant::OBJECT:
  163. case Variant::DICTIONARY: // 20
  164. case Variant::ARRAY:
  165. case Variant::_RID:
  166. default: {}
  167. }
  168. _write_string(f, 3, "<argument index=\"" + itos(i) + "\" name=\"" + _escape_string(arginfo.name) + "\" type=\"" + type_name + "\" default=\"" + _escape_string(default_arg_text) + "\">");
  169. } else
  170. _write_string(f, 3, "<argument index=\"" + itos(i) + "\" name=\"" + arginfo.name + "\" type=\"" + type_name + "\">");
  171. }
  172. String hint;
  173. switch (arginfo.hint) {
  174. case PROPERTY_HINT_DIR: hint = "A directory."; break;
  175. 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;
  176. case PROPERTY_HINT_ENUM:
  177. hint = "Values: ";
  178. for (int j = 0; j < arginfo.hint_string.get_slice_count(","); j++) {
  179. if (j > 0) hint += ", ";
  180. hint += arginfo.hint_string.get_slice(",", j) + "=" + itos(j);
  181. }
  182. break;
  183. case PROPERTY_HINT_LENGTH: hint = "Length: " + arginfo.hint_string; break;
  184. case PROPERTY_HINT_FLAGS:
  185. hint = "Values: ";
  186. for (int j = 0; j < arginfo.hint_string.get_slice_count(","); j++) {
  187. if (j > 0) hint += ", ";
  188. hint += arginfo.hint_string.get_slice(",", j) + "=" + itos((uint64_t)1 << j);
  189. }
  190. break;
  191. case PROPERTY_HINT_FILE: hint = "A file:"; break;
  192. default: {}
  193. //case PROPERTY_HINT_RESOURCE_TYPE: hint="Type: "+arginfo.hint_string; break;
  194. };
  195. if (hint != "")
  196. _write_string(f, 4, hint);
  197. _write_string(f, 3, (i == -1) ? "</return>" : "</argument>");
  198. }
  199. _write_string(f, 3, "<description>");
  200. _write_string(f, 3, "</description>");
  201. _write_string(f, 2, "</method>");
  202. }
  203. _write_string(f, 1, "</methods>");
  204. List<MethodInfo> signal_list;
  205. ClassDB::get_signal_list(name, &signal_list, true);
  206. if (signal_list.size()) {
  207. _write_string(f, 1, "<signals>");
  208. for (List<MethodInfo>::Element *EV = signal_list.front(); EV; EV = EV->next()) {
  209. _write_string(f, 2, "<signal name=\"" + EV->get().name + "\">");
  210. for (int i = 0; i < EV->get().arguments.size(); i++) {
  211. PropertyInfo arginfo = EV->get().arguments[i];
  212. _write_string(f, 3, "<argument index=\"" + itos(i) + "\" name=\"" + arginfo.name + "\" type=\"" + Variant::get_type_name(arginfo.type) + "\">");
  213. _write_string(f, 3, "</argument>");
  214. }
  215. _write_string(f, 3, "<description>");
  216. _write_string(f, 3, "</description>");
  217. _write_string(f, 2, "</signal>");
  218. }
  219. _write_string(f, 1, "</signals>");
  220. }
  221. _write_string(f, 1, "<constants>");
  222. List<String> constant_list;
  223. ClassDB::get_integer_constant_list(name, &constant_list, true);
  224. /* constants are sorted in a special way */
  225. List<_ConstantSort> constant_sort;
  226. for (List<String>::Element *E = constant_list.front(); E; E = E->next()) {
  227. _ConstantSort cs;
  228. cs.name = E->get();
  229. cs.value = ClassDB::get_integer_constant(name, E->get());
  230. constant_sort.push_back(cs);
  231. }
  232. constant_sort.sort();
  233. for (List<_ConstantSort>::Element *E = constant_sort.front(); E; E = E->next()) {
  234. _write_string(f, 2, "<constant name=\"" + E->get().name + "\" value=\"" + itos(E->get().value) + "\">");
  235. _write_string(f, 2, "</constant>");
  236. }
  237. _write_string(f, 1, "</constants>");
  238. _write_string(f, 0, "</class>");
  239. class_list.erase(name);
  240. }
  241. _write_string(f, 0, "</doc>");
  242. f->close();
  243. memdelete(f);
  244. }