api_generator.cpp 13 KB

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