extension_api_dump.cpp 62 KB

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