binding_generator.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. #!/usr/bin/env python
  2. import json
  3. # comment.
  4. # Convenience function for using template get_node
  5. def correct_method_name(method_list):
  6. for method in method_list:
  7. if method["name"] == "get_node":
  8. method["name"] = "get_node_internal"
  9. classes = []
  10. def generate_bindings(path, use_template_get_node):
  11. global classes
  12. classes = json.load(open(path))
  13. icalls = set()
  14. for c in classes:
  15. # print c['name']
  16. used_classes = get_used_classes(c)
  17. if use_template_get_node and c["name"] == "Node":
  18. correct_method_name(c["methods"])
  19. header = generate_class_header(used_classes, c, use_template_get_node)
  20. impl = generate_class_implementation(icalls, used_classes, c, use_template_get_node)
  21. header_file = open("include/gen/" + strip_name(c["name"]) + ".hpp", "w+")
  22. header_file.write(header)
  23. source_file = open("src/gen/" + strip_name(c["name"]) + ".cpp", "w+")
  24. source_file.write(impl)
  25. icall_header_file = open("include/gen/__icalls.hpp", "w+")
  26. icall_header_file.write(generate_icall_header(icalls))
  27. register_types_file = open("src/gen/__register_types.cpp", "w+")
  28. register_types_file.write(generate_type_registry(classes))
  29. init_method_bindings_file = open("src/gen/__init_method_bindings.cpp", "w+")
  30. init_method_bindings_file.write(generate_init_method_bindings(classes))
  31. def is_reference_type(t):
  32. for c in classes:
  33. if c['name'] != t:
  34. continue
  35. if c['is_reference']:
  36. return True
  37. return False
  38. def make_gdnative_type(t, ref_allowed):
  39. if is_enum(t):
  40. return remove_enum_prefix(t) + " "
  41. elif is_class_type(t):
  42. if is_reference_type(t) and ref_allowed:
  43. return "Ref<" + strip_name(t) + "> "
  44. else:
  45. return strip_name(t) + " *"
  46. else:
  47. if t == "int":
  48. return "int64_t "
  49. if t == "float" or t == "real":
  50. return "real_t "
  51. return strip_name(t) + " "
  52. def generate_class_header(used_classes, c, use_template_get_node):
  53. source = []
  54. source.append("#ifndef GODOT_CPP_" + strip_name(c["name"]).upper() + "_HPP")
  55. source.append("#define GODOT_CPP_" + strip_name(c["name"]).upper() + "_HPP")
  56. source.append("")
  57. source.append("")
  58. source.append("#include <gdnative_api_struct.gen.h>")
  59. source.append("#include <stdint.h>")
  60. source.append("")
  61. source.append("#include <core/CoreTypes.hpp>")
  62. class_name = strip_name(c["name"])
  63. # Ref<T> is not included in object.h in Godot either,
  64. # so don't include it here because it's not needed
  65. if class_name != "Object" and class_name != "Reference":
  66. source.append("#include <core/Ref.hpp>")
  67. ref_allowed = True
  68. else:
  69. source.append("#include <core/TagDB.hpp>")
  70. ref_allowed = False
  71. included = []
  72. for used_class in used_classes:
  73. if is_enum(used_class) and is_nested_type(used_class):
  74. used_class_name = remove_enum_prefix(extract_nested_type(used_class))
  75. if used_class_name not in included:
  76. included.append(used_class_name)
  77. source.append("#include \"" + used_class_name + ".hpp\"")
  78. elif is_enum(used_class) and is_nested_type(used_class) and not is_nested_type(used_class, class_name):
  79. used_class_name = remove_enum_prefix(used_class)
  80. if used_class_name not in included:
  81. included.append(used_class_name)
  82. source.append("#include \"" + used_class_name + ".hpp\"")
  83. source.append("")
  84. if c["base_class"] != "":
  85. source.append("#include \"" + strip_name(c["base_class"]) + ".hpp\"")
  86. source.append("namespace godot {")
  87. source.append("")
  88. for used_type in used_classes:
  89. if is_enum(used_type) or is_nested_type(used_type, class_name):
  90. continue
  91. else:
  92. source.append("class " + strip_name(used_type) + ";")
  93. source.append("")
  94. vararg_templates = ""
  95. # generate the class definition here
  96. source.append("class " + class_name + (" : public _Wrapped" if c["base_class"] == "" else (" : public " + strip_name(c["base_class"])) ) + " {")
  97. if c["base_class"] == "":
  98. source.append("public: enum { ___CLASS_IS_SCRIPT = 0, };")
  99. source.append("")
  100. source.append("private:")
  101. if c["singleton"]:
  102. source.append("\tstatic " + class_name + " *_singleton;")
  103. source.append("")
  104. source.append("\t" + class_name + "();")
  105. source.append("")
  106. # Generate method table
  107. source.append("\tstruct ___method_bindings {")
  108. for method in c["methods"]:
  109. source.append("\t\tgodot_method_bind *mb_" + method["name"] + ";")
  110. source.append("\t};")
  111. source.append("\tstatic ___method_bindings ___mb;")
  112. source.append("\tstatic void *_detail_class_tag;")
  113. source.append("")
  114. source.append("public:")
  115. source.append("\tstatic void ___init_method_bindings();")
  116. # class id from core engine for casting
  117. source.append("\tinline static size_t ___get_id() { return (size_t)_detail_class_tag; }")
  118. source.append("")
  119. if c["singleton"]:
  120. source.append("\tstatic inline " + class_name + " *get_singleton()")
  121. source.append("\t{")
  122. source.append("\t\tif (!" + class_name + "::_singleton) {")
  123. source.append("\t\t\t" + class_name + "::_singleton = new " + class_name + ";")
  124. source.append("\t\t}")
  125. source.append("\t\treturn " + class_name + "::_singleton;")
  126. source.append("\t}")
  127. source.append("")
  128. # godot::api->godot_global_get_singleton((char *) \"" + strip_name(c["name"]) + "\");"
  129. # ___get_class_name
  130. source.append("\tstatic inline const char *___get_class_name() { return (const char *) \"" + strip_name(c["name"]) + "\"; }")
  131. source.append("\tstatic inline Object *___get_from_variant(Variant a) { godot_object *o = (godot_object*) a; return (o) ? (Object *) godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, o) : nullptr; }")
  132. enum_values = []
  133. source.append("\n\t// enums")
  134. for enum in c["enums"]:
  135. source.append("\tenum " + strip_name(enum["name"]) + " {")
  136. for value in enum["values"]:
  137. source.append("\t\t" + remove_nested_type_prefix(value) + " = " + str(enum["values"][value]) + ",")
  138. enum_values.append(value)
  139. source.append("\t};")
  140. source.append("\n\t// constants")
  141. for name in c["constants"]:
  142. if name not in enum_values:
  143. source.append("\tconst static int " + name + " = " + str(c["constants"][name]) + ";")
  144. if c["instanciable"]:
  145. source.append("")
  146. source.append("")
  147. source.append("\tstatic " + class_name + " *_new();")
  148. source.append("\n\t// methods")
  149. if class_name == "Object":
  150. source.append("#ifndef GODOT_CPP_NO_OBJECT_CAST")
  151. source.append("\ttemplate<class T>")
  152. source.append("\tstatic T *cast_to(const Object *obj);")
  153. source.append("#endif")
  154. source.append("")
  155. for method in c["methods"]:
  156. method_signature = ""
  157. # TODO decide what to do about virtual methods
  158. # method_signature += "virtual " if method["is_virtual"] else ""
  159. method_signature += make_gdnative_type(method["return_type"], ref_allowed)
  160. method_name = escape_cpp(method["name"])
  161. method_signature += method_name + "("
  162. has_default_argument = False
  163. method_arguments = ""
  164. for i, argument in enumerate(method["arguments"]):
  165. method_signature += "const " + make_gdnative_type(argument["type"], ref_allowed)
  166. argument_name = escape_cpp(argument["name"])
  167. method_signature += argument_name
  168. method_arguments += argument_name
  169. # default arguments
  170. def escape_default_arg(_type, default_value):
  171. if _type == "Color":
  172. return "Color(" + default_value + ")"
  173. if _type == "bool" or _type == "int":
  174. return default_value.lower()
  175. if _type == "Array":
  176. return "Array()"
  177. if _type in ["PoolVector2Array", "PoolStringArray", "PoolVector3Array", "PoolColorArray", "PoolIntArray", "PoolRealArray"]:
  178. return _type + "()"
  179. if _type == "Vector2":
  180. return "Vector2" + default_value
  181. if _type == "Vector3":
  182. return "Vector3" + default_value
  183. if _type == "Transform":
  184. return "Transform()"
  185. if _type == "Transform2D":
  186. return "Transform2D()"
  187. if _type == "Rect2":
  188. return "Rect2" + default_value
  189. if _type == "Variant":
  190. return "Variant()" if default_value == "Null" else default_value
  191. if _type == "String":
  192. return "\"" + default_value + "\""
  193. if _type == "RID":
  194. return "RID()"
  195. if default_value == "Null" or default_value == "[Object:null]":
  196. return "nullptr"
  197. return default_value
  198. if argument["has_default_value"] or has_default_argument:
  199. method_signature += " = " + escape_default_arg(argument["type"], argument["default_value"])
  200. has_default_argument = True
  201. if i != len(method["arguments"]) - 1:
  202. method_signature += ", "
  203. method_arguments += ","
  204. if method["has_varargs"]:
  205. if len(method["arguments"]) > 0:
  206. method_signature += ", "
  207. method_arguments += ", "
  208. vararg_templates += "\ttemplate <class... Args> " + method_signature + "Args... args){\n\t\treturn " + method_name + "(" + method_arguments + "Array::make(args...));\n\t}\n"""
  209. method_signature += "const Array& __var_args = Array()"
  210. method_signature += ")" + (" const" if method["is_const"] else "")
  211. source.append("\t" + method_signature + ";")
  212. source.append(vararg_templates)
  213. if use_template_get_node and class_name == "Node":
  214. # Extra definition for template get_node that calls the renamed get_node_internal; has a default template parameter for backwards compatibility.
  215. source.append("\ttemplate <class T = Node>")
  216. source.append("\tT *get_node(const NodePath path) const {")
  217. source.append("\t\treturn Object::cast_to<T>(get_node_internal(path));")
  218. source.append("\t}")
  219. source.append("};")
  220. source.append("")
  221. # ...And a specialized version so we don't unnecessarily cast when using the default.
  222. source.append("template <>")
  223. source.append("inline Node *Node::get_node<Node>(const NodePath path) const {")
  224. source.append("\treturn get_node_internal(path);")
  225. source.append("}")
  226. source.append("")
  227. else:
  228. source.append("};")
  229. source.append("")
  230. source.append("}")
  231. source.append("")
  232. source.append("#endif")
  233. return "\n".join(source)
  234. def generate_class_implementation(icalls, used_classes, c, use_template_get_node):
  235. class_name = strip_name(c["name"])
  236. ref_allowed = class_name != "Object" and class_name != "Reference"
  237. source = []
  238. source.append("#include \"" + class_name + ".hpp\"")
  239. source.append("")
  240. source.append("")
  241. source.append("#include <core/GodotGlobal.hpp>")
  242. source.append("#include <core/CoreTypes.hpp>")
  243. source.append("#include <core/Ref.hpp>")
  244. source.append("#include <core/Godot.hpp>")
  245. source.append("")
  246. source.append("#include \"__icalls.hpp\"")
  247. source.append("")
  248. source.append("")
  249. for used_class in used_classes:
  250. if is_enum(used_class):
  251. continue
  252. else:
  253. source.append("#include \"" + strip_name(used_class) + ".hpp\"")
  254. source.append("")
  255. source.append("")
  256. source.append("namespace godot {")
  257. core_object_name = "this"
  258. source.append("")
  259. source.append("")
  260. if c["singleton"]:
  261. source.append("" + class_name + " *" + class_name + "::_singleton = NULL;")
  262. source.append("")
  263. source.append("")
  264. # FIXME Test if inlining has a huge impact on binary size
  265. source.append(class_name + "::" + class_name + "() {")
  266. source.append("\t_owner = godot::api->godot_global_get_singleton((char *) \"" + strip_name(c["name"]) + "\");")
  267. source.append("}")
  268. source.append("")
  269. source.append("")
  270. # Method table initialization
  271. source.append(class_name + "::___method_bindings " + class_name + "::___mb = {};")
  272. source.append("")
  273. source.append("void *" + class_name + "::_detail_class_tag = nullptr;")
  274. source.append("")
  275. source.append("void " + class_name + "::___init_method_bindings() {")
  276. for method in c["methods"]:
  277. source.append("\t___mb.mb_" + method["name"] + " = godot::api->godot_method_bind_get_method(\"" + c["name"] + "\", \"" + ("get_node" if use_template_get_node and method["name"] == "get_node_internal" else method["name"]) + "\");")
  278. source.append("\tgodot_string_name class_name;")
  279. source.append("\tgodot::api->godot_string_name_new_data(&class_name, \"" + c["name"] + "\");")
  280. source.append("\t_detail_class_tag = godot::core_1_2_api->godot_get_class_tag(&class_name);")
  281. source.append("\tgodot::api->godot_string_name_destroy(&class_name);")
  282. source.append("}")
  283. source.append("")
  284. if c["instanciable"]:
  285. source.append(class_name + " *" + strip_name(c["name"]) + "::_new()")
  286. source.append("{")
  287. source.append("\treturn (" + class_name + " *) godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, godot::api->godot_get_class_constructor((char *)\"" + c["name"] + "\")());")
  288. source.append("}")
  289. for method in c["methods"]:
  290. method_signature = ""
  291. method_signature += make_gdnative_type(method["return_type"], ref_allowed)
  292. method_signature += strip_name(c["name"]) + "::" + escape_cpp(method["name"]) + "("
  293. for i, argument in enumerate(method["arguments"]):
  294. method_signature += "const " + make_gdnative_type(argument["type"], ref_allowed)
  295. method_signature += escape_cpp(argument["name"])
  296. if i != len(method["arguments"]) - 1:
  297. method_signature += ", "
  298. if method["has_varargs"]:
  299. if len(method["arguments"]) > 0:
  300. method_signature += ", "
  301. method_signature += "const Array& __var_args"
  302. method_signature += ")" + (" const" if method["is_const"] else "")
  303. source.append(method_signature + " {")
  304. if method["name"] == "free":
  305. # dirty hack because Object::free is marked virtual but doesn't actually exist...
  306. source.append("\tgodot::api->godot_object_destroy(_owner);")
  307. source.append("}")
  308. source.append("")
  309. continue
  310. return_statement = ""
  311. return_type_is_ref = is_reference_type(method["return_type"]) and ref_allowed
  312. if method["return_type"] != "void":
  313. if is_class_type(method["return_type"]):
  314. if is_enum(method["return_type"]):
  315. return_statement += "return (" + remove_enum_prefix(method["return_type"]) + ") "
  316. elif return_type_is_ref:
  317. return_statement += "return Ref<" + strip_name(method["return_type"]) + ">::__internal_constructor(";
  318. else:
  319. return_statement += "return " + ("(" + strip_name(method["return_type"]) + " *) " if is_class_type(method["return_type"]) else "")
  320. else:
  321. return_statement += "return "
  322. def get_icall_type_name(name):
  323. if is_enum(name):
  324. return "int"
  325. if is_class_type(name):
  326. return "Object"
  327. return name
  328. if method["has_varargs"]:
  329. if len(method["arguments"]) != 0:
  330. source.append("\tVariant __given_args[" + str(len(method["arguments"])) + "];")
  331. for i, argument in enumerate(method["arguments"]):
  332. source.append("\tgodot::api->godot_variant_new_nil((godot_variant *) &__given_args[" + str(i) + "]);")
  333. source.append("")
  334. for i, argument in enumerate(method["arguments"]):
  335. source.append("\t__given_args[" + str(i) + "] = " + escape_cpp(argument["name"]) + ";")
  336. source.append("")
  337. size = ""
  338. if method["has_varargs"]:
  339. size = "(__var_args.size() + " + str(len(method["arguments"])) + ")"
  340. else:
  341. size = "(" + str(len(method["arguments"])) + ")"
  342. source.append("\tgodot_variant **__args = (godot_variant **) alloca(sizeof(godot_variant *) * " + size + ");")
  343. source.append("")
  344. for i, argument in enumerate(method["arguments"]):
  345. source.append("\t__args[" + str(i) + "] = (godot_variant *) &__given_args[" + str(i) + "];")
  346. source.append("")
  347. if method["has_varargs"]:
  348. source.append("\tfor (int i = 0; i < __var_args.size(); i++) {")
  349. source.append("\t\t__args[i + " + str(len(method["arguments"])) + "] = (godot_variant *) &((Array &) __var_args)[i];")
  350. source.append("\t}")
  351. source.append("")
  352. source.append("\tVariant __result;")
  353. source.append("\t*(godot_variant *) &__result = godot::api->godot_method_bind_call(___mb.mb_" + method["name"] + ", ((const Object *) " + core_object_name + ")->_owner, (const godot_variant **) __args, " + size + ", nullptr);")
  354. source.append("")
  355. if is_class_type(method["return_type"]):
  356. source.append("\tObject *obj = Object::___get_from_variant(__result);")
  357. source.append("\tif (obj->has_method(\"reference\"))")
  358. source.append("\t\tobj->callv(\"reference\", Array());")
  359. source.append("")
  360. for i, argument in enumerate(method["arguments"]):
  361. source.append("\tgodot::api->godot_variant_destroy((godot_variant *) &__given_args[" + str(i) + "]);")
  362. source.append("")
  363. if method["return_type"] != "void":
  364. cast = ""
  365. if is_class_type(method["return_type"]):
  366. if return_type_is_ref:
  367. cast += "Ref<" + strip_name(method["return_type"]) + ">::__internal_constructor(__result);"
  368. else:
  369. cast += "(" + strip_name(method["return_type"]) + " *) " + strip_name(method["return_type"] + "::___get_from_variant(") + "__result);"
  370. else:
  371. cast += "__result;"
  372. source.append("\treturn " + cast)
  373. else:
  374. args = []
  375. for arg in method["arguments"]:
  376. args.append(get_icall_type_name(arg["type"]))
  377. icall_ret_type = get_icall_type_name(method["return_type"])
  378. icall_sig = tuple((icall_ret_type, tuple(args)))
  379. icalls.add(icall_sig)
  380. icall_name = get_icall_name(icall_sig)
  381. return_statement += icall_name + "(___mb.mb_" + method["name"] + ", (const Object *) " + core_object_name
  382. for arg in method["arguments"]:
  383. arg_is_ref = is_reference_type(arg["type"]) and ref_allowed
  384. return_statement += ", " + escape_cpp(arg["name"]) + (".ptr()" if arg_is_ref else "")
  385. return_statement += ")"
  386. if return_type_is_ref:
  387. return_statement += ")"
  388. source.append("\t" + return_statement + ";")
  389. source.append("}")
  390. source.append("")
  391. source.append("}")
  392. return "\n".join(source)
  393. def generate_icall_header(icalls):
  394. source = []
  395. source.append("#ifndef GODOT_CPP__ICALLS_HPP")
  396. source.append("#define GODOT_CPP__ICALLS_HPP")
  397. source.append("")
  398. source.append("#include <gdnative_api_struct.gen.h>")
  399. source.append("#include <stdint.h>")
  400. source.append("")
  401. source.append("#include <core/GodotGlobal.hpp>")
  402. source.append("#include <core/CoreTypes.hpp>")
  403. source.append("#include \"Object.hpp\"")
  404. source.append("")
  405. source.append("")
  406. source.append("namespace godot {")
  407. source.append("")
  408. for icall in icalls:
  409. ret_type = icall[0]
  410. args = icall[1]
  411. method_signature = "static inline "
  412. method_signature += get_icall_return_type(ret_type) + get_icall_name(icall) + "(godot_method_bind *mb, const Object *inst"
  413. for i, arg in enumerate(args):
  414. method_signature += ", const "
  415. if is_core_type(arg):
  416. method_signature += arg + "&"
  417. elif arg == "int":
  418. method_signature += "int64_t "
  419. elif arg == "float":
  420. method_signature += "double "
  421. elif is_primitive(arg):
  422. method_signature += arg + " "
  423. else:
  424. method_signature += "Object *"
  425. method_signature += "arg" + str(i)
  426. method_signature += ")"
  427. source.append(method_signature + " {")
  428. if ret_type != "void":
  429. source.append("\t" + ("godot_object *" if is_class_type(ret_type) else get_icall_return_type(ret_type)) + "ret;")
  430. if is_class_type(ret_type):
  431. source.append("\tret = nullptr;")
  432. source.append("\tconst void *args[" + ("1" if len(args) == 0 else "") + "] = {")
  433. for i, arg in enumerate(args):
  434. wrapped_argument = "\t\t"
  435. if is_primitive(arg) or is_core_type(arg):
  436. wrapped_argument += "(void *) &arg" + str(i)
  437. else:
  438. wrapped_argument += "(void *) (arg" + str(i) + ") ? arg" + str(i) + "->_owner : nullptr"
  439. wrapped_argument += ","
  440. source.append(wrapped_argument)
  441. source.append("\t};")
  442. source.append("")
  443. source.append("\tgodot::api->godot_method_bind_ptrcall(mb, inst->_owner, args, " + ("nullptr" if ret_type == "void" else "&ret") + ");")
  444. if ret_type != "void":
  445. if is_class_type(ret_type):
  446. source.append("\tif (ret) {")
  447. source.append("\t\treturn (Object *) godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, ret);")
  448. source.append("\t}")
  449. source.append("")
  450. source.append("\treturn (Object *) ret;")
  451. else:
  452. source.append("\treturn ret;")
  453. source.append("}")
  454. source.append("")
  455. source.append("}")
  456. source.append("")
  457. source.append("#endif")
  458. return "\n".join(source)
  459. def generate_type_registry(classes):
  460. source = []
  461. source.append("#include \"TagDB.hpp\"")
  462. source.append("#include <typeinfo>")
  463. source.append("\n")
  464. for c in classes:
  465. source.append("#include <" + strip_name(c["name"]) + ".hpp>")
  466. source.append("")
  467. source.append("")
  468. source.append("namespace godot {")
  469. source.append("void ___register_types()")
  470. source.append("{")
  471. for c in classes:
  472. class_name = strip_name(c["name"])
  473. base_class_name = strip_name(c["base_class"])
  474. class_type_hash = "typeid(" + class_name + ").hash_code()"
  475. base_class_type_hash = "typeid(" + base_class_name + ").hash_code()"
  476. if base_class_name == "":
  477. base_class_type_hash = "0"
  478. source.append("\tgodot::_TagDB::register_global_type(\"" + c["name"] + "\", " + class_type_hash + ", " + base_class_type_hash + ");")
  479. source.append("}")
  480. source.append("")
  481. source.append("}")
  482. return "\n".join(source)
  483. def generate_init_method_bindings(classes):
  484. source = []
  485. for c in classes:
  486. source.append("#include <" + strip_name(c["name"]) + ".hpp>")
  487. source.append("")
  488. source.append("")
  489. source.append("namespace godot {")
  490. source.append("void ___init_method_bindings()")
  491. source.append("{")
  492. for c in classes:
  493. class_name = strip_name(c["name"])
  494. source.append("\t" + strip_name(c["name"]) + "::___init_method_bindings();")
  495. source.append("}")
  496. source.append("")
  497. source.append("}")
  498. return "\n".join(source)
  499. def get_icall_return_type(t):
  500. if is_class_type(t):
  501. return "Object *"
  502. if t == "int":
  503. return "int64_t "
  504. if t == "float" or t == "real":
  505. return "double "
  506. return t + " "
  507. def get_icall_name(sig):
  508. ret_type = sig[0]
  509. args = sig[1]
  510. name = "___godot_icall_"
  511. name += strip_name(ret_type)
  512. for arg in args:
  513. name += "_" + strip_name(arg)
  514. return name
  515. def get_used_classes(c):
  516. classes = []
  517. for method in c["methods"]:
  518. if is_class_type(method["return_type"]) and not (method["return_type"] in classes):
  519. classes.append(method["return_type"])
  520. for arg in method["arguments"]:
  521. if is_class_type(arg["type"]) and not (arg["type"] in classes):
  522. classes.append(arg["type"])
  523. return classes
  524. def strip_name(name):
  525. if len(name) == 0:
  526. return name
  527. if name[0] == '_':
  528. return name[1:]
  529. return name
  530. def extract_nested_type(nested_type):
  531. return strip_name(nested_type[:nested_type.find("::")])
  532. def remove_nested_type_prefix(name):
  533. return name if name.find("::") == -1 else strip_name(name[name.find("::") + 2:])
  534. def remove_enum_prefix(name):
  535. return strip_name(name[name.find("enum.") + 5:])
  536. def is_nested_type(name, type = ""):
  537. return name.find(type + "::") != -1
  538. def is_enum(name):
  539. return name.find("enum.") == 0
  540. def is_class_type(name):
  541. return not is_core_type(name) and not is_primitive(name)
  542. def is_core_type(name):
  543. core_types = ["Array",
  544. "Basis",
  545. "Color",
  546. "Dictionary",
  547. "Error",
  548. "NodePath",
  549. "Plane",
  550. "PoolByteArray",
  551. "PoolIntArray",
  552. "PoolRealArray",
  553. "PoolStringArray",
  554. "PoolVector2Array",
  555. "PoolVector3Array",
  556. "PoolColorArray",
  557. "PoolIntArray",
  558. "PoolRealArray",
  559. "Quat",
  560. "Rect2",
  561. "AABB",
  562. "RID",
  563. "String",
  564. "Transform",
  565. "Transform2D",
  566. "Variant",
  567. "Vector2",
  568. "Vector3"]
  569. return name in core_types
  570. def is_primitive(name):
  571. core_types = ["int", "bool", "real", "float", "void"]
  572. return name in core_types
  573. def escape_cpp(name):
  574. escapes = {
  575. "class": "_class",
  576. "char": "_char",
  577. "short": "_short",
  578. "bool": "_bool",
  579. "int": "_int",
  580. "default": "_default",
  581. "case": "_case",
  582. "switch": "_switch",
  583. "export": "_export",
  584. "template": "_template",
  585. "new": "new_",
  586. "operator": "_operator",
  587. "typename": "_typename"
  588. }
  589. if name in escapes:
  590. return escapes[name]
  591. return name