extension_api_dump.cpp 37 KB

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