extension_api_dump.cpp 62 KB

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