extension_api_dump.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /*************************************************************************/
  2. /* extension_api_dump.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 "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_type_name(const PropertyInfo &p_info) {
  39. if (p_info.type == Variant::INT && (p_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM)) {
  40. return String("enum::") + String(p_info.class_name);
  41. }
  42. if (p_info.class_name != StringName()) {
  43. return p_info.class_name;
  44. }
  45. if (p_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  46. return p_info.hint_string;
  47. }
  48. if (p_info.type == Variant::NIL && (p_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  49. return "Variant";
  50. }
  51. if (p_info.type == Variant::NIL) {
  52. return "void";
  53. }
  54. return Variant::get_type_name(p_info.type);
  55. }
  56. Dictionary NativeExtensionAPIDump::generate_extension_api() {
  57. Dictionary api_dump;
  58. {
  59. //header
  60. Dictionary header;
  61. header["version_major"] = VERSION_MAJOR;
  62. header["version_minor"] = VERSION_MINOR;
  63. #if VERSION_PATCH
  64. header["version_patch"] = VERSION_PATCH;
  65. #else
  66. header["version_patch"] = 0;
  67. #endif
  68. header["version_status"] = VERSION_STATUS;
  69. header["version_build"] = VERSION_BUILD;
  70. header["version_full_name"] = VERSION_FULL_NAME;
  71. api_dump["header"] = header;
  72. }
  73. const uint32_t vec3_elems = 3;
  74. const uint32_t ptrsize_32 = 4;
  75. const uint32_t ptrsize_64 = 8;
  76. static const char *build_config_name[4] = { "float_32", "float_64", "double_32", "double_64" };
  77. {
  78. //type sizes
  79. constexpr struct {
  80. Variant::Type type;
  81. uint32_t size_32_bits_real_float;
  82. uint32_t size_64_bits_real_float;
  83. uint32_t size_32_bits_real_double;
  84. uint32_t size_64_bits_real_double;
  85. // For compile-time size check.
  86. constexpr uint32_t operator[](int index) const {
  87. switch (index) {
  88. #ifndef REAL_T_IS_DOUBLE
  89. case sizeof(uint32_t):
  90. return size_32_bits_real_float;
  91. case sizeof(uint64_t):
  92. return size_64_bits_real_float;
  93. #else // REAL_T_IS_DOUBLE
  94. case sizeof(uint32_t):
  95. return size_32_bits_real_double;
  96. case sizeof(uint64_t):
  97. return size_64_bits_real_double;
  98. #endif
  99. }
  100. return -1;
  101. }
  102. } type_size_array[Variant::VARIANT_MAX + 1] = {
  103. { Variant::NIL, 0, 0, 0, 0 },
  104. { Variant::BOOL, sizeof(uint8_t), sizeof(uint8_t), sizeof(uint8_t), sizeof(uint8_t) },
  105. { Variant::INT, sizeof(int64_t), sizeof(int64_t), sizeof(int64_t), sizeof(int64_t) },
  106. { Variant::FLOAT, sizeof(double), sizeof(double), sizeof(double), sizeof(double) },
  107. { Variant::STRING, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  108. { Variant::VECTOR2, 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(double), 2 * sizeof(double) },
  109. { Variant::VECTOR2I, 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t) },
  110. { Variant::RECT2, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(double), 4 * sizeof(double) },
  111. { Variant::RECT2I, 4 * sizeof(int32_t), 4 * sizeof(int32_t), 4 * sizeof(int32_t), 4 * sizeof(int32_t) },
  112. { Variant::VECTOR3, vec3_elems * sizeof(float), vec3_elems * sizeof(float), vec3_elems * sizeof(double), vec3_elems * sizeof(double) },
  113. { Variant::VECTOR3I, 3 * sizeof(int32_t), 3 * sizeof(int32_t), 3 * sizeof(int32_t), 3 * sizeof(int32_t) },
  114. { Variant::TRANSFORM2D, 6 * sizeof(float), 6 * sizeof(float), 6 * sizeof(double), 6 * sizeof(double) },
  115. { Variant::PLANE, (vec3_elems + 1) * sizeof(float), (vec3_elems + 1) * sizeof(float), (vec3_elems + 1) * sizeof(double), (vec3_elems + 1) * sizeof(double) },
  116. { Variant::QUATERNION, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(double), 4 * sizeof(double) },
  117. { Variant::AABB, (vec3_elems * 2) * sizeof(float), (vec3_elems * 2) * sizeof(float), (vec3_elems * 2) * sizeof(double), (vec3_elems * 2) * sizeof(double) },
  118. { Variant::BASIS, (vec3_elems * 3) * sizeof(float), (vec3_elems * 3) * sizeof(float), (vec3_elems * 3) * sizeof(double), (vec3_elems * 3) * sizeof(double) },
  119. { Variant::TRANSFORM3D, (vec3_elems * 4) * sizeof(float), (vec3_elems * 4) * sizeof(float), (vec3_elems * 4) * sizeof(double), (vec3_elems * 4) * sizeof(double) },
  120. { Variant::COLOR, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(float) },
  121. { Variant::STRING_NAME, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  122. { Variant::NODE_PATH, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  123. { Variant::RID, sizeof(uint64_t), sizeof(uint64_t), sizeof(uint64_t), sizeof(uint64_t) },
  124. { Variant::OBJECT, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  125. { Variant::CALLABLE, sizeof(Callable), sizeof(Callable), sizeof(Callable), sizeof(Callable) }, // Hardcoded align.
  126. { Variant::SIGNAL, sizeof(Signal), sizeof(Signal), sizeof(Signal), sizeof(Signal) }, // Hardcoded align.
  127. { Variant::DICTIONARY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  128. { Variant::ARRAY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  129. { Variant::PACKED_BYTE_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  130. { Variant::PACKED_INT32_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  131. { Variant::PACKED_INT64_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  132. { Variant::PACKED_FLOAT32_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  133. { Variant::PACKED_FLOAT64_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  134. { Variant::PACKED_STRING_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  135. { Variant::PACKED_VECTOR2_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  136. { Variant::PACKED_VECTOR3_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  137. { Variant::PACKED_COLOR_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  138. { 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 },
  139. };
  140. // Validate sizes at compile time for the current build configuration.
  141. static_assert(type_size_array[Variant::BOOL][sizeof(void *)] == sizeof(GDNativeBool), "Size of bool mismatch");
  142. static_assert(type_size_array[Variant::INT][sizeof(void *)] == sizeof(GDNativeInt), "Size of int mismatch");
  143. static_assert(type_size_array[Variant::FLOAT][sizeof(void *)] == sizeof(double), "Size of float mismatch");
  144. static_assert(type_size_array[Variant::STRING][sizeof(void *)] == sizeof(String), "Size of String mismatch");
  145. static_assert(type_size_array[Variant::VECTOR2][sizeof(void *)] == sizeof(Vector2), "Size of Vector2 mismatch");
  146. static_assert(type_size_array[Variant::VECTOR2I][sizeof(void *)] == sizeof(Vector2i), "Size of Vector2i mismatch");
  147. static_assert(type_size_array[Variant::RECT2][sizeof(void *)] == sizeof(Rect2), "Size of Rect2 mismatch");
  148. static_assert(type_size_array[Variant::RECT2I][sizeof(void *)] == sizeof(Rect2i), "Size of Rect2i mismatch");
  149. static_assert(type_size_array[Variant::VECTOR3][sizeof(void *)] == sizeof(Vector3), "Size of Vector3 mismatch");
  150. static_assert(type_size_array[Variant::VECTOR3I][sizeof(void *)] == sizeof(Vector3i), "Size of Vector3i mismatch");
  151. static_assert(type_size_array[Variant::TRANSFORM2D][sizeof(void *)] == sizeof(Transform2D), "Size of Transform2D mismatch");
  152. static_assert(type_size_array[Variant::PLANE][sizeof(void *)] == sizeof(Plane), "Size of Plane mismatch");
  153. static_assert(type_size_array[Variant::QUATERNION][sizeof(void *)] == sizeof(Quaternion), "Size of Quaternion mismatch");
  154. static_assert(type_size_array[Variant::AABB][sizeof(void *)] == sizeof(AABB), "Size of AABB mismatch");
  155. static_assert(type_size_array[Variant::BASIS][sizeof(void *)] == sizeof(Basis), "Size of Basis mismatch");
  156. static_assert(type_size_array[Variant::TRANSFORM3D][sizeof(void *)] == sizeof(Transform3D), "Size of Transform3D mismatch");
  157. static_assert(type_size_array[Variant::COLOR][sizeof(void *)] == sizeof(Color), "Size of Color mismatch");
  158. static_assert(type_size_array[Variant::STRING_NAME][sizeof(void *)] == sizeof(StringName), "Size of StringName mismatch");
  159. static_assert(type_size_array[Variant::NODE_PATH][sizeof(void *)] == sizeof(NodePath), "Size of NodePath mismatch");
  160. static_assert(type_size_array[Variant::RID][sizeof(void *)] == sizeof(RID), "Size of RID mismatch");
  161. static_assert(type_size_array[Variant::OBJECT][sizeof(void *)] == sizeof(Object *), "Size of Object mismatch");
  162. static_assert(type_size_array[Variant::CALLABLE][sizeof(void *)] == sizeof(Callable), "Size of Callable mismatch");
  163. static_assert(type_size_array[Variant::SIGNAL][sizeof(void *)] == sizeof(Signal), "Size of Signal mismatch");
  164. static_assert(type_size_array[Variant::DICTIONARY][sizeof(void *)] == sizeof(Dictionary), "Size of Dictionary mismatch");
  165. static_assert(type_size_array[Variant::ARRAY][sizeof(void *)] == sizeof(Array), "Size of Array mismatch");
  166. static_assert(type_size_array[Variant::PACKED_BYTE_ARRAY][sizeof(void *)] == sizeof(PackedByteArray), "Size of PackedByteArray mismatch");
  167. static_assert(type_size_array[Variant::PACKED_INT32_ARRAY][sizeof(void *)] == sizeof(PackedInt32Array), "Size of PackedInt32Array mismatch");
  168. static_assert(type_size_array[Variant::PACKED_INT64_ARRAY][sizeof(void *)] == sizeof(PackedInt64Array), "Size of PackedInt64Array mismatch");
  169. static_assert(type_size_array[Variant::PACKED_FLOAT32_ARRAY][sizeof(void *)] == sizeof(PackedFloat32Array), "Size of PackedFloat32Array mismatch");
  170. static_assert(type_size_array[Variant::PACKED_FLOAT64_ARRAY][sizeof(void *)] == sizeof(PackedFloat64Array), "Size of PackedFloat64Array mismatch");
  171. static_assert(type_size_array[Variant::PACKED_STRING_ARRAY][sizeof(void *)] == sizeof(PackedStringArray), "Size of PackedStringArray mismatch");
  172. static_assert(type_size_array[Variant::PACKED_VECTOR2_ARRAY][sizeof(void *)] == sizeof(PackedVector2Array), "Size of PackedVector2Array mismatch");
  173. static_assert(type_size_array[Variant::PACKED_VECTOR3_ARRAY][sizeof(void *)] == sizeof(PackedVector3Array), "Size of PackedVector3Array mismatch");
  174. static_assert(type_size_array[Variant::PACKED_COLOR_ARRAY][sizeof(void *)] == sizeof(PackedColorArray), "Size of PackedColorArray mismatch");
  175. static_assert(type_size_array[Variant::VARIANT_MAX][sizeof(void *)] == sizeof(Variant), "Size of Variant mismatch");
  176. Array core_type_sizes;
  177. for (int i = 0; i < 4; i++) {
  178. Dictionary d;
  179. d["build_configuration"] = build_config_name[i];
  180. Array sizes;
  181. for (int j = 0; j <= Variant::VARIANT_MAX; j++) {
  182. Variant::Type t = type_size_array[j].type;
  183. String name = t == Variant::VARIANT_MAX ? String("Variant") : Variant::get_type_name(t);
  184. Dictionary d2;
  185. d2["name"] = name;
  186. uint32_t size;
  187. switch (i) {
  188. case 0:
  189. size = type_size_array[j].size_32_bits_real_float;
  190. break;
  191. case 1:
  192. size = type_size_array[j].size_64_bits_real_float;
  193. break;
  194. case 2:
  195. size = type_size_array[j].size_32_bits_real_double;
  196. break;
  197. case 3:
  198. size = type_size_array[j].size_64_bits_real_double;
  199. break;
  200. }
  201. d2["size"] = size;
  202. sizes.push_back(d2);
  203. }
  204. d["sizes"] = sizes;
  205. core_type_sizes.push_back(d);
  206. }
  207. api_dump["builtin_class_sizes"] = core_type_sizes;
  208. }
  209. {
  210. // Member offsets sizes.
  211. struct {
  212. Variant::Type type;
  213. const char *member;
  214. uint32_t offset_32_bits_real_float;
  215. uint32_t offset_64_bits_real_float;
  216. uint32_t offset_32_bits_real_double;
  217. uint32_t offset_64_bits_real_double;
  218. } member_offset_array[] = {
  219. { Variant::VECTOR2, "x", 0, 0, 0, 0 },
  220. { Variant::VECTOR2, "y", sizeof(float), sizeof(float), sizeof(double), sizeof(double) },
  221. { Variant::VECTOR2I, "x", 0, 0, 0, 0 },
  222. { Variant::VECTOR2I, "y", sizeof(int32_t), sizeof(int32_t), sizeof(int32_t), sizeof(int32_t) },
  223. { Variant::RECT2, "position", 0, 0, 0, 0 },
  224. { Variant::RECT2, "size", 2 * sizeof(Vector2), 2 * sizeof(float), 2 * sizeof(double), 2 * sizeof(double) },
  225. { Variant::RECT2I, "position", 0, 0, 0, 0 },
  226. { Variant::RECT2I, "size", 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t) },
  227. { Variant::VECTOR3, "x", 0, 0, 0, 0 },
  228. { Variant::VECTOR3, "y", sizeof(float), sizeof(float), sizeof(double), sizeof(double) },
  229. { Variant::VECTOR3, "z", 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(double), 2 * sizeof(double) },
  230. { Variant::VECTOR3I, "x", 0, 0, 0, 0 },
  231. { Variant::VECTOR3I, "y", sizeof(int32_t), sizeof(int32_t), sizeof(int32_t), sizeof(int32_t) },
  232. { Variant::VECTOR3I, "z", 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t) },
  233. { Variant::TRANSFORM2D, "x", 0, 0, 0, 0 },
  234. { Variant::TRANSFORM2D, "y", 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(double), 2 * sizeof(double) },
  235. { Variant::TRANSFORM2D, "origin", 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(double), 4 * sizeof(double) },
  236. { Variant::PLANE, "normal", 0, 0, 0, 0 },
  237. { Variant::PLANE, "d", vec3_elems * sizeof(float), vec3_elems * sizeof(float), vec3_elems * sizeof(double), vec3_elems * sizeof(double) },
  238. { Variant::QUATERNION, "x", 0, 0, 0, 0 },
  239. { Variant::QUATERNION, "y", sizeof(float), sizeof(float), sizeof(double), sizeof(double) },
  240. { Variant::QUATERNION, "z", 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(double), 2 * sizeof(double) },
  241. { Variant::QUATERNION, "w", 3 * sizeof(float), 3 * sizeof(float), 3 * sizeof(double), 3 * sizeof(double) },
  242. { Variant::AABB, "position", 0, 0, 0, 0 },
  243. { Variant::AABB, "size", vec3_elems * sizeof(float), vec3_elems * sizeof(float), vec3_elems * sizeof(double), vec3_elems * sizeof(double) },
  244. // Remember that basis vectors are flipped!
  245. { Variant::BASIS, "x", 0, 0, 0, 0 },
  246. { Variant::BASIS, "y", vec3_elems * sizeof(float), vec3_elems * sizeof(float), vec3_elems * sizeof(double), vec3_elems * sizeof(double) },
  247. { Variant::BASIS, "z", vec3_elems * 2 * sizeof(float), vec3_elems * 2 * sizeof(float), vec3_elems * 2 * sizeof(double), vec3_elems * 2 * sizeof(double) },
  248. { Variant::TRANSFORM3D, "basis", 0, 0, 0, 0 },
  249. { Variant::TRANSFORM3D, "origin", (vec3_elems * 3) * sizeof(float), (vec3_elems * 3) * sizeof(float), (vec3_elems * 3) * sizeof(double), (vec3_elems * 3) * sizeof(double) },
  250. { Variant::COLOR, "x", 0, 0, 0, 0 },
  251. { Variant::COLOR, "y", sizeof(float), sizeof(float), sizeof(float), sizeof(float) },
  252. { Variant::COLOR, "z", 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(float) },
  253. { Variant::COLOR, "w", 3 * sizeof(float), 3 * sizeof(float), 3 * sizeof(float), 3 * sizeof(float) },
  254. { Variant::NIL, nullptr, 0, 0, 0, 0 },
  255. };
  256. Array core_type_member_offsets;
  257. for (int i = 0; i < 4; i++) {
  258. Dictionary d;
  259. d["build_configuration"] = build_config_name[i];
  260. Array type_offsets;
  261. uint32_t idx = 0;
  262. Variant::Type last_type = Variant::NIL;
  263. Dictionary d2;
  264. Array members;
  265. while (true) {
  266. Variant::Type t = member_offset_array[idx].type;
  267. if (t != last_type) {
  268. if (last_type != Variant::NIL) {
  269. d2["members"] = members;
  270. type_offsets.push_back(d2);
  271. }
  272. if (t == Variant::NIL) {
  273. break;
  274. }
  275. String name = t == Variant::VARIANT_MAX ? String("Variant") : Variant::get_type_name(t);
  276. d2 = Dictionary();
  277. members = Array();
  278. d2["name"] = name;
  279. last_type = t;
  280. }
  281. Dictionary d3;
  282. uint32_t offset;
  283. switch (i) {
  284. case 0:
  285. offset = member_offset_array[idx].offset_32_bits_real_float;
  286. break;
  287. case 1:
  288. offset = member_offset_array[idx].offset_64_bits_real_float;
  289. break;
  290. case 2:
  291. offset = member_offset_array[idx].offset_32_bits_real_double;
  292. break;
  293. case 3:
  294. offset = member_offset_array[idx].offset_64_bits_real_double;
  295. break;
  296. }
  297. d3["member"] = member_offset_array[idx].member;
  298. d3["offset"] = offset;
  299. members.push_back(d3);
  300. idx++;
  301. }
  302. d["classes"] = type_offsets;
  303. core_type_member_offsets.push_back(d);
  304. }
  305. api_dump["builtin_class_member_offsets"] = core_type_member_offsets;
  306. }
  307. {
  308. // Global enums and constants.
  309. Array constants;
  310. Map<String, List<Pair<String, int>>> enum_list;
  311. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  312. int value = CoreConstants::get_global_constant_value(i);
  313. String enum_name = CoreConstants::get_global_constant_enum(i);
  314. String name = CoreConstants::get_global_constant_name(i);
  315. if (enum_name != String()) {
  316. enum_list[enum_name].push_back(Pair<String, int>(name, value));
  317. } else {
  318. Dictionary d;
  319. d["name"] = name;
  320. d["value"] = value;
  321. constants.push_back(d);
  322. }
  323. }
  324. api_dump["global_constants"] = constants;
  325. Array enums;
  326. for (Map<String, List<Pair<String, int>>>::Element *E = enum_list.front(); E; E = E->next()) {
  327. Dictionary d1;
  328. d1["name"] = E->key();
  329. Array values;
  330. for (const Pair<String, int> &F : E->get()) {
  331. Dictionary d2;
  332. d2["name"] = F.first;
  333. d2["value"] = F.second;
  334. values.push_back(d2);
  335. }
  336. d1["values"] = values;
  337. enums.push_back(d1);
  338. }
  339. api_dump["global_enums"] = enums;
  340. }
  341. {
  342. Array utility_funcs;
  343. List<StringName> utility_func_names;
  344. Variant::get_utility_function_list(&utility_func_names);
  345. for (const StringName &name : utility_func_names) {
  346. Dictionary func;
  347. func["name"] = String(name);
  348. if (Variant::has_utility_function_return_value(name)) {
  349. Variant::Type rt = Variant::get_utility_function_return_type(name);
  350. func["return_type"] = rt == Variant::NIL ? String("Variant") : Variant::get_type_name(rt);
  351. }
  352. switch (Variant::get_utility_function_type(name)) {
  353. case Variant::UTILITY_FUNC_TYPE_MATH:
  354. func["category"] = "math";
  355. break;
  356. case Variant::UTILITY_FUNC_TYPE_RANDOM:
  357. func["category"] = "random";
  358. break;
  359. case Variant::UTILITY_FUNC_TYPE_GENERAL:
  360. func["category"] = "general";
  361. break;
  362. }
  363. bool vararg = Variant::is_utility_function_vararg(name);
  364. func["is_vararg"] = Variant::is_utility_function_vararg(name);
  365. func["hash"] = Variant::get_utility_function_hash(name);
  366. Array arguments;
  367. int argcount = Variant::get_utility_function_argument_count(name);
  368. for (int i = 0; i < argcount; i++) {
  369. Dictionary arg;
  370. String argname = vararg ? "arg" + itos(i + 1) : Variant::get_utility_function_argument_name(name, i);
  371. arg["name"] = argname;
  372. Variant::Type argtype = Variant::get_utility_function_argument_type(name, i);
  373. arg["type"] = argtype == Variant::NIL ? String("Variant") : Variant::get_type_name(argtype);
  374. //no default value support in utility functions
  375. arguments.push_back(arg);
  376. }
  377. if (arguments.size()) {
  378. func["arguments"] = arguments;
  379. }
  380. utility_funcs.push_back(func);
  381. }
  382. api_dump["utility_functions"] = utility_funcs;
  383. }
  384. {
  385. // builtin types
  386. Array builtins;
  387. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  388. if (i == Variant::OBJECT) {
  389. continue;
  390. }
  391. Variant::Type type = Variant::Type(i);
  392. Dictionary d;
  393. d["name"] = Variant::get_type_name(type);
  394. if (Variant::has_indexing(type)) {
  395. Variant::Type index_type = Variant::get_indexed_element_type(type);
  396. d["indexing_return_type"] = index_type == Variant::NIL ? String("Variant") : Variant::get_type_name(index_type);
  397. }
  398. d["is_keyed"] = Variant::ValidatedKeyedSetter(type);
  399. {
  400. //members
  401. Array members;
  402. List<StringName> member_names;
  403. Variant::get_member_list(type, &member_names);
  404. for (const StringName &member_name : member_names) {
  405. Dictionary d2;
  406. d2["name"] = String(member_name);
  407. d2["type"] = Variant::get_type_name(Variant::get_member_type(type, member_name));
  408. members.push_back(d2);
  409. }
  410. if (members.size()) {
  411. d["members"] = members;
  412. }
  413. }
  414. {
  415. //constants
  416. Array constants;
  417. List<StringName> constant_names;
  418. Variant::get_constants_for_type(type, &constant_names);
  419. for (const StringName &constant_name : constant_names) {
  420. Dictionary d2;
  421. d2["name"] = String(constant_name);
  422. Variant constant = Variant::get_constant_value(type, constant_name);
  423. d2["type"] = Variant::get_type_name(constant.get_type());
  424. d2["value"] = constant.get_construct_string();
  425. constants.push_back(d2);
  426. }
  427. if (constants.size()) {
  428. d["constants"] = constants;
  429. }
  430. }
  431. {
  432. //operators
  433. Array operators;
  434. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  435. for (int k = 0; k < Variant::OP_MAX; k++) {
  436. Variant::Type rt = Variant::get_operator_return_type(Variant::Operator(k), type, Variant::Type(j));
  437. if (rt != Variant::NIL) {
  438. Dictionary d2;
  439. d2["name"] = Variant::get_operator_name(Variant::Operator(k));
  440. if (k != Variant::OP_NEGATE && k != Variant::OP_POSITIVE && k != Variant::OP_NOT && k != Variant::OP_BIT_NEGATE) {
  441. d2["right_type"] = Variant::get_type_name(Variant::Type(j));
  442. }
  443. d2["return_type"] = Variant::get_type_name(Variant::get_operator_return_type(Variant::Operator(k), type, Variant::Type(j)));
  444. operators.push_back(d2);
  445. }
  446. }
  447. }
  448. if (operators.size()) {
  449. d["operators"] = operators;
  450. }
  451. }
  452. {
  453. //methods
  454. Array methods;
  455. List<StringName> method_names;
  456. Variant::get_builtin_method_list(type, &method_names);
  457. for (const StringName &method_name : method_names) {
  458. Dictionary d2;
  459. d2["name"] = String(method_name);
  460. if (Variant::has_builtin_method_return_value(type, method_name)) {
  461. Variant::Type ret_type = Variant::get_builtin_method_return_type(type, method_name);
  462. d2["return_type"] = ret_type == Variant::NIL ? String("Variant") : Variant::get_type_name(ret_type);
  463. }
  464. d2["is_vararg"] = Variant::is_builtin_method_vararg(type, method_name);
  465. d2["is_const"] = Variant::is_builtin_method_const(type, method_name);
  466. d2["is_static"] = Variant::is_builtin_method_static(type, method_name);
  467. d2["hash"] = Variant::get_builtin_method_hash(type, method_name);
  468. Vector<Variant> default_args = Variant::get_builtin_method_default_arguments(type, method_name);
  469. Array arguments;
  470. int argcount = Variant::get_builtin_method_argument_count(type, method_name);
  471. for (int j = 0; j < argcount; j++) {
  472. Dictionary d3;
  473. d3["name"] = Variant::get_builtin_method_argument_name(type, method_name, j);
  474. Variant::Type argtype = Variant::get_builtin_method_argument_type(type, method_name, j);
  475. d3["type"] = argtype == Variant::NIL ? String("Variant") : Variant::get_type_name(argtype);
  476. if (j >= (argcount - default_args.size())) {
  477. int dargidx = j - (argcount - default_args.size());
  478. d3["default_value"] = default_args[dargidx].get_construct_string();
  479. }
  480. arguments.push_back(d3);
  481. }
  482. if (arguments.size()) {
  483. d2["arguments"] = arguments;
  484. }
  485. methods.push_back(d2);
  486. }
  487. if (methods.size()) {
  488. d["methods"] = methods;
  489. }
  490. }
  491. {
  492. //constructors
  493. Array constructors;
  494. for (int j = 0; j < Variant::get_constructor_count(type); j++) {
  495. Dictionary d2;
  496. d2["index"] = j;
  497. Array arguments;
  498. int argcount = Variant::get_constructor_argument_count(type, j);
  499. for (int k = 0; k < argcount; k++) {
  500. Dictionary d3;
  501. d3["name"] = Variant::get_constructor_argument_name(type, j, k);
  502. d3["type"] = Variant::get_type_name(Variant::get_constructor_argument_type(type, j, k));
  503. arguments.push_back(d3);
  504. }
  505. if (arguments.size()) {
  506. d2["arguments"] = arguments;
  507. }
  508. constructors.push_back(d2);
  509. }
  510. if (constructors.size()) {
  511. d["constructors"] = constructors;
  512. }
  513. }
  514. {
  515. //destructor
  516. d["has_destructor"] = Variant::has_destructor(type);
  517. }
  518. builtins.push_back(d);
  519. }
  520. api_dump["builtin_classes"] = builtins;
  521. }
  522. {
  523. // classes
  524. Array classes;
  525. List<StringName> class_list;
  526. ClassDB::get_class_list(&class_list);
  527. class_list.sort_custom<StringName::AlphCompare>();
  528. for (const StringName &class_name : class_list) {
  529. Dictionary d;
  530. d["name"] = String(class_name);
  531. d["is_refcounted"] = ClassDB::is_parent_class(class_name, "RefCounted");
  532. d["is_instantiable"] = ClassDB::can_instantiate(class_name);
  533. StringName parent_class = ClassDB::get_parent_class(class_name);
  534. if (parent_class != StringName()) {
  535. d["inherits"] = String(parent_class);
  536. }
  537. {
  538. ClassDB::APIType api = ClassDB::get_api_type(class_name);
  539. static const char *api_type[5] = { "core", "editor", "extension", "editor_extension" };
  540. d["api_type"] = api_type[api];
  541. }
  542. {
  543. //constants
  544. Array constants;
  545. List<String> constant_list;
  546. ClassDB::get_integer_constant_list(class_name, &constant_list, true);
  547. for (const String &F : constant_list) {
  548. StringName enum_name = ClassDB::get_integer_constant_enum(class_name, F);
  549. if (enum_name != StringName()) {
  550. continue; //enums will be handled on their own
  551. }
  552. Dictionary d2;
  553. d2["name"] = String(F);
  554. d2["value"] = ClassDB::get_integer_constant(class_name, F);
  555. constants.push_back(d2);
  556. }
  557. if (constants.size()) {
  558. d["constants"] = constants;
  559. }
  560. }
  561. {
  562. //enum
  563. Array enums;
  564. List<StringName> enum_list;
  565. ClassDB::get_enum_list(class_name, &enum_list, true);
  566. for (const StringName &F : enum_list) {
  567. Dictionary d2;
  568. d2["name"] = String(F);
  569. Array values;
  570. List<StringName> enum_constant_list;
  571. ClassDB::get_enum_constants(class_name, F, &enum_constant_list, true);
  572. for (List<StringName>::Element *G = enum_constant_list.front(); G; G = G->next()) {
  573. Dictionary d3;
  574. d3["name"] = String(G->get());
  575. d3["value"] = ClassDB::get_integer_constant(class_name, G->get());
  576. values.push_back(d3);
  577. }
  578. d2["values"] = values;
  579. enums.push_back(d2);
  580. }
  581. if (enums.size()) {
  582. d["enums"] = enums;
  583. }
  584. }
  585. {
  586. //methods
  587. Array methods;
  588. List<MethodInfo> method_list;
  589. ClassDB::get_method_list(class_name, &method_list, true);
  590. for (const MethodInfo &F : method_list) {
  591. StringName method_name = F.name;
  592. if ((F.flags & METHOD_FLAG_VIRTUAL) && !(F.flags & METHOD_FLAG_OBJECT_CORE)) {
  593. //virtual method
  594. const MethodInfo &mi = F;
  595. Dictionary d2;
  596. d2["name"] = String(method_name);
  597. d2["is_const"] = (F.flags & METHOD_FLAG_CONST) ? true : false;
  598. d2["is_vararg"] = false;
  599. d2["is_virtual"] = true;
  600. // virtual functions have no hash since no MethodBind is involved
  601. bool has_return = mi.return_val.type != Variant::NIL || (mi.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT);
  602. Array arguments;
  603. for (int i = (has_return ? -1 : 0); i < mi.arguments.size(); i++) {
  604. PropertyInfo pinfo = i == -1 ? mi.return_val : mi.arguments[i];
  605. Dictionary d3;
  606. if (i >= 0) {
  607. d3["name"] = pinfo.name;
  608. }
  609. d3["type"] = get_type_name(pinfo);
  610. if (i == -1) {
  611. d2["return_value"] = d3;
  612. } else {
  613. arguments.push_back(d3);
  614. }
  615. }
  616. if (arguments.size()) {
  617. d2["arguments"] = arguments;
  618. }
  619. methods.push_back(d2);
  620. } else if (F.name.begins_with("_")) {
  621. //hidden method, ignore
  622. } else {
  623. Dictionary d2;
  624. d2["name"] = String(method_name);
  625. MethodBind *method = ClassDB::get_method(class_name, method_name);
  626. if (!method) {
  627. continue;
  628. }
  629. d2["is_const"] = method->is_const();
  630. d2["is_vararg"] = method->is_vararg();
  631. d2["is_virtual"] = false;
  632. d2["hash"] = method->get_hash();
  633. Vector<Variant> default_args = method->get_default_arguments();
  634. Array arguments;
  635. for (int i = (method->has_return() ? -1 : 0); i < method->get_argument_count(); i++) {
  636. PropertyInfo pinfo = i == -1 ? method->get_return_info() : method->get_argument_info(i);
  637. Dictionary d3;
  638. if (i >= 0) {
  639. d3["name"] = pinfo.name;
  640. }
  641. d3["type"] = get_type_name(pinfo);
  642. if (method->get_argument_meta(i) > 0) {
  643. static const char *argmeta[11] = { "none", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64", "float", "double" };
  644. d3["meta"] = argmeta[method->get_argument_meta(i)];
  645. }
  646. if (i >= 0 && i >= (method->get_argument_count() - default_args.size())) {
  647. int dargidx = i - (method->get_argument_count() - default_args.size());
  648. d3["default_value"] = default_args[dargidx].get_construct_string();
  649. }
  650. if (i == -1) {
  651. d2["return_value"] = d3;
  652. } else {
  653. arguments.push_back(d3);
  654. }
  655. }
  656. if (arguments.size()) {
  657. d2["arguments"] = arguments;
  658. }
  659. methods.push_back(d2);
  660. }
  661. }
  662. if (methods.size()) {
  663. d["methods"] = methods;
  664. }
  665. }
  666. {
  667. //signals
  668. Array signals;
  669. List<MethodInfo> signal_list;
  670. ClassDB::get_signal_list(class_name, &signal_list, true);
  671. for (const MethodInfo &F : signal_list) {
  672. StringName signal_name = F.name;
  673. Dictionary d2;
  674. d2["name"] = String(signal_name);
  675. Array arguments;
  676. for (int i = 0; i < F.arguments.size(); i++) {
  677. Dictionary d3;
  678. d3["name"] = F.arguments[i].name;
  679. d3["type"] = get_type_name(F.arguments[i]);
  680. arguments.push_back(d3);
  681. }
  682. if (arguments.size()) {
  683. d2["arguments"] = arguments;
  684. }
  685. signals.push_back(d2);
  686. }
  687. if (signals.size()) {
  688. d["signals"] = signals;
  689. }
  690. }
  691. {
  692. //properties
  693. Array properties;
  694. List<PropertyInfo> property_list;
  695. ClassDB::get_property_list(class_name, &property_list, true);
  696. for (const PropertyInfo &F : property_list) {
  697. if (F.usage & PROPERTY_USAGE_CATEGORY || F.usage & PROPERTY_USAGE_GROUP || F.usage & PROPERTY_USAGE_SUBGROUP) {
  698. continue; //not real properties
  699. }
  700. if (F.name.begins_with("_")) {
  701. continue; //hidden property
  702. }
  703. StringName property_name = F.name;
  704. Dictionary d2;
  705. d2["type"] = get_type_name(F);
  706. d2["name"] = String(property_name);
  707. d2["setter"] = ClassDB::get_property_setter(class_name, F.name);
  708. d2["getter"] = ClassDB::get_property_getter(class_name, F.name);
  709. d2["index"] = ClassDB::get_property_index(class_name, F.name);
  710. properties.push_back(d2);
  711. }
  712. if (properties.size()) {
  713. d["properties"] = properties;
  714. }
  715. }
  716. classes.push_back(d);
  717. }
  718. api_dump["classes"] = classes;
  719. }
  720. {
  721. // singletons
  722. Array singletons;
  723. List<Engine::Singleton> singleton_list;
  724. Engine::get_singleton()->get_singletons(&singleton_list);
  725. for (const Engine::Singleton &s : singleton_list) {
  726. Dictionary d;
  727. d["name"] = s.name;
  728. if (s.class_name != StringName()) {
  729. d["type"] = String(s.class_name);
  730. } else {
  731. d["type"] = String(s.ptr->get_class());
  732. }
  733. singletons.push_back(d);
  734. }
  735. if (singletons.size()) {
  736. api_dump["singletons"] = singletons;
  737. }
  738. }
  739. return api_dump;
  740. }
  741. void NativeExtensionAPIDump::generate_extension_json_file(const String &p_path) {
  742. Dictionary api = generate_extension_api();
  743. Ref<JSON> json;
  744. json.instantiate();
  745. String text = json->stringify(api, "\t", false);
  746. FileAccessRef fa = FileAccess::open(p_path, FileAccess::WRITE);
  747. CharString cs = text.ascii();
  748. fa->store_buffer((const uint8_t *)cs.ptr(), cs.length());
  749. fa->close();
  750. }
  751. #endif