extension_api_dump.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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, "r", 0, 0, 0, 0 },
  258. { Variant::COLOR, "g", sizeof(float), sizeof(float), sizeof(float), sizeof(float) },
  259. { Variant::COLOR, "b", 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(float) },
  260. { Variant::COLOR, "a", 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. HashMap<String, List<Pair<String, int64_t>>> enum_list;
  318. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  319. int64_t 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, int64_t>(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, int64_t>>> &E : enum_list) {
  334. Dictionary d1;
  335. d1["name"] = E.key;
  336. Array values;
  337. for (const Pair<String, int64_t> &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. //enums
  440. Array enums;
  441. List<StringName> enum_names;
  442. Variant::get_enums_for_type(type, &enum_names);
  443. for (const StringName &enum_name : enum_names) {
  444. Dictionary enum_dict;
  445. enum_dict["name"] = String(enum_name);
  446. List<StringName> enumeration_names;
  447. Variant::get_enumerations_for_enum(type, enum_name, &enumeration_names);
  448. Array values;
  449. for (const StringName &enumeration : enumeration_names) {
  450. Dictionary values_dict;
  451. values_dict["name"] = String(enumeration);
  452. values_dict["value"] = Variant::get_enum_value(type, enum_name, enumeration);
  453. values.push_back(values_dict);
  454. }
  455. if (values.size()) {
  456. enum_dict["values"] = values;
  457. }
  458. enums.push_back(enum_dict);
  459. }
  460. if (enums.size()) {
  461. d["enums"] = enums;
  462. }
  463. }
  464. {
  465. //operators
  466. Array operators;
  467. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  468. for (int k = 0; k < Variant::OP_MAX; k++) {
  469. Variant::Type rt = Variant::get_operator_return_type(Variant::Operator(k), type, Variant::Type(j));
  470. if (rt != Variant::NIL) {
  471. Dictionary d2;
  472. d2["name"] = Variant::get_operator_name(Variant::Operator(k));
  473. if (k != Variant::OP_NEGATE && k != Variant::OP_POSITIVE && k != Variant::OP_NOT && k != Variant::OP_BIT_NEGATE) {
  474. d2["right_type"] = Variant::get_type_name(Variant::Type(j));
  475. }
  476. d2["return_type"] = Variant::get_type_name(Variant::get_operator_return_type(Variant::Operator(k), type, Variant::Type(j)));
  477. operators.push_back(d2);
  478. }
  479. }
  480. }
  481. if (operators.size()) {
  482. d["operators"] = operators;
  483. }
  484. }
  485. {
  486. //methods
  487. Array methods;
  488. List<StringName> method_names;
  489. Variant::get_builtin_method_list(type, &method_names);
  490. for (const StringName &method_name : method_names) {
  491. Dictionary d2;
  492. d2["name"] = String(method_name);
  493. if (Variant::has_builtin_method_return_value(type, method_name)) {
  494. Variant::Type ret_type = Variant::get_builtin_method_return_type(type, method_name);
  495. d2["return_type"] = ret_type == Variant::NIL ? String("Variant") : Variant::get_type_name(ret_type);
  496. }
  497. d2["is_vararg"] = Variant::is_builtin_method_vararg(type, method_name);
  498. d2["is_const"] = Variant::is_builtin_method_const(type, method_name);
  499. d2["is_static"] = Variant::is_builtin_method_static(type, method_name);
  500. d2["hash"] = Variant::get_builtin_method_hash(type, method_name);
  501. Vector<Variant> default_args = Variant::get_builtin_method_default_arguments(type, method_name);
  502. Array arguments;
  503. int argcount = Variant::get_builtin_method_argument_count(type, method_name);
  504. for (int j = 0; j < argcount; j++) {
  505. Dictionary d3;
  506. d3["name"] = Variant::get_builtin_method_argument_name(type, method_name, j);
  507. Variant::Type argtype = Variant::get_builtin_method_argument_type(type, method_name, j);
  508. d3["type"] = argtype == Variant::NIL ? String("Variant") : Variant::get_type_name(argtype);
  509. if (j >= (argcount - default_args.size())) {
  510. int dargidx = j - (argcount - default_args.size());
  511. d3["default_value"] = default_args[dargidx].get_construct_string();
  512. }
  513. arguments.push_back(d3);
  514. }
  515. if (arguments.size()) {
  516. d2["arguments"] = arguments;
  517. }
  518. methods.push_back(d2);
  519. }
  520. if (methods.size()) {
  521. d["methods"] = methods;
  522. }
  523. }
  524. {
  525. //constructors
  526. Array constructors;
  527. for (int j = 0; j < Variant::get_constructor_count(type); j++) {
  528. Dictionary d2;
  529. d2["index"] = j;
  530. Array arguments;
  531. int argcount = Variant::get_constructor_argument_count(type, j);
  532. for (int k = 0; k < argcount; k++) {
  533. Dictionary d3;
  534. d3["name"] = Variant::get_constructor_argument_name(type, j, k);
  535. d3["type"] = Variant::get_type_name(Variant::get_constructor_argument_type(type, j, k));
  536. arguments.push_back(d3);
  537. }
  538. if (arguments.size()) {
  539. d2["arguments"] = arguments;
  540. }
  541. constructors.push_back(d2);
  542. }
  543. if (constructors.size()) {
  544. d["constructors"] = constructors;
  545. }
  546. }
  547. {
  548. //destructor
  549. d["has_destructor"] = Variant::has_destructor(type);
  550. }
  551. builtins.push_back(d);
  552. }
  553. api_dump["builtin_classes"] = builtins;
  554. }
  555. {
  556. // classes
  557. Array classes;
  558. List<StringName> class_list;
  559. ClassDB::get_class_list(&class_list);
  560. class_list.sort_custom<StringName::AlphCompare>();
  561. for (const StringName &class_name : class_list) {
  562. Dictionary d;
  563. d["name"] = String(class_name);
  564. d["is_refcounted"] = ClassDB::is_parent_class(class_name, "RefCounted");
  565. d["is_instantiable"] = ClassDB::can_instantiate(class_name);
  566. StringName parent_class = ClassDB::get_parent_class(class_name);
  567. if (parent_class != StringName()) {
  568. d["inherits"] = String(parent_class);
  569. }
  570. {
  571. ClassDB::APIType api = ClassDB::get_api_type(class_name);
  572. static const char *api_type[5] = { "core", "editor", "extension", "editor_extension" };
  573. d["api_type"] = api_type[api];
  574. }
  575. {
  576. //constants
  577. Array constants;
  578. List<String> constant_list;
  579. ClassDB::get_integer_constant_list(class_name, &constant_list, true);
  580. for (const String &F : constant_list) {
  581. StringName enum_name = ClassDB::get_integer_constant_enum(class_name, F);
  582. if (enum_name != StringName()) {
  583. continue; //enums will be handled on their own
  584. }
  585. Dictionary d2;
  586. d2["name"] = String(F);
  587. d2["value"] = ClassDB::get_integer_constant(class_name, F);
  588. constants.push_back(d2);
  589. }
  590. if (constants.size()) {
  591. d["constants"] = constants;
  592. }
  593. }
  594. {
  595. //enum
  596. Array enums;
  597. List<StringName> enum_list;
  598. ClassDB::get_enum_list(class_name, &enum_list, true);
  599. for (const StringName &F : enum_list) {
  600. Dictionary d2;
  601. d2["name"] = String(F);
  602. Array values;
  603. List<StringName> enum_constant_list;
  604. ClassDB::get_enum_constants(class_name, F, &enum_constant_list, true);
  605. for (List<StringName>::Element *G = enum_constant_list.front(); G; G = G->next()) {
  606. Dictionary d3;
  607. d3["name"] = String(G->get());
  608. d3["value"] = ClassDB::get_integer_constant(class_name, G->get());
  609. values.push_back(d3);
  610. }
  611. d2["values"] = values;
  612. enums.push_back(d2);
  613. }
  614. if (enums.size()) {
  615. d["enums"] = enums;
  616. }
  617. }
  618. {
  619. //methods
  620. Array methods;
  621. List<MethodInfo> method_list;
  622. ClassDB::get_method_list(class_name, &method_list, true);
  623. for (const MethodInfo &F : method_list) {
  624. StringName method_name = F.name;
  625. if ((F.flags & METHOD_FLAG_VIRTUAL) && !(F.flags & METHOD_FLAG_OBJECT_CORE)) {
  626. //virtual method
  627. const MethodInfo &mi = F;
  628. Dictionary d2;
  629. d2["name"] = String(method_name);
  630. d2["is_const"] = (F.flags & METHOD_FLAG_CONST) ? true : false;
  631. d2["is_static"] = (F.flags & METHOD_FLAG_STATIC) ? true : false;
  632. d2["is_vararg"] = false;
  633. d2["is_virtual"] = true;
  634. // virtual functions have no hash since no MethodBind is involved
  635. bool has_return = mi.return_val.type != Variant::NIL || (mi.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT);
  636. Array arguments;
  637. for (int i = (has_return ? -1 : 0); i < mi.arguments.size(); i++) {
  638. PropertyInfo pinfo = i == -1 ? mi.return_val : mi.arguments[i];
  639. Dictionary d3;
  640. if (i >= 0) {
  641. d3["name"] = pinfo.name;
  642. }
  643. d3["type"] = get_type_name(pinfo);
  644. if (i == -1) {
  645. d2["return_value"] = d3;
  646. } else {
  647. arguments.push_back(d3);
  648. }
  649. }
  650. if (arguments.size()) {
  651. d2["arguments"] = arguments;
  652. }
  653. methods.push_back(d2);
  654. } else if (F.name.begins_with("_")) {
  655. //hidden method, ignore
  656. } else {
  657. Dictionary d2;
  658. d2["name"] = String(method_name);
  659. MethodBind *method = ClassDB::get_method(class_name, method_name);
  660. if (!method) {
  661. continue;
  662. }
  663. d2["is_const"] = method->is_const();
  664. d2["is_vararg"] = method->is_vararg();
  665. d2["is_static"] = method->is_static();
  666. d2["is_virtual"] = false;
  667. d2["hash"] = method->get_hash();
  668. Vector<Variant> default_args = method->get_default_arguments();
  669. Array arguments;
  670. for (int i = (method->has_return() ? -1 : 0); i < method->get_argument_count(); i++) {
  671. PropertyInfo pinfo = i == -1 ? method->get_return_info() : method->get_argument_info(i);
  672. Dictionary d3;
  673. if (i >= 0) {
  674. d3["name"] = pinfo.name;
  675. }
  676. d3["type"] = get_type_name(pinfo);
  677. if (method->get_argument_meta(i) > 0) {
  678. static const char *argmeta[11] = { "none", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64", "float", "double" };
  679. d3["meta"] = argmeta[method->get_argument_meta(i)];
  680. }
  681. if (i >= 0 && i >= (method->get_argument_count() - default_args.size())) {
  682. int dargidx = i - (method->get_argument_count() - default_args.size());
  683. d3["default_value"] = default_args[dargidx].get_construct_string();
  684. }
  685. if (i == -1) {
  686. d2["return_value"] = d3;
  687. } else {
  688. arguments.push_back(d3);
  689. }
  690. }
  691. if (arguments.size()) {
  692. d2["arguments"] = arguments;
  693. }
  694. methods.push_back(d2);
  695. }
  696. }
  697. if (methods.size()) {
  698. d["methods"] = methods;
  699. }
  700. }
  701. {
  702. //signals
  703. Array signals;
  704. List<MethodInfo> signal_list;
  705. ClassDB::get_signal_list(class_name, &signal_list, true);
  706. for (const MethodInfo &F : signal_list) {
  707. StringName signal_name = F.name;
  708. Dictionary d2;
  709. d2["name"] = String(signal_name);
  710. Array arguments;
  711. for (int i = 0; i < F.arguments.size(); i++) {
  712. Dictionary d3;
  713. d3["name"] = F.arguments[i].name;
  714. d3["type"] = get_type_name(F.arguments[i]);
  715. arguments.push_back(d3);
  716. }
  717. if (arguments.size()) {
  718. d2["arguments"] = arguments;
  719. }
  720. signals.push_back(d2);
  721. }
  722. if (signals.size()) {
  723. d["signals"] = signals;
  724. }
  725. }
  726. {
  727. //properties
  728. Array properties;
  729. List<PropertyInfo> property_list;
  730. ClassDB::get_property_list(class_name, &property_list, true);
  731. for (const PropertyInfo &F : property_list) {
  732. if (F.usage & PROPERTY_USAGE_CATEGORY || F.usage & PROPERTY_USAGE_GROUP || F.usage & PROPERTY_USAGE_SUBGROUP) {
  733. continue; //not real properties
  734. }
  735. if (F.name.begins_with("_")) {
  736. continue; //hidden property
  737. }
  738. StringName property_name = F.name;
  739. Dictionary d2;
  740. d2["type"] = get_type_name(F);
  741. d2["name"] = String(property_name);
  742. d2["setter"] = ClassDB::get_property_setter(class_name, F.name);
  743. d2["getter"] = ClassDB::get_property_getter(class_name, F.name);
  744. d2["index"] = ClassDB::get_property_index(class_name, F.name);
  745. properties.push_back(d2);
  746. }
  747. if (properties.size()) {
  748. d["properties"] = properties;
  749. }
  750. }
  751. classes.push_back(d);
  752. }
  753. api_dump["classes"] = classes;
  754. }
  755. {
  756. // singletons
  757. Array singletons;
  758. List<Engine::Singleton> singleton_list;
  759. Engine::get_singleton()->get_singletons(&singleton_list);
  760. for (const Engine::Singleton &s : singleton_list) {
  761. Dictionary d;
  762. d["name"] = s.name;
  763. if (s.class_name != StringName()) {
  764. d["type"] = String(s.class_name);
  765. } else {
  766. d["type"] = String(s.ptr->get_class());
  767. }
  768. singletons.push_back(d);
  769. }
  770. if (singletons.size()) {
  771. api_dump["singletons"] = singletons;
  772. }
  773. }
  774. {
  775. Array native_structures;
  776. List<StringName> native_structs;
  777. ClassDB::get_native_struct_list(&native_structs);
  778. native_structs.sort_custom<StringName::AlphCompare>();
  779. for (const StringName &E : native_structs) {
  780. String code = ClassDB::get_native_struct_code(E);
  781. Dictionary d;
  782. d["name"] = String(E);
  783. d["format"] = code;
  784. native_structures.push_back(d);
  785. }
  786. api_dump["native_structures"] = native_structures;
  787. }
  788. return api_dump;
  789. }
  790. void NativeExtensionAPIDump::generate_extension_json_file(const String &p_path) {
  791. Dictionary api = generate_extension_api();
  792. Ref<JSON> json;
  793. json.instantiate();
  794. String text = json->stringify(api, "\t", false);
  795. Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::WRITE);
  796. CharString cs = text.ascii();
  797. fa->store_buffer((const uint8_t *)cs.ptr(), cs.length());
  798. }
  799. #endif