extension_api_dump.cpp 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. /**************************************************************************/
  2. /* extension_api_dump.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_builtin_or_variant_type_name(const Variant::Type p_type) {
  39. if (p_type == Variant::NIL) {
  40. return "Variant";
  41. } else {
  42. return Variant::get_type_name(p_type);
  43. }
  44. }
  45. static String get_property_info_type_name(const PropertyInfo &p_info) {
  46. if (p_info.type == Variant::INT && (p_info.hint == PROPERTY_HINT_INT_IS_POINTER)) {
  47. if (p_info.hint_string.is_empty()) {
  48. return "void*";
  49. } else {
  50. return p_info.hint_string + "*";
  51. }
  52. }
  53. if (p_info.type == Variant::ARRAY && (p_info.hint == PROPERTY_HINT_ARRAY_TYPE)) {
  54. return String("typedarray::") + p_info.hint_string;
  55. }
  56. if (p_info.type == Variant::INT && (p_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM))) {
  57. return String("enum::") + String(p_info.class_name);
  58. }
  59. if (p_info.type == Variant::INT && (p_info.usage & (PROPERTY_USAGE_CLASS_IS_BITFIELD))) {
  60. return String("bitfield::") + String(p_info.class_name);
  61. }
  62. if (p_info.type == Variant::INT && (p_info.usage & PROPERTY_USAGE_ARRAY)) {
  63. return "int";
  64. }
  65. if (p_info.class_name != StringName()) {
  66. return p_info.class_name;
  67. }
  68. if (p_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  69. return p_info.hint_string;
  70. }
  71. if (p_info.type == Variant::NIL && (p_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  72. return "Variant";
  73. }
  74. if (p_info.type == Variant::NIL) {
  75. return "void";
  76. }
  77. return get_builtin_or_variant_type_name(p_info.type);
  78. }
  79. static String get_type_meta_name(const GodotTypeInfo::Metadata metadata) {
  80. static const char *argmeta[11] = { "none", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64", "float", "double" };
  81. return argmeta[metadata];
  82. }
  83. Dictionary GDExtensionAPIDump::generate_extension_api() {
  84. Dictionary api_dump;
  85. {
  86. //header
  87. Dictionary header;
  88. header["version_major"] = VERSION_MAJOR;
  89. header["version_minor"] = VERSION_MINOR;
  90. #if VERSION_PATCH
  91. header["version_patch"] = VERSION_PATCH;
  92. #else
  93. header["version_patch"] = 0;
  94. #endif
  95. header["version_status"] = VERSION_STATUS;
  96. header["version_build"] = VERSION_BUILD;
  97. header["version_full_name"] = VERSION_FULL_NAME;
  98. api_dump["header"] = header;
  99. }
  100. const uint32_t vec3_elems = 3;
  101. const uint32_t vec4_elems = 4;
  102. const uint32_t ptrsize_32 = 4;
  103. const uint32_t ptrsize_64 = 8;
  104. static const char *build_config_name[4] = { "float_32", "float_64", "double_32", "double_64" };
  105. {
  106. //type sizes
  107. constexpr struct {
  108. Variant::Type type;
  109. uint32_t size_32_bits_real_float;
  110. uint32_t size_64_bits_real_float;
  111. uint32_t size_32_bits_real_double;
  112. uint32_t size_64_bits_real_double;
  113. // For compile-time size check.
  114. constexpr uint32_t operator[](int index) const {
  115. switch (index) {
  116. #ifndef REAL_T_IS_DOUBLE
  117. case sizeof(uint32_t):
  118. return size_32_bits_real_float;
  119. case sizeof(uint64_t):
  120. return size_64_bits_real_float;
  121. #else // REAL_T_IS_DOUBLE
  122. case sizeof(uint32_t):
  123. return size_32_bits_real_double;
  124. case sizeof(uint64_t):
  125. return size_64_bits_real_double;
  126. #endif
  127. }
  128. return -1;
  129. }
  130. } type_size_array[Variant::VARIANT_MAX + 1] = {
  131. { Variant::NIL, 0, 0, 0, 0 },
  132. { Variant::BOOL, sizeof(uint8_t), sizeof(uint8_t), sizeof(uint8_t), sizeof(uint8_t) },
  133. { Variant::INT, sizeof(int64_t), sizeof(int64_t), sizeof(int64_t), sizeof(int64_t) },
  134. { Variant::FLOAT, sizeof(double), sizeof(double), sizeof(double), sizeof(double) },
  135. { Variant::STRING, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  136. { Variant::VECTOR2, 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(double), 2 * sizeof(double) },
  137. { Variant::VECTOR2I, 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t) },
  138. { Variant::RECT2, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(double), 4 * sizeof(double) },
  139. { Variant::RECT2I, 4 * sizeof(int32_t), 4 * sizeof(int32_t), 4 * sizeof(int32_t), 4 * sizeof(int32_t) },
  140. { Variant::VECTOR3, vec3_elems * sizeof(float), vec3_elems * sizeof(float), vec3_elems * sizeof(double), vec3_elems * sizeof(double) },
  141. { Variant::VECTOR3I, 3 * sizeof(int32_t), 3 * sizeof(int32_t), 3 * sizeof(int32_t), 3 * sizeof(int32_t) },
  142. { Variant::TRANSFORM2D, 6 * sizeof(float), 6 * sizeof(float), 6 * sizeof(double), 6 * sizeof(double) },
  143. { Variant::VECTOR4, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(double), 4 * sizeof(double) },
  144. { Variant::VECTOR4I, 4 * sizeof(int32_t), 4 * sizeof(int32_t), 4 * sizeof(int32_t), 4 * sizeof(int32_t) },
  145. { Variant::PLANE, (vec3_elems + 1) * sizeof(float), (vec3_elems + 1) * sizeof(float), (vec3_elems + 1) * sizeof(double), (vec3_elems + 1) * sizeof(double) },
  146. { Variant::QUATERNION, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(double), 4 * sizeof(double) },
  147. { Variant::AABB, (vec3_elems * 2) * sizeof(float), (vec3_elems * 2) * sizeof(float), (vec3_elems * 2) * sizeof(double), (vec3_elems * 2) * sizeof(double) },
  148. { Variant::BASIS, (vec3_elems * 3) * sizeof(float), (vec3_elems * 3) * sizeof(float), (vec3_elems * 3) * sizeof(double), (vec3_elems * 3) * sizeof(double) },
  149. { Variant::TRANSFORM3D, (vec3_elems * 4) * sizeof(float), (vec3_elems * 4) * sizeof(float), (vec3_elems * 4) * sizeof(double), (vec3_elems * 4) * sizeof(double) },
  150. { Variant::PROJECTION, (vec4_elems * 4) * sizeof(float), (vec4_elems * 4) * sizeof(float), (vec4_elems * 4) * sizeof(double), (vec4_elems * 4) * sizeof(double) },
  151. { Variant::COLOR, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(float) },
  152. { Variant::STRING_NAME, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  153. { Variant::NODE_PATH, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  154. { Variant::RID, sizeof(uint64_t), sizeof(uint64_t), sizeof(uint64_t), sizeof(uint64_t) },
  155. { Variant::OBJECT, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  156. { Variant::CALLABLE, sizeof(Callable), sizeof(Callable), sizeof(Callable), sizeof(Callable) }, // Hardcoded align.
  157. { Variant::SIGNAL, sizeof(Signal), sizeof(Signal), sizeof(Signal), sizeof(Signal) }, // Hardcoded align.
  158. { Variant::DICTIONARY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  159. { Variant::ARRAY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  160. { Variant::PACKED_BYTE_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  161. { Variant::PACKED_INT32_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  162. { Variant::PACKED_INT64_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  163. { Variant::PACKED_FLOAT32_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  164. { Variant::PACKED_FLOAT64_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  165. { Variant::PACKED_STRING_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  166. { Variant::PACKED_VECTOR2_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  167. { Variant::PACKED_VECTOR3_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  168. { Variant::PACKED_COLOR_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },
  169. { 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 },
  170. };
  171. // Validate sizes at compile time for the current build configuration.
  172. static_assert(type_size_array[Variant::BOOL][sizeof(void *)] == sizeof(GDExtensionBool), "Size of bool mismatch");
  173. static_assert(type_size_array[Variant::INT][sizeof(void *)] == sizeof(GDExtensionInt), "Size of int mismatch");
  174. static_assert(type_size_array[Variant::FLOAT][sizeof(void *)] == sizeof(double), "Size of float mismatch");
  175. static_assert(type_size_array[Variant::STRING][sizeof(void *)] == sizeof(String), "Size of String mismatch");
  176. static_assert(type_size_array[Variant::VECTOR2][sizeof(void *)] == sizeof(Vector2), "Size of Vector2 mismatch");
  177. static_assert(type_size_array[Variant::VECTOR2I][sizeof(void *)] == sizeof(Vector2i), "Size of Vector2i mismatch");
  178. static_assert(type_size_array[Variant::RECT2][sizeof(void *)] == sizeof(Rect2), "Size of Rect2 mismatch");
  179. static_assert(type_size_array[Variant::RECT2I][sizeof(void *)] == sizeof(Rect2i), "Size of Rect2i mismatch");
  180. static_assert(type_size_array[Variant::VECTOR3][sizeof(void *)] == sizeof(Vector3), "Size of Vector3 mismatch");
  181. static_assert(type_size_array[Variant::VECTOR3I][sizeof(void *)] == sizeof(Vector3i), "Size of Vector3i mismatch");
  182. static_assert(type_size_array[Variant::TRANSFORM2D][sizeof(void *)] == sizeof(Transform2D), "Size of Transform2D mismatch");
  183. static_assert(type_size_array[Variant::VECTOR4][sizeof(void *)] == sizeof(Vector4), "Size of Vector4 mismatch");
  184. static_assert(type_size_array[Variant::VECTOR4I][sizeof(void *)] == sizeof(Vector4i), "Size of Vector4i mismatch");
  185. static_assert(type_size_array[Variant::PLANE][sizeof(void *)] == sizeof(Plane), "Size of Plane mismatch");
  186. static_assert(type_size_array[Variant::QUATERNION][sizeof(void *)] == sizeof(Quaternion), "Size of Quaternion mismatch");
  187. static_assert(type_size_array[Variant::AABB][sizeof(void *)] == sizeof(AABB), "Size of AABB mismatch");
  188. static_assert(type_size_array[Variant::BASIS][sizeof(void *)] == sizeof(Basis), "Size of Basis mismatch");
  189. static_assert(type_size_array[Variant::TRANSFORM3D][sizeof(void *)] == sizeof(Transform3D), "Size of Transform3D mismatch");
  190. static_assert(type_size_array[Variant::PROJECTION][sizeof(void *)] == sizeof(Projection), "Size of Projection mismatch");
  191. static_assert(type_size_array[Variant::COLOR][sizeof(void *)] == sizeof(Color), "Size of Color mismatch");
  192. static_assert(type_size_array[Variant::STRING_NAME][sizeof(void *)] == sizeof(StringName), "Size of StringName mismatch");
  193. static_assert(type_size_array[Variant::NODE_PATH][sizeof(void *)] == sizeof(NodePath), "Size of NodePath mismatch");
  194. static_assert(type_size_array[Variant::RID][sizeof(void *)] == sizeof(RID), "Size of RID mismatch");
  195. static_assert(type_size_array[Variant::OBJECT][sizeof(void *)] == sizeof(Object *), "Size of Object mismatch");
  196. static_assert(type_size_array[Variant::CALLABLE][sizeof(void *)] == sizeof(Callable), "Size of Callable mismatch");
  197. static_assert(type_size_array[Variant::SIGNAL][sizeof(void *)] == sizeof(Signal), "Size of Signal mismatch");
  198. static_assert(type_size_array[Variant::DICTIONARY][sizeof(void *)] == sizeof(Dictionary), "Size of Dictionary mismatch");
  199. static_assert(type_size_array[Variant::ARRAY][sizeof(void *)] == sizeof(Array), "Size of Array mismatch");
  200. static_assert(type_size_array[Variant::PACKED_BYTE_ARRAY][sizeof(void *)] == sizeof(PackedByteArray), "Size of PackedByteArray mismatch");
  201. static_assert(type_size_array[Variant::PACKED_INT32_ARRAY][sizeof(void *)] == sizeof(PackedInt32Array), "Size of PackedInt32Array mismatch");
  202. static_assert(type_size_array[Variant::PACKED_INT64_ARRAY][sizeof(void *)] == sizeof(PackedInt64Array), "Size of PackedInt64Array mismatch");
  203. static_assert(type_size_array[Variant::PACKED_FLOAT32_ARRAY][sizeof(void *)] == sizeof(PackedFloat32Array), "Size of PackedFloat32Array mismatch");
  204. static_assert(type_size_array[Variant::PACKED_FLOAT64_ARRAY][sizeof(void *)] == sizeof(PackedFloat64Array), "Size of PackedFloat64Array mismatch");
  205. static_assert(type_size_array[Variant::PACKED_STRING_ARRAY][sizeof(void *)] == sizeof(PackedStringArray), "Size of PackedStringArray mismatch");
  206. static_assert(type_size_array[Variant::PACKED_VECTOR2_ARRAY][sizeof(void *)] == sizeof(PackedVector2Array), "Size of PackedVector2Array mismatch");
  207. static_assert(type_size_array[Variant::PACKED_VECTOR3_ARRAY][sizeof(void *)] == sizeof(PackedVector3Array), "Size of PackedVector3Array mismatch");
  208. static_assert(type_size_array[Variant::PACKED_COLOR_ARRAY][sizeof(void *)] == sizeof(PackedColorArray), "Size of PackedColorArray mismatch");
  209. static_assert(type_size_array[Variant::VARIANT_MAX][sizeof(void *)] == sizeof(Variant), "Size of Variant mismatch");
  210. Array core_type_sizes;
  211. for (int i = 0; i < 4; i++) {
  212. Dictionary d;
  213. d["build_configuration"] = build_config_name[i];
  214. Array sizes;
  215. for (int j = 0; j <= Variant::VARIANT_MAX; j++) {
  216. Variant::Type t = type_size_array[j].type;
  217. String name = t == Variant::VARIANT_MAX ? String("Variant") : Variant::get_type_name(t);
  218. Dictionary d2;
  219. d2["name"] = name;
  220. uint32_t size = 0;
  221. switch (i) {
  222. case 0:
  223. size = type_size_array[j].size_32_bits_real_float;
  224. break;
  225. case 1:
  226. size = type_size_array[j].size_64_bits_real_float;
  227. break;
  228. case 2:
  229. size = type_size_array[j].size_32_bits_real_double;
  230. break;
  231. case 3:
  232. size = type_size_array[j].size_64_bits_real_double;
  233. break;
  234. }
  235. d2["size"] = size;
  236. sizes.push_back(d2);
  237. }
  238. d["sizes"] = sizes;
  239. core_type_sizes.push_back(d);
  240. }
  241. api_dump["builtin_class_sizes"] = core_type_sizes;
  242. }
  243. {
  244. // Member offsets, meta types and sizes.
  245. #define REAL_MEMBER_OFFSET(type, member) \
  246. { \
  247. type, \
  248. member, \
  249. "float", \
  250. sizeof(float), \
  251. "float", \
  252. sizeof(float), \
  253. "double", \
  254. sizeof(double), \
  255. "double", \
  256. sizeof(double), \
  257. }
  258. #define INT32_MEMBER_OFFSET(type, member) \
  259. { \
  260. type, \
  261. member, \
  262. "int32", \
  263. sizeof(int32_t), \
  264. "int32", \
  265. sizeof(int32_t), \
  266. "int32", \
  267. sizeof(int32_t), \
  268. "int32", \
  269. sizeof(int32_t), \
  270. }
  271. #define INT32_BASED_BUILTIN_MEMBER_OFFSET(type, member, member_type, member_elems) \
  272. { \
  273. type, \
  274. member, \
  275. member_type, \
  276. sizeof(int32_t) * member_elems, \
  277. member_type, \
  278. sizeof(int32_t) * member_elems, \
  279. member_type, \
  280. sizeof(int32_t) * member_elems, \
  281. member_type, \
  282. sizeof(int32_t) * member_elems, \
  283. }
  284. #define REAL_BASED_BUILTIN_MEMBER_OFFSET(type, member, member_type, member_elems) \
  285. { \
  286. type, \
  287. member, \
  288. member_type, \
  289. sizeof(float) * member_elems, \
  290. member_type, \
  291. sizeof(float) * member_elems, \
  292. member_type, \
  293. sizeof(double) * member_elems, \
  294. member_type, \
  295. sizeof(double) * member_elems, \
  296. }
  297. struct {
  298. Variant::Type type;
  299. const char *member;
  300. const char *member_meta_32_bits_real_float;
  301. const uint32_t member_size_32_bits_real_float;
  302. const char *member_meta_64_bits_real_float;
  303. const uint32_t member_size_64_bits_real_float;
  304. const char *member_meta_32_bits_real_double;
  305. const uint32_t member_size_32_bits_real_double;
  306. const char *member_meta_64_bits_real_double;
  307. const uint32_t member_size_64_bits_real_double;
  308. } member_offset_array[] = {
  309. // Vector2
  310. REAL_MEMBER_OFFSET(Variant::VECTOR2, "x"),
  311. REAL_MEMBER_OFFSET(Variant::VECTOR2, "y"),
  312. // Vector2i
  313. INT32_MEMBER_OFFSET(Variant::VECTOR2I, "x"),
  314. INT32_MEMBER_OFFSET(Variant::VECTOR2I, "y"),
  315. // Rect2
  316. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::RECT2, "position", "Vector2", 2),
  317. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::RECT2, "size", "Vector2", 2),
  318. // Rect2i
  319. INT32_BASED_BUILTIN_MEMBER_OFFSET(Variant::RECT2I, "position", "Vector2i", 2),
  320. INT32_BASED_BUILTIN_MEMBER_OFFSET(Variant::RECT2I, "size", "Vector2i", 2),
  321. // Vector3
  322. REAL_MEMBER_OFFSET(Variant::VECTOR3, "x"),
  323. REAL_MEMBER_OFFSET(Variant::VECTOR3, "y"),
  324. REAL_MEMBER_OFFSET(Variant::VECTOR3, "z"),
  325. // Vector3i
  326. INT32_MEMBER_OFFSET(Variant::VECTOR3I, "x"),
  327. INT32_MEMBER_OFFSET(Variant::VECTOR3I, "y"),
  328. INT32_MEMBER_OFFSET(Variant::VECTOR3I, "z"),
  329. // Transform2D
  330. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::TRANSFORM2D, "x", "Vector2", 2),
  331. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::TRANSFORM2D, "y", "Vector2", 2),
  332. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::TRANSFORM2D, "origin", "Vector2", 2),
  333. // Vector4
  334. REAL_MEMBER_OFFSET(Variant::VECTOR4, "x"),
  335. REAL_MEMBER_OFFSET(Variant::VECTOR4, "y"),
  336. REAL_MEMBER_OFFSET(Variant::VECTOR4, "z"),
  337. REAL_MEMBER_OFFSET(Variant::VECTOR4, "w"),
  338. // Vector4i
  339. INT32_MEMBER_OFFSET(Variant::VECTOR4I, "x"),
  340. INT32_MEMBER_OFFSET(Variant::VECTOR4I, "y"),
  341. INT32_MEMBER_OFFSET(Variant::VECTOR4I, "z"),
  342. INT32_MEMBER_OFFSET(Variant::VECTOR4I, "w"),
  343. // Plane
  344. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::PLANE, "normal", "Vector3", vec3_elems),
  345. REAL_MEMBER_OFFSET(Variant::PLANE, "d"),
  346. // Quaternion
  347. REAL_MEMBER_OFFSET(Variant::QUATERNION, "x"),
  348. REAL_MEMBER_OFFSET(Variant::QUATERNION, "y"),
  349. REAL_MEMBER_OFFSET(Variant::QUATERNION, "z"),
  350. REAL_MEMBER_OFFSET(Variant::QUATERNION, "w"),
  351. // AABB
  352. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::AABB, "position", "Vector3", vec3_elems),
  353. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::AABB, "size", "Vector3", vec3_elems),
  354. // Basis (remember that basis vectors are flipped!)
  355. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::BASIS, "x", "Vector3", vec3_elems),
  356. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::BASIS, "y", "Vector3", vec3_elems),
  357. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::BASIS, "z", "Vector3", vec3_elems),
  358. // Transform3D
  359. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::TRANSFORM3D, "basis", "Basis", vec3_elems * 3),
  360. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::TRANSFORM3D, "origin", "Vector3", vec3_elems),
  361. // Projection
  362. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::PROJECTION, "x", "Vector4", vec4_elems),
  363. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::PROJECTION, "y", "Vector4", vec4_elems),
  364. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::PROJECTION, "z", "Vector4", vec4_elems),
  365. REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::PROJECTION, "w", "Vector4", vec4_elems),
  366. // Color (always composed of 4bytes floats)
  367. { Variant::COLOR, "r", "float", sizeof(float), "float", sizeof(float), "float", sizeof(float), "float", sizeof(float) },
  368. { Variant::COLOR, "g", "float", sizeof(float), "float", sizeof(float), "float", sizeof(float), "float", sizeof(float) },
  369. { Variant::COLOR, "b", "float", sizeof(float), "float", sizeof(float), "float", sizeof(float), "float", sizeof(float) },
  370. { Variant::COLOR, "a", "float", sizeof(float), "float", sizeof(float), "float", sizeof(float), "float", sizeof(float) },
  371. // End marker, must stay last
  372. { Variant::NIL, nullptr, nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0 },
  373. };
  374. Array core_type_member_offsets;
  375. for (int i = 0; i < 4; i++) {
  376. Dictionary d;
  377. d["build_configuration"] = build_config_name[i];
  378. Array type_offsets;
  379. uint32_t idx = 0;
  380. Variant::Type previous_type = Variant::NIL;
  381. Dictionary d2;
  382. Array members;
  383. uint32_t offset = 0;
  384. while (true) {
  385. Variant::Type t = member_offset_array[idx].type;
  386. if (t != previous_type) {
  387. if (previous_type != Variant::NIL) {
  388. d2["members"] = members;
  389. type_offsets.push_back(d2);
  390. }
  391. if (t == Variant::NIL) {
  392. break;
  393. }
  394. String name = t == Variant::VARIANT_MAX ? String("Variant") : Variant::get_type_name(t);
  395. d2 = Dictionary();
  396. members = Array();
  397. offset = 0;
  398. d2["name"] = name;
  399. previous_type = t;
  400. }
  401. Dictionary d3;
  402. const char *member_meta = nullptr;
  403. uint32_t member_size = 0;
  404. switch (i) {
  405. case 0:
  406. member_meta = member_offset_array[idx].member_meta_32_bits_real_float;
  407. member_size = member_offset_array[idx].member_size_32_bits_real_float;
  408. break;
  409. case 1:
  410. member_meta = member_offset_array[idx].member_meta_64_bits_real_float;
  411. member_size = member_offset_array[idx].member_size_64_bits_real_float;
  412. break;
  413. case 2:
  414. member_meta = member_offset_array[idx].member_meta_32_bits_real_double;
  415. member_size = member_offset_array[idx].member_size_32_bits_real_double;
  416. break;
  417. case 3:
  418. member_meta = member_offset_array[idx].member_meta_64_bits_real_double;
  419. member_size = member_offset_array[idx].member_size_64_bits_real_double;
  420. break;
  421. }
  422. d3["member"] = member_offset_array[idx].member;
  423. d3["offset"] = offset;
  424. d3["meta"] = member_meta;
  425. offset += member_size;
  426. members.push_back(d3);
  427. idx++;
  428. }
  429. d["classes"] = type_offsets;
  430. core_type_member_offsets.push_back(d);
  431. }
  432. api_dump["builtin_class_member_offsets"] = core_type_member_offsets;
  433. }
  434. {
  435. // Global enums and constants.
  436. Array constants;
  437. HashMap<String, List<Pair<String, int64_t>>> enum_list;
  438. HashMap<String, bool> enum_is_bitfield;
  439. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  440. int64_t value = CoreConstants::get_global_constant_value(i);
  441. String enum_name = CoreConstants::get_global_constant_enum(i);
  442. String name = CoreConstants::get_global_constant_name(i);
  443. bool bitfield = CoreConstants::is_global_constant_bitfield(i);
  444. if (!enum_name.is_empty()) {
  445. enum_list[enum_name].push_back(Pair<String, int64_t>(name, value));
  446. enum_is_bitfield[enum_name] = bitfield;
  447. } else {
  448. Dictionary d;
  449. d["name"] = name;
  450. d["value"] = value;
  451. d["is_bitfield"] = bitfield;
  452. constants.push_back(d);
  453. }
  454. }
  455. api_dump["global_constants"] = constants;
  456. Array enums;
  457. for (const KeyValue<String, List<Pair<String, int64_t>>> &E : enum_list) {
  458. Dictionary d1;
  459. d1["name"] = E.key;
  460. d1["is_bitfield"] = enum_is_bitfield[E.key];
  461. Array values;
  462. for (const Pair<String, int64_t> &F : E.value) {
  463. Dictionary d2;
  464. d2["name"] = F.first;
  465. d2["value"] = F.second;
  466. values.push_back(d2);
  467. }
  468. d1["values"] = values;
  469. enums.push_back(d1);
  470. }
  471. api_dump["global_enums"] = enums;
  472. }
  473. {
  474. Array utility_funcs;
  475. List<StringName> utility_func_names;
  476. Variant::get_utility_function_list(&utility_func_names);
  477. for (const StringName &name : utility_func_names) {
  478. Dictionary func;
  479. func["name"] = String(name);
  480. if (Variant::has_utility_function_return_value(name)) {
  481. Variant::Type rt = Variant::get_utility_function_return_type(name);
  482. func["return_type"] = rt == Variant::NIL ? String("Variant") : Variant::get_type_name(rt);
  483. }
  484. switch (Variant::get_utility_function_type(name)) {
  485. case Variant::UTILITY_FUNC_TYPE_MATH:
  486. func["category"] = "math";
  487. break;
  488. case Variant::UTILITY_FUNC_TYPE_RANDOM:
  489. func["category"] = "random";
  490. break;
  491. case Variant::UTILITY_FUNC_TYPE_GENERAL:
  492. func["category"] = "general";
  493. break;
  494. }
  495. bool vararg = Variant::is_utility_function_vararg(name);
  496. func["is_vararg"] = Variant::is_utility_function_vararg(name);
  497. func["hash"] = Variant::get_utility_function_hash(name);
  498. Array arguments;
  499. int argcount = Variant::get_utility_function_argument_count(name);
  500. for (int i = 0; i < argcount; i++) {
  501. Dictionary arg;
  502. String argname = vararg ? "arg" + itos(i + 1) : Variant::get_utility_function_argument_name(name, i);
  503. arg["name"] = argname;
  504. arg["type"] = get_builtin_or_variant_type_name(Variant::get_utility_function_argument_type(name, i));
  505. //no default value support in utility functions
  506. arguments.push_back(arg);
  507. }
  508. if (arguments.size()) {
  509. func["arguments"] = arguments;
  510. }
  511. utility_funcs.push_back(func);
  512. }
  513. api_dump["utility_functions"] = utility_funcs;
  514. }
  515. {
  516. // builtin types
  517. Array builtins;
  518. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  519. if (i == Variant::OBJECT) {
  520. continue;
  521. }
  522. Variant::Type type = Variant::Type(i);
  523. Dictionary d;
  524. d["name"] = Variant::get_type_name(type);
  525. if (Variant::has_indexing(type)) {
  526. d["indexing_return_type"] = get_builtin_or_variant_type_name(Variant::get_indexed_element_type(type));
  527. }
  528. d["is_keyed"] = Variant::is_keyed(type);
  529. {
  530. //members
  531. Array members;
  532. List<StringName> member_names;
  533. Variant::get_member_list(type, &member_names);
  534. for (const StringName &member_name : member_names) {
  535. Dictionary d2;
  536. d2["name"] = String(member_name);
  537. d2["type"] = get_builtin_or_variant_type_name(Variant::get_member_type(type, member_name));
  538. members.push_back(d2);
  539. }
  540. if (members.size()) {
  541. d["members"] = members;
  542. }
  543. }
  544. {
  545. //constants
  546. Array constants;
  547. List<StringName> constant_names;
  548. Variant::get_constants_for_type(type, &constant_names);
  549. for (const StringName &constant_name : constant_names) {
  550. Dictionary d2;
  551. d2["name"] = String(constant_name);
  552. Variant constant = Variant::get_constant_value(type, constant_name);
  553. d2["type"] = get_builtin_or_variant_type_name(constant.get_type());
  554. d2["value"] = constant.get_construct_string();
  555. constants.push_back(d2);
  556. }
  557. if (constants.size()) {
  558. d["constants"] = constants;
  559. }
  560. }
  561. {
  562. //enums
  563. Array enums;
  564. List<StringName> enum_names;
  565. Variant::get_enums_for_type(type, &enum_names);
  566. for (const StringName &enum_name : enum_names) {
  567. Dictionary enum_dict;
  568. enum_dict["name"] = String(enum_name);
  569. List<StringName> enumeration_names;
  570. Variant::get_enumerations_for_enum(type, enum_name, &enumeration_names);
  571. Array values;
  572. for (const StringName &enumeration : enumeration_names) {
  573. Dictionary values_dict;
  574. values_dict["name"] = String(enumeration);
  575. values_dict["value"] = Variant::get_enum_value(type, enum_name, enumeration);
  576. values.push_back(values_dict);
  577. }
  578. if (values.size()) {
  579. enum_dict["values"] = values;
  580. }
  581. enums.push_back(enum_dict);
  582. }
  583. if (enums.size()) {
  584. d["enums"] = enums;
  585. }
  586. }
  587. {
  588. //operators
  589. Array operators;
  590. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  591. for (int k = 0; k < Variant::OP_MAX; k++) {
  592. Variant::Type rt = Variant::get_operator_return_type(Variant::Operator(k), type, Variant::Type(j));
  593. if (rt != Variant::NIL) {
  594. Dictionary d2;
  595. d2["name"] = Variant::get_operator_name(Variant::Operator(k));
  596. if (k != Variant::OP_NEGATE && k != Variant::OP_POSITIVE && k != Variant::OP_NOT && k != Variant::OP_BIT_NEGATE) {
  597. d2["right_type"] = get_builtin_or_variant_type_name(Variant::Type(j));
  598. }
  599. d2["return_type"] = get_builtin_or_variant_type_name(Variant::get_operator_return_type(Variant::Operator(k), type, Variant::Type(j)));
  600. operators.push_back(d2);
  601. }
  602. }
  603. }
  604. if (operators.size()) {
  605. d["operators"] = operators;
  606. }
  607. }
  608. {
  609. //methods
  610. Array methods;
  611. List<StringName> method_names;
  612. Variant::get_builtin_method_list(type, &method_names);
  613. for (const StringName &method_name : method_names) {
  614. Dictionary d2;
  615. d2["name"] = String(method_name);
  616. if (Variant::has_builtin_method_return_value(type, method_name)) {
  617. Variant::Type ret_type = Variant::get_builtin_method_return_type(type, method_name);
  618. d2["return_type"] = ret_type == Variant::NIL ? String("Variant") : Variant::get_type_name(ret_type);
  619. }
  620. d2["is_vararg"] = Variant::is_builtin_method_vararg(type, method_name);
  621. d2["is_const"] = Variant::is_builtin_method_const(type, method_name);
  622. d2["is_static"] = Variant::is_builtin_method_static(type, method_name);
  623. d2["hash"] = Variant::get_builtin_method_hash(type, method_name);
  624. Vector<Variant> default_args = Variant::get_builtin_method_default_arguments(type, method_name);
  625. Array arguments;
  626. int argcount = Variant::get_builtin_method_argument_count(type, method_name);
  627. for (int j = 0; j < argcount; j++) {
  628. Dictionary d3;
  629. d3["name"] = Variant::get_builtin_method_argument_name(type, method_name, j);
  630. d3["type"] = get_builtin_or_variant_type_name(Variant::get_builtin_method_argument_type(type, method_name, j));
  631. if (j >= (argcount - default_args.size())) {
  632. int dargidx = j - (argcount - default_args.size());
  633. d3["default_value"] = default_args[dargidx].get_construct_string();
  634. }
  635. arguments.push_back(d3);
  636. }
  637. if (arguments.size()) {
  638. d2["arguments"] = arguments;
  639. }
  640. methods.push_back(d2);
  641. }
  642. if (methods.size()) {
  643. d["methods"] = methods;
  644. }
  645. }
  646. {
  647. //constructors
  648. Array constructors;
  649. for (int j = 0; j < Variant::get_constructor_count(type); j++) {
  650. Dictionary d2;
  651. d2["index"] = j;
  652. Array arguments;
  653. int argcount = Variant::get_constructor_argument_count(type, j);
  654. for (int k = 0; k < argcount; k++) {
  655. Dictionary d3;
  656. d3["name"] = Variant::get_constructor_argument_name(type, j, k);
  657. d3["type"] = get_builtin_or_variant_type_name(Variant::get_constructor_argument_type(type, j, k));
  658. arguments.push_back(d3);
  659. }
  660. if (arguments.size()) {
  661. d2["arguments"] = arguments;
  662. }
  663. constructors.push_back(d2);
  664. }
  665. if (constructors.size()) {
  666. d["constructors"] = constructors;
  667. }
  668. }
  669. {
  670. //destructor
  671. d["has_destructor"] = Variant::has_destructor(type);
  672. }
  673. builtins.push_back(d);
  674. }
  675. api_dump["builtin_classes"] = builtins;
  676. }
  677. {
  678. // classes
  679. Array classes;
  680. List<StringName> class_list;
  681. ClassDB::get_class_list(&class_list);
  682. class_list.sort_custom<StringName::AlphCompare>();
  683. for (const StringName &class_name : class_list) {
  684. Dictionary d;
  685. d["name"] = String(class_name);
  686. d["is_refcounted"] = ClassDB::is_parent_class(class_name, "RefCounted");
  687. d["is_instantiable"] = ClassDB::can_instantiate(class_name);
  688. StringName parent_class = ClassDB::get_parent_class(class_name);
  689. if (parent_class != StringName()) {
  690. d["inherits"] = String(parent_class);
  691. }
  692. {
  693. ClassDB::APIType api = ClassDB::get_api_type(class_name);
  694. static const char *api_type[5] = { "core", "editor", "extension", "editor_extension" };
  695. d["api_type"] = api_type[api];
  696. }
  697. {
  698. //constants
  699. Array constants;
  700. List<String> constant_list;
  701. ClassDB::get_integer_constant_list(class_name, &constant_list, true);
  702. for (const String &F : constant_list) {
  703. StringName enum_name = ClassDB::get_integer_constant_enum(class_name, F);
  704. if (enum_name != StringName()) {
  705. continue; //enums will be handled on their own
  706. }
  707. Dictionary d2;
  708. d2["name"] = String(F);
  709. d2["value"] = ClassDB::get_integer_constant(class_name, F);
  710. constants.push_back(d2);
  711. }
  712. if (constants.size()) {
  713. d["constants"] = constants;
  714. }
  715. }
  716. {
  717. //enum
  718. Array enums;
  719. List<StringName> enum_list;
  720. ClassDB::get_enum_list(class_name, &enum_list, true);
  721. for (const StringName &F : enum_list) {
  722. Dictionary d2;
  723. d2["name"] = String(F);
  724. d2["is_bitfield"] = ClassDB::is_enum_bitfield(class_name, F);
  725. Array values;
  726. List<StringName> enum_constant_list;
  727. ClassDB::get_enum_constants(class_name, F, &enum_constant_list, true);
  728. for (List<StringName>::Element *G = enum_constant_list.front(); G; G = G->next()) {
  729. Dictionary d3;
  730. d3["name"] = String(G->get());
  731. d3["value"] = ClassDB::get_integer_constant(class_name, G->get());
  732. values.push_back(d3);
  733. }
  734. d2["values"] = values;
  735. enums.push_back(d2);
  736. }
  737. if (enums.size()) {
  738. d["enums"] = enums;
  739. }
  740. }
  741. {
  742. //methods
  743. Array methods;
  744. List<MethodInfo> method_list;
  745. ClassDB::get_method_list(class_name, &method_list, true);
  746. for (const MethodInfo &F : method_list) {
  747. StringName method_name = F.name;
  748. if ((F.flags & METHOD_FLAG_VIRTUAL) && !(F.flags & METHOD_FLAG_OBJECT_CORE)) {
  749. //virtual method
  750. const MethodInfo &mi = F;
  751. Dictionary d2;
  752. d2["name"] = String(method_name);
  753. d2["is_const"] = (F.flags & METHOD_FLAG_CONST) ? true : false;
  754. d2["is_static"] = (F.flags & METHOD_FLAG_STATIC) ? true : false;
  755. d2["is_vararg"] = false;
  756. d2["is_virtual"] = true;
  757. // virtual functions have no hash since no MethodBind is involved
  758. bool has_return = mi.return_val.type != Variant::NIL || (mi.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT);
  759. Array arguments;
  760. for (int i = (has_return ? -1 : 0); i < mi.arguments.size(); i++) {
  761. PropertyInfo pinfo = i == -1 ? mi.return_val : mi.arguments[i];
  762. Dictionary d3;
  763. if (i >= 0) {
  764. d3["name"] = pinfo.name;
  765. }
  766. d3["type"] = get_property_info_type_name(pinfo);
  767. if (mi.get_argument_meta(i) > 0) {
  768. d3["meta"] = get_type_meta_name((GodotTypeInfo::Metadata)mi.get_argument_meta(i));
  769. }
  770. if (i == -1) {
  771. d2["return_value"] = d3;
  772. } else {
  773. arguments.push_back(d3);
  774. }
  775. }
  776. if (arguments.size()) {
  777. d2["arguments"] = arguments;
  778. }
  779. methods.push_back(d2);
  780. } else if (F.name.begins_with("_")) {
  781. //hidden method, ignore
  782. } else {
  783. Dictionary d2;
  784. d2["name"] = String(method_name);
  785. MethodBind *method = ClassDB::get_method(class_name, method_name);
  786. if (!method) {
  787. continue;
  788. }
  789. d2["is_const"] = method->is_const();
  790. d2["is_vararg"] = method->is_vararg();
  791. d2["is_static"] = method->is_static();
  792. d2["is_virtual"] = false;
  793. d2["hash"] = method->get_hash();
  794. Vector<uint32_t> compat_hashes = ClassDB::get_method_compatibility_hashes(class_name, method_name);
  795. if (compat_hashes.size()) {
  796. Array compatibility;
  797. for (int i = 0; i < compat_hashes.size(); i++) {
  798. compatibility.push_back(compat_hashes[i]);
  799. }
  800. d2["hash_compatibility"] = compatibility;
  801. }
  802. Vector<Variant> default_args = method->get_default_arguments();
  803. Array arguments;
  804. for (int i = (method->has_return() ? -1 : 0); i < method->get_argument_count(); i++) {
  805. PropertyInfo pinfo = i == -1 ? method->get_return_info() : method->get_argument_info(i);
  806. Dictionary d3;
  807. if (i >= 0) {
  808. d3["name"] = pinfo.name;
  809. }
  810. d3["type"] = get_property_info_type_name(pinfo);
  811. if (method->get_argument_meta(i) > 0) {
  812. d3["meta"] = get_type_meta_name(method->get_argument_meta(i));
  813. }
  814. if (i >= 0 && i >= (method->get_argument_count() - default_args.size())) {
  815. int dargidx = i - (method->get_argument_count() - default_args.size());
  816. d3["default_value"] = default_args[dargidx].get_construct_string();
  817. }
  818. if (i == -1) {
  819. d2["return_value"] = d3;
  820. } else {
  821. arguments.push_back(d3);
  822. }
  823. }
  824. if (arguments.size()) {
  825. d2["arguments"] = arguments;
  826. }
  827. methods.push_back(d2);
  828. }
  829. }
  830. if (methods.size()) {
  831. d["methods"] = methods;
  832. }
  833. }
  834. {
  835. //signals
  836. Array signals;
  837. List<MethodInfo> signal_list;
  838. ClassDB::get_signal_list(class_name, &signal_list, true);
  839. for (const MethodInfo &F : signal_list) {
  840. StringName signal_name = F.name;
  841. Dictionary d2;
  842. d2["name"] = String(signal_name);
  843. Array arguments;
  844. for (int i = 0; i < F.arguments.size(); i++) {
  845. Dictionary d3;
  846. d3["name"] = F.arguments[i].name;
  847. d3["type"] = get_property_info_type_name(F.arguments[i]);
  848. if (F.get_argument_meta(i) > 0) {
  849. d3["meta"] = get_type_meta_name((GodotTypeInfo::Metadata)F.get_argument_meta(i));
  850. }
  851. arguments.push_back(d3);
  852. }
  853. if (arguments.size()) {
  854. d2["arguments"] = arguments;
  855. }
  856. signals.push_back(d2);
  857. }
  858. if (signals.size()) {
  859. d["signals"] = signals;
  860. }
  861. }
  862. {
  863. //properties
  864. Array properties;
  865. List<PropertyInfo> property_list;
  866. ClassDB::get_property_list(class_name, &property_list, true);
  867. for (const PropertyInfo &F : property_list) {
  868. 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)) {
  869. continue; //not real properties
  870. }
  871. if (F.name.begins_with("_")) {
  872. continue; //hidden property
  873. }
  874. if (F.name.find("/") >= 0) {
  875. // Ignore properties with '/' (slash) in the name. These are only meant for use in the inspector.
  876. continue;
  877. }
  878. StringName property_name = F.name;
  879. Dictionary d2;
  880. d2["type"] = get_property_info_type_name(F);
  881. d2["name"] = String(property_name);
  882. StringName setter = ClassDB::get_property_setter(class_name, F.name);
  883. if (!(setter == "")) {
  884. d2["setter"] = setter;
  885. }
  886. StringName getter = ClassDB::get_property_getter(class_name, F.name);
  887. if (!(getter == "")) {
  888. d2["getter"] = getter;
  889. }
  890. int index = ClassDB::get_property_index(class_name, F.name);
  891. if (index != -1) {
  892. d2["index"] = index;
  893. }
  894. properties.push_back(d2);
  895. }
  896. if (properties.size()) {
  897. d["properties"] = properties;
  898. }
  899. }
  900. classes.push_back(d);
  901. }
  902. api_dump["classes"] = classes;
  903. }
  904. {
  905. // singletons
  906. Array singletons;
  907. List<Engine::Singleton> singleton_list;
  908. Engine::get_singleton()->get_singletons(&singleton_list);
  909. for (const Engine::Singleton &s : singleton_list) {
  910. Dictionary d;
  911. d["name"] = s.name;
  912. if (s.class_name != StringName()) {
  913. d["type"] = String(s.class_name);
  914. } else {
  915. d["type"] = String(s.ptr->get_class());
  916. }
  917. singletons.push_back(d);
  918. }
  919. if (singletons.size()) {
  920. api_dump["singletons"] = singletons;
  921. }
  922. }
  923. {
  924. Array native_structures;
  925. List<StringName> native_structs;
  926. ClassDB::get_native_struct_list(&native_structs);
  927. native_structs.sort_custom<StringName::AlphCompare>();
  928. for (const StringName &E : native_structs) {
  929. String code = ClassDB::get_native_struct_code(E);
  930. Dictionary d;
  931. d["name"] = String(E);
  932. d["format"] = code;
  933. native_structures.push_back(d);
  934. }
  935. api_dump["native_structures"] = native_structures;
  936. }
  937. return api_dump;
  938. }
  939. void GDExtensionAPIDump::generate_extension_json_file(const String &p_path) {
  940. Dictionary api = generate_extension_api();
  941. Ref<JSON> json;
  942. json.instantiate();
  943. String text = json->stringify(api, "\t", false) + "\n";
  944. Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::WRITE);
  945. ERR_FAIL_COND_MSG(fa.is_null(), vformat("Cannot open file '%s' for writing.", p_path));
  946. fa->store_string(text);
  947. }
  948. static bool compare_value(const String &p_path, const String &p_field, const Variant &p_old_value, const Variant &p_new_value, bool p_allow_name_change) {
  949. bool failed = false;
  950. String path = p_path + "/" + p_field;
  951. if (p_old_value.get_type() == Variant::ARRAY && p_new_value.get_type() == Variant::ARRAY) {
  952. Array old_array = p_old_value;
  953. Array new_array = p_new_value;
  954. if (!compare_value(path, "size", old_array.size(), new_array.size(), p_allow_name_change)) {
  955. failed = true;
  956. }
  957. for (int i = 0; i < old_array.size() && i < new_array.size(); i++) {
  958. if (!compare_value(path, itos(i), old_array[i], new_array[i], p_allow_name_change)) {
  959. failed = true;
  960. }
  961. }
  962. } else if (p_old_value.get_type() == Variant::DICTIONARY && p_new_value.get_type() == Variant::DICTIONARY) {
  963. Dictionary old_dict = p_old_value;
  964. Dictionary new_dict = p_new_value;
  965. Array old_keys = old_dict.keys();
  966. for (int i = 0; i < old_keys.size(); i++) {
  967. Variant key = old_keys[i];
  968. if (!new_dict.has(key)) {
  969. failed = true;
  970. print_error(vformat("Validate extension JSON: Error: Field '%s': %s was removed.", p_path, key));
  971. continue;
  972. }
  973. if (p_allow_name_change && key == "name") {
  974. continue;
  975. }
  976. if (!compare_value(path, key, old_dict[key], new_dict[key], p_allow_name_change)) {
  977. failed = true;
  978. }
  979. }
  980. Array new_keys = old_dict.keys();
  981. for (int i = 0; i < new_keys.size(); i++) {
  982. Variant key = new_keys[i];
  983. if (!old_dict.has(key)) {
  984. failed = true;
  985. print_error(vformat("Validate extension JSON: Error: Field '%s': %s was added with value %s.", p_path, key, new_dict[key]));
  986. }
  987. }
  988. } else {
  989. bool equal = Variant::evaluate(Variant::OP_EQUAL, p_old_value, p_new_value);
  990. if (!equal) {
  991. print_error(vformat("Validate extension JSON: Error: Field '%s': %s changed value in new API, from %s to %s.", p_path, p_field, p_old_value.get_construct_string(), p_new_value.get_construct_string()));
  992. return false;
  993. }
  994. }
  995. return !failed;
  996. }
  997. static bool compare_dict_array(const Dictionary &p_old_api, const Dictionary &p_new_api, const String &p_base_array, const String &p_name_field, const Vector<String> &p_fields_to_compare, bool p_compare_hashes, const String &p_outer_class = String(), bool p_compare_operators = false, bool p_compare_enum_value = false) {
  998. String base_array = p_outer_class + p_base_array;
  999. if (!p_old_api.has(p_base_array)) {
  1000. return true; // May just not have this array and its still good. Probably added recently.
  1001. }
  1002. bool failed = false;
  1003. ERR_FAIL_COND_V_MSG(!p_new_api.has(p_base_array), false, "New API lacks base array: " + p_base_array);
  1004. Array new_api = p_new_api[p_base_array];
  1005. HashMap<String, Dictionary> new_api_assoc;
  1006. for (int i = 0; i < new_api.size(); i++) {
  1007. Dictionary elem = new_api[i];
  1008. ERR_FAIL_COND_V_MSG(!elem.has(p_name_field), false, vformat("Validate extension JSON: Element of base_array '%s' is missing field '%s'. This is a bug.", base_array, p_name_field));
  1009. String name = elem[p_name_field];
  1010. if (p_compare_operators && elem.has("right_type")) {
  1011. name += " " + String(elem["right_type"]);
  1012. }
  1013. new_api_assoc.insert(name, elem);
  1014. }
  1015. Array old_api = p_old_api[p_base_array];
  1016. for (int i = 0; i < old_api.size(); i++) {
  1017. Dictionary old_elem = old_api[i];
  1018. if (!old_elem.has(p_name_field)) {
  1019. failed = true;
  1020. print_error(vformat("Validate extension JSON: JSON file: element of base array '%s' is missing the field: '%s'.", base_array, p_name_field));
  1021. continue;
  1022. }
  1023. String name = old_elem[p_name_field];
  1024. if (p_compare_operators && old_elem.has("right_type")) {
  1025. name += " " + String(old_elem["right_type"]);
  1026. }
  1027. if (!new_api_assoc.has(name)) {
  1028. failed = true;
  1029. print_error(vformat("Validate extension JSON: API was removed: %s/%s", base_array, name));
  1030. continue;
  1031. }
  1032. Dictionary new_elem = new_api_assoc[name];
  1033. for (int j = 0; j < p_fields_to_compare.size(); j++) {
  1034. String field = p_fields_to_compare[j];
  1035. bool optional = field.begins_with("*");
  1036. if (optional) {
  1037. // This is an optional field, but if exists it has to exist in both.
  1038. field = field.substr(1, field.length());
  1039. }
  1040. bool added = field.begins_with("+");
  1041. if (added) {
  1042. // Meaning this field must either exist or contents may not exist.
  1043. field = field.substr(1, field.length());
  1044. }
  1045. bool enum_values = field.begins_with("$");
  1046. if (enum_values) {
  1047. // Meaning this field is a list of enum values.
  1048. field = field.substr(1, field.length());
  1049. }
  1050. bool allow_name_change = field.begins_with("@");
  1051. if (allow_name_change) {
  1052. // Meaning that when structurally comparing the old and new value, the dictionary entry 'name' may change.
  1053. field = field.substr(1, field.length());
  1054. }
  1055. Variant old_value;
  1056. if (!old_elem.has(field)) {
  1057. if (optional) {
  1058. if (new_elem.has(field)) {
  1059. failed = true;
  1060. print_error(vformat("Validate extension JSON: JSON file: Field was added in a way that breaks compatibility '%s/%s': %s", base_array, name, field));
  1061. }
  1062. } else if (added && new_elem.has(field)) {
  1063. // Should be ok, field now exists, should not be verified in prior versions where it does not.
  1064. } else {
  1065. failed = true;
  1066. print_error(vformat("Validate extension JSON: JSON file: Missing field in '%s/%s': %s", base_array, name, field));
  1067. }
  1068. continue;
  1069. } else {
  1070. old_value = old_elem[field];
  1071. }
  1072. if (!new_elem.has(field)) {
  1073. failed = true;
  1074. ERR_PRINT(vformat("Validate extension JSON: Missing field in current API '%s/%s': %s. This is a bug.", base_array, name, field));
  1075. continue;
  1076. }
  1077. Variant new_value = new_elem[field];
  1078. if (p_compare_enum_value && name.ends_with("_MAX")) {
  1079. if (static_cast<int64_t>(new_value) > static_cast<int64_t>(old_value)) {
  1080. // Ignore the _MAX value of an enum increasing.
  1081. continue;
  1082. }
  1083. }
  1084. if (enum_values) {
  1085. if (!compare_dict_array(old_elem, new_elem, field, "name", { "value" }, false, base_array + "/" + name + "/", false, true)) {
  1086. failed = true;
  1087. }
  1088. } else if (!compare_value(base_array + "/" + name, field, old_value, new_value, allow_name_change)) {
  1089. failed = true;
  1090. }
  1091. }
  1092. if (p_compare_hashes) {
  1093. if (!old_elem.has("hash")) {
  1094. if (old_elem.has("is_virtual") && bool(old_elem["is_virtual"]) && !new_elem.has("hash")) {
  1095. continue; // No hash for virtual methods, go on.
  1096. }
  1097. failed = true;
  1098. print_error(vformat("Validate extension JSON: JSON file: element of base array '%s' is missing the field: 'hash'.", base_array));
  1099. continue;
  1100. }
  1101. uint64_t old_hash = old_elem["hash"];
  1102. if (!new_elem.has("hash")) {
  1103. failed = true;
  1104. print_error(vformat("Validate extension JSON: Error: Field '%s' is missing the field: 'hash'.", base_array));
  1105. continue;
  1106. }
  1107. uint64_t new_hash = new_elem["hash"];
  1108. bool hash_found = false;
  1109. if (old_hash == new_hash) {
  1110. hash_found = true;
  1111. } else if (new_elem.has("hash_compatibility")) {
  1112. Array compatibility = new_elem["hash_compatibility"];
  1113. for (int j = 0; j < compatibility.size(); j++) {
  1114. new_hash = compatibility[j];
  1115. if (new_hash == old_hash) {
  1116. hash_found = true;
  1117. break;
  1118. }
  1119. }
  1120. }
  1121. if (!hash_found) {
  1122. failed = true;
  1123. print_error(vformat("Validate extension JSON: Error: Hash changed for '%s/%s', from %08X to %08X. This means that the function has changed and no compatibility function was provided.", base_array, name, old_hash, new_hash));
  1124. continue;
  1125. }
  1126. }
  1127. }
  1128. return !failed;
  1129. }
  1130. static bool compare_sub_dict_array(HashSet<String> &r_removed_classes_registered, const String &p_outer, const String &p_outer_name, const Dictionary &p_old_api, const Dictionary &p_new_api, const String &p_base_array, const String &p_name_field, const Vector<String> &p_fields_to_compare, bool p_compare_hashes, bool p_compare_operators = false) {
  1131. if (!p_old_api.has(p_outer)) {
  1132. return true; // May just not have this array and its still good. Probably added recently or optional.
  1133. }
  1134. bool failed = false;
  1135. ERR_FAIL_COND_V_MSG(!p_new_api.has(p_outer), false, "New API lacks base array: " + p_outer);
  1136. Array new_api = p_new_api[p_outer];
  1137. HashMap<String, Dictionary> new_api_assoc;
  1138. for (int i = 0; i < new_api.size(); i++) {
  1139. Dictionary elem = new_api[i];
  1140. ERR_FAIL_COND_V_MSG(!elem.has(p_outer_name), false, vformat("Validate extension JSON: Element of base_array '%s' is missing field '%s'. This is a bug.", p_outer, p_outer_name));
  1141. new_api_assoc.insert(elem[p_outer_name], elem);
  1142. }
  1143. Array old_api = p_old_api[p_outer];
  1144. for (int i = 0; i < old_api.size(); i++) {
  1145. Dictionary old_elem = old_api[i];
  1146. if (!old_elem.has(p_outer_name)) {
  1147. failed = true;
  1148. print_error(vformat("Validate extension JSON: JSON file: element of base array '%s' is missing the field: '%s'.", p_outer, p_outer_name));
  1149. continue;
  1150. }
  1151. String name = old_elem[p_outer_name];
  1152. if (!new_api_assoc.has(name)) {
  1153. failed = true;
  1154. if (!r_removed_classes_registered.has(name)) {
  1155. print_error(vformat("Validate extension JSON: API was removed: %s/%s", p_outer, name));
  1156. r_removed_classes_registered.insert(name);
  1157. }
  1158. continue;
  1159. }
  1160. Dictionary new_elem = new_api_assoc[name];
  1161. if (!compare_dict_array(old_elem, new_elem, p_base_array, p_name_field, p_fields_to_compare, p_compare_hashes, p_outer + "/" + name + "/", p_compare_operators)) {
  1162. failed = true;
  1163. }
  1164. }
  1165. return !failed;
  1166. }
  1167. Error GDExtensionAPIDump::validate_extension_json_file(const String &p_path) {
  1168. Error error;
  1169. String text = FileAccess::get_file_as_string(p_path, &error);
  1170. if (error != OK) {
  1171. ERR_PRINT(vformat("Validate extension JSON: Could not open file '%s'.", p_path));
  1172. return error;
  1173. }
  1174. Ref<JSON> json;
  1175. json.instantiate();
  1176. error = json->parse(text);
  1177. if (error != OK) {
  1178. ERR_PRINT(vformat("Validate extension JSON: Error parsing '%s' at line %d: %s", p_path, json->get_error_line(), json->get_error_message()));
  1179. return error;
  1180. }
  1181. Dictionary old_api = json->get_data();
  1182. Dictionary new_api = generate_extension_api();
  1183. { // Validate header:
  1184. Dictionary header = old_api["header"];
  1185. ERR_FAIL_COND_V(!header.has("version_major"), ERR_INVALID_DATA);
  1186. ERR_FAIL_COND_V(!header.has("version_minor"), ERR_INVALID_DATA);
  1187. int major = header["version_major"];
  1188. int minor = header["version_minor"];
  1189. ERR_FAIL_COND_V_MSG(major != VERSION_MAJOR, ERR_INVALID_DATA, vformat("JSON API dump is for a different engine version (%d) than this one (%d)", major, VERSION_MAJOR));
  1190. ERR_FAIL_COND_V_MSG(minor > VERSION_MINOR, ERR_INVALID_DATA, vformat("JSON API dump is for a newer version of the engine: %d.%d", major, minor));
  1191. }
  1192. bool failed = false;
  1193. HashSet<String> removed_classes_registered;
  1194. if (!compare_dict_array(old_api, new_api, "global_constants", "name", Vector<String>({ "value", "is_bitfield" }), false)) {
  1195. failed = true;
  1196. }
  1197. if (!compare_dict_array(old_api, new_api, "global_enums", "name", Vector<String>({ "$values", "is_bitfield" }), false)) {
  1198. failed = true;
  1199. }
  1200. if (!compare_dict_array(old_api, new_api, "utility_functions", "name", Vector<String>({ "category", "is_vararg", "*return_type", "*@arguments" }), true)) {
  1201. failed = true;
  1202. }
  1203. if (!compare_sub_dict_array(removed_classes_registered, "builtin_classes", "name", old_api, new_api, "members", "name", { "type" }, false)) {
  1204. failed = true;
  1205. }
  1206. if (!compare_sub_dict_array(removed_classes_registered, "builtin_classes", "name", old_api, new_api, "constants", "name", { "type", "value" }, false)) {
  1207. failed = true;
  1208. }
  1209. if (!compare_sub_dict_array(removed_classes_registered, "builtin_classes", "name", old_api, new_api, "operators", "name", { "return_type" }, false, true)) {
  1210. failed = true;
  1211. }
  1212. if (!compare_sub_dict_array(removed_classes_registered, "builtin_classes", "name", old_api, new_api, "methods", "name", { "is_vararg", "is_static", "is_const", "*return_type", "*@arguments" }, true)) {
  1213. failed = true;
  1214. }
  1215. if (!compare_sub_dict_array(removed_classes_registered, "builtin_classes", "name", old_api, new_api, "constructors", "index", { "*@arguments" }, false)) {
  1216. failed = true;
  1217. }
  1218. if (!compare_sub_dict_array(removed_classes_registered, "classes", "name", old_api, new_api, "constants", "name", { "value" }, false)) {
  1219. failed = true;
  1220. }
  1221. if (!compare_sub_dict_array(removed_classes_registered, "classes", "name", old_api, new_api, "enums", "name", { "is_bitfield", "$values" }, false)) {
  1222. failed = true;
  1223. }
  1224. if (!compare_sub_dict_array(removed_classes_registered, "classes", "name", old_api, new_api, "methods", "name", { "is_virtual", "is_vararg", "is_static", "is_const", "*return_value", "*@arguments" }, true)) {
  1225. failed = true;
  1226. }
  1227. if (!compare_sub_dict_array(removed_classes_registered, "classes", "name", old_api, new_api, "signals", "name", { "*@arguments" }, false)) {
  1228. failed = true;
  1229. }
  1230. if (!compare_sub_dict_array(removed_classes_registered, "classes", "name", old_api, new_api, "properties", "name", { "type", "*setter", "*getter", "*index" }, false)) {
  1231. failed = true;
  1232. }
  1233. if (!compare_dict_array(old_api, new_api, "singletons", "name", Vector<String>({ "type" }), false)) {
  1234. failed = true;
  1235. }
  1236. if (!compare_dict_array(old_api, new_api, "native_structures", "name", Vector<String>({ "format" }), false)) {
  1237. failed = true;
  1238. }
  1239. if (failed) {
  1240. return ERR_INVALID_DATA;
  1241. } else {
  1242. return OK;
  1243. }
  1244. }
  1245. #endif // TOOLS_ENABLED