Path: blob/master/core/extension/extension_api_dump.cpp
9903 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[13] = { "none", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64", "float", "double", "char16", "char32" };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("\t\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<Variant> default_args = Variant::get_builtin_method_default_arguments(type, method_name);800801Array arguments;802int argcount = Variant::get_builtin_method_argument_count(type, method_name);803for (int j = 0; j < argcount; j++) {804Dictionary d3;805d3["name"] = Variant::get_builtin_method_argument_name(type, method_name, j);806d3["type"] = get_builtin_or_variant_type_name(Variant::get_builtin_method_argument_type(type, method_name, j));807808if (j >= (argcount - default_args.size())) {809int dargidx = j - (argcount - default_args.size());810d3["default_value"] = default_args[dargidx].get_construct_string();811}812arguments.push_back(d3);813}814815if (arguments.size()) {816d2["arguments"] = arguments;817}818819if (p_include_docs) {820for (const DocData::MethodDoc &method_doc : builtin_doc->methods) {821if (method_doc.name == method_name) {822d2["description"] = fix_doc_description(method_doc.description);823break;824}825}826}827828methods.push_back(d2);829}830if (methods.size()) {831d["methods"] = methods;832}833}834{835//constructors836Array constructors;837838for (int j = 0; j < Variant::get_constructor_count(type); j++) {839Dictionary d2;840d2["index"] = j;841842Array arguments;843int argcount = Variant::get_constructor_argument_count(type, j);844for (int k = 0; k < argcount; k++) {845Dictionary d3;846d3["name"] = Variant::get_constructor_argument_name(type, j, k);847d3["type"] = get_builtin_or_variant_type_name(Variant::get_constructor_argument_type(type, j, k));848arguments.push_back(d3);849}850if (arguments.size()) {851d2["arguments"] = arguments;852}853854if (p_include_docs && builtin_doc) {855for (const DocData::MethodDoc &constructor_doc : builtin_doc->constructors) {856if (constructor_doc.arguments.size() != argcount) {857continue;858}859bool constructor_found = true;860for (int k = 0; k < argcount; k++) {861const DocData::ArgumentDoc &argument_doc = constructor_doc.arguments[k];862const Dictionary &argument_dict = arguments[k];863const String &argument_string = argument_dict["type"];864if (argument_doc.type != argument_string) {865constructor_found = false;866break;867}868}869if (constructor_found) {870d2["description"] = fix_doc_description(constructor_doc.description);871}872}873}874875constructors.push_back(d2);876}877878if (constructors.size()) {879d["constructors"] = constructors;880}881}882{883//destructor884d["has_destructor"] = Variant::has_destructor(type);885}886887if (p_include_docs && builtin_doc != nullptr) {888d["brief_description"] = fix_doc_description(builtin_doc->brief_description);889d["description"] = fix_doc_description(builtin_doc->description);890}891892builtins.push_back(d);893}894895api_dump["builtin_classes"] = builtins;896}897898{899// classes900Array classes;901902List<StringName> class_list;903904ClassDB::get_class_list(&class_list);905906class_list.sort_custom<StringName::AlphCompare>();907908for (const StringName &class_name : class_list) {909if (!ClassDB::is_class_exposed(class_name)) {910continue;911}912Dictionary d;913d["name"] = String(class_name);914d["is_refcounted"] = ClassDB::is_parent_class(class_name, "RefCounted");915d["is_instantiable"] = ClassDB::can_instantiate(class_name);916StringName parent_class = ClassDB::get_parent_class(class_name);917if (parent_class != StringName()) {918d["inherits"] = String(parent_class);919}920921DocData::ClassDoc *class_doc = nullptr;922if (p_include_docs) {923class_doc = EditorHelp::get_doc_data()->class_list.getptr(class_name);924CRASH_COND_MSG(!class_doc, vformat("Could not find '%s' in DocData.", class_name));925}926927{928ClassDB::APIType api = ClassDB::get_api_type(class_name);929static const char *api_type[5] = { "core", "editor", "extension", "editor_extension" };930d["api_type"] = api_type[api];931}932933{934//constants935Array constants;936List<String> constant_list;937ClassDB::get_integer_constant_list(class_name, &constant_list, true);938for (const String &F : constant_list) {939StringName enum_name = ClassDB::get_integer_constant_enum(class_name, F);940if (enum_name != StringName()) {941continue; //enums will be handled on their own942}943944Dictionary d2;945d2["name"] = String(F);946d2["value"] = ClassDB::get_integer_constant(class_name, F);947948if (p_include_docs) {949for (const DocData::ConstantDoc &constant_doc : class_doc->constants) {950if (constant_doc.name == F) {951d2["description"] = fix_doc_description(constant_doc.description);952break;953}954}955}956957constants.push_back(d2);958}959960if (constants.size()) {961d["constants"] = constants;962}963}964{965//enum966Array enums;967List<StringName> enum_list;968ClassDB::get_enum_list(class_name, &enum_list, true);969for (const StringName &F : enum_list) {970Dictionary d2;971d2["name"] = String(F);972d2["is_bitfield"] = ClassDB::is_enum_bitfield(class_name, F);973974Array values;975List<StringName> enum_constant_list;976ClassDB::get_enum_constants(class_name, F, &enum_constant_list, true);977for (const StringName &enum_constant : enum_constant_list) {978Dictionary d3;979d3["name"] = String(enum_constant);980d3["value"] = ClassDB::get_integer_constant(class_name, enum_constant);981982if (p_include_docs) {983for (const DocData::ConstantDoc &constant_doc : class_doc->constants) {984if (constant_doc.name == enum_constant) {985d3["description"] = fix_doc_description(constant_doc.description);986break;987}988}989}990991values.push_back(d3);992}993994d2["values"] = values;995996if (p_include_docs) {997const DocData::EnumDoc *enum_doc = class_doc->enums.getptr(F);998if (enum_doc) {999d2["description"] = fix_doc_description(enum_doc->description);1000}1001}10021003enums.push_back(d2);1004}10051006if (enums.size()) {1007d["enums"] = enums;1008}1009}1010{1011//methods1012Array methods;1013List<MethodInfo> method_list;1014ClassDB::get_method_list(class_name, &method_list, true);1015for (const MethodInfo &F : method_list) {1016StringName method_name = F.name;1017if ((F.flags & METHOD_FLAG_VIRTUAL) && !(F.flags & METHOD_FLAG_OBJECT_CORE)) {1018//virtual method1019const MethodInfo &mi = F;1020Dictionary d2;1021d2["name"] = String(method_name);1022d2["is_const"] = (F.flags & METHOD_FLAG_CONST) ? true : false;1023d2["is_static"] = (F.flags & METHOD_FLAG_STATIC) ? true : false;1024d2["is_required"] = (F.flags & METHOD_FLAG_VIRTUAL_REQUIRED) ? true : false;1025d2["is_vararg"] = false;1026d2["is_virtual"] = true;1027d2["hash"] = mi.get_compatibility_hash();10281029Vector<uint32_t> compat_hashes = ClassDB::get_virtual_method_compatibility_hashes(class_name, method_name);1030Array compatibility;1031if (compat_hashes.size()) {1032for (int i = 0; i < compat_hashes.size(); i++) {1033compatibility.push_back(compat_hashes[i]);1034}1035}1036if (compatibility.size() > 0) {1037d2["hash_compatibility"] = compatibility;1038}10391040bool has_return = mi.return_val.type != Variant::NIL || (mi.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT);1041if (has_return) {1042PropertyInfo pinfo = mi.return_val;1043Dictionary d3;10441045d3["type"] = get_property_info_type_name(pinfo);10461047if (mi.get_argument_meta(-1) > 0) {1048d3["meta"] = get_type_meta_name((GodotTypeInfo::Metadata)mi.get_argument_meta(-1));1049}10501051d2["return_value"] = d3;1052}10531054Array arguments;1055for (int64_t i = 0; i < mi.arguments.size(); ++i) {1056const PropertyInfo &pinfo = mi.arguments[i];1057Dictionary d3;10581059d3["name"] = pinfo.name;10601061d3["type"] = get_property_info_type_name(pinfo);10621063if (mi.get_argument_meta(i) > 0) {1064d3["meta"] = get_type_meta_name((GodotTypeInfo::Metadata)mi.get_argument_meta(i));1065}10661067arguments.push_back(d3);1068}10691070if (arguments.size()) {1071d2["arguments"] = arguments;1072}10731074if (p_include_docs) {1075for (const DocData::MethodDoc &method_doc : class_doc->methods) {1076if (method_doc.name == method_name) {1077d2["description"] = fix_doc_description(method_doc.description);1078break;1079}1080}1081}10821083methods.push_back(d2);10841085} else if (F.name.begins_with("_")) {1086//hidden method, ignore10871088} else {1089Dictionary d2;1090d2["name"] = String(method_name);10911092MethodBind *method = ClassDB::get_method(class_name, method_name);1093if (!method) {1094continue;1095}10961097d2["is_const"] = method->is_const();1098d2["is_vararg"] = method->is_vararg();1099d2["is_static"] = method->is_static();1100d2["is_virtual"] = false;1101d2["hash"] = method->get_hash();11021103Vector<uint32_t> compat_hashes = ClassDB::get_method_compatibility_hashes(class_name, method_name);1104Array compatibility;1105if (compat_hashes.size()) {1106for (int i = 0; i < compat_hashes.size(); i++) {1107compatibility.push_back(compat_hashes[i]);1108}1109}11101111#ifndef DISABLE_DEPRECATED1112GDExtensionSpecialCompatHashes::get_legacy_hashes(class_name, method_name, compatibility);1113#endif11141115if (compatibility.size() > 0) {1116d2["hash_compatibility"] = compatibility;1117}11181119Vector<Variant> default_args = method->get_default_arguments();11201121Array arguments;1122for (int i = (method->has_return() ? -1 : 0); i < method->get_argument_count(); i++) {1123PropertyInfo pinfo = i == -1 ? method->get_return_info() : method->get_argument_info(i);1124Dictionary d3;11251126if (i >= 0) {1127d3["name"] = pinfo.name;1128}1129d3["type"] = get_property_info_type_name(pinfo);11301131if (method->get_argument_meta(i) > 0) {1132d3["meta"] = get_type_meta_name(method->get_argument_meta(i));1133}11341135if (i >= 0 && i >= (method->get_argument_count() - default_args.size())) {1136int dargidx = i - (method->get_argument_count() - default_args.size());1137d3["default_value"] = default_args[dargidx].get_construct_string();1138}11391140if (i == -1) {1141d2["return_value"] = d3;1142} else {1143arguments.push_back(d3);1144}1145}11461147if (arguments.size()) {1148d2["arguments"] = arguments;1149}11501151if (p_include_docs) {1152for (const DocData::MethodDoc &method_doc : class_doc->methods) {1153if (method_doc.name == method_name) {1154d2["description"] = fix_doc_description(method_doc.description);1155break;1156}1157}1158}11591160methods.push_back(d2);1161}1162}11631164if (methods.size()) {1165d["methods"] = methods;1166}1167}11681169{1170//signals1171Array signals;1172List<MethodInfo> signal_list;1173ClassDB::get_signal_list(class_name, &signal_list, true);1174for (const MethodInfo &F : signal_list) {1175StringName signal_name = F.name;1176Dictionary d2;1177d2["name"] = String(signal_name);11781179Array arguments;11801181for (int64_t i = 0; i < F.arguments.size(); ++i) {1182Dictionary d3;1183d3["name"] = F.arguments[i].name;1184d3["type"] = get_property_info_type_name(F.arguments[i]);1185if (F.get_argument_meta(i) > 0) {1186d3["meta"] = get_type_meta_name((GodotTypeInfo::Metadata)F.get_argument_meta(i));1187}1188arguments.push_back(d3);1189}1190if (arguments.size()) {1191d2["arguments"] = arguments;1192}11931194if (p_include_docs) {1195for (const DocData::MethodDoc &signal_doc : class_doc->signals) {1196if (signal_doc.name == signal_name) {1197d2["description"] = fix_doc_description(signal_doc.description);1198break;1199}1200}1201}12021203signals.push_back(d2);1204}12051206if (signals.size()) {1207d["signals"] = signals;1208}1209}1210{1211//properties1212Array properties;1213List<PropertyInfo> property_list;1214ClassDB::get_property_list(class_name, &property_list, true);1215for (const PropertyInfo &F : property_list) {1216if (F.usage & PROPERTY_USAGE_CATEGORY || F.usage & PROPERTY_USAGE_GROUP || F.usage & PROPERTY_USAGE_SUBGROUP || (F.type == Variant::NIL && F.usage & PROPERTY_USAGE_ARRAY)) {1217continue; //not real properties1218}1219if (F.name.begins_with("_")) {1220continue; //hidden property1221}1222if (F.name.contains_char('/')) {1223// Ignore properties with '/' (slash) in the name. These are only meant for use in the inspector.1224continue;1225}1226StringName property_name = F.name;1227Dictionary d2;1228d2["type"] = get_property_info_type_name(F);1229d2["name"] = String(property_name);1230StringName setter = ClassDB::get_property_setter(class_name, F.name);1231if (!(setter == "")) {1232d2["setter"] = setter;1233}1234StringName getter = ClassDB::get_property_getter(class_name, F.name);1235if (!(getter == "")) {1236d2["getter"] = getter;1237}1238int index = ClassDB::get_property_index(class_name, F.name);1239if (index != -1) {1240d2["index"] = index;1241}12421243if (p_include_docs) {1244for (const DocData::PropertyDoc &property_doc : class_doc->properties) {1245if (property_doc.name == property_name) {1246d2["description"] = fix_doc_description(property_doc.description);1247break;1248}1249}1250}12511252properties.push_back(d2);1253}12541255if (properties.size()) {1256d["properties"] = properties;1257}1258}12591260if (p_include_docs && class_doc != nullptr) {1261d["brief_description"] = fix_doc_description(class_doc->brief_description);1262d["description"] = fix_doc_description(class_doc->description);1263}12641265classes.push_back(d);1266}12671268api_dump["classes"] = classes;1269}12701271{1272// singletons12731274Array singletons;1275List<Engine::Singleton> singleton_list;1276Engine::get_singleton()->get_singletons(&singleton_list);12771278for (const Engine::Singleton &s : singleton_list) {1279Dictionary d;1280d["name"] = s.name;1281if (s.class_name != StringName()) {1282d["type"] = String(s.class_name);1283} else {1284d["type"] = String(s.ptr->get_class());1285}1286singletons.push_back(d);1287}12881289if (singletons.size()) {1290api_dump["singletons"] = singletons;1291}1292}12931294{1295Array native_structures;12961297List<StringName> native_structs;1298ClassDB::get_native_struct_list(&native_structs);1299native_structs.sort_custom<StringName::AlphCompare>();13001301for (const StringName &E : native_structs) {1302String code = ClassDB::get_native_struct_code(E);13031304Dictionary d;1305d["name"] = String(E);1306d["format"] = code;13071308native_structures.push_back(d);1309}13101311api_dump["native_structures"] = native_structures;1312}13131314return api_dump;1315}13161317void GDExtensionAPIDump::generate_extension_json_file(const String &p_path, bool p_include_docs) {1318Dictionary api = generate_extension_api(p_include_docs);1319Ref<JSON> json;1320json.instantiate();13211322String text = json->stringify(api, "\t", false) + "\n";1323Ref<FileAccess> fa = FileAccess::open(p_path, FileAccess::WRITE);1324ERR_FAIL_COND_MSG(fa.is_null(), vformat("Cannot open file '%s' for writing.", p_path));1325fa->store_string(text);1326}13271328static 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) {1329bool failed = false;1330String path = p_path + "/" + p_field;1331if (p_old_value.get_type() == Variant::ARRAY && p_new_value.get_type() == Variant::ARRAY) {1332Array old_array = p_old_value;1333Array new_array = p_new_value;1334if (!compare_value(path, "size", old_array.size(), new_array.size(), p_allow_name_change)) {1335failed = true;1336}1337for (int i = 0; i < old_array.size() && i < new_array.size(); i++) {1338if (!compare_value(path, itos(i), old_array[i], new_array[i], p_allow_name_change)) {1339failed = true;1340}1341}1342} else if (p_old_value.get_type() == Variant::DICTIONARY && p_new_value.get_type() == Variant::DICTIONARY) {1343Dictionary old_dict = p_old_value;1344Dictionary new_dict = p_new_value;1345for (const KeyValue<Variant, Variant> &kv : old_dict) {1346if (!new_dict.has(kv.key)) {1347failed = true;1348print_error(vformat("Validate extension JSON: Error: Field '%s': %s was removed.", p_path, kv.key));1349continue;1350}1351if (p_allow_name_change && kv.key == "name") {1352continue;1353}1354if (!compare_value(path, kv.key, kv.value, new_dict[kv.key], p_allow_name_change)) {1355failed = true;1356}1357}1358for (const Variant &key : old_dict.keys()) {1359if (!old_dict.has(key)) {1360failed = true;1361print_error(vformat("Validate extension JSON: Error: Field '%s': %s was added with value %s.", p_path, key, new_dict[key]));1362}1363}1364} else {1365bool equal = Variant::evaluate(Variant::OP_EQUAL, p_old_value, p_new_value);1366if (!equal) {1367print_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()));1368return false;1369}1370}1371return !failed;1372}13731374static 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) {1375String base_array = p_outer_class + p_base_array;1376if (!p_old_api.has(p_base_array)) {1377return true; // May just not have this array and its still good. Probably added recently.1378}1379bool failed = false;1380ERR_FAIL_COND_V_MSG(!p_new_api.has(p_base_array), false, vformat("New API lacks base array: %s", p_base_array));1381Array new_api = p_new_api[p_base_array];1382HashMap<String, Dictionary> new_api_assoc;13831384for (const Variant &var : new_api) {1385Dictionary elem = var;1386ERR_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));1387String name = elem[p_name_field];1388if (name.is_valid_float()) {1389name = name.trim_suffix(".0"); // Make "integers" stringified as integers.1390}1391if (p_compare_operators && elem.has("right_type")) {1392name += " " + String(elem["right_type"]);1393}1394new_api_assoc.insert(name, elem);1395}13961397Array old_api = p_old_api[p_base_array];1398for (const Variant &var : old_api) {1399Dictionary old_elem = var;1400if (!old_elem.has(p_name_field)) {1401failed = true;1402print_error(vformat("Validate extension JSON: JSON file: element of base array '%s' is missing the field: '%s'.", base_array, p_name_field));1403continue;1404}1405String name = old_elem[p_name_field];1406if (name.is_valid_float()) {1407name = name.trim_suffix(".0"); // Make "integers" stringified as integers.1408}1409if (p_compare_operators && old_elem.has("right_type")) {1410name += " " + String(old_elem["right_type"]);1411}1412if (!new_api_assoc.has(name)) {1413failed = true;1414print_error(vformat("Validate extension JSON: API was removed: %s/%s", base_array, name));1415continue;1416}14171418Dictionary new_elem = new_api_assoc[name];14191420for (int j = 0; j < p_fields_to_compare.size(); j++) {1421String field = p_fields_to_compare[j];1422bool optional = field.begins_with("*");1423if (optional) {1424// This is an optional field, but if exists it has to exist in both.1425field = field.substr(1);1426}14271428bool added = field.begins_with("+");1429if (added) {1430// Meaning this field must either exist or contents may not exist.1431field = field.substr(1);1432}14331434bool enum_values = field.begins_with("$");1435if (enum_values) {1436// Meaning this field is a list of enum values.1437field = field.substr(1);1438}14391440bool allow_name_change = field.begins_with("@");1441if (allow_name_change) {1442// Meaning that when structurally comparing the old and new value, the dictionary entry 'name' may change.1443field = field.substr(1);1444}14451446Variant old_value;14471448if (!old_elem.has(field)) {1449if (optional) {1450if (new_elem.has(field)) {1451failed = true;1452print_error(vformat("Validate extension JSON: JSON file: Field was added in a way that breaks compatibility '%s/%s': %s", base_array, name, field));1453}1454} else if (added && new_elem.has(field)) {1455// Should be ok, field now exists, should not be verified in prior versions where it does not.1456} else {1457failed = true;1458print_error(vformat("Validate extension JSON: JSON file: Missing field in '%s/%s': %s", base_array, name, field));1459}1460continue;1461} else {1462old_value = old_elem[field];1463}14641465if (!new_elem.has(field)) {1466failed = true;1467ERR_PRINT(vformat("Validate extension JSON: Missing field in current API '%s/%s': %s. This is a bug.", base_array, name, field));1468continue;1469}14701471Variant new_value = new_elem[field];14721473if (p_compare_enum_value && name.ends_with("_MAX")) {1474if (static_cast<int64_t>(new_value) > static_cast<int64_t>(old_value)) {1475// Ignore the _MAX value of an enum increasing.1476continue;1477}1478}1479if (enum_values) {1480if (!compare_dict_array(old_elem, new_elem, field, "name", { "value" }, false, base_array + "/" + name + "/", false, true)) {1481failed = true;1482}1483} else if (!compare_value(base_array + "/" + name, field, old_value, new_value, allow_name_change)) {1484failed = true;1485}1486}14871488if (p_compare_hashes) {1489if (!old_elem.has("hash")) {1490if (old_elem.has("is_virtual") && bool(old_elem["is_virtual"]) && !old_elem.has("hash")) {1491continue; // Virtual methods didn't use to have hashes, so skip check if it's missing in the old file.1492}14931494failed = true;1495print_error(vformat("Validate extension JSON: JSON file: element of base array '%s' is missing the field: 'hash'.", base_array));1496continue;1497}14981499uint64_t old_hash = old_elem["hash"];15001501if (!new_elem.has("hash")) {1502failed = true;1503print_error(vformat("Validate extension JSON: Error: Field '%s' is missing the field: 'hash'.", base_array));1504continue;1505}15061507uint64_t new_hash = new_elem["hash"];1508bool hash_found = false;1509if (old_hash == new_hash) {1510hash_found = true;1511} else if (new_elem.has("hash_compatibility")) {1512Array compatibility = new_elem["hash_compatibility"];1513for (int j = 0; j < compatibility.size(); j++) {1514new_hash = compatibility[j];1515if (new_hash == old_hash) {1516hash_found = true;1517break;1518}1519}1520}15211522if (!hash_found) {1523failed = true;1524print_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));1525continue;1526}1527}1528}15291530return !failed;1531}15321533static 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) {1534if (!p_old_api.has(p_outer)) {1535return true; // May just not have this array and its still good. Probably added recently or optional.1536}1537bool failed = false;1538ERR_FAIL_COND_V_MSG(!p_new_api.has(p_outer), false, vformat("New API lacks base array: %s", p_outer));1539Array new_api = p_new_api[p_outer];1540HashMap<String, Dictionary> new_api_assoc;15411542for (const Variant &var : new_api) {1543Dictionary elem = var;1544ERR_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));1545new_api_assoc.insert(elem[p_outer_name], elem);1546}15471548Array old_api = p_old_api[p_outer];15491550for (const Variant &var : old_api) {1551Dictionary old_elem = var;1552if (!old_elem.has(p_outer_name)) {1553failed = true;1554print_error(vformat("Validate extension JSON: JSON file: element of base array '%s' is missing the field: '%s'.", p_outer, p_outer_name));1555continue;1556}1557String name = old_elem[p_outer_name];1558if (!new_api_assoc.has(name)) {1559failed = true;1560if (!r_removed_classes_registered.has(name)) {1561print_error(vformat("Validate extension JSON: API was removed: %s/%s", p_outer, name));1562r_removed_classes_registered.insert(name);1563}1564continue;1565}15661567Dictionary new_elem = new_api_assoc[name];15681569if (!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)) {1570failed = true;1571}1572}15731574return !failed;1575}15761577Error GDExtensionAPIDump::validate_extension_json_file(const String &p_path) {1578Error error;1579String text = FileAccess::get_file_as_string(p_path, &error);1580if (error != OK) {1581ERR_PRINT(vformat("Validate extension JSON: Could not open file '%s'.", p_path));1582return error;1583}15841585Ref<JSON> json;1586json.instantiate();1587error = json->parse(text);1588if (error != OK) {1589ERR_PRINT(vformat("Validate extension JSON: Error parsing '%s' at line %d: %s", p_path, json->get_error_line(), json->get_error_message()));1590return error;1591}15921593Dictionary old_api = json->get_data();1594Dictionary new_api = generate_extension_api();15951596{ // Validate header:1597Dictionary header = old_api["header"];1598ERR_FAIL_COND_V(!header.has("version_major"), ERR_INVALID_DATA);1599ERR_FAIL_COND_V(!header.has("version_minor"), ERR_INVALID_DATA);1600int major = header["version_major"];1601int minor = header["version_minor"];16021603ERR_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));1604ERR_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));1605}16061607bool failed = false;16081609HashSet<String> removed_classes_registered;16101611if (!compare_dict_array(old_api, new_api, "global_constants", "name", Vector<String>({ "value", "is_bitfield" }), false)) {1612failed = true;1613}16141615if (!compare_dict_array(old_api, new_api, "global_enums", "name", Vector<String>({ "$values", "is_bitfield" }), false)) {1616failed = true;1617}16181619if (!compare_dict_array(old_api, new_api, "utility_functions", "name", Vector<String>({ "category", "is_vararg", "*return_type", "*@arguments" }), true)) {1620failed = true;1621}16221623if (!compare_sub_dict_array(removed_classes_registered, "builtin_classes", "name", old_api, new_api, "members", "name", { "type" }, false)) {1624failed = true;1625}16261627if (!compare_sub_dict_array(removed_classes_registered, "builtin_classes", "name", old_api, new_api, "constants", "name", { "type", "value" }, false)) {1628failed = true;1629}16301631if (!compare_sub_dict_array(removed_classes_registered, "builtin_classes", "name", old_api, new_api, "operators", "name", { "return_type" }, false, true)) {1632failed = true;1633}16341635if (!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)) {1636failed = true;1637}16381639if (!compare_sub_dict_array(removed_classes_registered, "builtin_classes", "name", old_api, new_api, "constructors", "index", { "*@arguments" }, false)) {1640failed = true;1641}16421643if (!compare_sub_dict_array(removed_classes_registered, "classes", "name", old_api, new_api, "constants", "name", { "value" }, false)) {1644failed = true;1645}16461647if (!compare_sub_dict_array(removed_classes_registered, "classes", "name", old_api, new_api, "enums", "name", { "is_bitfield", "$values" }, false)) {1648failed = true;1649}16501651if (!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)) {1652failed = true;1653}16541655if (!compare_sub_dict_array(removed_classes_registered, "classes", "name", old_api, new_api, "signals", "name", { "*@arguments" }, false)) {1656failed = true;1657}16581659if (!compare_sub_dict_array(removed_classes_registered, "classes", "name", old_api, new_api, "properties", "name", { "type", "*setter", "*getter", "*index" }, false)) {1660failed = true;1661}16621663if (!compare_dict_array(old_api, new_api, "singletons", "name", Vector<String>({ "type" }), false)) {1664failed = true;1665}16661667if (!compare_dict_array(old_api, new_api, "native_structures", "name", Vector<String>({ "format" }), false)) {1668failed = true;1669}16701671if (failed) {1672return ERR_INVALID_DATA;1673} else {1674return OK;1675}1676}16771678#endif // TOOLS_ENABLED167916801681