class_db_api_json.cpp 7.5 KB

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