class_db_api_json.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_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. LocalVector<StringName> class_list;
  39. ClassDB::get_class_list(class_list);
  40. for (const StringName &class_name : class_list) {
  41. ClassDB::ClassInfo *t = ClassDB::classes.getptr(class_name);
  42. ERR_FAIL_NULL(t);
  43. if (t->api != p_api || !t->exposed) {
  44. continue;
  45. }
  46. Dictionary class_dict;
  47. classes_dict[t->name] = class_dict;
  48. class_dict["inherits"] = t->inherits;
  49. { //methods
  50. List<StringName> snames;
  51. for (const KeyValue<StringName, MethodBind *> &F : t->method_map) {
  52. String name = F.key.operator String();
  53. ERR_CONTINUE(name.is_empty());
  54. if (name[0] == '_') {
  55. continue; // Ignore non-virtual methods that start with an underscore
  56. }
  57. snames.push_back(F.key);
  58. }
  59. snames.sort_custom<StringName::AlphCompare>();
  60. Array methods;
  61. for (const StringName &F : snames) {
  62. Dictionary method_dict;
  63. methods.push_back(method_dict);
  64. MethodBind *mb = t->method_map[F];
  65. method_dict["name"] = mb->get_name();
  66. method_dict["argument_count"] = mb->get_argument_count();
  67. method_dict["return_type"] = mb->get_argument_type(-1);
  68. Array arguments;
  69. method_dict["arguments"] = arguments;
  70. for (int i = 0; i < mb->get_argument_count(); i++) {
  71. Dictionary argument_dict;
  72. arguments.push_back(argument_dict);
  73. const PropertyInfo info = mb->get_argument_info(i);
  74. argument_dict["type"] = info.type;
  75. argument_dict["name"] = info.name;
  76. argument_dict["hint"] = info.hint;
  77. argument_dict["hint_string"] = info.hint_string;
  78. }
  79. method_dict["default_argument_count"] = mb->get_default_argument_count();
  80. Array default_arguments;
  81. method_dict["default_arguments"] = default_arguments;
  82. for (int i = 0; i < mb->get_default_argument_count(); i++) {
  83. Dictionary default_argument_dict;
  84. default_arguments.push_back(default_argument_dict);
  85. //hash should not change, i hope for tis
  86. Variant da = mb->get_default_argument(i);
  87. default_argument_dict["value"] = da;
  88. }
  89. method_dict["hint_flags"] = mb->get_hint_flags();
  90. }
  91. if (!methods.is_empty()) {
  92. class_dict["methods"] = methods;
  93. }
  94. }
  95. { //constants
  96. List<StringName> snames;
  97. for (const KeyValue<StringName, int64_t> &F : t->constant_map) {
  98. snames.push_back(F.key);
  99. }
  100. snames.sort_custom<StringName::AlphCompare>();
  101. Array constants;
  102. for (const StringName &F : snames) {
  103. Dictionary constant_dict;
  104. constants.push_back(constant_dict);
  105. constant_dict["name"] = F;
  106. constant_dict["value"] = t->constant_map[F];
  107. }
  108. if (!constants.is_empty()) {
  109. class_dict["constants"] = constants;
  110. }
  111. }
  112. { //signals
  113. List<StringName> snames;
  114. for (const KeyValue<StringName, MethodInfo> &F : t->signal_map) {
  115. snames.push_back(F.key);
  116. }
  117. snames.sort_custom<StringName::AlphCompare>();
  118. Array signals;
  119. for (const StringName &F : snames) {
  120. Dictionary signal_dict;
  121. signals.push_back(signal_dict);
  122. MethodInfo &mi = t->signal_map[F];
  123. signal_dict["name"] = F;
  124. Array arguments;
  125. signal_dict["arguments"] = arguments;
  126. for (const PropertyInfo &pi : mi.arguments) {
  127. Dictionary argument_dict;
  128. arguments.push_back(argument_dict);
  129. argument_dict["type"] = pi.type;
  130. }
  131. }
  132. if (!signals.is_empty()) {
  133. class_dict["signals"] = signals;
  134. }
  135. }
  136. { //properties
  137. List<StringName> snames;
  138. for (const KeyValue<StringName, ClassDB::PropertySetGet> &F : t->property_setget) {
  139. snames.push_back(F.key);
  140. }
  141. snames.sort_custom<StringName::AlphCompare>();
  142. Array properties;
  143. for (const StringName &F : snames) {
  144. Dictionary property_dict;
  145. properties.push_back(property_dict);
  146. ClassDB::PropertySetGet *psg = t->property_setget.getptr(F);
  147. property_dict["name"] = F;
  148. property_dict["setter"] = psg->setter;
  149. property_dict["getter"] = psg->getter;
  150. }
  151. if (!properties.is_empty()) {
  152. class_dict["property_setget"] = properties;
  153. }
  154. }
  155. Array property_list;
  156. //property list
  157. for (const PropertyInfo &F : t->property_list) {
  158. Dictionary property_dict;
  159. property_list.push_back(property_dict);
  160. property_dict["name"] = F.name;
  161. property_dict["type"] = F.type;
  162. property_dict["hint"] = F.hint;
  163. property_dict["hint_string"] = F.hint_string;
  164. property_dict["usage"] = F.usage;
  165. }
  166. if (!property_list.is_empty()) {
  167. class_dict["property_list"] = property_list;
  168. }
  169. }
  170. Ref<FileAccess> f = FileAccess::open(p_output_file, FileAccess::WRITE);
  171. ERR_FAIL_COND_MSG(f.is_null(), "Cannot open file '" + p_output_file + "'.");
  172. f->store_string(JSON::stringify(classes_dict, "\t"));
  173. print_line(String() + "ClassDB API JSON written to: " + ProjectSettings::get_singleton()->globalize_path(p_output_file));
  174. }
  175. #endif // DEBUG_ENABLED