extension_api_dump.cpp 61 KB

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