extension_api_dump.cpp 36 KB

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