extension_api_dump.cpp 34 KB

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