Path: blob/master/core/extension/extension_api_dump.cpp
20841 views
/**************************************************************************/1/* extension_api_dump.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "extension_api_dump.h"3132#include "core/config/engine.h"33#include "core/core_constants.h"34#include "core/extension/gdextension_special_compat_hashes.h"35#include "core/io/file_access.h"36#include "core/io/json.h"37#include "core/templates/pair.h"38#include "core/version.h"3940#ifdef TOOLS_ENABLED41#include "editor/doc/editor_help.h"4243static String get_builtin_or_variant_type_name(const Variant::Type p_type) {44if (p_type == Variant::NIL) {45return "Variant";46} else {47return Variant::get_type_name(p_type);48}49}5051static String get_property_info_type_name(const PropertyInfo &p_info) {52if (p_info.type == Variant::INT && (p_info.hint == PROPERTY_HINT_INT_IS_POINTER)) {53if (p_info.hint_string.is_empty()) {54return "void*";55} else {56return p_info.hint_string + "*";57}58}59if (p_info.type == Variant::ARRAY && (p_info.hint == PROPERTY_HINT_ARRAY_TYPE)) {60return String("typedarray::") + p_info.hint_string;61}62if (p_info.type == Variant::DICTIONARY && (p_info.hint == PROPERTY_HINT_DICTIONARY_TYPE)) {63return String("typeddictionary::") + p_info.hint_string;64}65if (p_info.type == Variant::INT && (p_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM))) {66return String("enum::") + String(p_info.class_name);67}68if (p_info.type == Variant::INT && (p_info.usage & (PROPERTY_USAGE_CLASS_IS_BITFIELD))) {69return String("bitfield::") + String(p_info.class_name);70}71if (p_info.type == Variant::INT && (p_info.usage & PROPERTY_USAGE_ARRAY)) {72return "int";73}74if (p_info.class_name != StringName()) {75return p_info.class_name;76}77if (p_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {78return p_info.hint_string;79}80if (p_info.type == Variant::NIL && (p_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {81return "Variant";82}83if (p_info.type == Variant::NIL) {84return "void";85}86return get_builtin_or_variant_type_name(p_info.type);87}8889static String get_type_meta_name(const GodotTypeInfo::Metadata metadata) {90static const char *argmeta[14] = { "none", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64", "float", "double", "char16", "char32", "required" };91return argmeta[metadata];92}9394static String fix_doc_description(const String &p_bbcode) {95// Based on what EditorHelp does.9697return p_bbcode.dedent()98.remove_chars("\r")99.strip_edges();100}101102Dictionary GDExtensionAPIDump::generate_extension_api(bool p_include_docs) {103Dictionary api_dump;104105{106//header107Dictionary header;108header["version_major"] = GODOT_VERSION_MAJOR;109header["version_minor"] = GODOT_VERSION_MINOR;110#if GODOT_VERSION_PATCH111header["version_patch"] = GODOT_VERSION_PATCH;112#else113header["version_patch"] = 0;114#endif115header["version_status"] = GODOT_VERSION_STATUS;116header["version_build"] = GODOT_VERSION_BUILD;117header["version_full_name"] = GODOT_VERSION_FULL_NAME;118119#if REAL_T_IS_DOUBLE120header["precision"] = "double";121#else122header["precision"] = "single";123#endif124125api_dump["header"] = header;126}127128const uint32_t vec3_elems = 3;129const uint32_t vec4_elems = 4;130const uint32_t ptrsize_32 = 4;131const uint32_t ptrsize_64 = 8;132static const char *build_config_name[4] = { "float_32", "float_64", "double_32", "double_64" };133134{135//type sizes136constexpr struct {137Variant::Type type;138uint32_t size_32_bits_real_float;139uint32_t size_64_bits_real_float;140uint32_t size_32_bits_real_double;141uint32_t size_64_bits_real_double;142143// For compile-time size check.144constexpr uint32_t operator[](int index) const {145switch (index) {146#ifndef REAL_T_IS_DOUBLE147case sizeof(uint32_t):148return size_32_bits_real_float;149case sizeof(uint64_t):150return size_64_bits_real_float;151#else // REAL_T_IS_DOUBLE152case sizeof(uint32_t):153return size_32_bits_real_double;154case sizeof(uint64_t):155return size_64_bits_real_double;156#endif157}158return -1;159}160} type_size_array[Variant::VARIANT_MAX + 1] = {161{ Variant::NIL, 0, 0, 0, 0 },162{ Variant::BOOL, sizeof(uint8_t), sizeof(uint8_t), sizeof(uint8_t), sizeof(uint8_t) },163{ Variant::INT, sizeof(int64_t), sizeof(int64_t), sizeof(int64_t), sizeof(int64_t) },164{ Variant::FLOAT, sizeof(double), sizeof(double), sizeof(double), sizeof(double) },165{ Variant::STRING, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },166{ Variant::VECTOR2, 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(double), 2 * sizeof(double) },167{ Variant::VECTOR2I, 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t) },168{ Variant::RECT2, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(double), 4 * sizeof(double) },169{ Variant::RECT2I, 4 * sizeof(int32_t), 4 * sizeof(int32_t), 4 * sizeof(int32_t), 4 * sizeof(int32_t) },170{ Variant::VECTOR3, vec3_elems * sizeof(float), vec3_elems * sizeof(float), vec3_elems * sizeof(double), vec3_elems * sizeof(double) },171{ Variant::VECTOR3I, 3 * sizeof(int32_t), 3 * sizeof(int32_t), 3 * sizeof(int32_t), 3 * sizeof(int32_t) },172{ Variant::TRANSFORM2D, 6 * sizeof(float), 6 * sizeof(float), 6 * sizeof(double), 6 * sizeof(double) },173{ Variant::VECTOR4, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(double), 4 * sizeof(double) },174{ Variant::VECTOR4I, 4 * sizeof(int32_t), 4 * sizeof(int32_t), 4 * sizeof(int32_t), 4 * sizeof(int32_t) },175{ Variant::PLANE, (vec3_elems + 1) * sizeof(float), (vec3_elems + 1) * sizeof(float), (vec3_elems + 1) * sizeof(double), (vec3_elems + 1) * sizeof(double) },176{ Variant::QUATERNION, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(double), 4 * sizeof(double) },177{ Variant::AABB, (vec3_elems * 2) * sizeof(float), (vec3_elems * 2) * sizeof(float), (vec3_elems * 2) * sizeof(double), (vec3_elems * 2) * sizeof(double) },178{ Variant::BASIS, (vec3_elems * 3) * sizeof(float), (vec3_elems * 3) * sizeof(float), (vec3_elems * 3) * sizeof(double), (vec3_elems * 3) * sizeof(double) },179{ Variant::TRANSFORM3D, (vec3_elems * 4) * sizeof(float), (vec3_elems * 4) * sizeof(float), (vec3_elems * 4) * sizeof(double), (vec3_elems * 4) * sizeof(double) },180{ Variant::PROJECTION, (vec4_elems * 4) * sizeof(float), (vec4_elems * 4) * sizeof(float), (vec4_elems * 4) * sizeof(double), (vec4_elems * 4) * sizeof(double) },181{ Variant::COLOR, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(float) },182{ Variant::STRING_NAME, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },183{ Variant::NODE_PATH, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },184{ Variant::RID, sizeof(uint64_t), sizeof(uint64_t), sizeof(uint64_t), sizeof(uint64_t) },185{ Variant::OBJECT, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },186{ Variant::CALLABLE, sizeof(Callable), sizeof(Callable), sizeof(Callable), sizeof(Callable) }, // Hardcoded align.187{ Variant::SIGNAL, sizeof(Signal), sizeof(Signal), sizeof(Signal), sizeof(Signal) }, // Hardcoded align.188{ Variant::DICTIONARY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },189{ Variant::ARRAY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },190{ Variant::PACKED_BYTE_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },191{ Variant::PACKED_INT32_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },192{ Variant::PACKED_INT64_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },193{ Variant::PACKED_FLOAT32_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },194{ Variant::PACKED_FLOAT64_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },195{ Variant::PACKED_STRING_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },196{ Variant::PACKED_VECTOR2_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },197{ Variant::PACKED_VECTOR3_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },198{ Variant::PACKED_COLOR_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },199{ Variant::PACKED_VECTOR4_ARRAY, ptrsize_32 * 2, ptrsize_64 * 2, ptrsize_32 * 2, ptrsize_64 * 2 },200{ 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 },201};202203// Validate sizes at compile time for the current build configuration.204static_assert(type_size_array[Variant::BOOL][sizeof(void *)] == sizeof(GDExtensionBool), "Size of bool mismatch");205static_assert(type_size_array[Variant::INT][sizeof(void *)] == sizeof(GDExtensionInt), "Size of int mismatch");206static_assert(type_size_array[Variant::FLOAT][sizeof(void *)] == sizeof(double), "Size of float mismatch");207static_assert(type_size_array[Variant::STRING][sizeof(void *)] == sizeof(String), "Size of String mismatch");208static_assert(type_size_array[Variant::VECTOR2][sizeof(void *)] == sizeof(Vector2), "Size of Vector2 mismatch");209static_assert(type_size_array[Variant::VECTOR2I][sizeof(void *)] == sizeof(Vector2i), "Size of Vector2i mismatch");210static_assert(type_size_array[Variant::RECT2][sizeof(void *)] == sizeof(Rect2), "Size of Rect2 mismatch");211static_assert(type_size_array[Variant::RECT2I][sizeof(void *)] == sizeof(Rect2i), "Size of Rect2i mismatch");212static_assert(type_size_array[Variant::VECTOR3][sizeof(void *)] == sizeof(Vector3), "Size of Vector3 mismatch");213static_assert(type_size_array[Variant::VECTOR3I][sizeof(void *)] == sizeof(Vector3i), "Size of Vector3i mismatch");214static_assert(type_size_array[Variant::TRANSFORM2D][sizeof(void *)] == sizeof(Transform2D), "Size of Transform2D mismatch");215static_assert(type_size_array[Variant::VECTOR4][sizeof(void *)] == sizeof(Vector4), "Size of Vector4 mismatch");216static_assert(type_size_array[Variant::VECTOR4I][sizeof(void *)] == sizeof(Vector4i), "Size of Vector4i mismatch");217static_assert(type_size_array[Variant::PLANE][sizeof(void *)] == sizeof(Plane), "Size of Plane mismatch");218static_assert(type_size_array[Variant::QUATERNION][sizeof(void *)] == sizeof(Quaternion), "Size of Quaternion mismatch");219static_assert(type_size_array[Variant::AABB][sizeof(void *)] == sizeof(AABB), "Size of AABB mismatch");220static_assert(type_size_array[Variant::BASIS][sizeof(void *)] == sizeof(Basis), "Size of Basis mismatch");221static_assert(type_size_array[Variant::TRANSFORM3D][sizeof(void *)] == sizeof(Transform3D), "Size of Transform3D mismatch");222static_assert(type_size_array[Variant::PROJECTION][sizeof(void *)] == sizeof(Projection), "Size of Projection mismatch");223static_assert(type_size_array[Variant::COLOR][sizeof(void *)] == sizeof(Color), "Size of Color mismatch");224static_assert(type_size_array[Variant::STRING_NAME][sizeof(void *)] == sizeof(StringName), "Size of StringName mismatch");225static_assert(type_size_array[Variant::NODE_PATH][sizeof(void *)] == sizeof(NodePath), "Size of NodePath mismatch");226static_assert(type_size_array[Variant::RID][sizeof(void *)] == sizeof(RID), "Size of RID mismatch");227static_assert(type_size_array[Variant::OBJECT][sizeof(void *)] == sizeof(Object *), "Size of Object mismatch");228static_assert(type_size_array[Variant::CALLABLE][sizeof(void *)] == sizeof(Callable), "Size of Callable mismatch");229static_assert(type_size_array[Variant::SIGNAL][sizeof(void *)] == sizeof(Signal), "Size of Signal mismatch");230static_assert(type_size_array[Variant::DICTIONARY][sizeof(void *)] == sizeof(Dictionary), "Size of Dictionary mismatch");231static_assert(type_size_array[Variant::ARRAY][sizeof(void *)] == sizeof(Array), "Size of Array mismatch");232static_assert(type_size_array[Variant::PACKED_BYTE_ARRAY][sizeof(void *)] == sizeof(PackedByteArray), "Size of PackedByteArray mismatch");233static_assert(type_size_array[Variant::PACKED_INT32_ARRAY][sizeof(void *)] == sizeof(PackedInt32Array), "Size of PackedInt32Array mismatch");234static_assert(type_size_array[Variant::PACKED_INT64_ARRAY][sizeof(void *)] == sizeof(PackedInt64Array), "Size of PackedInt64Array mismatch");235static_assert(type_size_array[Variant::PACKED_FLOAT32_ARRAY][sizeof(void *)] == sizeof(PackedFloat32Array), "Size of PackedFloat32Array mismatch");236static_assert(type_size_array[Variant::PACKED_FLOAT64_ARRAY][sizeof(void *)] == sizeof(PackedFloat64Array), "Size of PackedFloat64Array mismatch");237static_assert(type_size_array[Variant::PACKED_STRING_ARRAY][sizeof(void *)] == sizeof(PackedStringArray), "Size of PackedStringArray mismatch");238static_assert(type_size_array[Variant::PACKED_VECTOR2_ARRAY][sizeof(void *)] == sizeof(PackedVector2Array), "Size of PackedVector2Array mismatch");239static_assert(type_size_array[Variant::PACKED_VECTOR3_ARRAY][sizeof(void *)] == sizeof(PackedVector3Array), "Size of PackedVector3Array mismatch");240static_assert(type_size_array[Variant::PACKED_COLOR_ARRAY][sizeof(void *)] == sizeof(PackedColorArray), "Size of PackedColorArray mismatch");241static_assert(type_size_array[Variant::PACKED_VECTOR4_ARRAY][sizeof(void *)] == sizeof(PackedVector4Array), "Size of PackedVector4Array mismatch");242static_assert(type_size_array[Variant::VARIANT_MAX][sizeof(void *)] == sizeof(Variant), "Size of Variant mismatch");243244Array core_type_sizes;245246for (int i = 0; i < 4; i++) {247Dictionary d;248d["build_configuration"] = build_config_name[i];249Array sizes;250for (int j = 0; j <= Variant::VARIANT_MAX; j++) {251Variant::Type t = type_size_array[j].type;252String name = t == Variant::VARIANT_MAX ? String("Variant") : Variant::get_type_name(t);253Dictionary d2;254d2["name"] = name;255uint32_t size = 0;256switch (i) {257case 0:258size = type_size_array[j].size_32_bits_real_float;259break;260case 1:261size = type_size_array[j].size_64_bits_real_float;262break;263case 2:264size = type_size_array[j].size_32_bits_real_double;265break;266case 3:267size = type_size_array[j].size_64_bits_real_double;268break;269}270d2["size"] = size;271sizes.push_back(d2);272}273d["sizes"] = sizes;274core_type_sizes.push_back(d);275}276api_dump["builtin_class_sizes"] = core_type_sizes;277}278279{280// Member offsets, meta types and sizes.281282#define REAL_MEMBER_OFFSET(type, member) \283{ \284type, \285member, \286"float", \287sizeof(float), \288"float", \289sizeof(float), \290"double", \291sizeof(double), \292"double", \293sizeof(double), \294}295296#define INT32_MEMBER_OFFSET(type, member) \297{ \298type, \299member, \300"int32", \301sizeof(int32_t), \302"int32", \303sizeof(int32_t), \304"int32", \305sizeof(int32_t), \306"int32", \307sizeof(int32_t), \308}309310#define INT32_BASED_BUILTIN_MEMBER_OFFSET(type, member, member_type, member_elems) \311{ \312type, \313member, \314member_type, \315sizeof(int32_t) * member_elems, \316member_type, \317sizeof(int32_t) * member_elems, \318member_type, \319sizeof(int32_t) * member_elems, \320member_type, \321sizeof(int32_t) * member_elems, \322}323324#define REAL_BASED_BUILTIN_MEMBER_OFFSET(type, member, member_type, member_elems) \325{ \326type, \327member, \328member_type, \329sizeof(float) * member_elems, \330member_type, \331sizeof(float) * member_elems, \332member_type, \333sizeof(double) * member_elems, \334member_type, \335sizeof(double) * member_elems, \336}337338struct {339Variant::Type type;340const char *member;341const char *member_meta_32_bits_real_float;342const uint32_t member_size_32_bits_real_float;343const char *member_meta_64_bits_real_float;344const uint32_t member_size_64_bits_real_float;345const char *member_meta_32_bits_real_double;346const uint32_t member_size_32_bits_real_double;347const char *member_meta_64_bits_real_double;348const uint32_t member_size_64_bits_real_double;349} member_offset_array[] = {350// Vector2351REAL_MEMBER_OFFSET(Variant::VECTOR2, "x"),352REAL_MEMBER_OFFSET(Variant::VECTOR2, "y"),353// Vector2i354INT32_MEMBER_OFFSET(Variant::VECTOR2I, "x"),355INT32_MEMBER_OFFSET(Variant::VECTOR2I, "y"),356// Rect2357REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::RECT2, "position", "Vector2", 2),358REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::RECT2, "size", "Vector2", 2),359// Rect2i360INT32_BASED_BUILTIN_MEMBER_OFFSET(Variant::RECT2I, "position", "Vector2i", 2),361INT32_BASED_BUILTIN_MEMBER_OFFSET(Variant::RECT2I, "size", "Vector2i", 2),362// Vector3363REAL_MEMBER_OFFSET(Variant::VECTOR3, "x"),364REAL_MEMBER_OFFSET(Variant::VECTOR3, "y"),365REAL_MEMBER_OFFSET(Variant::VECTOR3, "z"),366// Vector3i367INT32_MEMBER_OFFSET(Variant::VECTOR3I, "x"),368INT32_MEMBER_OFFSET(Variant::VECTOR3I, "y"),369INT32_MEMBER_OFFSET(Variant::VECTOR3I, "z"),370// Transform2D371REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::TRANSFORM2D, "x", "Vector2", 2),372REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::TRANSFORM2D, "y", "Vector2", 2),373REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::TRANSFORM2D, "origin", "Vector2", 2),374// Vector4375REAL_MEMBER_OFFSET(Variant::VECTOR4, "x"),376REAL_MEMBER_OFFSET(Variant::VECTOR4, "y"),377REAL_MEMBER_OFFSET(Variant::VECTOR4, "z"),378REAL_MEMBER_OFFSET(Variant::VECTOR4, "w"),379// Vector4i380INT32_MEMBER_OFFSET(Variant::VECTOR4I, "x"),381INT32_MEMBER_OFFSET(Variant::VECTOR4I, "y"),382INT32_MEMBER_OFFSET(Variant::VECTOR4I, "z"),383INT32_MEMBER_OFFSET(Variant::VECTOR4I, "w"),384// Plane385REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::PLANE, "normal", "Vector3", vec3_elems),386REAL_MEMBER_OFFSET(Variant::PLANE, "d"),387// Quaternion388REAL_MEMBER_OFFSET(Variant::QUATERNION, "x"),389REAL_MEMBER_OFFSET(Variant::QUATERNION, "y"),390REAL_MEMBER_OFFSET(Variant::QUATERNION, "z"),391REAL_MEMBER_OFFSET(Variant::QUATERNION, "w"),392// AABB393REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::AABB, "position", "Vector3", vec3_elems),394REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::AABB, "size", "Vector3", vec3_elems),395// Basis (remember that basis vectors are flipped!)396REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::BASIS, "x", "Vector3", vec3_elems),397REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::BASIS, "y", "Vector3", vec3_elems),398REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::BASIS, "z", "Vector3", vec3_elems),399// Transform3D400REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::TRANSFORM3D, "basis", "Basis", vec3_elems * 3),401REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::TRANSFORM3D, "origin", "Vector3", vec3_elems),402// Projection403REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::PROJECTION, "x", "Vector4", vec4_elems),404REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::PROJECTION, "y", "Vector4", vec4_elems),405REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::PROJECTION, "z", "Vector4", vec4_elems),406REAL_BASED_BUILTIN_MEMBER_OFFSET(Variant::PROJECTION, "w", "Vector4", vec4_elems),407// Color (always composed of 4bytes floats)408{ Variant::COLOR, "r", "float", sizeof(float), "float", sizeof(float), "float", sizeof(float), "float", sizeof(float) },409{ Variant::COLOR, "g", "float", sizeof(float), "float", sizeof(float), "float", sizeof(float), "float", sizeof(float) },410{ Variant::COLOR, "b", "float", sizeof(float), "float", sizeof(float), "float", sizeof(float), "float", sizeof(float) },411{ Variant::COLOR, "a", "float", sizeof(float), "float", sizeof(float), "float", sizeof(float), "float", sizeof(float) },412// End marker, must stay last413{ Variant::NIL, nullptr, nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0 },414};415416Array core_type_member_offsets;417418for (int i = 0; i < 4; i++) {419Dictionary d;420d["build_configuration"] = build_config_name[i];421Array type_offsets;422uint32_t idx = 0;423424Variant::Type previous_type = Variant::NIL;425426Dictionary d2;427Array members;428uint32_t offset = 0;429430while (true) {431Variant::Type t = member_offset_array[idx].type;432if (t != previous_type) {433if (previous_type != Variant::NIL) {434d2["members"] = members;435type_offsets.push_back(d2);436}437if (t == Variant::NIL) {438break;439}440441String name = t == Variant::VARIANT_MAX ? String("Variant") : Variant::get_type_name(t);442d2 = Dictionary();443members = Array();444offset = 0;445d2["name"] = name;446previous_type = t;447}448Dictionary d3;449const char *member_meta = nullptr;450uint32_t member_size = 0;451switch (i) {452case 0:453member_meta = member_offset_array[idx].member_meta_32_bits_real_float;454member_size = member_offset_array[idx].member_size_32_bits_real_float;455break;456case 1:457member_meta = member_offset_array[idx].member_meta_64_bits_real_float;458member_size = member_offset_array[idx].member_size_64_bits_real_float;459break;460case 2:461member_meta = member_offset_array[idx].member_meta_32_bits_real_double;462member_size = member_offset_array[idx].member_size_32_bits_real_double;463break;464case 3:465member_meta = member_offset_array[idx].member_meta_64_bits_real_double;466member_size = member_offset_array[idx].member_size_64_bits_real_double;467break;468}469d3["member"] = member_offset_array[idx].member;470d3["offset"] = offset;471d3["meta"] = member_meta;472offset += member_size;473members.push_back(d3);474idx++;475}476d["classes"] = type_offsets;477core_type_member_offsets.push_back(d);478}479api_dump["builtin_class_member_offsets"] = core_type_member_offsets;480}481482if (p_include_docs) {483EditorHelp::generate_doc(false);484}485486{487// Global enums and constants.488Array constants;489HashMap<String, List<Pair<String, int64_t>>> enum_list;490HashMap<String, bool> enum_is_bitfield;491492const DocData::ClassDoc *global_scope_doc = nullptr;493if (p_include_docs) {494global_scope_doc = EditorHelp::get_doc_data()->class_list.getptr("@GlobalScope");495CRASH_COND_MSG(!global_scope_doc, "Could not find '@GlobalScope' in DocData.");496}497498for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {499int64_t value = CoreConstants::get_global_constant_value(i);500String enum_name = CoreConstants::get_global_constant_enum(i);501String name = CoreConstants::get_global_constant_name(i);502bool bitfield = CoreConstants::is_global_constant_bitfield(i);503if (!enum_name.is_empty()) {504enum_list[enum_name].push_back(Pair<String, int64_t>(name, value));505enum_is_bitfield[enum_name] = bitfield;506} else {507Dictionary d;508d["name"] = name;509d["value"] = value;510d["is_bitfield"] = bitfield;511if (p_include_docs) {512for (const DocData::ConstantDoc &constant_doc : global_scope_doc->constants) {513if (constant_doc.name == name) {514d["description"] = fix_doc_description(constant_doc.description);515break;516}517}518}519constants.push_back(d);520}521}522523api_dump["global_constants"] = constants;524525Array enums;526for (const KeyValue<String, List<Pair<String, int64_t>>> &E : enum_list) {527Dictionary d1;528d1["name"] = E.key;529d1["is_bitfield"] = enum_is_bitfield[E.key];530if (p_include_docs) {531const DocData::EnumDoc *enum_doc = global_scope_doc->enums.getptr(E.key);532if (enum_doc) {533d1["description"] = fix_doc_description(enum_doc->description);534}535}536Array values;537for (const Pair<String, int64_t> &F : E.value) {538Dictionary d2;539d2["name"] = F.first;540d2["value"] = F.second;541if (p_include_docs) {542for (const DocData::ConstantDoc &constant_doc : global_scope_doc->constants) {543if (constant_doc.name == F.first) {544d2["description"] = fix_doc_description(constant_doc.description);545break;546}547}548}549values.push_back(d2);550}551d1["values"] = values;552enums.push_back(d1);553}554555api_dump["global_enums"] = enums;556}557{558Array utility_funcs;559560List<StringName> utility_func_names;561Variant::get_utility_function_list(&utility_func_names);562563const DocData::ClassDoc *global_scope_doc = nullptr;564if (p_include_docs) {565global_scope_doc = EditorHelp::get_doc_data()->class_list.getptr("@GlobalScope");566CRASH_COND_MSG(!global_scope_doc, "Could not find '@GlobalScope' in DocData.");567}568569for (const StringName &name : utility_func_names) {570Dictionary func;571func["name"] = String(name);572if (Variant::has_utility_function_return_value(name)) {573Variant::Type rt = Variant::get_utility_function_return_type(name);574func["return_type"] = rt == Variant::NIL ? String("Variant") : Variant::get_type_name(rt);575}576switch (Variant::get_utility_function_type(name)) {577case Variant::UTILITY_FUNC_TYPE_MATH:578func["category"] = "math";579break;580case Variant::UTILITY_FUNC_TYPE_RANDOM:581func["category"] = "random";582break;583case Variant::UTILITY_FUNC_TYPE_GENERAL:584func["category"] = "general";585break;586}587bool vararg = Variant::is_utility_function_vararg(name);588func["is_vararg"] = Variant::is_utility_function_vararg(name);589func["hash"] = Variant::get_utility_function_hash(name);590Array arguments;591int argcount = Variant::get_utility_function_argument_count(name);592for (int i = 0; i < argcount; i++) {593Dictionary arg;594String argname = vararg ? "arg" + itos(i + 1) : Variant::get_utility_function_argument_name(name, i);595arg["name"] = argname;596arg["type"] = get_builtin_or_variant_type_name(Variant::get_utility_function_argument_type(name, i));597//no default value support in utility functions598arguments.push_back(arg);599}600601if (arguments.size()) {602func["arguments"] = arguments;603}604605if (p_include_docs) {606for (const DocData::MethodDoc &method_doc : global_scope_doc->methods) {607if (method_doc.name == name) {608func["description"] = fix_doc_description(method_doc.description);609break;610}611}612}613614utility_funcs.push_back(func);615}616617api_dump["utility_functions"] = utility_funcs;618}619620{621// builtin types622623Array builtins;624625for (int i = 0; i < Variant::VARIANT_MAX; i++) {626if (i == Variant::OBJECT) {627continue;628}629630Variant::Type type = Variant::Type(i);631632Dictionary d;633d["name"] = Variant::get_type_name(type);634if (Variant::has_indexing(type)) {635d["indexing_return_type"] = get_builtin_or_variant_type_name(Variant::get_indexed_element_type(type));636}637638d["is_keyed"] = Variant::is_keyed(type);639640DocData::ClassDoc *builtin_doc = nullptr;641if (p_include_docs && d["name"] != "Nil") {642builtin_doc = EditorHelp::get_doc_data()->class_list.getptr(d["name"]);643CRASH_COND_MSG(!builtin_doc, vformat("Could not find '%s' in DocData.", d["name"]));644}645646{647//members648Array members;649650List<StringName> member_names;651Variant::get_member_list(type, &member_names);652for (const StringName &member_name : member_names) {653Dictionary d2;654d2["name"] = String(member_name);655d2["type"] = get_builtin_or_variant_type_name(Variant::get_member_type(type, member_name));656if (p_include_docs) {657for (const DocData::PropertyDoc &property_doc : builtin_doc->properties) {658if (property_doc.name == member_name) {659d2["description"] = fix_doc_description(property_doc.description);660break;661}662}663}664members.push_back(d2);665}666if (members.size()) {667d["members"] = members;668}669}670{671//constants672Array constants;673674List<StringName> constant_names;675Variant::get_constants_for_type(type, &constant_names);676for (const StringName &constant_name : constant_names) {677Dictionary d2;678d2["name"] = String(constant_name);679Variant constant = Variant::get_constant_value(type, constant_name);680d2["type"] = get_builtin_or_variant_type_name(constant.get_type());681d2["value"] = constant.get_construct_string();682if (p_include_docs) {683for (const DocData::ConstantDoc &constant_doc : builtin_doc->constants) {684if (constant_doc.name == constant_name) {685d2["description"] = fix_doc_description(constant_doc.description);686break;687}688}689}690constants.push_back(d2);691}692if (constants.size()) {693d["constants"] = constants;694}695}696{697//enums698Array enums;699700List<StringName> enum_names;701Variant::get_enums_for_type(type, &enum_names);702for (const StringName &enum_name : enum_names) {703Dictionary enum_dict;704enum_dict["name"] = String(enum_name);705706List<StringName> enumeration_names;707Variant::get_enumerations_for_enum(type, enum_name, &enumeration_names);708709Array values;710711for (const StringName &enumeration : enumeration_names) {712Dictionary values_dict;713values_dict["name"] = String(enumeration);714values_dict["value"] = Variant::get_enum_value(type, enum_name, enumeration);715if (p_include_docs) {716for (const DocData::ConstantDoc &constant_doc : builtin_doc->constants) {717if (constant_doc.name == enumeration) {718values_dict["description"] = fix_doc_description(constant_doc.description);719break;720}721}722}723values.push_back(values_dict);724}725726if (p_include_docs) {727const DocData::EnumDoc *enum_doc = builtin_doc->enums.getptr(enum_name);728if (enum_doc) {729enum_dict["description"] = fix_doc_description(enum_doc->description);730}731}732733if (values.size()) {734enum_dict["values"] = values;735}736enums.push_back(enum_dict);737}738739if (enums.size()) {740d["enums"] = enums;741}742}743{744//operators745Array operators;746747for (int j = 0; j < Variant::VARIANT_MAX; j++) {748for (int k = 0; k < Variant::OP_MAX; k++) {749Variant::Type rt = Variant::get_operator_return_type(Variant::Operator(k), type, Variant::Type(j));750if (rt != Variant::NIL) {751Dictionary d2;752String operator_name = Variant::get_operator_name(Variant::Operator(k));753d2["name"] = operator_name;754755String right_type_name = get_builtin_or_variant_type_name(Variant::Type(j));756bool is_unary = k == Variant::OP_NEGATE || k == Variant::OP_POSITIVE || k == Variant::OP_NOT || k == Variant::OP_BIT_NEGATE;757if (!is_unary) {758d2["right_type"] = right_type_name;759}760761d2["return_type"] = get_builtin_or_variant_type_name(Variant::get_operator_return_type(Variant::Operator(k), type, Variant::Type(j)));762763if (p_include_docs && builtin_doc != nullptr) {764for (const DocData::MethodDoc &operator_doc : builtin_doc->operators) {765if (operator_doc.name == "operator " + operator_name &&766(is_unary || operator_doc.arguments[0].type == right_type_name)) {767d2["description"] = fix_doc_description(operator_doc.description);768break;769}770}771}772773operators.push_back(d2);774}775}776}777if (operators.size()) {778d["operators"] = operators;779}780}781{782//methods783Array methods;784785List<StringName> method_names;786Variant::get_builtin_method_list(type, &method_names);787for (const StringName &method_name : method_names) {788Dictionary d2;789d2["name"] = String(method_name);790if (Variant::has_builtin_method_return_value(type, method_name)) {791Variant::Type ret_type = Variant::get_builtin_method_return_type(type, method_name);792d2["return_type"] = ret_type == Variant::NIL ? String("Variant") : Variant::get_type_name(ret_type);793}794d2["is_vararg"] = Variant::is_builtin_method_vararg(type, method_name);795d2["is_const"] = Variant::is_builtin_method_const(type, method_name);796d2["is_static"] = Variant::is_builtin_method_static(type, method_name);797d2["hash"] = Variant::get_builtin_method_hash(type, method_name);798799Vector<uint32_t> compat_hashes = Variant::get_builtin_method_compatibility_hashes(type, method_name);800Array compatibility;801if (compat_hashes.size()) {802for (int j = 0; j < compat_hashes.size(); j++) {803compatibility.push_back(compat_hashes[j]);804}805}806if (compatibility.size() > 0) {807d2["hash_compatibility"] = compatibility;808}809810Vector<Variant> default_args = Variant::get_builtin_method_default_arguments(type, method_name);811812Array arguments;813int argcount = Variant::get_builtin_method_argument_count(type, method_name);814for (int j = 0; j < argcount; j++) {815Dictionary d3;816d3["name"] = Variant::get_builtin_method_argument_name(type, method_name, j);817d3["type"] = get_builtin_or_variant_type_name(Variant::get_builtin_method_argument_type(type, method_name, j));818819if (j >= (argcount - default_args.size())) {820int dargidx = j - (argcount - default_args.size());821d3["default_value"] = default_args[dargidx].get_construct_string();822}823arguments.push_back(d3);824}825826if (arguments.size()) {827d2["arguments"] = arguments;828}829830if (p_include_docs) {831for (const DocData::MethodDoc &method_doc : builtin_doc->methods) {832if (method_doc.name == method_name) {833d2["description"] = fix_doc_description(method_doc.description);834break;835}836}837}838839methods.push_back(d2);840}841if (methods.size()) {842d["methods"] = methods;843}844}845{846//constructors847Array constructors;848849for (int j = 0; j < Variant::get_constructor_count(type); j++) {850Dictionary d2;851d2["index"] = j;852853Array arguments;854int argcount = Variant::get_constructor_argument_count(type, j);855for (int k = 0; k < argcount; k++) {856Dictionary d3;857d3["name"] = Variant::get_constructor_argument_name(type, j, k);858d3["type"] = get_builtin_or_variant_type_name(Variant::get_constructor_argument_type(type, j, k));859arguments.push_back(d3);860}861if (arguments.size()) {862d2["arguments"] = arguments;863}864865if (p_include_docs && builtin_doc) {866for (const DocData::MethodDoc &constructor_doc : builtin_doc->constructors) {867if (constructor_doc.arguments.size() != argcount) {868continue;869}870bool constructor_found = true;871for (int k = 0; k < argcount; k++) {872const DocData::ArgumentDoc &argument_doc = constructor_doc.arguments[k];873const Dictionary &argument_dict = arguments[k];874const String &argument_string = argument_dict["type"];875if (argument_doc.type != argument_string) {876constructor_found = false;877break;878}879}880if (constructor_found) {881d2["description"] = fix_doc_description(constructor_doc.description);882}883}884}885886constructors.push_back(d2);887}888889if (constructors.size()) {890d["constructors"] = constructors;891}892}893{894//destructor895d["has_destructor"] = Variant::has_destructor(type);896}897898if (p_include_docs && builtin_doc != nullptr) {899d["brief_description"] = fix_doc_description(builtin_doc->brief_description);900d["description"] = fix_doc_description(builtin_doc->description);901}902903builtins.push_back(d);904}905906api_dump["builtin_classes"] = builtins;907}908909{910// classes911Array classes;912913LocalVector<StringName> class_list;914915ClassDB::get_class_list(class_list);916917for (const StringName &class_name : class_list) {918if (!ClassDB::is_class_exposed(class_name)) {919continue;920}921Dictionary d;922d["name"] = String(class_name);923d["is_refcounted"] = ClassDB::is_parent_class(class_name, "RefCounted");924d["is_instantiable"] = ClassDB::can_instantiate(class_name);925StringName parent_class = ClassDB::get_parent_class(class_name);926if (parent_class != StringName()) {927d["inherits"] = String(parent_class);928}929930DocData::ClassDoc *class_doc = nullptr;931if (p_include_docs) {932class_doc = EditorHelp::get_doc_data()->class_list.getptr(class_name);933CRASH_COND_MSG(!class_doc, vformat("Could not find '%s' in DocData.", class_name));934}935936{937ClassDB::APIType api = ClassDB::get_api_type(class_name);938static const char *api_type[5] = { "core", "editor", "extension", "editor_extension" };939d["api_type"] = api_type[api];940}941942{943//constants944Array constants;945List<String> constant_list;946ClassDB::get_integer_constant_list(class_name, &constant_list, true);947for (const String &F : constant_list) {948StringName enum_name = ClassDB::get_integer_constant_enum(class_name, F);949if (enum_name != StringName()) {950continue; //enums will be handled on their own951}952953Dictionary d2;954d2["name"] = String(F);955d2["value"] = ClassDB::get_integer_constant(class_name, F);956957if (p_include_docs) {958for (const DocData::ConstantDoc &constant_doc : class_doc->constants) {959if (constant_doc.name == F) {960d2["description"] = fix_doc_description(constant_doc.description);961break;962}963}964}965966constants.push_back(d2);967}968969if (constants.size()) {970d["constants"] = constants;971}972}973{974//enum975Array enums;976List<StringName> enum_list;977ClassDB::get_enum_list(class_name, &enum_list, true);978for (const StringName &F : enum_list) {979Dictionary d2;980d2["name"] = String(F);981d2["is_bitfield"] = ClassDB::is_enum_bitfield(class_name, F);982983Array values;984List<StringName> enum_constant_list;985ClassDB::get_enum_constants(class_name, F, &enum_constant_list, true);986for (const StringName &enum_constant : enum_constant_list) {987Dictionary d3;988d3["name"] = String(enum_constant);989d3["value"] = ClassDB::get_integer_constant(class_name, enum_constant);990991if (p_include_docs) {992for (const DocData::ConstantDoc &constant_doc : class_doc->constants) {993if (constant_doc.name == enum_constant) {994d3["description"] = fix_doc_description(constant_doc.description);995break;996}997}998}9991000values.push_back(d3);1001}10021003d2["values"] = values;10041005if (p_include_docs) {1006const DocData::EnumDoc *enum_doc = class_doc->enums.getptr(F);1007if (enum_doc) {1008d2["description"] = fix_doc_description(enum_doc->description);1009}1010}10111012enums.push_back(d2);1013}10141015if (enums.size()) {1016d["enums"] = enums;1017}1018}1019{1020//methods1021Array methods;1022List<MethodInfo> method_list;1023ClassDB::get_method_list(class_name, &method_list, true);1024for (const MethodInfo &F : method_list) {1025StringName method_name = F.name;1026if ((F.flags & METHOD_FLAG_VIRTUAL) && !(F.flags & METHOD_FLAG_OBJECT_CORE)) {1027//virtual method1028const MethodInfo &mi = F;1029Dictionary d2;1030d2["name"] = String(method_name);1031d2["is_const"] = (F.flags & METHOD_FLAG_CONST) ? true : false;1032d2["is_static"] = (F.flags & METHOD_FLAG_STATIC) ? true : false;1033d2["is_required"] = (F.flags & METHOD_FLAG_VIRTUAL_REQUIRED) ? true : false;1034d2["is_vararg"] = false;1035d2["is_virtual"] = true;1036d2["hash"] = mi.get_compatibility_hash();10371038Vector<uint32_t> compat_hashes = ClassDB::get_virtual_method_compatibility_hashes(class_name, method_name);1039Array compatibility;1040if (compat_hashes.size()) {1041for (int i = 0; i < compat_hashes.size(); i++) {1042compatibility.push_back(compat_hashes[i]);1043}1044}1045if (compatibility.size() > 0) {1046d2["hash_compatibility"] = compatibility;1047}10481049bool has_return = mi.return_val.type != Variant::NIL || (mi.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT);1050if (has_return) {1051PropertyInfo pinfo = mi.return_val;1052Dictionary d3;10531054d3["type"] = get_property_info_type_name(pinfo);10551056if (mi.get_argument_meta(-1) > 0) {1057d3["meta"] = get_type_meta_name((GodotTypeInfo::Metadata)mi.get_argument_meta(-1));1058}10591060d2["return_value"] = d3;1061}10621063Array arguments;1064for (int64_t i = 0; i < mi.arguments.size(); ++i) {1065const PropertyInfo &pinfo = mi.arguments[i];1066Dictionary d3;10671068d3["name"] = pinfo.name;10691070d3["type"] = get_property_info_type_name(pinfo);10711072if (mi.get_argument_meta(i) > 0) {1073d3["meta"] = get_type_meta_name((GodotTypeInfo::Metadata)mi.get_argument_meta(i));1074}10751076arguments.push_back(d3);1077}10781079if (arguments.size()) {1080d2["arguments"] = arguments;1081}10821083if (p_include_docs) {1084for (const DocData::MethodDoc &method_doc : class_doc->methods) {1085if (method_doc.name == method_name) {1086d2["description"] = fix_doc_description(method_doc.description);1087break;1088}1089}1090}10911092methods.push_back(d2);10931094} else if (F.name.begins_with("_")) {1095//hidden method, ignore10961097} else {1098Dictionary d2;1099d2["name"] = String(method_name);11001101MethodBind *method = ClassDB::get_method(class_name, method_name);1102if (!method) {1103continue;1104}11051106d2["is_const"] = method->is_const();1107d2["is_vararg"] = method->is_vararg();1108d2["is_static"] = method->is_static();1109d2["is_virtual"] = false;1110d2["hash"] = method->get_hash();11111112Vector<uint32_t> compat_hashes = ClassDB::get_method_compatibility_hashes(class_name, method_name);1113Array compatibility;1114if (compat_hashes.size()) {1115for (int i = 0; i < compat_hashes.size(); i++) {1116compatibility.push_back(compat_hashes[i]);1117}1118}11191120#ifndef DISABLE_DEPRECATED1121GDExtensionSpecialCompatHashes::get_legacy_hashes(class_name, method_name, compatibility);1122#endif11231124if (compatibility.size() > 0) {1125d2["hash_compatibility"] = compatibility;1126}11271128Vector<Variant> default_args = method->get_default_arguments();11291130Array arguments;1131for (int i = (method->has_return() ? -1 : 0); i < method->get_argument_count(); i++) {1132PropertyInfo pinfo = i == -1 ? method->get_return_info() : method->get_argument_info(i);1133Dictionary d3;11341135if (i >= 0) {1136d3["name"] = pinfo.name;1137}1138d3["type"] = get_property_info_type_name(pinfo);11391140if (method->get_argument_meta(i) > 0) {1141d3["meta"] = get_type_meta_name(method->get_argument_meta(i));1142}11431144if (i >= 0 && i >= (method->get_argument_count() - default_args.size())) {1145int dargidx = i - (method->get_argument_count() - default_args.size());1146d3["default_value"] = default_args[dargidx].get_construct_string();1147}11481149if (i == -1) {1150d2["return_value"] = d3;1151} else {1152arguments.push_back(d3);1153}1154}11551156if (arguments.size()) {1157d2["arguments"] = arguments;1158}11591160if (p_include_docs) {1161for (const DocData::MethodDoc &method_doc : class_doc->methods) {1162if (method_doc.name == method_name) {1163d2["description"] = fix_doc_description(method_doc.description);1164break;1165}1166}1167}11681169methods.push_back(d2);1170}1171}11721173if (methods.size()) {1174d["methods"] = methods;1175}1176}11771178{1179//signals1180Array signals;1181List<MethodInfo> signal_list;1182ClassDB::get_signal_list(class_name, &signal_list, true);1183for (const MethodInfo &F : signal_list) {1184if (F.name.begins_with("_")) {1185continue; // Hidden signal.1186}11871188StringName signal_name = F.name;1189Dictionary d2;1190d2["name"] = String(signal_name);11911192Array arguments;11931194for (int64_t i = 0; i < F.arguments.size(); ++i) {1195Dictionary d3;1196d3["name"] = F.arguments[i].name;1197d3["type"] = get_property_info_type_name(F.arguments[i]);1198if (F.get_argument_meta(i) > 0) {1199d3["meta"] = get_type_meta_name((GodotTypeInfo::Metadata)F.get_argument_meta(i));1200}1201arguments.push_back(d3);1202}1203if (arguments.size()) {1204d2["arguments"] = arguments;1205}12061207if (p_include_docs) {1208for (const DocData::MethodDoc &signal_doc : class_doc->signals) {1209if (signal_doc.name == signal_name) {1210d2["description"] = fix_doc_description(signal_doc.description);1211break;1212}1213}1214}12151216signals.push_back(d2);1217}12181219if (signals.size()) {1220d["signals"] = signals;1221}1222}1223{1224//properties1225Array properties;1226List<PropertyInfo> property_list;1227ClassDB::get_property_list(class_name, &property_list, true);1228for (const PropertyInfo &F : property_list) {1229if (F.usage & PROPERTY_USAGE_CATEGORY || F.usage & PROPERTY_USAGE_GROUP || F.usage & PROPERTY_USAGE_SUBGROUP || (F.type == Variant::NIL && F.usage & PROPERTY_USAGE_ARRAY)) {1230continue; //not real properties1231}1232if (F.name.begins_with("_")) {1233continue; //hidden property1234}1235if (F.name.contains_char('/')) {1236// Ignore properties with '/' (slash) in the name. These are only meant for use in the inspector.1237continue;1238}1239StringName property_name = F.name;1240Dictionary d2;1241d2["type"] = get_property_info_type_name(F);1242d2["name"] = String(property_name);1243StringName setter = ClassDB::get_property_setter(class_name, F.name);1244if (!(setter == "")) {1245d2["setter"] = setter;1246}1247StringName getter = ClassDB::get_property_getter(class_name, F.name);1248if (!(getter == "")) {1249d2["getter"] = getter;1250}1251int index = ClassDB::get_property_index(class_name, F.name);1252if (index != -1) {1253d2["index"] = index;1254}12551256if (p_include_docs) {1257for (const DocData::PropertyDoc &property_doc : class_doc->properties) {1258if (property_doc.name == property_name) {1259d2["description"] = fix_doc_description(property_doc.description);1260break;1261}1262}1263}12641265properties.push_back(d2);1266}12671268if (properties.size()) {1269d["properties"] = properties;1270}1271}12721273if (p_include_docs && class_doc != nullptr) {1274d["brief_description"] = fix_doc_description(class_doc->brief_description);1275d["description"] = fix_doc_description(class_doc->description);1276}12771278classes.push_back(d);1279}12801281api_dump["classes"] = classes;1282}12831284{1285// singletons12861287Array singletons;1288List<Engine::Singleton> singleton_list;1289Engine::get_singleton()->get_singletons(&singleton_list);12901291for (const Engine::Singleton &s : singleton_list) {1292Dictionary d;1293d["name"] = s.name;1294if (s.class_name != StringName()) {1295d["type"] = String(s.class_name);1296} else {1297d["type"] = String(s.ptr->get_class());1298}1299singletons.push_back(d);1300}13011302if (singletons.size()) {1303api_dump["singletons"] = singletons;1304}1305}13061307{1308Array native_structures;13091310List<StringName> native_structs;1311ClassDB::get_native_struct_list(&native_structs);1312native_structs.sort_custom<StringName::AlphCompare>();13131314for (const StringName &E : native_structs) {1315String code = ClassDB::get_native_struct_code(E);13161317Dictionary d;1318d["name"] = String(E);1319d["format"] = code;13201321native_structures.push_back(d);1322}13231324api_dump["native_structures"] = native_structures;1325}13261327return api_dump;1328}13291330void GDExtensionAPIDump::generate_extension_json_file(const String &p_path, bool p_include_docs) {1331Dictionary api = generate_extension_api(p_include_docs);1332Ref<JSON> json;1333json.instantiate();13341335String text = json->stringify(api, "\t", false) + "\n";1336Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::WRITE);1337ERR_FAIL_COND_MSG(fa.is_null(), vformat("Cannot open file '%s' for writing.", p_path));1338fa->store_string(text);1339}13401341static 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) {1342bool failed = false;1343String path = p_path + "/" + p_field;1344if (p_old_value.get_type() == Variant::ARRAY && p_new_value.get_type() == Variant::ARRAY) {1345Array old_array = p_old_value;1346Array new_array = p_new_value;1347if (!compare_value(path, "size", old_array.size(), new_array.size(), p_allow_name_change)) {1348failed = true;1349}1350for (int i = 0; i < old_array.size() && i < new_array.size(); i++) {1351if (!compare_value(path, itos(i), old_array[i], new_array[i], p_allow_name_change)) {1352failed = true;1353}1354}1355} else if (p_old_value.get_type() == Variant::DICTIONARY && p_new_value.get_type() == Variant::DICTIONARY) {1356Dictionary old_dict = p_old_value;1357Dictionary new_dict = p_new_value;1358for (const KeyValue<Variant, Variant> &kv : old_dict) {1359if (!new_dict.has(kv.key)) {1360failed = true;1361print_error(vformat("Validate extension JSON: Error: Field '%s': %s was removed.", p_path, kv.key));1362continue;1363}1364if (p_allow_name_change && kv.key == "name") {1365continue;1366}1367if (!compare_value(path, kv.key, kv.value, new_dict[kv.key], p_allow_name_change)) {1368failed = true;1369}1370}1371for (const Variant &key : old_dict.keys()) {1372if (!old_dict.has(key)) {1373failed = true;1374print_error(vformat("Validate extension JSON: Error: Field '%s': %s was added with value %s.", p_path, key, new_dict[key]));1375}1376}1377} else {1378bool equal = Variant::evaluate(Variant::OP_EQUAL, p_old_value, p_new_value);1379if (!equal) {1380print_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()));1381return false;1382}1383}1384return !failed;1385}13861387static 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) {1388String base_array = p_outer_class + p_base_array;1389if (!p_old_api.has(p_base_array)) {1390return true; // May just not have this array and its still good. Probably added recently.1391}1392bool failed = false;1393ERR_FAIL_COND_V_MSG(!p_new_api.has(p_base_array), false, vformat("New API lacks base array: %s", p_base_array));1394Array new_api = p_new_api[p_base_array];1395HashMap<String, Dictionary> new_api_assoc;13961397for (const Variant &var : new_api) {1398Dictionary elem = var;1399ERR_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));1400String name = elem[p_name_field];1401if (name.is_valid_float()) {1402name = name.trim_suffix(".0"); // Make "integers" stringified as integers.1403}1404if (p_compare_operators && elem.has("right_type")) {1405name += " " + String(elem["right_type"]);1406}1407new_api_assoc.insert(name, elem);1408}14091410Array old_api = p_old_api[p_base_array];1411for (const Variant &var : old_api) {1412Dictionary old_elem = var;1413if (!old_elem.has(p_name_field)) {1414failed = true;1415print_error(vformat("Validate extension JSON: JSON file: element of base array '%s' is missing the field: '%s'.", base_array, p_name_field));1416continue;1417}1418String name = old_elem[p_name_field];1419if (name.is_valid_float()) {1420name = name.trim_suffix(".0"); // Make "integers" stringified as integers.1421}1422if (p_compare_operators && old_elem.has("right_type")) {1423name += " " + String(old_elem["right_type"]);1424}1425if (!new_api_assoc.has(name)) {1426failed = true;1427print_error(vformat("Validate extension JSON: API was removed: %s/%s", base_array, name));1428continue;1429}14301431Dictionary new_elem = new_api_assoc[name];14321433for (int j = 0; j < p_fields_to_compare.size(); j++) {1434String field = p_fields_to_compare[j];1435bool optional = field.begins_with("*");1436if (optional) {1437// This is an optional field, but if exists it has to exist in both.1438field = field.substr(1);1439}14401441bool added = field.begins_with("+");1442if (added) {1443// Meaning this field must either exist or contents may not exist.1444field = field.substr(1);1445}14461447bool enum_values = field.begins_with("$");1448if (enum_values) {1449// Meaning this field is a list of enum values.1450field = field.substr(1);1451}14521453bool allow_name_change = field.begins_with("@");1454if (allow_name_change) {1455// Meaning that when structurally comparing the old and new value, the dictionary entry 'name' may change.1456field = field.substr(1);1457}14581459Variant old_value;14601461if (!old_elem.has(field)) {1462if (optional) {1463if (new_elem.has(field)) {1464failed = true;1465print_error(vformat("Validate extension JSON: JSON file: Field was added in a way that breaks compatibility '%s/%s': %s", base_array, name, field));1466}1467} else if (added && new_elem.has(field)) {1468// Should be ok, field now exists, should not be verified in prior versions where it does not.1469} else {1470failed = true;1471print_error(vformat("Validate extension JSON: JSON file: Missing field in '%s/%s': %s", base_array, name, field));1472}1473continue;1474} else {1475old_value = old_elem[field];1476}14771478if (!new_elem.has(field)) {1479failed = true;1480ERR_PRINT(vformat("Validate extension JSON: Missing field in current API '%s/%s': %s. This is a bug.", base_array, name, field));1481continue;1482}14831484Variant new_value = new_elem[field];14851486if (p_compare_enum_value && name.ends_with("_MAX")) {1487if (static_cast<int64_t>(new_value) > static_cast<int64_t>(old_value)) {1488// Ignore the _MAX value of an enum increasing.1489continue;1490}1491}1492if (enum_values) {1493if (!compare_dict_array(old_elem, new_elem, field, "name", { "value" }, false, base_array + "/" + name + "/", false, true)) {1494failed = true;1495}1496} else if (!compare_value(base_array + "/" + name, field, old_value, new_value, allow_name_change)) {1497failed = true;1498}1499}15001501if (p_compare_hashes) {1502if (!old_elem.has("hash")) {1503if (old_elem.has("is_virtual") && bool(old_elem["is_virtual"]) && !old_elem.has("hash")) {1504continue; // Virtual methods didn't use to have hashes, so skip check if it's missing in the old file.1505}15061507failed = true;1508print_error(vformat("Validate extension JSON: JSON file: element of base array '%s' is missing the field: 'hash'.", base_array));1509continue;1510}15111512uint64_t old_hash = old_elem["hash"];15131514if (!new_elem.has("hash")) {1515failed = true;1516print_error(vformat("Validate extension JSON: Error: Field '%s' is missing the field: 'hash'.", base_array));1517continue;1518}15191520uint64_t new_hash = new_elem["hash"];1521bool hash_found = false;1522if (old_hash == new_hash) {1523hash_found = true;1524} else if (new_elem.has("hash_compatibility")) {1525Array compatibility = new_elem["hash_compatibility"];1526for (int j = 0; j < compatibility.size(); j++) {1527new_hash = compatibility[j];1528if (new_hash == old_hash) {1529hash_found = true;1530break;1531}1532}1533}15341535if (!hash_found) {1536failed = true;1537print_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));1538continue;1539}1540}1541}15421543return !failed;1544}15451546static 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) {1547if (!p_old_api.has(p_outer)) {1548return true; // May just not have this array and its still good. Probably added recently or optional.1549}1550bool failed = false;1551ERR_FAIL_COND_V_MSG(!p_new_api.has(p_outer), false, vformat("New API lacks base array: %s", p_outer));1552Array new_api = p_new_api[p_outer];1553HashMap<String, Dictionary> new_api_assoc;15541555for (const Variant &var : new_api) {1556Dictionary elem = var;1557ERR_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));1558new_api_assoc.insert(elem[p_outer_name], elem);1559}15601561Array old_api = p_old_api[p_outer];15621563for (const Variant &var : old_api) {1564Dictionary old_elem = var;1565if (!old_elem.has(p_outer_name)) {1566failed = true;1567print_error(vformat("Validate extension JSON: JSON file: element of base array '%s' is missing the field: '%s'.", p_outer, p_outer_name));1568continue;1569}1570String name = old_elem[p_outer_name];1571if (!new_api_assoc.has(name)) {1572failed = true;1573if (!r_removed_classes_registered.has(name)) {1574print_error(vformat("Validate extension JSON: API was removed: %s/%s", p_outer, name));1575r_removed_classes_registered.insert(name);1576}1577continue;1578}15791580Dictionary new_elem = new_api_assoc[name];15811582if (!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)) {1583failed = true;1584}1585}15861587return !failed;1588}15891590Error GDExtensionAPIDump::validate_extension_json_file(const String &p_path) {1591Error error;1592String text = FileAccess::get_file_as_string(p_path, &error);1593if (error != OK) {1594ERR_PRINT(vformat("Validate extension JSON: Could not open file '%s'.", p_path));1595return error;1596}15971598Ref<JSON> json;1599json.instantiate();1600error = json->parse(text);1601if (error != OK) {1602ERR_PRINT(vformat("Validate extension JSON: Error parsing '%s' at line %d: %s", p_path, json->get_error_line(), json->get_error_message()));1603return error;1604}16051606Dictionary old_api = json->get_data();1607Dictionary new_api = generate_extension_api();16081609{ // Validate header:1610Dictionary header = old_api["header"];1611ERR_FAIL_COND_V(!header.has("version_major"), ERR_INVALID_DATA);1612ERR_FAIL_COND_V(!header.has("version_minor"), ERR_INVALID_DATA);1613int major = header["version_major"];1614int minor = header["version_minor"];16151616ERR_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));1617ERR_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));1618}16191620bool failed = false;16211622HashSet<String> removed_classes_registered;16231624if (!compare_dict_array(old_api, new_api, "global_constants", "name", Vector<String>({ "value", "is_bitfield" }), false)) {1625failed = true;1626}16271628if (!compare_dict_array(old_api, new_api, "global_enums", "name", Vector<String>({ "$values", "is_bitfield" }), false)) {1629failed = true;1630}16311632if (!compare_dict_array(old_api, new_api, "utility_functions", "name", Vector<String>({ "category", "is_vararg", "*return_type", "*@arguments" }), true)) {1633failed = true;1634}16351636if (!compare_sub_dict_array(removed_classes_registered, "builtin_classes", "name", old_api, new_api, "members", "name", { "type" }, false)) {1637failed = true;1638}16391640if (!compare_sub_dict_array(removed_classes_registered, "builtin_classes", "name", old_api, new_api, "constants", "name", { "type", "value" }, false)) {1641failed = true;1642}16431644if (!compare_sub_dict_array(removed_classes_registered, "builtin_classes", "name", old_api, new_api, "operators", "name", { "return_type" }, false, true)) {1645failed = true;1646}16471648if (!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)) {1649failed = true;1650}16511652if (!compare_sub_dict_array(removed_classes_registered, "builtin_classes", "name", old_api, new_api, "constructors", "index", { "*@arguments" }, false)) {1653failed = true;1654}16551656if (!compare_sub_dict_array(removed_classes_registered, "classes", "name", old_api, new_api, "constants", "name", { "value" }, false)) {1657failed = true;1658}16591660if (!compare_sub_dict_array(removed_classes_registered, "classes", "name", old_api, new_api, "enums", "name", { "is_bitfield", "$values" }, false)) {1661failed = true;1662}16631664if (!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)) {1665failed = true;1666}16671668if (!compare_sub_dict_array(removed_classes_registered, "classes", "name", old_api, new_api, "signals", "name", { "*@arguments" }, false)) {1669failed = true;1670}16711672if (!compare_sub_dict_array(removed_classes_registered, "classes", "name", old_api, new_api, "properties", "name", { "type", "*setter", "*getter", "*index" }, false)) {1673failed = true;1674}16751676if (!compare_dict_array(old_api, new_api, "singletons", "name", Vector<String>({ "type" }), false)) {1677failed = true;1678}16791680if (!compare_dict_array(old_api, new_api, "native_structures", "name", Vector<String>({ "format" }), false)) {1681failed = true;1682}16831684if (failed) {1685return ERR_INVALID_DATA;1686} else {1687return OK;1688}1689}16901691#endif // TOOLS_ENABLED169216931694