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