extension_api_dump.cpp 61 KB

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