extension_api_dump.cpp 62 KB

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