extension_api_dump.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*************************************************************************/
  2. /* extension_api_dump.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "extension_api_dump.h"
  31. #include "core/config/engine.h"
  32. #include "core/core_constants.h"
  33. #include "core/io/file_access.h"
  34. #include "core/io/json.h"
  35. #include "core/templates/pair.h"
  36. #include "core/version.h"
  37. #ifdef TOOLS_ENABLED
  38. Dictionary NativeExtensionAPIDump::generate_extension_api() {
  39. Dictionary api_dump;
  40. {
  41. //header
  42. Dictionary header;
  43. header["version_major"] = VERSION_MAJOR;
  44. header["version_minor"] = VERSION_MINOR;
  45. #if VERSION_PATCH
  46. header["version_patch"] = VERSION_PATCH;
  47. #else
  48. header["version_patch"] = 0;
  49. #endif
  50. header["version_status"] = VERSION_STATUS;
  51. header["version_build"] = VERSION_BUILD;
  52. header["version_full_name"] = VERSION_FULL_NAME;
  53. api_dump["header"] = header;
  54. }
  55. const uint32_t vec3_elems = 3;
  56. const uint32_t ptrsize_32 = 4;
  57. const uint32_t ptrsize_64 = 4;
  58. static const char *build_config_name[4] = { "float_32", "float_64", "double_32", "double_64" };
  59. {
  60. //type sizes
  61. struct {
  62. Variant::Type type;
  63. uint32_t size_32_bits_real_float;
  64. uint32_t size_64_bits_real_float;
  65. uint32_t size_32_bits_real_double;
  66. uint32_t size_64_bits_real_double;
  67. } type_size_array[Variant::VARIANT_MAX + 1] = {
  68. { Variant::NIL, 0, 0, 0, 0 },
  69. { Variant::BOOL, sizeof(uint32_t), sizeof(uint32_t), sizeof(uint32_t), sizeof(uint32_t) },
  70. { Variant::INT, sizeof(int64_t), sizeof(int64_t), sizeof(int64_t), sizeof(int64_t) },
  71. { Variant::FLOAT, sizeof(double), sizeof(double), sizeof(double), sizeof(double) },
  72. { Variant::STRING, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  73. { Variant::VECTOR2, 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(double), 2 * sizeof(double) },
  74. { Variant::VECTOR2I, 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t) },
  75. { Variant::RECT2, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(double), 4 * sizeof(double) },
  76. { Variant::RECT2I, 4 * sizeof(int32_t), 4 * sizeof(int32_t), 4 * sizeof(int32_t), 4 * sizeof(int32_t) },
  77. { Variant::VECTOR3, vec3_elems * sizeof(float), vec3_elems * sizeof(float), vec3_elems * sizeof(double), vec3_elems * sizeof(double) },
  78. { Variant::VECTOR3I, 3 * sizeof(int32_t), 3 * sizeof(int32_t), 3 * sizeof(int32_t), 3 * sizeof(int32_t) },
  79. { Variant::TRANSFORM2D, 6 * sizeof(float), 6 * sizeof(float), 6 * sizeof(double), 6 * sizeof(double) },
  80. { Variant::PLANE, (vec3_elems + 1) * sizeof(float), (vec3_elems + 1) * sizeof(float), (vec3_elems + 1) * sizeof(double), (vec3_elems + 1) * sizeof(double) },
  81. { Variant::QUATERNION, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(double), 4 * sizeof(double) },
  82. { Variant::AABB, (vec3_elems * 2) * sizeof(float), (vec3_elems * 2) * sizeof(float), (vec3_elems * 2) * sizeof(double), (vec3_elems * 2) * sizeof(double) },
  83. { Variant::BASIS, (vec3_elems * 3) * sizeof(float), (vec3_elems * 3) * sizeof(float), (vec3_elems * 3) * sizeof(double), (vec3_elems * 3) * sizeof(double) },
  84. { Variant::TRANSFORM3D, (vec3_elems * 4) * sizeof(float), (vec3_elems * 4) * sizeof(float), (vec3_elems * 4) * sizeof(double), (vec3_elems * 4) * sizeof(double) },
  85. { Variant::COLOR, 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(float) },
  86. { Variant::STRING_NAME, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  87. { Variant::NODE_PATH, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  88. { Variant::RID, sizeof(uint64_t), sizeof(uint64_t), sizeof(uint64_t), sizeof(uint64_t) },
  89. { Variant::OBJECT, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  90. { Variant::CALLABLE, sizeof(Callable), sizeof(Callable), sizeof(Callable), sizeof(Callable) }, // Hardcoded align.
  91. { Variant::SIGNAL, sizeof(Signal), sizeof(Signal), sizeof(Signal), sizeof(Signal) }, // Hardcoded align.
  92. { Variant::DICTIONARY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  93. { Variant::ARRAY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  94. { Variant::PACKED_BYTE_ARRAY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  95. { Variant::PACKED_INT32_ARRAY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  96. { Variant::PACKED_INT64_ARRAY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  97. { Variant::PACKED_FLOAT32_ARRAY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  98. { Variant::PACKED_FLOAT64_ARRAY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  99. { Variant::PACKED_STRING_ARRAY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  100. { Variant::PACKED_VECTOR2_ARRAY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  101. { Variant::PACKED_VECTOR3_ARRAY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  102. { Variant::PACKED_COLOR_ARRAY, ptrsize_32, ptrsize_64, ptrsize_32, ptrsize_64 },
  103. { 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 },
  104. };
  105. Array core_type_sizes;
  106. for (int i = 0; i < 4; i++) {
  107. Dictionary d;
  108. d["build_configuration"] = build_config_name[i];
  109. Array sizes;
  110. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  111. Variant::Type t = type_size_array[j].type;
  112. String name = t == Variant::VARIANT_MAX ? String("Variant") : Variant::get_type_name(t);
  113. Dictionary d2;
  114. d2["name"] = name;
  115. uint32_t size;
  116. switch (i) {
  117. case 0:
  118. size = type_size_array[j].size_32_bits_real_float;
  119. break;
  120. case 1:
  121. size = type_size_array[j].size_64_bits_real_float;
  122. break;
  123. case 2:
  124. size = type_size_array[j].size_32_bits_real_double;
  125. break;
  126. case 3:
  127. size = type_size_array[j].size_64_bits_real_double;
  128. break;
  129. }
  130. d2["size"] = size;
  131. sizes.push_back(d2);
  132. }
  133. d["sizes"] = sizes;
  134. core_type_sizes.push_back(d);
  135. }
  136. api_dump["builtin_class_sizes"] = core_type_sizes;
  137. }
  138. {
  139. // Member offsets sizes.
  140. struct {
  141. Variant::Type type;
  142. const char *member;
  143. uint32_t offset_32_bits_real_float;
  144. uint32_t offset_64_bits_real_float;
  145. uint32_t offset_32_bits_real_double;
  146. uint32_t offset_64_bits_real_double;
  147. } member_offset_array[] = {
  148. { Variant::VECTOR2, "x", 0, 0, 0, 0 },
  149. { Variant::VECTOR2, "y", sizeof(float), sizeof(float), sizeof(double), sizeof(double) },
  150. { Variant::VECTOR2I, "x", 0, 0, 0, 0 },
  151. { Variant::VECTOR2I, "y", sizeof(int32_t), sizeof(int32_t), sizeof(int32_t), sizeof(int32_t) },
  152. { Variant::RECT2, "position", 0, 0, 0, 0 },
  153. { Variant::RECT2, "size", 2 * sizeof(Vector2), 2 * sizeof(float), 2 * sizeof(double), 2 * sizeof(double) },
  154. { Variant::RECT2I, "position", 0, 0, 0, 0 },
  155. { Variant::RECT2I, "size", 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t) },
  156. { Variant::VECTOR3, "x", 0, 0, 0, 0 },
  157. { Variant::VECTOR3, "y", sizeof(float), sizeof(float), sizeof(double), sizeof(double) },
  158. { Variant::VECTOR3, "z", 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(double), 2 * sizeof(double) },
  159. { Variant::VECTOR3I, "x", 0, 0, 0, 0 },
  160. { Variant::VECTOR3I, "y", sizeof(int32_t), sizeof(int32_t), sizeof(int32_t), sizeof(int32_t) },
  161. { Variant::VECTOR3I, "z", 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t), 2 * sizeof(int32_t) },
  162. { Variant::TRANSFORM2D, "x", 0, 0, 0, 0 },
  163. { Variant::TRANSFORM2D, "y", 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(double), 2 * sizeof(double) },
  164. { Variant::TRANSFORM2D, "origin", 4 * sizeof(float), 4 * sizeof(float), 4 * sizeof(double), 4 * sizeof(double) },
  165. { Variant::PLANE, "normal", 0, 0, 0, 0 },
  166. { Variant::PLANE, "d", vec3_elems * sizeof(float), vec3_elems * sizeof(float), vec3_elems * sizeof(double), vec3_elems * sizeof(double) },
  167. { Variant::QUATERNION, "x", 0, 0, 0, 0 },
  168. { Variant::QUATERNION, "y", sizeof(float), sizeof(float), sizeof(double), sizeof(double) },
  169. { Variant::QUATERNION, "z", 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(double), 2 * sizeof(double) },
  170. { Variant::QUATERNION, "w", 3 * sizeof(float), 3 * sizeof(float), 3 * sizeof(double), 3 * sizeof(double) },
  171. { Variant::AABB, "position", 0, 0, 0, 0 },
  172. { Variant::AABB, "size", vec3_elems * sizeof(float), vec3_elems * sizeof(float), vec3_elems * sizeof(double), vec3_elems * sizeof(double) },
  173. // Remember that basis vectors are flipped!
  174. { Variant::BASIS, "x", 0, 0, 0, 0 },
  175. { Variant::BASIS, "y", vec3_elems * sizeof(float), vec3_elems * sizeof(float), vec3_elems * sizeof(double), vec3_elems * sizeof(double) },
  176. { Variant::BASIS, "z", vec3_elems * 2 * sizeof(float), vec3_elems * 2 * sizeof(float), vec3_elems * 2 * sizeof(double), vec3_elems * 2 * sizeof(double) },
  177. { Variant::TRANSFORM3D, "basis", 0, 0, 0, 0 },
  178. { Variant::TRANSFORM3D, "origin", (vec3_elems * 3) * sizeof(float), (vec3_elems * 3) * sizeof(float), (vec3_elems * 3) * sizeof(double), (vec3_elems * 3) * sizeof(double) },
  179. { Variant::COLOR, "x", 0, 0, 0, 0 },
  180. { Variant::COLOR, "y", sizeof(float), sizeof(float), sizeof(float), sizeof(float) },
  181. { Variant::COLOR, "z", 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(float), 2 * sizeof(float) },
  182. { Variant::COLOR, "w", 3 * sizeof(float), 3 * sizeof(float), 3 * sizeof(float), 3 * sizeof(float) },
  183. { Variant::NIL, nullptr, 0, 0, 0, 0 },
  184. };
  185. Array core_type_member_offsets;
  186. for (int i = 0; i < 4; i++) {
  187. Dictionary d;
  188. d["build_configuration"] = build_config_name[i];
  189. Array type_offsets;
  190. uint32_t idx = 0;
  191. Variant::Type last_type = Variant::NIL;
  192. Dictionary d2;
  193. Array members;
  194. while (true) {
  195. Variant::Type t = member_offset_array[idx].type;
  196. if (t != last_type) {
  197. if (last_type != Variant::NIL) {
  198. d2["members"] = members;
  199. type_offsets.push_back(d2);
  200. }
  201. if (t == Variant::NIL) {
  202. break;
  203. }
  204. String name = t == Variant::VARIANT_MAX ? String("Variant") : Variant::get_type_name(t);
  205. d2 = Dictionary();
  206. members = Array();
  207. d2["name"] = name;
  208. last_type = t;
  209. }
  210. Dictionary d3;
  211. uint32_t offset;
  212. switch (i) {
  213. case 0:
  214. offset = member_offset_array[idx].offset_32_bits_real_float;
  215. break;
  216. case 1:
  217. offset = member_offset_array[idx].offset_64_bits_real_float;
  218. break;
  219. case 2:
  220. offset = member_offset_array[idx].offset_32_bits_real_double;
  221. break;
  222. case 3:
  223. offset = member_offset_array[idx].offset_64_bits_real_double;
  224. break;
  225. }
  226. d3["member"] = member_offset_array[idx].member;
  227. d3["offset"] = offset;
  228. members.push_back(d3);
  229. idx++;
  230. }
  231. d["classes"] = type_offsets;
  232. core_type_member_offsets.push_back(d);
  233. }
  234. api_dump["builtin_class_member_offsets"] = core_type_member_offsets;
  235. }
  236. {
  237. // Global enums and constants.
  238. Array constants;
  239. Map<String, List<Pair<String, int>>> enum_list;
  240. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  241. int value = CoreConstants::get_global_constant_value(i);
  242. String enum_name = CoreConstants::get_global_constant_enum(i);
  243. String name = CoreConstants::get_global_constant_name(i);
  244. if (enum_name != String()) {
  245. enum_list[enum_name].push_back(Pair<String, int>(name, value));
  246. } else {
  247. Dictionary d;
  248. d["name"] = name;
  249. d["value"] = value;
  250. constants.push_back(d);
  251. }
  252. }
  253. api_dump["global_constants"] = constants;
  254. Array enums;
  255. for (Map<String, List<Pair<String, int>>>::Element *E = enum_list.front(); E; E = E->next()) {
  256. Dictionary d1;
  257. d1["name"] = E->key();
  258. Array values;
  259. for (const Pair<String, int> &F : E->get()) {
  260. Dictionary d2;
  261. d2["name"] = F.first;
  262. d2["value"] = F.second;
  263. values.push_back(d2);
  264. }
  265. d1["values"] = values;
  266. enums.push_back(d1);
  267. }
  268. api_dump["global_enums"] = enums;
  269. }
  270. {
  271. Array utility_funcs;
  272. List<StringName> utility_func_names;
  273. Variant::get_utility_function_list(&utility_func_names);
  274. for (const StringName &name : utility_func_names) {
  275. Dictionary func;
  276. func["name"] = String(name);
  277. if (Variant::has_utility_function_return_value(name)) {
  278. Variant::Type rt = Variant::get_utility_function_return_type(name);
  279. func["return_type"] = rt == Variant::NIL ? String("Variant") : Variant::get_type_name(rt);
  280. }
  281. switch (Variant::get_utility_function_type(name)) {
  282. case Variant::UTILITY_FUNC_TYPE_MATH:
  283. func["category"] = "math";
  284. break;
  285. case Variant::UTILITY_FUNC_TYPE_RANDOM:
  286. func["category"] = "random";
  287. break;
  288. case Variant::UTILITY_FUNC_TYPE_GENERAL:
  289. func["category"] = "general";
  290. break;
  291. }
  292. bool vararg = Variant::is_utility_function_vararg(name);
  293. func["is_vararg"] = Variant::is_utility_function_vararg(name);
  294. func["hash"] = Variant::get_utility_function_hash(name);
  295. Array arguments;
  296. int argcount = Variant::get_utility_function_argument_count(name);
  297. for (int i = 0; i < argcount; i++) {
  298. Dictionary arg;
  299. String argname = vararg ? "arg" + itos(i + 1) : Variant::get_utility_function_argument_name(name, i);
  300. arg["name"] = argname;
  301. Variant::Type argtype = Variant::get_utility_function_argument_type(name, i);
  302. arg["type"] = argtype == Variant::NIL ? String("Variant") : Variant::get_type_name(argtype);
  303. //no default value support in utility functions
  304. arguments.push_back(arg);
  305. }
  306. if (arguments.size()) {
  307. func["arguments"] = arguments;
  308. }
  309. utility_funcs.push_back(func);
  310. }
  311. api_dump["utility_functions"] = utility_funcs;
  312. }
  313. {
  314. // builtin types
  315. Array builtins;
  316. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  317. if (i == Variant::OBJECT) {
  318. continue;
  319. }
  320. Variant::Type type = Variant::Type(i);
  321. Dictionary d;
  322. d["name"] = Variant::get_type_name(type);
  323. if (Variant::has_indexing(type)) {
  324. Variant::Type index_type = Variant::get_indexed_element_type(type);
  325. d["indexing_return_type"] = index_type == Variant::NIL ? String("Variant") : Variant::get_type_name(index_type);
  326. }
  327. {
  328. //members
  329. Array members;
  330. List<StringName> member_names;
  331. Variant::get_member_list(type, &member_names);
  332. for (const StringName &member_name : member_names) {
  333. Dictionary d2;
  334. d2["name"] = String(member_name);
  335. d2["type"] = Variant::get_type_name(Variant::get_member_type(type, member_name));
  336. members.push_back(d2);
  337. }
  338. if (members.size()) {
  339. d["members"] = members;
  340. }
  341. }
  342. {
  343. //constants
  344. Array constants;
  345. List<StringName> constant_names;
  346. Variant::get_constants_for_type(type, &constant_names);
  347. for (const StringName &constant_name : constant_names) {
  348. Dictionary d2;
  349. d2["name"] = String(constant_name);
  350. Variant constant = Variant::get_constant_value(type, constant_name);
  351. d2["type"] = Variant::get_type_name(constant.get_type());
  352. d2["value"] = constant.get_construct_string();
  353. constants.push_back(d2);
  354. }
  355. if (constants.size()) {
  356. d["constants"] = constants;
  357. }
  358. }
  359. {
  360. //operators
  361. Array operators;
  362. for (int j = 0; j < Variant::VARIANT_MAX; j++) {
  363. for (int k = 0; k < Variant::OP_MAX; k++) {
  364. Variant::Type rt = Variant::get_operator_return_type(Variant::Operator(k), type, Variant::Type(j));
  365. if (rt != Variant::NIL) {
  366. Dictionary d2;
  367. d2["name"] = Variant::get_operator_name(Variant::Operator(k));
  368. if (k != Variant::OP_NEGATE && k != Variant::OP_POSITIVE && k != Variant::OP_NOT && k != Variant::OP_BIT_NEGATE) {
  369. d2["right_type"] = Variant::get_type_name(Variant::Type(j));
  370. }
  371. operators.push_back(d2);
  372. }
  373. }
  374. }
  375. if (operators.size()) {
  376. d["operators"] = operators;
  377. }
  378. }
  379. {
  380. //methods
  381. Array methods;
  382. List<StringName> method_names;
  383. Variant::get_builtin_method_list(type, &method_names);
  384. for (const StringName &method_name : method_names) {
  385. Dictionary d2;
  386. d2["name"] = String(method_name);
  387. if (Variant::has_builtin_method_return_value(type, method_name)) {
  388. Variant::Type ret_type = Variant::get_builtin_method_return_type(type, method_name);
  389. d2["return_type"] = ret_type == Variant::NIL ? String("Variant") : Variant::get_type_name(ret_type);
  390. }
  391. d2["is_vararg"] = Variant::is_builtin_method_vararg(type, method_name);
  392. d2["is_const"] = Variant::is_builtin_method_const(type, method_name);
  393. d2["is_static"] = Variant::is_builtin_method_static(type, method_name);
  394. d2["hash"] = Variant::get_builtin_method_hash(type, method_name);
  395. Vector<Variant> default_args = Variant::get_builtin_method_default_arguments(type, method_name);
  396. Array arguments;
  397. int argcount = Variant::get_builtin_method_argument_count(type, method_name);
  398. for (int j = 0; j < argcount; j++) {
  399. Dictionary d3;
  400. d3["name"] = Variant::get_builtin_method_argument_name(type, method_name, j);
  401. Variant::Type argtype = Variant::get_builtin_method_argument_type(type, method_name, j);
  402. d3["type"] = argtype == Variant::NIL ? String("Variant") : Variant::get_type_name(argtype);
  403. if (j >= (argcount - default_args.size())) {
  404. int dargidx = j - (argcount - default_args.size());
  405. d3["default_value"] = default_args[dargidx].get_construct_string();
  406. }
  407. arguments.push_back(d3);
  408. }
  409. if (arguments.size()) {
  410. d2["arguments"] = arguments;
  411. }
  412. methods.push_back(d2);
  413. }
  414. if (methods.size()) {
  415. d["methods"] = methods;
  416. }
  417. }
  418. {
  419. //constructors
  420. Array constructors;
  421. for (int j = 0; j < Variant::get_constructor_count(type); j++) {
  422. Dictionary d2;
  423. d2["index"] = j;
  424. Array arguments;
  425. int argcount = Variant::get_constructor_argument_count(type, j);
  426. for (int k = 0; k < argcount; k++) {
  427. Dictionary d3;
  428. d3["name"] = Variant::get_constructor_argument_name(type, j, k);
  429. d3["type"] = Variant::get_type_name(Variant::get_constructor_argument_type(type, j, k));
  430. arguments.push_back(d3);
  431. }
  432. if (arguments.size()) {
  433. d2["arguments"] = arguments;
  434. }
  435. constructors.push_back(d2);
  436. }
  437. if (constructors.size()) {
  438. d["constructors"] = constructors;
  439. }
  440. }
  441. builtins.push_back(d);
  442. }
  443. api_dump["builtin_classes"] = builtins;
  444. }
  445. {
  446. // classes
  447. Array classes;
  448. List<StringName> class_list;
  449. ClassDB::get_class_list(&class_list);
  450. class_list.sort_custom<StringName::AlphCompare>();
  451. for (const StringName &class_name : class_list) {
  452. Dictionary d;
  453. d["name"] = String(class_name);
  454. d["is_refcounted"] = ClassDB::is_parent_class(class_name, "RefCounted");
  455. d["is_instantiable"] = ClassDB::can_instantiate(class_name);
  456. StringName parent_class = ClassDB::get_parent_class(class_name);
  457. if (parent_class != StringName()) {
  458. d["inherits"] = String(parent_class);
  459. }
  460. {
  461. ClassDB::APIType api = ClassDB::get_api_type(class_name);
  462. static const char *api_type[5] = { "core", "editor", "extension", "editor_extension" };
  463. d["api_type"] = api_type[api];
  464. }
  465. {
  466. //constants
  467. Array constants;
  468. List<String> constant_list;
  469. ClassDB::get_integer_constant_list(class_name, &constant_list, true);
  470. for (const String &F : constant_list) {
  471. StringName enum_name = ClassDB::get_integer_constant_enum(class_name, F);
  472. if (enum_name != StringName()) {
  473. continue; //enums will be handled on their own
  474. }
  475. Dictionary d2;
  476. d2["name"] = String(F);
  477. d2["value"] = ClassDB::get_integer_constant(class_name, F);
  478. constants.push_back(d2);
  479. }
  480. if (constants.size()) {
  481. d["constants"] = constants;
  482. }
  483. }
  484. {
  485. //enum
  486. Array enums;
  487. List<StringName> enum_list;
  488. ClassDB::get_enum_list(class_name, &enum_list, true);
  489. for (const StringName &F : enum_list) {
  490. Dictionary d2;
  491. d2["name"] = String(F);
  492. Array values;
  493. List<StringName> enum_constant_list;
  494. ClassDB::get_enum_constants(class_name, F, &enum_constant_list, true);
  495. for (List<StringName>::Element *G = enum_constant_list.front(); G; G = G->next()) {
  496. Dictionary d3;
  497. d3["name"] = String(G->get());
  498. d3["value"] = ClassDB::get_integer_constant(class_name, G->get());
  499. values.push_back(d3);
  500. }
  501. d2["values"] = values;
  502. enums.push_back(d2);
  503. }
  504. if (enums.size()) {
  505. d["enums"] = enums;
  506. }
  507. }
  508. {
  509. //methods
  510. Array methods;
  511. List<MethodInfo> method_list;
  512. ClassDB::get_method_list(class_name, &method_list, true);
  513. for (const MethodInfo &F : method_list) {
  514. StringName method_name = F.name;
  515. if (F.flags & METHOD_FLAG_VIRTUAL) {
  516. //virtual method
  517. const MethodInfo &mi = F;
  518. Dictionary d2;
  519. d2["name"] = String(method_name);
  520. d2["is_const"] = (F.flags & METHOD_FLAG_CONST) ? true : false;
  521. d2["is_vararg"] = false;
  522. d2["is_virtual"] = true;
  523. // virtual functions have no hash since no MethodBind is involved
  524. bool has_return = mi.return_val.type != Variant::NIL || (mi.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT);
  525. Array arguments;
  526. for (int i = (has_return ? -1 : 0); i < mi.arguments.size(); i++) {
  527. PropertyInfo pinfo = i == -1 ? mi.return_val : mi.arguments[i];
  528. Dictionary d3;
  529. if (i >= 0) {
  530. d3["name"] = pinfo.name;
  531. }
  532. if (pinfo.class_name != StringName()) {
  533. d3["type"] = String(pinfo.class_name);
  534. } else {
  535. Variant::Type type = pinfo.type;
  536. if (type == Variant::NIL) {
  537. d3["type"] = "Variant";
  538. } else {
  539. d3["type"] = Variant::get_type_name(type);
  540. }
  541. }
  542. if (i == -1) {
  543. d2["return_value"] = d3;
  544. } else {
  545. arguments.push_back(d3);
  546. }
  547. }
  548. if (arguments.size()) {
  549. d2["arguments"] = arguments;
  550. }
  551. methods.push_back(d2);
  552. } else if (F.name.begins_with("_")) {
  553. //hidden method, ignore
  554. } else {
  555. Dictionary d2;
  556. d2["name"] = String(method_name);
  557. MethodBind *method = ClassDB::get_method(class_name, method_name);
  558. if (!method) {
  559. continue;
  560. }
  561. d2["is_const"] = method->is_const();
  562. d2["is_vararg"] = method->is_vararg();
  563. d2["is_virtual"] = false;
  564. d2["hash"] = method->get_hash();
  565. Vector<Variant> default_args = method->get_default_arguments();
  566. Array arguments;
  567. for (int i = (method->has_return() ? -1 : 0); i < method->get_argument_count(); i++) {
  568. PropertyInfo pinfo = i == -1 ? method->get_return_info() : method->get_argument_info(i);
  569. Dictionary d3;
  570. if (i >= 0) {
  571. d3["name"] = pinfo.name;
  572. }
  573. if (pinfo.class_name != StringName()) {
  574. d3["type"] = String(pinfo.class_name);
  575. } else {
  576. Variant::Type type = pinfo.type;
  577. if (type == Variant::NIL) {
  578. d3["type"] = "Variant";
  579. } else {
  580. d3["type"] = Variant::get_type_name(type);
  581. }
  582. }
  583. if (method->get_argument_meta(i) > 0) {
  584. static const char *argmeta[11] = { "none", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64", "float", "double" };
  585. d3["meta"] = argmeta[method->get_argument_meta(i)];
  586. }
  587. if (i >= 0 && i >= (method->get_argument_count() - default_args.size())) {
  588. int dargidx = i - (method->get_argument_count() - default_args.size());
  589. d3["default_value"] = default_args[dargidx].get_construct_string();
  590. }
  591. if (i == -1) {
  592. d2["return_value"] = d3;
  593. } else {
  594. arguments.push_back(d3);
  595. }
  596. }
  597. if (arguments.size()) {
  598. d2["arguments"] = arguments;
  599. }
  600. methods.push_back(d2);
  601. }
  602. }
  603. if (methods.size()) {
  604. d["methods"] = methods;
  605. }
  606. }
  607. {
  608. //signals
  609. Array signals;
  610. List<MethodInfo> signal_list;
  611. ClassDB::get_signal_list(class_name, &signal_list, true);
  612. for (const MethodInfo &F : signal_list) {
  613. StringName signal_name = F.name;
  614. Dictionary d2;
  615. d2["name"] = String(signal_name);
  616. Array arguments;
  617. for (int i = 0; i < F.arguments.size(); i++) {
  618. Dictionary d3;
  619. d3["name"] = F.arguments[i].name;
  620. Variant::Type type = F.arguments[i].type;
  621. if (F.arguments[i].class_name != StringName()) {
  622. d3["type"] = String(F.arguments[i].class_name);
  623. } else if (type == Variant::NIL) {
  624. d3["type"] = "Variant";
  625. } else {
  626. d3["type"] = Variant::get_type_name(type);
  627. }
  628. arguments.push_back(d3);
  629. }
  630. if (arguments.size()) {
  631. d2["arguments"] = arguments;
  632. }
  633. signals.push_back(d2);
  634. }
  635. if (signals.size()) {
  636. d["signals"] = signals;
  637. }
  638. }
  639. {
  640. //properties
  641. Array properties;
  642. List<PropertyInfo> property_list;
  643. ClassDB::get_property_list(class_name, &property_list, true);
  644. for (const PropertyInfo &F : property_list) {
  645. if (F.usage & PROPERTY_USAGE_CATEGORY || F.usage & PROPERTY_USAGE_GROUP || F.usage & PROPERTY_USAGE_SUBGROUP) {
  646. continue; //not real properties
  647. }
  648. if (F.name.begins_with("_")) {
  649. continue; //hidden property
  650. }
  651. StringName property_name = F.name;
  652. Dictionary d2;
  653. d2["name"] = String(property_name);
  654. if (F.class_name != StringName()) {
  655. d2["type"] = String(F.class_name);
  656. } else if (F.type == Variant::NIL && F.usage & PROPERTY_USAGE_NIL_IS_VARIANT) {
  657. d2["type"] = "Variant";
  658. } else {
  659. d2["type"] = Variant::get_type_name(F.type);
  660. }
  661. d2["setter"] = ClassDB::get_property_setter(class_name, F.name);
  662. d2["getter"] = ClassDB::get_property_getter(class_name, F.name);
  663. d2["index"] = ClassDB::get_property_index(class_name, F.name);
  664. properties.push_back(d2);
  665. }
  666. if (properties.size()) {
  667. d["properties"] = properties;
  668. }
  669. }
  670. classes.push_back(d);
  671. }
  672. api_dump["classes"] = classes;
  673. }
  674. {
  675. // singletons
  676. Array singletons;
  677. List<Engine::Singleton> singleton_list;
  678. Engine::get_singleton()->get_singletons(&singleton_list);
  679. for (const Engine::Singleton &s : singleton_list) {
  680. Dictionary d;
  681. d["name"] = s.name;
  682. if (s.class_name != StringName()) {
  683. d["type"] = String(s.class_name);
  684. } else {
  685. d["type"] = String(s.ptr->get_class());
  686. }
  687. singletons.push_back(d);
  688. }
  689. if (singletons.size()) {
  690. api_dump["singletons"] = singletons;
  691. }
  692. }
  693. return api_dump;
  694. }
  695. void NativeExtensionAPIDump::generate_extension_json_file(const String &p_path) {
  696. Dictionary api = generate_extension_api();
  697. Ref<JSON> json;
  698. json.instantiate();
  699. String text = json->stringify(api, "\t", false);
  700. FileAccessRef fa = FileAccess::open(p_path, FileAccess::WRITE);
  701. CharString cs = text.ascii();
  702. fa->store_buffer((const uint8_t *)cs.ptr(), cs.length());
  703. fa->close();
  704. }
  705. #endif