api_generator.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /**************************************************************************/
  2. /* api_generator.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "api_generator.h"
  31. #ifdef TOOLS_ENABLED
  32. #include "core/class_db.h"
  33. #include "core/engine.h"
  34. #include "core/global_constants.h"
  35. #include "core/os/file_access.h"
  36. #include "core/pair.h"
  37. // helper stuff
  38. static Error save_file(const String &p_path, const List<String> &p_content) {
  39. FileAccessRef file = FileAccess::open(p_path, FileAccess::WRITE);
  40. ERR_FAIL_COND_V(!file, ERR_FILE_CANT_WRITE);
  41. for (const List<String>::Element *e = p_content.front(); e != nullptr; e = e->next()) {
  42. file->store_string(e->get());
  43. }
  44. file->close();
  45. return OK;
  46. }
  47. // helper stuff end
  48. struct MethodAPI {
  49. String method_name;
  50. String return_type;
  51. List<String> argument_types;
  52. List<String> argument_names;
  53. Map<int, Variant> default_arguments;
  54. int argument_count;
  55. bool has_varargs;
  56. bool is_editor;
  57. bool is_noscript;
  58. bool is_const;
  59. bool is_reverse;
  60. bool is_virtual;
  61. bool is_from_script;
  62. };
  63. struct PropertyAPI {
  64. String name;
  65. String getter;
  66. String setter;
  67. String type;
  68. int index;
  69. };
  70. struct ConstantAPI {
  71. String constant_name;
  72. int constant_value;
  73. };
  74. struct SignalAPI {
  75. String name;
  76. List<String> argument_types;
  77. List<String> argument_names;
  78. Map<int, Variant> default_arguments;
  79. };
  80. struct EnumAPI {
  81. String name;
  82. List<Pair<int, String>> values;
  83. };
  84. struct ClassAPI {
  85. String class_name;
  86. String super_class_name;
  87. ClassDB::APIType api_type;
  88. bool is_singleton;
  89. String singleton_name;
  90. bool is_instanciable;
  91. // @Unclear
  92. bool is_reference;
  93. List<MethodAPI> methods;
  94. List<PropertyAPI> properties;
  95. List<ConstantAPI> constants;
  96. List<SignalAPI> signals_;
  97. List<EnumAPI> enums;
  98. };
  99. static String get_type_name(const PropertyInfo &info) {
  100. if (info.type == Variant::INT && (info.usage & PROPERTY_USAGE_CLASS_IS_ENUM)) {
  101. return String("enum.") + String(info.class_name).replace(".", "::");
  102. }
  103. if (info.class_name != StringName()) {
  104. return info.class_name;
  105. }
  106. if (info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  107. return info.hint_string;
  108. }
  109. if (info.type == Variant::NIL && (info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  110. return "Variant";
  111. }
  112. if (info.type == Variant::NIL) {
  113. return "void";
  114. }
  115. return Variant::get_type_name(info.type);
  116. }
  117. /*
  118. * Some comparison helper functions we need
  119. */
  120. struct MethodInfoComparator {
  121. StringName::AlphCompare compare;
  122. bool operator()(const MethodInfo &p_a, const MethodInfo &p_b) const {
  123. return compare(p_a.name, p_b.name);
  124. }
  125. };
  126. struct PropertyInfoComparator {
  127. StringName::AlphCompare compare;
  128. bool operator()(const PropertyInfo &p_a, const PropertyInfo &p_b) const {
  129. return compare(p_a.name, p_b.name);
  130. }
  131. };
  132. struct ConstantAPIComparator {
  133. NoCaseComparator compare;
  134. bool operator()(const ConstantAPI &p_a, const ConstantAPI &p_b) const {
  135. return compare(p_a.constant_name, p_b.constant_name);
  136. }
  137. };
  138. /*
  139. * Reads the entire Godot API to a list
  140. */
  141. List<ClassAPI> generate_c_api_classes() {
  142. List<ClassAPI> api;
  143. List<StringName> classes;
  144. ClassDB::get_class_list(&classes);
  145. classes.sort_custom<StringName::AlphCompare>();
  146. // Register global constants as a fake GlobalConstants singleton class
  147. {
  148. ClassAPI global_constants_api;
  149. global_constants_api.class_name = L"GlobalConstants";
  150. global_constants_api.api_type = ClassDB::API_CORE;
  151. global_constants_api.is_singleton = true;
  152. global_constants_api.singleton_name = L"GlobalConstants";
  153. global_constants_api.is_instanciable = false;
  154. const int constants_count = GlobalConstants::get_global_constant_count();
  155. for (int i = 0; i < constants_count; ++i) {
  156. ConstantAPI constant_api;
  157. constant_api.constant_name = GlobalConstants::get_global_constant_name(i);
  158. constant_api.constant_value = GlobalConstants::get_global_constant_value(i);
  159. global_constants_api.constants.push_back(constant_api);
  160. }
  161. global_constants_api.constants.sort_custom<ConstantAPIComparator>();
  162. api.push_back(global_constants_api);
  163. }
  164. for (List<StringName>::Element *e = classes.front(); e != nullptr; e = e->next()) {
  165. StringName class_name = e->get();
  166. ClassAPI class_api;
  167. class_api.api_type = ClassDB::get_api_type(e->get());
  168. class_api.class_name = class_name;
  169. class_api.super_class_name = ClassDB::get_parent_class(class_name);
  170. {
  171. String name = class_name;
  172. if (name.begins_with("_")) {
  173. name.remove(0);
  174. }
  175. class_api.is_singleton = Engine::get_singleton()->has_singleton(name);
  176. if (class_api.is_singleton) {
  177. class_api.singleton_name = name;
  178. }
  179. }
  180. class_api.is_instanciable = !class_api.is_singleton && ClassDB::can_instance(class_name);
  181. {
  182. List<StringName> inheriters;
  183. ClassDB::get_inheriters_from_class("Reference", &inheriters);
  184. bool is_reference = !!inheriters.find(class_name) || class_name == "Reference";
  185. // @Unclear
  186. class_api.is_reference = !class_api.is_singleton && is_reference;
  187. }
  188. // constants
  189. {
  190. List<String> constant;
  191. ClassDB::get_integer_constant_list(class_name, &constant, true);
  192. constant.sort_custom<NoCaseComparator>();
  193. for (List<String>::Element *c = constant.front(); c != nullptr; c = c->next()) {
  194. ConstantAPI constant_api;
  195. constant_api.constant_name = c->get();
  196. constant_api.constant_value = ClassDB::get_integer_constant(class_name, c->get());
  197. class_api.constants.push_back(constant_api);
  198. }
  199. }
  200. // signals
  201. {
  202. List<MethodInfo> signals_;
  203. ClassDB::get_signal_list(class_name, &signals_, true);
  204. signals_.sort_custom<MethodInfoComparator>();
  205. for (int i = 0; i < signals_.size(); i++) {
  206. SignalAPI signal;
  207. MethodInfo method_info = signals_[i];
  208. signal.name = method_info.name;
  209. for (int j = 0; j < method_info.arguments.size(); j++) {
  210. PropertyInfo argument = method_info.arguments[j];
  211. String type;
  212. String name = argument.name;
  213. if (argument.name.find(":") != -1) {
  214. type = argument.name.get_slice(":", 1);
  215. name = argument.name.get_slice(":", 0);
  216. } else {
  217. type = get_type_name(argument);
  218. }
  219. signal.argument_names.push_back(name);
  220. signal.argument_types.push_back(type);
  221. }
  222. Vector<Variant> default_arguments = method_info.default_arguments;
  223. int default_start = signal.argument_names.size() - default_arguments.size();
  224. for (int j = 0; j < default_arguments.size(); j++) {
  225. signal.default_arguments[default_start + j] = default_arguments[j];
  226. }
  227. class_api.signals_.push_back(signal);
  228. }
  229. }
  230. //properties
  231. {
  232. List<PropertyInfo> properties;
  233. ClassDB::get_property_list(class_name, &properties, true);
  234. properties.sort_custom<PropertyInfoComparator>();
  235. for (List<PropertyInfo>::Element *p = properties.front(); p != nullptr; p = p->next()) {
  236. PropertyAPI property_api;
  237. property_api.name = p->get().name;
  238. property_api.getter = ClassDB::get_property_getter(class_name, p->get().name);
  239. property_api.setter = ClassDB::get_property_setter(class_name, p->get().name);
  240. if (p->get().name.find(":") != -1) {
  241. property_api.type = p->get().name.get_slice(":", 1);
  242. property_api.name = p->get().name.get_slice(":", 0);
  243. } else {
  244. property_api.type = get_type_name(p->get());
  245. }
  246. property_api.index = ClassDB::get_property_index(class_name, p->get().name);
  247. if (!property_api.setter.empty() || !property_api.getter.empty()) {
  248. class_api.properties.push_back(property_api);
  249. }
  250. }
  251. }
  252. //methods
  253. {
  254. List<MethodInfo> methods;
  255. ClassDB::get_method_list(class_name, &methods, true);
  256. methods.sort_custom<MethodInfoComparator>();
  257. for (List<MethodInfo>::Element *m = methods.front(); m != nullptr; m = m->next()) {
  258. MethodAPI method_api;
  259. MethodBind *method_bind = ClassDB::get_method(class_name, m->get().name);
  260. MethodInfo &method_info = m->get();
  261. //method name
  262. method_api.method_name = method_info.name;
  263. //method return type
  264. if (method_api.method_name.find(":") != -1) {
  265. method_api.return_type = method_api.method_name.get_slice(":", 1);
  266. method_api.method_name = method_api.method_name.get_slice(":", 0);
  267. } else {
  268. method_api.return_type = get_type_name(m->get().return_val);
  269. }
  270. method_api.argument_count = method_info.arguments.size();
  271. method_api.has_varargs = method_bind && method_bind->is_vararg();
  272. // Method flags
  273. method_api.is_virtual = false;
  274. if (method_info.flags) {
  275. const uint32_t flags = method_info.flags;
  276. method_api.is_editor = flags & METHOD_FLAG_EDITOR;
  277. method_api.is_noscript = flags & METHOD_FLAG_NOSCRIPT;
  278. method_api.is_const = flags & METHOD_FLAG_CONST;
  279. method_api.is_reverse = flags & METHOD_FLAG_REVERSE;
  280. method_api.is_virtual = flags & METHOD_FLAG_VIRTUAL;
  281. method_api.is_from_script = flags & METHOD_FLAG_FROM_SCRIPT;
  282. }
  283. method_api.is_virtual = method_api.is_virtual || method_api.method_name[0] == '_';
  284. // method argument name and type
  285. for (int i = 0; i < method_api.argument_count; i++) {
  286. String arg_name;
  287. String arg_type;
  288. PropertyInfo arg_info = method_info.arguments[i];
  289. arg_name = arg_info.name;
  290. if (arg_info.name.find(":") != -1) {
  291. arg_type = arg_info.name.get_slice(":", 1);
  292. arg_name = arg_info.name.get_slice(":", 0);
  293. } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  294. arg_type = arg_info.hint_string;
  295. } else if (arg_info.type == Variant::NIL) {
  296. arg_type = "Variant";
  297. } else if (arg_info.type == Variant::OBJECT) {
  298. arg_type = arg_info.class_name;
  299. if (arg_type == "") {
  300. arg_type = Variant::get_type_name(arg_info.type);
  301. }
  302. } else {
  303. arg_type = Variant::get_type_name(arg_info.type);
  304. }
  305. method_api.argument_names.push_back(arg_name);
  306. method_api.argument_types.push_back(arg_type);
  307. if (method_bind && method_bind->has_default_argument(i)) {
  308. method_api.default_arguments[i] = method_bind->get_default_argument(i);
  309. }
  310. }
  311. class_api.methods.push_back(method_api);
  312. }
  313. }
  314. // enums
  315. {
  316. List<EnumAPI> enums;
  317. List<StringName> enum_names;
  318. ClassDB::get_enum_list(class_name, &enum_names, true);
  319. for (List<StringName>::Element *E = enum_names.front(); E; E = E->next()) {
  320. List<StringName> value_names;
  321. EnumAPI enum_api;
  322. enum_api.name = E->get();
  323. ClassDB::get_enum_constants(class_name, E->get(), &value_names, true);
  324. for (List<StringName>::Element *val_e = value_names.front(); val_e; val_e = val_e->next()) {
  325. int int_val = ClassDB::get_integer_constant(class_name, val_e->get(), nullptr);
  326. enum_api.values.push_back(Pair<int, String>(int_val, val_e->get()));
  327. }
  328. enum_api.values.sort_custom<PairSort<int, String>>();
  329. class_api.enums.push_back(enum_api);
  330. }
  331. }
  332. api.push_back(class_api);
  333. }
  334. return api;
  335. }
  336. /*
  337. * Generates the JSON source from the API in p_api
  338. */
  339. static List<String> generate_c_api_json(const List<ClassAPI> &p_api) {
  340. // I'm sorry for the \t mess
  341. List<String> source;
  342. source.push_back("[\n");
  343. for (const List<ClassAPI>::Element *c = p_api.front(); c != nullptr; c = c->next()) {
  344. ClassAPI api = c->get();
  345. source.push_back("\t{\n");
  346. source.push_back("\t\t\"name\": \"" + api.class_name + "\",\n");
  347. source.push_back("\t\t\"base_class\": \"" + api.super_class_name + "\",\n");
  348. source.push_back(String("\t\t\"api_type\": \"") + (api.api_type == ClassDB::API_CORE ? "core" : (api.api_type == ClassDB::API_EDITOR ? "tools" : "none")) + "\",\n");
  349. source.push_back(String("\t\t\"singleton\": ") + (api.is_singleton ? "true" : "false") + ",\n");
  350. source.push_back("\t\t\"singleton_name\": \"" + api.singleton_name + "\",\n");
  351. source.push_back(String("\t\t\"instanciable\": ") + (api.is_instanciable ? "true" : "false") + ",\n");
  352. source.push_back(String("\t\t\"is_reference\": ") + (api.is_reference ? "true" : "false") + ",\n");
  353. // @Unclear
  354. source.push_back("\t\t\"constants\": {\n");
  355. for (List<ConstantAPI>::Element *e = api.constants.front(); e; e = e->next()) {
  356. source.push_back("\t\t\t\"" + e->get().constant_name + "\": " + String::num_int64(e->get().constant_value) + (e->next() ? "," : "") + "\n");
  357. }
  358. source.push_back("\t\t},\n");
  359. source.push_back("\t\t\"properties\": [\n");
  360. for (List<PropertyAPI>::Element *e = api.properties.front(); e; e = e->next()) {
  361. source.push_back("\t\t\t{\n");
  362. source.push_back("\t\t\t\t\"name\": \"" + e->get().name + "\",\n");
  363. source.push_back("\t\t\t\t\"type\": \"" + e->get().type + "\",\n");
  364. source.push_back("\t\t\t\t\"getter\": \"" + e->get().getter + "\",\n");
  365. source.push_back("\t\t\t\t\"setter\": \"" + e->get().setter + "\",\n");
  366. source.push_back(String("\t\t\t\t\"index\": ") + itos(e->get().index) + "\n");
  367. source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n");
  368. }
  369. source.push_back("\t\t],\n");
  370. source.push_back("\t\t\"signals\": [\n");
  371. for (List<SignalAPI>::Element *e = api.signals_.front(); e; e = e->next()) {
  372. source.push_back("\t\t\t{\n");
  373. source.push_back("\t\t\t\t\"name\": \"" + e->get().name + "\",\n");
  374. source.push_back("\t\t\t\t\"arguments\": [\n");
  375. for (int i = 0; i < e->get().argument_names.size(); i++) {
  376. source.push_back("\t\t\t\t\t{\n");
  377. source.push_back("\t\t\t\t\t\t\"name\": \"" + e->get().argument_names[i] + "\",\n");
  378. source.push_back("\t\t\t\t\t\t\"type\": \"" + e->get().argument_types[i] + "\",\n");
  379. source.push_back(String("\t\t\t\t\t\t\"has_default_value\": ") + (e->get().default_arguments.has(i) ? "true" : "false") + ",\n");
  380. source.push_back("\t\t\t\t\t\t\"default_value\": \"" + (e->get().default_arguments.has(i) ? (String)e->get().default_arguments[i] : "") + "\"\n");
  381. source.push_back(String("\t\t\t\t\t}") + ((i < e->get().argument_names.size() - 1) ? "," : "") + "\n");
  382. }
  383. source.push_back("\t\t\t\t]\n");
  384. source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n");
  385. }
  386. source.push_back("\t\t],\n");
  387. source.push_back("\t\t\"methods\": [\n");
  388. for (List<MethodAPI>::Element *e = api.methods.front(); e; e = e->next()) {
  389. source.push_back("\t\t\t{\n");
  390. source.push_back("\t\t\t\t\"name\": \"" + e->get().method_name + "\",\n");
  391. source.push_back("\t\t\t\t\"return_type\": \"" + e->get().return_type + "\",\n");
  392. source.push_back(String("\t\t\t\t\"is_editor\": ") + (e->get().is_editor ? "true" : "false") + ",\n");
  393. source.push_back(String("\t\t\t\t\"is_noscript\": ") + (e->get().is_noscript ? "true" : "false") + ",\n");
  394. source.push_back(String("\t\t\t\t\"is_const\": ") + (e->get().is_const ? "true" : "false") + ",\n");
  395. source.push_back(String("\t\t\t\t\"is_reverse\": ") + (e->get().is_reverse ? "true" : "false") + ",\n");
  396. source.push_back(String("\t\t\t\t\"is_virtual\": ") + (e->get().is_virtual ? "true" : "false") + ",\n");
  397. source.push_back(String("\t\t\t\t\"has_varargs\": ") + (e->get().has_varargs ? "true" : "false") + ",\n");
  398. source.push_back(String("\t\t\t\t\"is_from_script\": ") + (e->get().is_from_script ? "true" : "false") + ",\n");
  399. source.push_back("\t\t\t\t\"arguments\": [\n");
  400. for (int i = 0; i < e->get().argument_names.size(); i++) {
  401. source.push_back("\t\t\t\t\t{\n");
  402. source.push_back("\t\t\t\t\t\t\"name\": \"" + e->get().argument_names[i] + "\",\n");
  403. source.push_back("\t\t\t\t\t\t\"type\": \"" + e->get().argument_types[i] + "\",\n");
  404. source.push_back(String("\t\t\t\t\t\t\"has_default_value\": ") + (e->get().default_arguments.has(i) ? "true" : "false") + ",\n");
  405. source.push_back("\t\t\t\t\t\t\"default_value\": \"" + (e->get().default_arguments.has(i) ? (String)e->get().default_arguments[i] : "") + "\"\n");
  406. source.push_back(String("\t\t\t\t\t}") + ((i < e->get().argument_names.size() - 1) ? "," : "") + "\n");
  407. }
  408. source.push_back("\t\t\t\t]\n");
  409. source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n");
  410. }
  411. source.push_back("\t\t],\n");
  412. source.push_back("\t\t\"enums\": [\n");
  413. for (List<EnumAPI>::Element *e = api.enums.front(); e; e = e->next()) {
  414. source.push_back("\t\t\t{\n");
  415. source.push_back("\t\t\t\t\"name\": \"" + e->get().name + "\",\n");
  416. source.push_back("\t\t\t\t\"values\": {\n");
  417. for (List<Pair<int, String>>::Element *val_e = e->get().values.front(); val_e; val_e = val_e->next()) {
  418. source.push_back("\t\t\t\t\t\"" + val_e->get().second + "\": " + itos(val_e->get().first));
  419. source.push_back(String((val_e->next() ? "," : "")) + "\n");
  420. }
  421. source.push_back("\t\t\t\t}\n");
  422. source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n");
  423. }
  424. source.push_back("\t\t]\n");
  425. source.push_back(String("\t}") + (c->next() ? "," : "") + "\n");
  426. }
  427. source.push_back("]");
  428. return source;
  429. }
  430. #endif
  431. /*
  432. * Saves the whole Godot API to a JSON file located at
  433. * p_path
  434. */
  435. Error generate_c_api(const String &p_path) {
  436. #ifndef TOOLS_ENABLED
  437. return ERR_BUG;
  438. #else
  439. List<ClassAPI> api = generate_c_api_classes();
  440. List<String> json_source = generate_c_api_json(api);
  441. return save_file(p_path, json_source);
  442. #endif
  443. }