api_generator.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*************************************************************************/
  2. /* api_generator.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "api_generator.h"
  31. #ifdef TOOLS_ENABLED
  32. #include "class_db.h"
  33. #include "core/global_constants.h"
  34. #include "core/pair.h"
  35. #include "core/project_settings.h"
  36. #include "os/file_access.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 != NULL; 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. };
  69. struct ConstantAPI {
  70. String constant_name;
  71. int constant_value;
  72. };
  73. struct SignalAPI {
  74. String name;
  75. List<String> argument_types;
  76. List<String> argument_names;
  77. Map<int, Variant> default_arguments;
  78. };
  79. struct EnumAPI {
  80. String name;
  81. List<Pair<int, String> > values;
  82. };
  83. struct ClassAPI {
  84. String class_name;
  85. String super_class_name;
  86. ClassDB::APIType api_type;
  87. bool is_singleton;
  88. bool is_instanciable;
  89. // @Unclear
  90. bool is_creatable;
  91. bool is_reference;
  92. List<MethodAPI> methods;
  93. List<PropertyAPI> properties;
  94. List<ConstantAPI> constants;
  95. List<SignalAPI> signals_;
  96. List<EnumAPI> enums;
  97. };
  98. static String get_type_name(const PropertyInfo &info) {
  99. if (info.type == Variant::INT && (info.usage & PROPERTY_USAGE_CLASS_IS_ENUM)) {
  100. return String("enum.") + String(info.class_name).replace(".", "::");
  101. }
  102. if (info.class_name != StringName()) {
  103. return info.class_name;
  104. }
  105. if (info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  106. return info.hint_string;
  107. }
  108. if (info.type == Variant::NIL && (info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  109. return "Variant";
  110. }
  111. if (info.type == Variant::NIL) {
  112. return "void";
  113. }
  114. return Variant::get_type_name(info.type);
  115. }
  116. /*
  117. * Reads the entire Godot API to a list
  118. */
  119. List<ClassAPI> generate_c_api_classes() {
  120. List<ClassAPI> api;
  121. List<StringName> classes;
  122. ClassDB::get_class_list(&classes);
  123. // Register global constants as a fake GlobalConstants singleton class
  124. {
  125. ClassAPI global_constants_api;
  126. global_constants_api.class_name = L"GlobalConstants";
  127. global_constants_api.api_type = ClassDB::API_CORE;
  128. global_constants_api.is_singleton = true;
  129. global_constants_api.is_instanciable = false;
  130. const int constants_count = GlobalConstants::get_global_constant_count();
  131. for (int i = 0; i < constants_count; ++i) {
  132. ConstantAPI constant_api;
  133. constant_api.constant_name = GlobalConstants::get_global_constant_name(i);
  134. constant_api.constant_value = GlobalConstants::get_global_constant_value(i);
  135. global_constants_api.constants.push_back(constant_api);
  136. }
  137. api.push_back(global_constants_api);
  138. }
  139. for (List<StringName>::Element *e = classes.front(); e != NULL; e = e->next()) {
  140. StringName class_name = e->get();
  141. ClassAPI class_api;
  142. class_api.api_type = ClassDB::get_api_type(e->get());
  143. class_api.class_name = class_name;
  144. class_api.super_class_name = ClassDB::get_parent_class(class_name);
  145. {
  146. String name = class_name;
  147. if (name.begins_with("_")) {
  148. name.remove(0);
  149. }
  150. class_api.is_singleton = ProjectSettings::get_singleton()->has_singleton(name);
  151. }
  152. class_api.is_instanciable = !class_api.is_singleton && ClassDB::can_instance(class_name);
  153. {
  154. List<StringName> inheriters;
  155. ClassDB::get_inheriters_from_class("Reference", &inheriters);
  156. bool is_reference = !!inheriters.find(class_name);
  157. // @Unclear
  158. class_api.is_reference = !class_api.is_singleton && is_reference;
  159. }
  160. // constants
  161. {
  162. List<String> constant;
  163. ClassDB::get_integer_constant_list(class_name, &constant, true);
  164. for (List<String>::Element *c = constant.front(); c != NULL; c = c->next()) {
  165. ConstantAPI constant_api;
  166. constant_api.constant_name = c->get();
  167. constant_api.constant_value = ClassDB::get_integer_constant(class_name, c->get());
  168. class_api.constants.push_back(constant_api);
  169. }
  170. }
  171. // signals
  172. {
  173. List<MethodInfo> signals_;
  174. ClassDB::get_signal_list(class_name, &signals_, true);
  175. for (int i = 0; i < signals_.size(); i++) {
  176. SignalAPI signal;
  177. MethodInfo method_info = signals_[i];
  178. signal.name = method_info.name;
  179. for (int j = 0; j < method_info.arguments.size(); j++) {
  180. PropertyInfo argument = method_info.arguments[j];
  181. String type;
  182. String name = argument.name;
  183. if (argument.name.find(":") != -1) {
  184. type = argument.name.get_slice(":", 1);
  185. name = argument.name.get_slice(":", 0);
  186. } else {
  187. type = get_type_name(argument);
  188. }
  189. signal.argument_names.push_back(name);
  190. signal.argument_types.push_back(type);
  191. }
  192. Vector<Variant> default_arguments = method_info.default_arguments;
  193. int default_start = signal.argument_names.size() - default_arguments.size();
  194. for (int j = 0; j < default_arguments.size(); j++) {
  195. signal.default_arguments[default_start + j] = default_arguments[j];
  196. }
  197. class_api.signals_.push_back(signal);
  198. }
  199. }
  200. //properties
  201. {
  202. List<PropertyInfo> properties;
  203. ClassDB::get_property_list(class_name, &properties, true);
  204. for (List<PropertyInfo>::Element *p = properties.front(); p != NULL; p = p->next()) {
  205. PropertyAPI property_api;
  206. property_api.name = p->get().name;
  207. property_api.getter = ClassDB::get_property_getter(class_name, p->get().name);
  208. property_api.setter = ClassDB::get_property_setter(class_name, p->get().name);
  209. if (p->get().name.find(":") != -1) {
  210. property_api.type = p->get().name.get_slice(":", 1);
  211. property_api.name = p->get().name.get_slice(":", 0);
  212. } else {
  213. property_api.type = get_type_name(p->get());
  214. }
  215. if (!property_api.setter.empty() || !property_api.getter.empty()) {
  216. class_api.properties.push_back(property_api);
  217. }
  218. }
  219. }
  220. //methods
  221. {
  222. List<MethodInfo> methods;
  223. ClassDB::get_method_list(class_name, &methods, true);
  224. for (List<MethodInfo>::Element *m = methods.front(); m != NULL; m = m->next()) {
  225. MethodAPI method_api;
  226. MethodBind *method_bind = ClassDB::get_method(class_name, m->get().name);
  227. MethodInfo &method_info = m->get();
  228. //method name
  229. method_api.method_name = m->get().name;
  230. //method return type
  231. if (method_api.method_name.find(":") != -1) {
  232. method_api.return_type = method_api.method_name.get_slice(":", 1);
  233. method_api.method_name = method_api.method_name.get_slice(":", 0);
  234. } else {
  235. method_api.return_type = get_type_name(m->get().return_val);
  236. }
  237. method_api.argument_count = method_info.arguments.size();
  238. method_api.has_varargs = method_bind && method_bind->is_vararg();
  239. // Method flags
  240. if (method_info.flags) {
  241. const uint32_t flags = method_info.flags;
  242. method_api.is_editor = flags & METHOD_FLAG_EDITOR;
  243. method_api.is_noscript = flags & METHOD_FLAG_NOSCRIPT;
  244. method_api.is_const = flags & METHOD_FLAG_CONST;
  245. method_api.is_reverse = flags & METHOD_FLAG_REVERSE;
  246. method_api.is_virtual = flags & METHOD_FLAG_VIRTUAL;
  247. method_api.is_from_script = flags & METHOD_FLAG_FROM_SCRIPT;
  248. }
  249. method_api.is_virtual = method_api.is_virtual || method_api.method_name[0] == '_';
  250. // method argument name and type
  251. for (int i = 0; i < method_api.argument_count; i++) {
  252. String arg_name;
  253. String arg_type;
  254. PropertyInfo arg_info = method_info.arguments[i];
  255. arg_name = arg_info.name;
  256. if (arg_info.name.find(":") != -1) {
  257. arg_type = arg_info.name.get_slice(":", 1);
  258. arg_name = arg_info.name.get_slice(":", 0);
  259. } else if (arg_info.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  260. arg_type = arg_info.hint_string;
  261. } else if (arg_info.type == Variant::NIL) {
  262. arg_type = "Variant";
  263. } else {
  264. arg_type = Variant::get_type_name(arg_info.type);
  265. }
  266. method_api.argument_names.push_back(arg_name);
  267. method_api.argument_types.push_back(arg_type);
  268. if (method_bind && method_bind->has_default_argument(i)) {
  269. method_api.default_arguments[i] = method_bind->get_default_argument(i);
  270. }
  271. }
  272. class_api.methods.push_back(method_api);
  273. }
  274. }
  275. // enums
  276. {
  277. List<EnumAPI> enums;
  278. List<StringName> enum_names;
  279. ClassDB::get_enum_list(class_name, &enum_names, true);
  280. for (List<StringName>::Element *E = enum_names.front(); E; E = E->next()) {
  281. List<StringName> value_names;
  282. EnumAPI enum_api;
  283. enum_api.name = E->get();
  284. ClassDB::get_enum_constants(class_name, E->get(), &value_names, true);
  285. for (List<StringName>::Element *val_e = value_names.front(); val_e; val_e = val_e->next()) {
  286. int int_val = ClassDB::get_integer_constant(class_name, val_e->get(), NULL);
  287. enum_api.values.push_back(Pair<int, String>(int_val, val_e->get()));
  288. }
  289. enum_api.values.sort_custom<PairSort<int, String> >();
  290. class_api.enums.push_back(enum_api);
  291. }
  292. }
  293. api.push_back(class_api);
  294. }
  295. return api;
  296. }
  297. /*
  298. * Generates the JSON source from the API in p_api
  299. */
  300. static List<String> generate_c_api_json(const List<ClassAPI> &p_api) {
  301. // I'm sorry for the \t mess
  302. List<String> source;
  303. source.push_back("[\n");
  304. for (const List<ClassAPI>::Element *c = p_api.front(); c != NULL; c = c->next()) {
  305. ClassAPI api = c->get();
  306. source.push_back("\t{\n");
  307. source.push_back("\t\t\"name\": \"" + api.class_name + "\",\n");
  308. source.push_back("\t\t\"base_class\": \"" + api.super_class_name + "\",\n");
  309. source.push_back(String("\t\t\"api_type\": \"") + (api.api_type == ClassDB::API_CORE ? "core" : (api.api_type == ClassDB::API_EDITOR ? "tools" : "none")) + "\",\n");
  310. source.push_back(String("\t\t\"singleton\": ") + (api.is_singleton ? "true" : "false") + ",\n");
  311. source.push_back(String("\t\t\"instanciable\": ") + (api.is_instanciable ? "true" : "false") + ",\n");
  312. source.push_back(String("\t\t\"is_reference\": ") + (api.is_reference ? "true" : "false") + ",\n");
  313. // @Unclear
  314. // source.push_back(String("\t\t\"createable\": ") + (api.is_creatable ? "true" : "false") + ",\n");
  315. source.push_back("\t\t\"constants\": {\n");
  316. for (List<ConstantAPI>::Element *e = api.constants.front(); e; e = e->next()) {
  317. source.push_back("\t\t\t\"" + e->get().constant_name + "\": " + String::num_int64(e->get().constant_value) + (e->next() ? "," : "") + "\n");
  318. }
  319. source.push_back("\t\t},\n");
  320. source.push_back("\t\t\"properties\": [\n");
  321. for (List<PropertyAPI>::Element *e = api.properties.front(); e; e = e->next()) {
  322. source.push_back("\t\t\t{\n");
  323. source.push_back("\t\t\t\t\"name\": \"" + e->get().name + "\",\n");
  324. source.push_back("\t\t\t\t\"type\": \"" + e->get().type + "\",\n");
  325. source.push_back("\t\t\t\t\"getter\": \"" + e->get().getter + "\",\n");
  326. source.push_back("\t\t\t\t\"setter\": \"" + e->get().setter + "\"\n");
  327. source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n");
  328. }
  329. source.push_back("\t\t],\n");
  330. source.push_back("\t\t\"signals\": [\n");
  331. for (List<SignalAPI>::Element *e = api.signals_.front(); e; e = e->next()) {
  332. source.push_back("\t\t\t{\n");
  333. source.push_back("\t\t\t\t\"name\": \"" + e->get().name + "\",\n");
  334. source.push_back("\t\t\t\t\"arguments\": [\n");
  335. for (int i = 0; i < e->get().argument_names.size(); i++) {
  336. source.push_back("\t\t\t\t\t{\n");
  337. source.push_back("\t\t\t\t\t\t\"name\": \"" + e->get().argument_names[i] + "\",\n");
  338. source.push_back("\t\t\t\t\t\t\"type\": \"" + e->get().argument_types[i] + "\",\n");
  339. source.push_back("\t\t\t\t\t\t\"default_value\": \"" + (e->get().default_arguments.has(i) ? (String)e->get().default_arguments[i] : "") + "\"\n");
  340. source.push_back(String("\t\t\t\t\t}") + ((i < e->get().argument_names.size() - 1) ? "," : "") + "\n");
  341. }
  342. source.push_back("\t\t\t\t]\n");
  343. source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n");
  344. }
  345. source.push_back("\t\t],\n");
  346. source.push_back("\t\t\"methods\": [\n");
  347. for (List<MethodAPI>::Element *e = api.methods.front(); e; e = e->next()) {
  348. source.push_back("\t\t\t{\n");
  349. source.push_back("\t\t\t\t\"name\": \"" + e->get().method_name + "\",\n");
  350. source.push_back("\t\t\t\t\"return_type\": \"" + e->get().return_type + "\",\n");
  351. source.push_back(String("\t\t\t\t\"is_editor\": ") + (e->get().is_editor ? "true" : "false") + ",\n");
  352. source.push_back(String("\t\t\t\t\"is_noscript\": ") + (e->get().is_noscript ? "true" : "false") + ",\n");
  353. source.push_back(String("\t\t\t\t\"is_const\": ") + (e->get().is_const ? "true" : "false") + ",\n");
  354. source.push_back(String("\t\t\t\t\"is_reverse\": ") + (e->get().is_reverse ? "true" : "false") + ",\n");
  355. source.push_back(String("\t\t\t\t\"is_virtual\": ") + (e->get().is_virtual ? "true" : "false") + ",\n");
  356. source.push_back(String("\t\t\t\t\"has_varargs\": ") + (e->get().has_varargs ? "true" : "false") + ",\n");
  357. source.push_back(String("\t\t\t\t\"is_from_script\": ") + (e->get().is_from_script ? "true" : "false") + ",\n");
  358. source.push_back("\t\t\t\t\"arguments\": [\n");
  359. for (int i = 0; i < e->get().argument_names.size(); i++) {
  360. source.push_back("\t\t\t\t\t{\n");
  361. source.push_back("\t\t\t\t\t\t\"name\": \"" + e->get().argument_names[i] + "\",\n");
  362. source.push_back("\t\t\t\t\t\t\"type\": \"" + e->get().argument_types[i] + "\",\n");
  363. source.push_back(String("\t\t\t\t\t\t\"has_default_value\": ") + (e->get().default_arguments.has(i) ? "true" : "false") + ",\n");
  364. source.push_back("\t\t\t\t\t\t\"default_value\": \"" + (e->get().default_arguments.has(i) ? (String)e->get().default_arguments[i] : "") + "\"\n");
  365. source.push_back(String("\t\t\t\t\t}") + ((i < e->get().argument_names.size() - 1) ? "," : "") + "\n");
  366. }
  367. source.push_back("\t\t\t\t]\n");
  368. source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n");
  369. }
  370. source.push_back("\t\t],\n");
  371. source.push_back("\t\t\"enums\": [\n");
  372. for (List<EnumAPI>::Element *e = api.enums.front(); e; e = e->next()) {
  373. source.push_back("\t\t\t{\n");
  374. source.push_back("\t\t\t\t\"name\": \"" + e->get().name + "\",\n");
  375. source.push_back("\t\t\t\t\"values\": {\n");
  376. for (List<Pair<int, String> >::Element *val_e = e->get().values.front(); val_e; val_e = val_e->next()) {
  377. source.push_back("\t\t\t\t\t\"" + val_e->get().second + "\": " + itos(val_e->get().first));
  378. source.push_back(String((val_e->next() ? "," : "")) + "\n");
  379. }
  380. source.push_back("\t\t\t\t}\n");
  381. source.push_back(String("\t\t\t}") + (e->next() ? "," : "") + "\n");
  382. }
  383. source.push_back("\t\t]\n");
  384. source.push_back(String("\t}") + (c->next() ? "," : "") + "\n");
  385. }
  386. source.push_back("]");
  387. return source;
  388. }
  389. //
  390. #endif
  391. /*
  392. * Saves the whole Godot API to a JSON file located at
  393. * p_path
  394. */
  395. Error generate_c_api(const String &p_path) {
  396. #ifndef TOOLS_ENABLED
  397. return ERR_BUG;
  398. #else
  399. List<ClassAPI> api = generate_c_api_classes();
  400. List<String> json_source = generate_c_api_json(api);
  401. return save_file(p_path, json_source);
  402. #endif
  403. }