api_generator.cpp 16 KB

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