extension_api_dump.cpp 62 KB

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