class_db_api_json.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*************************************************************************/
  2. /* class_db_api_json.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "class_db_api_json.h"
  31. #include "core/io/json.h"
  32. #include "core/os/file_access.h"
  33. #include "core/project_settings.h"
  34. #include "core/version.h"
  35. void class_db_api_to_json(const String &p_output_file, ClassDB::APIType p_api) {
  36. Dictionary classes_dict;
  37. List<StringName> names;
  38. const StringName *k = NULL;
  39. while ((k = ClassDB::classes.next(k))) {
  40. names.push_back(*k);
  41. }
  42. //must be alphabetically sorted for hash to compute
  43. names.sort_custom<StringName::AlphCompare>();
  44. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  45. ClassDB::ClassInfo *t = ClassDB::classes.getptr(E->get());
  46. ERR_FAIL_COND(!t);
  47. if (t->api != p_api || !t->exposed)
  48. continue;
  49. Dictionary class_dict;
  50. classes_dict[t->name] = class_dict;
  51. class_dict["inherits"] = t->inherits;
  52. { //methods
  53. List<StringName> snames;
  54. k = NULL;
  55. while ((k = t->method_map.next(k))) {
  56. snames.push_back(*k);
  57. }
  58. snames.sort_custom<StringName::AlphCompare>();
  59. Array methods;
  60. for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {
  61. Dictionary method_dict;
  62. methods.push_back(method_dict);
  63. MethodBind *mb = t->method_map[F->get()];
  64. method_dict["name"] = mb->get_name();
  65. method_dict["argument_count"] = mb->get_argument_count();
  66. method_dict["return_type"] = mb->get_argument_type(-1);
  67. Array arguments;
  68. method_dict["arguments"] = arguments;
  69. for (int i = 0; i < mb->get_argument_count(); i++) {
  70. Dictionary argument_dict;
  71. arguments.push_back(argument_dict);
  72. const PropertyInfo info = mb->get_argument_info(i);
  73. argument_dict["type"] = info.type;
  74. argument_dict["name"] = info.name;
  75. argument_dict["hint"] = info.hint;
  76. argument_dict["hint_string"] = info.hint_string;
  77. }
  78. method_dict["default_argument_count"] = mb->get_default_argument_count();
  79. Array default_arguments;
  80. method_dict["default_arguments"] = default_arguments;
  81. for (int i = 0; i < mb->get_default_argument_count(); i++) {
  82. Dictionary default_argument_dict;
  83. default_arguments.push_back(default_argument_dict);
  84. //hash should not change, i hope for tis
  85. Variant da = mb->get_default_argument(i);
  86. default_argument_dict["value"] = da;
  87. }
  88. method_dict["hint_flags"] = mb->get_hint_flags();
  89. }
  90. if (!methods.empty()) {
  91. class_dict["methods"] = methods;
  92. }
  93. }
  94. { //constants
  95. List<StringName> snames;
  96. k = NULL;
  97. while ((k = t->constant_map.next(k))) {
  98. snames.push_back(*k);
  99. }
  100. snames.sort_custom<StringName::AlphCompare>();
  101. Array constants;
  102. for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {
  103. Dictionary constant_dict;
  104. constants.push_back(constant_dict);
  105. constant_dict["name"] = F->get();
  106. constant_dict["value"] = t->constant_map[F->get()];
  107. }
  108. if (!constants.empty()) {
  109. class_dict["constants"] = constants;
  110. }
  111. }
  112. { //signals
  113. List<StringName> snames;
  114. k = NULL;
  115. while ((k = t->signal_map.next(k))) {
  116. snames.push_back(*k);
  117. }
  118. snames.sort_custom<StringName::AlphCompare>();
  119. Array signals;
  120. for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {
  121. Dictionary signal_dict;
  122. signals.push_back(signal_dict);
  123. MethodInfo &mi = t->signal_map[F->get()];
  124. signal_dict["name"] = F->get();
  125. Array arguments;
  126. signal_dict["arguments"] = arguments;
  127. for (int i = 0; i < mi.arguments.size(); i++) {
  128. Dictionary argument_dict;
  129. arguments.push_back(argument_dict);
  130. argument_dict["type"] = mi.arguments[i].type;
  131. }
  132. }
  133. if (!signals.empty()) {
  134. class_dict["signals"] = signals;
  135. }
  136. }
  137. { //properties
  138. List<StringName> snames;
  139. k = NULL;
  140. while ((k = t->property_setget.next(k))) {
  141. snames.push_back(*k);
  142. }
  143. snames.sort_custom<StringName::AlphCompare>();
  144. Array properties;
  145. for (List<StringName>::Element *F = snames.front(); F; F = F->next()) {
  146. Dictionary property_dict;
  147. properties.push_back(property_dict);
  148. ClassDB::PropertySetGet *psg = t->property_setget.getptr(F->get());
  149. property_dict["name"] = F->get();
  150. property_dict["setter"] = psg->setter;
  151. property_dict["getter"] = psg->getter;
  152. }
  153. if (!properties.empty()) {
  154. class_dict["property_setget"] = properties;
  155. }
  156. }
  157. Array property_list;
  158. //property list
  159. for (List<PropertyInfo>::Element *F = t->property_list.front(); F; F = F->next()) {
  160. Dictionary property_dict;
  161. property_list.push_back(property_dict);
  162. property_dict["name"] = F->get().name;
  163. property_dict["type"] = F->get().type;
  164. property_dict["hint"] = F->get().hint;
  165. property_dict["hint_string"] = F->get().hint_string;
  166. property_dict["usage"] = F->get().usage;
  167. }
  168. if (!property_list.empty()) {
  169. class_dict["property_list"] = property_list;
  170. }
  171. }
  172. FileAccessRef f = FileAccess::open(p_output_file, FileAccess::WRITE);
  173. ERR_FAIL_COND(!f);
  174. f->store_string(JSON::print(classes_dict, /*indent: */ "\t"));
  175. f->close();
  176. print_line(String() + "ClassDB API JSON written to: " + ProjectSettings::get_singleton()->globalize_path(p_output_file));
  177. }