extension_api_dump.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /**************************************************************************/
  2. /* extension_api_dump.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 "extension_api_dump.h"
  31. #include "core/config/engine.h"
  32. #include "core/core_constants.h"
  33. #include "core/io/file_access.h"
  34. #include "core/io/json.h"
  35. #include "core/templates/pair.h"
  36. #include "core/version.h"
  37. #ifdef TOOLS_ENABLED
  38. static String get_builtin_or_variant_type_name(const Variant::Type p_type) {
  39. if (p_type == Variant::NIL) {
  40. return "Variant";
  41. } else {
  42. return Variant::get_type_name(p_type);
  43. }
  44. }
  45. static String get_property_info_type_name(const PropertyInfo &p_info) {
  46. if (p_info.type == Variant::INT && (p_info.hint == PROPERTY_HINT_INT_IS_POINTER)) {
  47. if (p_info.hint_string.is_empty()) {
  48. return "void*";
  49. } else {
  50. return p_info.hint_string + "*";
  51. }
  52. }
  53. if (p_info.type == Variant::ARRAY && (p_info.hint == PROPERTY_HINT_ARRAY_TYPE)) {
  54. return String("typedarray::") + p_info.hint_string;
  55. }
  56. if (p_info.type == Variant::INT && (p_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM))) {
  57. return String("enum::") + String(p_info.class_name);
  58. }
  59. if (p_info.type == Variant::INT && (p_info.usage & (PROPERTY_USAGE_CLASS_IS_BITFIELD))) {
  60. return String("bitfield::") + String(p_info.class_name);
  61. }
  62. if (p_info.type == Variant::INT && (p_info.usage & PROPERTY_USAGE_ARRAY)) {
  63. return "int";
  64. }
  65. if (p_info.class_name != StringName()) {
  66. return p_info.class_name;
  67. }
  68. if (p_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  69. return p_info.hint_string;
  70. }
  71. if (p_info.type == Variant::NIL && (p_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  72. return "Variant";
  73. }
  74. if (p_info.type == Variant::NIL) {
  75. return "void";
  76. }
  77. return get_builtin_or_variant_type_name(p_info.type);
  78. }
  79. Dictionary GDExtensionAPIDump::generate_extension_api() {
  80. Dictionary api_dump;
  81. {
  82. //header
  83. Dictionary header;
  84. header["version_major"] = VERSION_MAJOR;
  85. header["version_minor"] = VERSION_MINOR;
  86. #if VERSION_PATCH
  87. header["version_patch"] = VERSION_PATCH;
  88. #else
  89. header["version_patch"] = 0;
  90. #endif
  91. header["version_status"] = VERSION_STATUS;
  92. header["version_build"] = VERSION_BUILD;
  93. header["version_full_name"] = VERSION_FULL_NAME;
  94. api_dump["header"] = header;
  95. }
  96. const uint32_t vec3_elems = 3;
  97. const uint32_t vec4_elems = 4;
  98. const uint32_t ptrsize_32 = 4;
  99. const uint32_t ptrsize_64 = 8;
  100. static const char *build_config_name[4] = { "float_32", "float_64", "double_32", "double_64" };
  101. {
  102. //type sizes
  103. constexpr struct {
  104. Variant::Type type;
  105. uint32_t size_32_bits_real_float;
  106. uint32_t size_64_bits_real_float;
  107. uint32_t size_32_bits_real_double;
  108. uint32_t size_64_bits_real_double;
  109. // For compile-time size check.
  110. constexpr uint32_t operator[](int index) const {
  111. switch (index) {
  112. #ifndef REAL_T_IS_DOUBLE
  113. case sizeof(uint32_t):
  114. return size_32_bits_real_float;
  115. case sizeof(uint64_t):
  116. return size_64_bits_real_float;
  117. #else // REAL_T_IS_DOUBLE
  118. case sizeof(uint32_t):
  119. return size_32_bits_real_double;
  120. case sizeof(uint64_t):
  121. return size_64_bits_real_double;
  122. #endif
  123. }
  124. return -1;
  125. }
  126. } type_size_array[Variant::VARIANT_MAX + 1] = {
  127. { Variant::NIL, 0, 0, 0, 0 },
  128. { Variant::BOOL, sizeof(uint8_t), sizeof(uint8_t), sizeof(uint8_t), sizeof(uint8_t) },
  129. { Variant::INT, sizeof(int64_t), sizeof(int64_t), sizeof(int64_t), sizeof(int64_t) },
  130. { Variant::FLOAT, sizeof(double), sizeof(double), sizeof(double), sizeof(double) },
  131. { Variant::STRING, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  132. { Variant::VECTOR2, 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(double), 2 * sizeof(double) },
  133. { Variant::VECTOR2I, 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t) },
  134. { Variant::RECT2, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(double), 4 * sizeof(double) },
  135. { Variant::RECT2I, 4 * sizeof(int32_t), 4 * sizeof(int32_t), 4 * sizeof(int32_t), 4 * sizeof(int32_t) },
  136. { Variant::VECTOR3, vec3_elems * sizeof(float), vec3_elems * sizeof(float), vec3_elems * sizeof(double), vec3_elems * sizeof(double) },
  137. { Variant::VECTOR3I, 3 * sizeof(int32_t), 3 * sizeof(int32_t), 3 * sizeof(int32_t), 3 * sizeof(int32_t) },
  138. { Variant::TRANSFORM2D, 6 * sizeof(float), 6 * sizeof(float), 6 * sizeof(double), 6 * sizeof(double) },
  139. { Variant::VECTOR4, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(double), 4 * sizeof(double) },
  140. { Variant::VECTOR4I, 4 * sizeof(int32_t), 4 * sizeof(int32_t), 4 * sizeof(int32_t), 4 * sizeof(int32_t) },
  141. { Variant::PLANE, (vec3_elems + 1) * sizeof(float), (vec3_elems + 1) * sizeof(float), (vec3_elems + 1) * sizeof(double), (vec3_elems + 1) * sizeof(double) },
  142. { Variant::QUATERNION, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(double), 4 * sizeof(double) },
  143. { Variant::AABB, (vec3_elems * 2) * sizeof(float), (vec3_elems * 2) * sizeof(float), (vec3_elems * 2) * sizeof(double), (vec3_elems * 2) * sizeof(double) },
  144. { Variant::BASIS, (vec3_elems * 3) * sizeof(float), (vec3_elems * 3) * sizeof(float), (vec3_elems * 3) * sizeof(double), (vec3_elems * 3) * sizeof(double) },
  145. { Variant::TRANSFORM3D, (vec3_elems * 4) * sizeof(float), (vec3_elems * 4) * sizeof(float), (vec3_elems * 4) * sizeof(double), (vec3_elems * 4) * sizeof(double) },
  146. { Variant::PROJECTION, (vec4_elems * 4) * sizeof(float), (vec4_elems * 4) * sizeof(float), (vec4_elems * 4) * sizeof(double), (vec4_elems * 4) * sizeof(double) },
  147. { Variant::COLOR, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(float) },
  148. { Variant::STRING_NAME, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  149. { Variant::NODE_PATH, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  150. { Variant::RID, sizeof(uint64_t), sizeof(uint64_t), sizeof(uint64_t), sizeof(uint64_t) },
  151. { Variant::OBJECT, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  152. { Variant::CALLABLE, sizeof(Callable), sizeof(Callable), sizeof(Callable), sizeof(Callable) }, // Hardcoded align.
  153. { Variant::SIGNAL, sizeof(Signal), sizeof(Signal), sizeof(Signal), sizeof(Signal) }, // Hardcoded align.
  154. { Variant::DICTIONARY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  155. { Variant::ARRAY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  156. { Variant::PACKED_BYTE_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  157. { Variant::PACKED_INT32_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  158. { Variant::PACKED_INT64_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  159. { Variant::PACKED_FLOAT32_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  160. { Variant::PACKED_FLOAT64_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  161. { Variant::PACKED_STRING_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  162. { Variant::PACKED_VECTOR2_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  163. { Variant::PACKED_VECTOR3_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  164. { Variant::PACKED_COLOR_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  165. { Variant::VARIANT_MAX, sizeof(uint64_t) + sizeof(float) * 4, sizeof(uint64_t) + sizeof(float) * 4, sizeof(uint64_t) + sizeof(double) * 4, sizeof(uint64_t) + sizeof(double) * 4 },
  166. };
  167. // Validate sizes at compile time for the current build configuration.
  168. static_assert(type_size_array[Variant::BOOL][sizeof(void *)] == sizeof(GDExtensionBool), "Size of bool mismatch");
  169. static_assert(type_size_array[Variant::INT][sizeof(void *)] == sizeof(GDExtensionInt), "Size of int mismatch");
  170. static_assert(type_size_array[Variant::FLOAT][sizeof(void *)] == sizeof(double), "Size of float mismatch");
  171. static_assert(type_size_array[Variant::STRING][sizeof(void *)] == sizeof(String), "Size of String mismatch");
  172. static_assert(type_size_array[Variant::VECTOR2][sizeof(void *)] == sizeof(Vector2), "Size of Vector2 mismatch");
  173. static_assert(type_size_array[Variant::VECTOR2I][sizeof(void *)] == sizeof(Vector2i), "Size of Vector2i mismatch");
  174. static_assert(type_size_array[Variant::RECT2][sizeof(void *)] == sizeof(Rect2), "Size of Rect2 mismatch");
  175. static_assert(type_size_array[Variant::RECT2I][sizeof(void *)] == sizeof(Rect2i), "Size of Rect2i mismatch");
  176. static_assert(type_size_array[Variant::VECTOR3][sizeof(void *)] == sizeof(Vector3), "Size of Vector3 mismatch");
  177. static_assert(type_size_array[Variant::VECTOR3I][sizeof(void *)] == sizeof(Vector3i), "Size of Vector3i mismatch");
  178. static_assert(type_size_array[Variant::TRANSFORM2D][sizeof(void *)] == sizeof(Transform2D), "Size of Transform2D mismatch");
  179. static_assert(type_size_array[Variant::VECTOR4][sizeof(void *)] == sizeof(Vector4), "Size of Vector4 mismatch");
  180. static_assert(type_size_array[Variant::VECTOR4I][sizeof(void *)] == sizeof(Vector4i), "Size of Vector4i mismatch");
  181. static_assert(type_size_array[Variant::PLANE][sizeof(void *)] == sizeof(Plane), "Size of Plane mismatch");
  182. static_assert(type_size_array[Variant::QUATERNION][sizeof(void *)] == sizeof(Quaternion), "Size of Quaternion mismatch");
  183. static_assert(type_size_array[Variant::AABB][sizeof(void *)] == sizeof(AABB), "Size of AABB mismatch");
  184. static_assert(type_size_array[Variant::BASIS][sizeof(void *)] == sizeof(Basis), "Size of Basis mismatch");
  185. static_assert(type_size_array[Variant::TRANSFORM3D][sizeof(void *)] == sizeof(Transform3D), "Size of Transform3D mismatch");
  186. static_assert(type_size_array[Variant::PROJECTION][sizeof(void *)] == sizeof(Projection), "Size of Projection mismatch");
  187. static_assert(type_size_array[Variant::COLOR][sizeof(void *)] == sizeof(Color), "Size of Color mismatch");
  188. static_assert(type_size_array[Variant::STRING_NAME][sizeof(void *)] == sizeof(StringName), "Size of StringName mismatch");
  189. static_assert(type_size_array[Variant::NODE_PATH][sizeof(void *)] == sizeof(NodePath), "Size of NodePath mismatch");
  190. static_assert(type_size_array[Variant::RID][sizeof(void *)] == sizeof(RID), "Size of RID mismatch");
  191. static_assert(type_size_array[Variant::OBJECT][sizeof(void *)] == sizeof(Object *), "Size of Object mismatch");
  192. static_assert(type_size_array[Variant::CALLABLE][sizeof(void *)] == sizeof(Callable), "Size of Callable mismatch");
  193. static_assert(type_size_array[Variant::SIGNAL][sizeof(void *)] == sizeof(Signal), "Size of Signal mismatch");
  194. static_assert(type_size_array[Variant::DICTIONARY][sizeof(void *)] == sizeof(Dictionary), "Size of Dictionary mismatch");
  195. static_assert(type_size_array[Variant::ARRAY][sizeof(void *)] == sizeof(Array), "Size of Array mismatch");
  196. static_assert(type_size_array[Variant::PACKED_BYTE_ARRAY][sizeof(void *)] == sizeof(PackedByteArray), "Size of PackedByteArray mismatch");
  197. static_assert(type_size_array[Variant::PACKED_INT32_ARRAY][sizeof(void *)] == sizeof(PackedInt32Array), "Size of PackedInt32Array mismatch");
  198. static_assert(type_size_array[Variant::PACKED_INT64_ARRAY][sizeof(void *)] == sizeof(PackedInt64Array), "Size of PackedInt64Array mismatch");
  199. static_assert(type_size_array[Variant::PACKED_FLOAT32_ARRAY][sizeof(void *)] == sizeof(PackedFloat32Array), "Size of PackedFloat32Array mismatch");
  200. static_assert(type_size_array[Variant::PACKED_FLOAT64_ARRAY][sizeof(void *)] == sizeof(PackedFloat64Array), "Size of PackedFloat64Array mismatch");
  201. static_assert(type_size_array[Variant::PACKED_STRING_ARRAY][sizeof(void *)] == sizeof(PackedStringArray), "Size of PackedStringArray mismatch");
  202. static_assert(type_size_array[Variant::PACKED_VECTOR2_ARRAY][sizeof(void *)] == sizeof(PackedVector2Array), "Size of PackedVector2Array mismatch");
  203. static_assert(type_size_array[Variant::PACKED_VECTOR3_ARRAY][sizeof(void *)] == sizeof(PackedVector3Array), "Size of PackedVector3Array mismatch");
  204. static_assert(type_size_array[Variant::PACKED_COLOR_ARRAY][sizeof(void *)] == sizeof(PackedColorArray), "Size of PackedColorArray mismatch");
  205. static_assert(type_size_array[Variant::VARIANT_MAX][sizeof(void *)] == sizeof(Variant), "Size of Variant mismatch");
  206. Array core_type_sizes;
  207. for (int i = 0; i < 4; i++) {
  208. Dictionary d;
  209. d["build_configuration"] = build_config_name[i];
  210. Array sizes;
  211. for (int j = 0; j <= Variant::VARIANT_MAX; j++) {
  212. Variant::Type t = type_size_array[j].type;
  213. String name = t == Variant::VARIANT_MAX ? String("Variant") : Variant::get_type_name(t);
  214. Dictionary d2;
  215. d2["name"] = name;
  216. uint32_t size = 0;
  217. switch (i) {
  218. case 0:
  219. size = type_size_array[j].size_32_bits_real_float;
  220. break;
  221. case 1:
  222. size = type_size_array[j].size_64_bits_real_float;
  223. break;
  224. case 2:
  225. size = type_size_array[j].size_32_bits_real_double;
  226. break;
  227. case 3:
  228. size = type_size_array[j].size_64_bits_real_double;
  229. break;
  230. }
  231. d2["size"] = size;
  232. sizes.push_back(d2);
  233. }
  234. d["sizes"] = sizes;
  235. core_type_sizes.push_back(d);
  236. }
  237. api_dump["builtin_class_sizes"] = core_type_sizes;
  238. }
  239. {
  240. // Member offsets, meta types and sizes.
  241. #define REAL_MEMBER_OFFSET(type, member) \
  242. { \
  243. type, \
  244. member, \
  245. "float", \
  246. sizeof(float), \
  247. "float", \
  248. sizeof(float), \
  249. "double", \
  250. sizeof(double), \
  251. "double", \
  252. sizeof(double), \
  253. }
  254. #define INT32_MEMBER_OFFSET(type, member) \
  255. { \
  256. type, \
  257. member, \
  258. "int32", \
  259. sizeof(int32_t), \
  260. "int32", \
  261. sizeof(int32_t), \
  262. "int32", \
  263. sizeof(int32_t), \
  264. "int32", \
  265. sizeof(int32_t), \
  266. }
  267. #define INT32_BASED_BUILTIN_MEMBER_OFFSET(type, member, member_type, member_elems) \
  268. { \
  269. type, \
  270. member, \
  271. member_type, \
  272. sizeof(int32_t) * member_elems, \
  273. member_type, \
  274. sizeof(int32_t) * member_elems, \
  275. member_type, \
  276. sizeof(int32_t) * member_elems, \
  277. member_type, \
  278. sizeof(int32_t) * member_elems, \
  279. }
  280. #define REAL_BASED_BUILTIN_MEMBER_OFFSET(type, member, member_type, member_elems) \
  281. { \
  282. type, \
  283. member, \
  284. member_type, \
  285. sizeof(float) * member_elems, \
  286. member_type, \
  287. sizeof(float) * member_elems, \
  288. member_type, \
  289. sizeof(double) * member_elems, \
  290. member_type, \
  291. sizeof(double) * member_elems, \
  292. }
  293. struct {
  294. Variant::Type type;
  295. const char *member;
  296. const char *member_meta_32_bits_real_float;
  297. const uint32_t member_size_32_bits_real_float;
  298. const char *member_meta_64_bits_real_float;
  299. const uint32_t member_size_64_bits_real_float;
  300. const char *member_meta_32_bits_real_double;
  301. const uint32_t member_size_32_bits_real_double;
  302. const char *member_meta_64_bits_real_double;
  303. const uint32_t member_size_64_bits_real_double;
  304. } member_offset_array[] = {
  305. // Vector2
  306. REAL_MEMBER_OFFSET(Variant::VECTOR2, "x"),
  307. REAL_MEMBER_OFFSET(Variant::VECTOR2, "y"),
  308. // Vector2i
  309. INT32_MEMBER_OFFSET(Variant::VECTOR2I, "x"),
  310. INT32_MEMBER_OFFSET(Variant::VECTOR2I, "y"),
  311. // Rect2
  312. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::RECT2, "position", "Vector2", 2),
  313. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::RECT2, "size", "Vector2", 2),
  314. // Rect2i
  315. INT32_BASED_BUILTIN_MEMBER_OFFSET(Variant::RECT2I, "position", "Vector2i", 2),
  316. INT32_BASED_BUILTIN_MEMBER_OFFSET(Variant::RECT2I, "size", "Vector2i", 2),
  317. // Vector3
  318. REAL_MEMBER_OFFSET(Variant::VECTOR3, "x"),
  319. REAL_MEMBER_OFFSET(Variant::VECTOR3, "y"),
  320. REAL_MEMBER_OFFSET(Variant::VECTOR3, "z"),
  321. // Vector3i
  322. INT32_MEMBER_OFFSET(Variant::VECTOR3I, "x"),
  323. INT32_MEMBER_OFFSET(Variant::VECTOR3I, "y"),
  324. INT32_MEMBER_OFFSET(Variant::VECTOR3I, "z"),
  325. // Transform2D
  326. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::TRANSFORM2D, "x", "Vector2", 2),
  327. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::TRANSFORM2D, "y", "Vector2", 2),
  328. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::TRANSFORM2D, "origin", "Vector2", 2),
  329. // Vector4
  330. REAL_MEMBER_OFFSET(Variant::VECTOR4, "x"),
  331. REAL_MEMBER_OFFSET(Variant::VECTOR4, "y"),
  332. REAL_MEMBER_OFFSET(Variant::VECTOR4, "z"),
  333. REAL_MEMBER_OFFSET(Variant::VECTOR4, "w"),
  334. // Vector4i
  335. INT32_MEMBER_OFFSET(Variant::VECTOR4I, "x"),
  336. INT32_MEMBER_OFFSET(Variant::VECTOR4I, "y"),
  337. INT32_MEMBER_OFFSET(Variant::VECTOR4I, "z"),
  338. INT32_MEMBER_OFFSET(Variant::VECTOR4I, "w"),
  339. // Plane
  340. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::PLANE, "normal", "Vector3", vec3_elems),
  341. REAL_MEMBER_OFFSET(Variant::PLANE, "d"),
  342. // Quaternion
  343. REAL_MEMBER_OFFSET(Variant::QUATERNION, "x"),
  344. REAL_MEMBER_OFFSET(Variant::QUATERNION, "y"),
  345. REAL_MEMBER_OFFSET(Variant::QUATERNION, "z"),
  346. REAL_MEMBER_OFFSET(Variant::QUATERNION, "w"),
  347. // AABB
  348. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::AABB, "position", "Vector3", vec3_elems),
  349. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::AABB, "size", "Vector3", vec3_elems),
  350. // Basis (remember that basis vectors are flipped!)
  351. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::BASIS, "x", "Vector3", vec3_elems),
  352. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::BASIS, "y", "Vector3", vec3_elems),
  353. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::BASIS, "z", "Vector3", vec3_elems),
  354. // Transform3D
  355. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::TRANSFORM3D, "basis", "Basis", vec3_elems * 3),
  356. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::TRANSFORM3D, "origin", "Vector3", vec3_elems),
  357. // Projection
  358. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::PROJECTION, "x", "Vector4", vec4_elems),
  359. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::PROJECTION, "y", "Vector4", vec4_elems),
  360. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::PROJECTION, "z", "Vector4", vec4_elems),
  361. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::PROJECTION, "w", "Vector4", vec4_elems),
  362. // Color (always composed of 4bytes floats)
  363. { Variant::COLOR, "r", "float", sizeof(float), "float", sizeof(float), "float", sizeof(float), "float", sizeof(float) },
  364. { Variant::COLOR, "g", "float", sizeof(float), "float", sizeof(float), "float", sizeof(float), "float", sizeof(float) },
  365. { Variant::COLOR, "b", "float", sizeof(float), "float", sizeof(float), "float", sizeof(float), "float", sizeof(float) },
  366. { Variant::COLOR, "a", "float", sizeof(float), "float", sizeof(float), "float", sizeof(float), "float", sizeof(float) },
  367. // End marker, must stay last
  368. { Variant::NIL, nullptr, nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0 },
  369. };
  370. Array core_type_member_offsets;
  371. for (int i = 0; i < 4; i++) {
  372. Dictionary d;
  373. d["build_configuration"] = build_config_name[i];
  374. Array type_offsets;
  375. uint32_t idx = 0;
  376. Variant::Type previous_type = Variant::NIL;
  377. Dictionary d2;
  378. Array members;
  379. uint32_t offset = 0;
  380. while (true) {
  381. Variant::Type t = member_offset_array[idx].type;
  382. if (t != previous_type) {
  383. if (previous_type != Variant::NIL) {
  384. d2["members"] = members;
  385. type_offsets.push_back(d2);
  386. }
  387. if (t == Variant::NIL) {
  388. break;
  389. }
  390. String name = t == Variant::VARIANT_MAX ? String("Variant") : Variant::get_type_name(t);
  391. d2 = Dictionary();
  392. members = Array();
  393. offset = 0;
  394. d2["name"] = name;
  395. previous_type = t;
  396. }
  397. Dictionary d3;
  398. const char *member_meta = nullptr;
  399. uint32_t member_size = 0;
  400. switch (i) {
  401. case 0:
  402. member_meta = member_offset_array[idx].member_meta_32_bits_real_float;
  403. member_size = member_offset_array[idx].member_size_32_bits_real_float;
  404. break;
  405. case 1:
  406. member_meta = member_offset_array[idx].member_meta_64_bits_real_float;
  407. member_size = member_offset_array[idx].member_size_64_bits_real_float;
  408. break;
  409. case 2:
  410. member_meta = member_offset_array[idx].member_meta_32_bits_real_double;
  411. member_size = member_offset_array[idx].member_size_32_bits_real_double;
  412. break;
  413. case 3:
  414. member_meta = member_offset_array[idx].member_meta_64_bits_real_double;
  415. member_size = member_offset_array[idx].member_size_64_bits_real_double;
  416. break;
  417. }
  418. d3["member"] = member_offset_array[idx].member;
  419. d3["offset"] = offset;
  420. d3["meta"] = member_meta;
  421. offset += member_size;
  422. members.push_back(d3);
  423. idx++;
  424. }
  425. d["classes"] = type_offsets;
  426. core_type_member_offsets.push_back(d);
  427. }
  428. api_dump["builtin_class_member_offsets"] = core_type_member_offsets;
  429. }
  430. {
  431. // Global enums and constants.
  432. Array constants;
  433. HashMap<String, List<Pair<String, int64_t>>> enum_list;
  434. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  435. int64_t value = CoreConstants::get_global_constant_value(i);
  436. String enum_name = CoreConstants::get_global_constant_enum(i);
  437. String name = CoreConstants::get_global_constant_name(i);
  438. if (!enum_name.is_empty()) {
  439. enum_list[enum_name].push_back(Pair<String, int64_t>(name, value));
  440. } else {
  441. Dictionary d;
  442. d["name"] = name;
  443. d["value"] = value;
  444. constants.push_back(d);
  445. }
  446. }
  447. api_dump["global_constants"] = constants;
  448. Array enums;
  449. for (const KeyValue<String, List<Pair<String, int64_t>>> &E : enum_list) {
  450. Dictionary d1;
  451. d1["name"] = E.key;
  452. Array values;
  453. for (const Pair<String, int64_t> &F : E.value) {
  454. Dictionary d2;
  455. d2["name"] = F.first;
  456. d2["value"] = F.second;
  457. values.push_back(d2);
  458. }
  459. d1["values"] = values;
  460. enums.push_back(d1);
  461. }
  462. api_dump["global_enums"] = enums;
  463. }
  464. {
  465. Array utility_funcs;
  466. List<StringName> utility_func_names;
  467. Variant::get_utility_function_list(&utility_func_names);
  468. for (const StringName &name : utility_func_names) {
  469. Dictionary func;
  470. func["name"] = String(name);
  471. if (Variant::has_utility_function_return_value(name)) {
  472. Variant::Type rt = Variant::get_utility_function_return_type(name);
  473. func["return_type"] = rt == Variant::NIL ? String("Variant") : Variant::get_type_name(rt);
  474. }
  475. switch (Variant::get_utility_function_type(name)) {
  476. case Variant::UTILITY_FUNC_TYPE_MATH:
  477. func["category"] = "math";
  478. break;
  479. case Variant::UTILITY_FUNC_TYPE_RANDOM:
  480. func["category"] = "random";
  481. break;
  482. case Variant::UTILITY_FUNC_TYPE_GENERAL:
  483. func["category"] = "general";
  484. break;
  485. }
  486. bool vararg = Variant::is_utility_function_vararg(name);
  487. func["is_vararg"] = Variant::is_utility_function_vararg(name);
  488. func["hash"] = Variant::get_utility_function_hash(name);
  489. Array arguments;
  490. int argcount = Variant::get_utility_function_argument_count(name);
  491. for (int i = 0; i < argcount; i++) {
  492. Dictionary arg;
  493. String argname = vararg ? "arg" + itos(i + 1) : Variant::get_utility_function_argument_name(name, i);
  494. arg["name"] = argname;
  495. arg["type"] = get_builtin_or_variant_type_name(Variant::get_utility_function_argument_type(name, i));
  496. //no default value support in utility functions
  497. arguments.push_back(arg);
  498. }
  499. if (arguments.size()) {
  500. func["arguments"] = arguments;
  501. }
  502. utility_funcs.push_back(func);
  503. }
  504. api_dump["utility_functions"] = utility_funcs;
  505. }
  506. {
  507. // builtin types
  508. Array builtins;
  509. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  510. if (i == Variant::OBJECT) {
  511. continue;
  512. }
  513. Variant::Type type = Variant::Type(i);
  514. Dictionary d;
  515. d["name"] = Variant::get_type_name(type);
  516. if (Variant::has_indexing(type)) {
  517. d["indexing_return_type"] = get_builtin_or_variant_type_name(Variant::get_indexed_element_type(type));
  518. }
  519. d["is_keyed"] = Variant::is_keyed(type);
  520. {
  521. //members
  522. Array members;
  523. List<StringName> member_names;
  524. Variant::get_member_list(type, &member_names);
  525. for (const StringName &member_name : member_names) {
  526. Dictionary d2;
  527. d2["name"] = String(member_name);
  528. d2["type"] = get_builtin_or_variant_type_name(Variant::get_member_type(type, member_name));
  529. members.push_back(d2);
  530. }
  531. if (members.size()) {
  532. d["members"] = members;
  533. }
  534. }
  535. {
  536. //constants
  537. Array constants;
  538. List<StringName> constant_names;
  539. Variant::get_constants_for_type(type, &constant_names);
  540. for (const StringName &constant_name : constant_names) {
  541. Dictionary d2;
  542. d2["name"] = String(constant_name);
  543. Variant constant = Variant::get_constant_value(type, constant_name);
  544. d2["type"] = get_builtin_or_variant_type_name(constant.get_type());
  545. d2["value"] = constant.get_construct_string();
  546. constants.push_back(d2);
  547. }
  548. if (constants.size()) {
  549. d["constants"] = constants;
  550. }
  551. }
  552. {
  553. //enums
  554. Array enums;
  555. List<StringName> enum_names;
  556. Variant::get_enums_for_type(type, &enum_names);
  557. for (const StringName &enum_name : enum_names) {
  558. Dictionary enum_dict;
  559. enum_dict["name"] = String(enum_name);
  560. List<StringName> enumeration_names;
  561. Variant::get_enumerations_for_enum(type, enum_name, &enumeration_names);
  562. Array values;
  563. for (const StringName &enumeration : enumeration_names) {
  564. Dictionary values_dict;
  565. values_dict["name"] = String(enumeration);
  566. values_dict["value"] = Variant::get_enum_value(type, enum_name, enumeration);
  567. values.push_back(values_dict);
  568. }
  569. if (values.size()) {
  570. enum_dict["values"] = values;
  571. }
  572. enums.push_back(enum_dict);
  573. }
  574. if (enums.size()) {
  575. d["enums"] = enums;
  576. }
  577. }
  578. {
  579. //operators
  580. Array operators;
  581. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  582. for (int k = 0; k < Variant::OP_MAX; k++) {
  583. Variant::Type rt = Variant::get_operator_return_type(Variant::Operator(k), type, Variant::Type(j));
  584. if (rt != Variant::NIL) {
  585. Dictionary d2;
  586. d2["name"] = Variant::get_operator_name(Variant::Operator(k));
  587. if (k != Variant::OP_NEGATE && k != Variant::OP_POSITIVE && k != Variant::OP_NOT && k != Variant::OP_BIT_NEGATE) {
  588. d2["right_type"] = get_builtin_or_variant_type_name(Variant::Type(j));
  589. }
  590. d2["return_type"] = get_builtin_or_variant_type_name(Variant::get_operator_return_type(Variant::Operator(k), type, Variant::Type(j)));
  591. operators.push_back(d2);
  592. }
  593. }
  594. }
  595. if (operators.size()) {
  596. d["operators"] = operators;
  597. }
  598. }
  599. {
  600. //methods
  601. Array methods;
  602. List<StringName> method_names;
  603. Variant::get_builtin_method_list(type, &method_names);
  604. for (const StringName &method_name : method_names) {
  605. Dictionary d2;
  606. d2["name"] = String(method_name);
  607. if (Variant::has_builtin_method_return_value(type, method_name)) {
  608. Variant::Type ret_type = Variant::get_builtin_method_return_type(type, method_name);
  609. d2["return_type"] = ret_type == Variant::NIL ? String("Variant") : Variant::get_type_name(ret_type);
  610. }
  611. d2["is_vararg"] = Variant::is_builtin_method_vararg(type, method_name);
  612. d2["is_const"] = Variant::is_builtin_method_const(type, method_name);
  613. d2["is_static"] = Variant::is_builtin_method_static(type, method_name);
  614. d2["hash"] = Variant::get_builtin_method_hash(type, method_name);
  615. Vector<Variant> default_args = Variant::get_builtin_method_default_arguments(type, method_name);
  616. Array arguments;
  617. int argcount = Variant::get_builtin_method_argument_count(type, method_name);
  618. for (int j = 0; j < argcount; j++) {
  619. Dictionary d3;
  620. d3["name"] = Variant::get_builtin_method_argument_name(type, method_name, j);
  621. d3["type"] = get_builtin_or_variant_type_name(Variant::get_builtin_method_argument_type(type, method_name, j));
  622. if (j >= (argcount - default_args.size())) {
  623. int dargidx = j - (argcount - default_args.size());
  624. d3["default_value"] = default_args[dargidx].get_construct_string();
  625. }
  626. arguments.push_back(d3);
  627. }
  628. if (arguments.size()) {
  629. d2["arguments"] = arguments;
  630. }
  631. methods.push_back(d2);
  632. }
  633. if (methods.size()) {
  634. d["methods"] = methods;
  635. }
  636. }
  637. {
  638. //constructors
  639. Array constructors;
  640. for (int j = 0; j < Variant::get_constructor_count(type); j++) {
  641. Dictionary d2;
  642. d2["index"] = j;
  643. Array arguments;
  644. int argcount = Variant::get_constructor_argument_count(type, j);
  645. for (int k = 0; k < argcount; k++) {
  646. Dictionary d3;
  647. d3["name"] = Variant::get_constructor_argument_name(type, j, k);
  648. d3["type"] = get_builtin_or_variant_type_name(Variant::get_constructor_argument_type(type, j, k));
  649. arguments.push_back(d3);
  650. }
  651. if (arguments.size()) {
  652. d2["arguments"] = arguments;
  653. }
  654. constructors.push_back(d2);
  655. }
  656. if (constructors.size()) {
  657. d["constructors"] = constructors;
  658. }
  659. }
  660. {
  661. //destructor
  662. d["has_destructor"] = Variant::has_destructor(type);
  663. }
  664. builtins.push_back(d);
  665. }
  666. api_dump["builtin_classes"] = builtins;
  667. }
  668. {
  669. // classes
  670. Array classes;
  671. List<StringName> class_list;
  672. ClassDB::get_class_list(&class_list);
  673. class_list.sort_custom<StringName::AlphCompare>();
  674. for (const StringName &class_name : class_list) {
  675. Dictionary d;
  676. d["name"] = String(class_name);
  677. d["is_refcounted"] = ClassDB::is_parent_class(class_name, "RefCounted");
  678. d["is_instantiable"] = ClassDB::can_instantiate(class_name);
  679. StringName parent_class = ClassDB::get_parent_class(class_name);
  680. if (parent_class != StringName()) {
  681. d["inherits"] = String(parent_class);
  682. }
  683. {
  684. ClassDB::APIType api = ClassDB::get_api_type(class_name);
  685. static const char *api_type[5] = { "core", "editor", "extension", "editor_extension" };
  686. d["api_type"] = api_type[api];
  687. }
  688. {
  689. //constants
  690. Array constants;
  691. List<String> constant_list;
  692. ClassDB::get_integer_constant_list(class_name, &constant_list, true);
  693. for (const String &F : constant_list) {
  694. StringName enum_name = ClassDB::get_integer_constant_enum(class_name, F);
  695. if (enum_name != StringName()) {
  696. continue; //enums will be handled on their own
  697. }
  698. Dictionary d2;
  699. d2["name"] = String(F);
  700. d2["value"] = ClassDB::get_integer_constant(class_name, F);
  701. constants.push_back(d2);
  702. }
  703. if (constants.size()) {
  704. d["constants"] = constants;
  705. }
  706. }
  707. {
  708. //enum
  709. Array enums;
  710. List<StringName> enum_list;
  711. ClassDB::get_enum_list(class_name, &enum_list, true);
  712. for (const StringName &F : enum_list) {
  713. Dictionary d2;
  714. d2["name"] = String(F);
  715. d2["is_bitfield"] = ClassDB::is_enum_bitfield(class_name, F);
  716. Array values;
  717. List<StringName> enum_constant_list;
  718. ClassDB::get_enum_constants(class_name, F, &enum_constant_list, true);
  719. for (List<StringName>::Element *G = enum_constant_list.front(); G; G = G->next()) {
  720. Dictionary d3;
  721. d3["name"] = String(G->get());
  722. d3["value"] = ClassDB::get_integer_constant(class_name, G->get());
  723. values.push_back(d3);
  724. }
  725. d2["values"] = values;
  726. enums.push_back(d2);
  727. }
  728. if (enums.size()) {
  729. d["enums"] = enums;
  730. }
  731. }
  732. {
  733. //methods
  734. Array methods;
  735. List<MethodInfo> method_list;
  736. ClassDB::get_method_list(class_name, &method_list, true);
  737. for (const MethodInfo &F : method_list) {
  738. StringName method_name = F.name;
  739. if ((F.flags & METHOD_FLAG_VIRTUAL) && !(F.flags & METHOD_FLAG_OBJECT_CORE)) {
  740. //virtual method
  741. const MethodInfo &mi = F;
  742. Dictionary d2;
  743. d2["name"] = String(method_name);
  744. d2["is_const"] = (F.flags & METHOD_FLAG_CONST) ? true : false;
  745. d2["is_static"] = (F.flags & METHOD_FLAG_STATIC) ? true : false;
  746. d2["is_vararg"] = false;
  747. d2["is_virtual"] = true;
  748. // virtual functions have no hash since no MethodBind is involved
  749. bool has_return = mi.return_val.type != Variant::NIL || (mi.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT);
  750. Array arguments;
  751. for (int i = (has_return ? -1 : 0); i < mi.arguments.size(); i++) {
  752. PropertyInfo pinfo = i == -1 ? mi.return_val : mi.arguments[i];
  753. Dictionary d3;
  754. if (i >= 0) {
  755. d3["name"] = pinfo.name;
  756. }
  757. d3["type"] = get_property_info_type_name(pinfo);
  758. if (i == -1) {
  759. d2["return_value"] = d3;
  760. } else {
  761. arguments.push_back(d3);
  762. }
  763. }
  764. if (arguments.size()) {
  765. d2["arguments"] = arguments;
  766. }
  767. methods.push_back(d2);
  768. } else if (F.name.begins_with("_")) {
  769. //hidden method, ignore
  770. } else {
  771. Dictionary d2;
  772. d2["name"] = String(method_name);
  773. MethodBind *method = ClassDB::get_method(class_name, method_name);
  774. if (!method) {
  775. continue;
  776. }
  777. d2["is_const"] = method->is_const();
  778. d2["is_vararg"] = method->is_vararg();
  779. d2["is_static"] = method->is_static();
  780. d2["is_virtual"] = false;
  781. d2["hash"] = method->get_hash();
  782. Vector<Variant> default_args = method->get_default_arguments();
  783. Array arguments;
  784. for (int i = (method->has_return() ? -1 : 0); i < method->get_argument_count(); i++) {
  785. PropertyInfo pinfo = i == -1 ? method->get_return_info() : method->get_argument_info(i);
  786. Dictionary d3;
  787. if (i >= 0) {
  788. d3["name"] = pinfo.name;
  789. }
  790. d3["type"] = get_property_info_type_name(pinfo);
  791. if (method->get_argument_meta(i) > 0) {
  792. static const char *argmeta[11] = { "none", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64", "float", "double" };
  793. d3["meta"] = argmeta[method->get_argument_meta(i)];
  794. }
  795. if (i >= 0 && i >= (method->get_argument_count() - default_args.size())) {
  796. int dargidx = i - (method->get_argument_count() - default_args.size());
  797. d3["default_value"] = default_args[dargidx].get_construct_string();
  798. }
  799. if (i == -1) {
  800. d2["return_value"] = d3;
  801. } else {
  802. arguments.push_back(d3);
  803. }
  804. }
  805. if (arguments.size()) {
  806. d2["arguments"] = arguments;
  807. }
  808. methods.push_back(d2);
  809. }
  810. }
  811. if (methods.size()) {
  812. d["methods"] = methods;
  813. }
  814. }
  815. {
  816. //signals
  817. Array signals;
  818. List<MethodInfo> signal_list;
  819. ClassDB::get_signal_list(class_name, &signal_list, true);
  820. for (const MethodInfo &F : signal_list) {
  821. StringName signal_name = F.name;
  822. Dictionary d2;
  823. d2["name"] = String(signal_name);
  824. Array arguments;
  825. for (int i = 0; i < F.arguments.size(); i++) {
  826. Dictionary d3;
  827. d3["name"] = F.arguments[i].name;
  828. d3["type"] = get_property_info_type_name(F.arguments[i]);
  829. arguments.push_back(d3);
  830. }
  831. if (arguments.size()) {
  832. d2["arguments"] = arguments;
  833. }
  834. signals.push_back(d2);
  835. }
  836. if (signals.size()) {
  837. d["signals"] = signals;
  838. }
  839. }
  840. {
  841. //properties
  842. Array properties;
  843. List<PropertyInfo> property_list;
  844. ClassDB::get_property_list(class_name, &property_list, true);
  845. for (const PropertyInfo &F : property_list) {
  846. if (F.usage & PROPERTY_USAGE_CATEGORY || F.usage & PROPERTY_USAGE_GROUP || F.usage & PROPERTY_USAGE_SUBGROUP || (F.type == Variant::NIL && F.usage & PROPERTY_USAGE_ARRAY)) {
  847. continue; //not real properties
  848. }
  849. if (F.name.begins_with("_")) {
  850. continue; //hidden property
  851. }
  852. if (F.name.find("/") >= 0) {
  853. // Ignore properties with '/' (slash) in the name. These are only meant for use in the inspector.
  854. continue;
  855. }
  856. StringName property_name = F.name;
  857. Dictionary d2;
  858. d2["type"] = get_property_info_type_name(F);
  859. d2["name"] = String(property_name);
  860. StringName setter = ClassDB::get_property_setter(class_name, F.name);
  861. if (!(setter == "")) {
  862. d2["setter"] = setter;
  863. }
  864. StringName getter = ClassDB::get_property_getter(class_name, F.name);
  865. if (!(getter == "")) {
  866. d2["getter"] = getter;
  867. }
  868. int index = ClassDB::get_property_index(class_name, F.name);
  869. if (index != -1) {
  870. d2["index"] = index;
  871. }
  872. properties.push_back(d2);
  873. }
  874. if (properties.size()) {
  875. d["properties"] = properties;
  876. }
  877. }
  878. classes.push_back(d);
  879. }
  880. api_dump["classes"] = classes;
  881. }
  882. {
  883. // singletons
  884. Array singletons;
  885. List<Engine::Singleton> singleton_list;
  886. Engine::get_singleton()->get_singletons(&singleton_list);
  887. for (const Engine::Singleton &s : singleton_list) {
  888. Dictionary d;
  889. d["name"] = s.name;
  890. if (s.class_name != StringName()) {
  891. d["type"] = String(s.class_name);
  892. } else {
  893. d["type"] = String(s.ptr->get_class());
  894. }
  895. singletons.push_back(d);
  896. }
  897. if (singletons.size()) {
  898. api_dump["singletons"] = singletons;
  899. }
  900. }
  901. {
  902. Array native_structures;
  903. List<StringName> native_structs;
  904. ClassDB::get_native_struct_list(&native_structs);
  905. native_structs.sort_custom<StringName::AlphCompare>();
  906. for (const StringName &E : native_structs) {
  907. String code = ClassDB::get_native_struct_code(E);
  908. Dictionary d;
  909. d["name"] = String(E);
  910. d["format"] = code;
  911. native_structures.push_back(d);
  912. }
  913. api_dump["native_structures"] = native_structures;
  914. }
  915. return api_dump;
  916. }
  917. void GDExtensionAPIDump::generate_extension_json_file(const String &p_path) {
  918. Dictionary api = generate_extension_api();
  919. Ref<JSON> json;
  920. json.instantiate();
  921. String text = json->stringify(api, "\t", false) + "\n";
  922. Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::WRITE);
  923. fa->store_string(text);
  924. }
  925. #endif // TOOLS_ENABLED