api_generator.cpp 12 KB

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