extension_api_dump.cpp 62 KB

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