api_generator.cpp 13 KB

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