Godot.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. #ifndef GODOT_HPP
  2. #define GODOT_HPP
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <gdnative_api_struct.gen.h>
  6. #include <nativescript/godot_nativescript.h>
  7. #include <typeinfo>
  8. #include "CoreTypes.hpp"
  9. #include "Ref.hpp"
  10. #include "TagDB.hpp"
  11. #include "Variant.hpp"
  12. #include "Object.hpp"
  13. #include "GodotGlobal.hpp"
  14. namespace godot {
  15. namespace detail {
  16. // Godot classes are wrapped by heap-allocated instances mimicking them through the C API.
  17. // They all inherit `_Wrapped`.
  18. template <class T>
  19. T *get_wrapper(godot_object *obj) {
  20. return (T *)godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, obj);
  21. }
  22. // Custom class instances are not obtainable by just casting the pointer to the base class they inherit,
  23. // partly because in Godot, scripts are not instances of the classes themselves, they are only attached to them.
  24. // Yet we want to "fake" it as if they were the same entity.
  25. template <class T>
  26. T *get_custom_class_instance(const Object *obj) {
  27. return (obj) ? (T *)godot::nativescript_api->godot_nativescript_get_userdata(obj->_owner) : nullptr;
  28. }
  29. template <class T>
  30. inline T *create_custom_class_instance() {
  31. // Usually, script instances hold a reference to their NativeScript resource.
  32. // that resource is obtained from a `.gdns` file, which in turn exists because
  33. // of the resource system of Godot. We can't cleanly hardcode that here,
  34. // so the easiest for now (though not really clean) is to create new resource instances,
  35. // individually attached to the script instances.
  36. // We cannot use wrappers because of https://github.com/godotengine/godot/issues/39181
  37. // godot::NativeScript *script = godot::NativeScript::_new();
  38. // script->set_library(get_wrapper<godot::GDNativeLibrary>((godot_object *)godot::gdnlib));
  39. // script->set_class_name(T::___get_type_name());
  40. // So we use the C API directly.
  41. static godot_class_constructor script_constructor = godot::api->godot_get_class_constructor("NativeScript");
  42. static godot_method_bind *mb_set_library = godot::api->godot_method_bind_get_method("NativeScript", "set_library");
  43. static godot_method_bind *mb_set_class_name = godot::api->godot_method_bind_get_method("NativeScript", "set_class_name");
  44. godot_object *script = script_constructor();
  45. {
  46. const void *args[] = { godot::gdnlib };
  47. godot::api->godot_method_bind_ptrcall(mb_set_library, script, args, nullptr);
  48. }
  49. {
  50. const String class_name = T::___get_type_name();
  51. const void *args[] = { &class_name };
  52. godot::api->godot_method_bind_ptrcall(mb_set_class_name, script, args, nullptr);
  53. }
  54. // Now to instanciate T, we initially did this, however in case of Reference it returns a variant with refcount
  55. // already initialized, which woud cause inconsistent behavior compared to other classes (we still have to return a pointer).
  56. //Variant instance_variant = script->new_();
  57. //T *instance = godot::get_custom_class_instance<T>(instance_variant);
  58. // So we should do this instead, however while convenient, it uses unnecessary wrapper objects.
  59. // Object *base_obj = T::___new_godot_base();
  60. // base_obj->set_script(script);
  61. // return get_custom_class_instance<T>(base_obj);
  62. // Again using the C API to do exactly what we have to do.
  63. static godot_class_constructor base_constructor = godot::api->godot_get_class_constructor(T::___get_godot_base_class_name());
  64. static godot_method_bind *mb_set_script = godot::api->godot_method_bind_get_method("Object", "set_script");
  65. godot_object *base_obj = base_constructor();
  66. {
  67. const void *args[] = { script };
  68. godot::api->godot_method_bind_ptrcall(mb_set_script, base_obj, args, nullptr);
  69. }
  70. return (T *)godot::nativescript_api->godot_nativescript_get_userdata(base_obj);
  71. }
  72. } // namespace detail
  73. // Used in the definition of a custom class where the base is a Godot class
  74. #define GODOT_CLASS(Name, Base) \
  75. \
  76. public: \
  77. inline static const char *___get_type_name() { return #Name; } \
  78. enum { ___CLASS_IS_SCRIPT = 1 }; \
  79. inline static const char *___get_godot_base_class_name() { \
  80. return Base::___get_class_name(); \
  81. } \
  82. inline static Name *_new() { \
  83. return godot::detail::create_custom_class_instance<Name>(); \
  84. } \
  85. inline static size_t ___get_id() { return typeid(Name).hash_code(); } \
  86. inline static size_t ___get_base_id() { return Base::___get_id(); } \
  87. inline static const char *___get_base_type_name() { return Base::___get_class_name(); } \
  88. inline static godot::Object *___get_from_variant(godot::Variant a) { \
  89. return (godot::Object *)godot::detail::get_custom_class_instance<Name>( \
  90. godot::Object::___get_from_variant(a)); \
  91. } \
  92. \
  93. private:
  94. // Used in the definition of a custom class where the base is another custom class
  95. #define GODOT_SUBCLASS(Name, Base) \
  96. \
  97. public: \
  98. inline static const char *___get_type_name() { return #Name; } \
  99. enum { ___CLASS_IS_SCRIPT = 1 }; \
  100. inline static const char *___get_godot_base_class_name() { \
  101. return Base::___get_godot_base_class_name(); \
  102. } \
  103. inline static Name *_new() { \
  104. return godot::detail::create_custom_class_instance<Name>(); \
  105. } \
  106. inline static size_t ___get_id() { return typeid(Name).hash_code(); }; \
  107. inline static size_t ___get_base_id() { return typeid(Base).hash_code(); }; \
  108. inline static const char *___get_base_type_name() { return Base::___get_type_name(); } \
  109. inline static godot::Object *___get_from_variant(godot::Variant a) { \
  110. return (godot::Object *)godot::detail::get_custom_class_instance<Name>( \
  111. godot::Object::___get_from_variant(a)); \
  112. } \
  113. \
  114. private:
  115. template <class T>
  116. struct _ArgCast {
  117. static T _arg_cast(Variant a) {
  118. return a;
  119. }
  120. };
  121. template <class T>
  122. struct _ArgCast<T *> {
  123. static T *_arg_cast(Variant a) {
  124. return (T *)T::___get_from_variant(a);
  125. }
  126. };
  127. template <>
  128. struct _ArgCast<Variant> {
  129. static Variant _arg_cast(Variant a) {
  130. return a;
  131. }
  132. };
  133. // instance and destroy funcs
  134. template <class T>
  135. void *_godot_class_instance_func(godot_object *p, void *method_data) {
  136. T *d = new T();
  137. d->_owner = p;
  138. d->_type_tag = typeid(T).hash_code();
  139. d->_init();
  140. return d;
  141. }
  142. template <class T>
  143. void _godot_class_destroy_func(godot_object *p, void *method_data, void *data) {
  144. T *d = (T *)data;
  145. delete d;
  146. }
  147. template <class T>
  148. void register_class() {
  149. godot_instance_create_func create = {};
  150. create.create_func = _godot_class_instance_func<T>;
  151. godot_instance_destroy_func destroy = {};
  152. destroy.destroy_func = _godot_class_destroy_func<T>;
  153. _TagDB::register_type(T::___get_id(), T::___get_base_id());
  154. godot::nativescript_api->godot_nativescript_register_class(godot::_RegisterState::nativescript_handle, T::___get_type_name(), T::___get_base_type_name(), create, destroy);
  155. godot::nativescript_1_1_api->godot_nativescript_set_type_tag(godot::_RegisterState::nativescript_handle, T::___get_type_name(), (const void *)typeid(T).hash_code());
  156. T::_register_methods();
  157. }
  158. template <class T>
  159. void register_tool_class() {
  160. godot_instance_create_func create = {};
  161. create.create_func = _godot_class_instance_func<T>;
  162. godot_instance_destroy_func destroy = {};
  163. destroy.destroy_func = _godot_class_destroy_func<T>;
  164. _TagDB::register_type(T::___get_id(), T::___get_base_id());
  165. godot::nativescript_api->godot_nativescript_register_tool_class(godot::_RegisterState::nativescript_handle, T::___get_type_name(), T::___get_base_type_name(), create, destroy);
  166. godot::nativescript_1_1_api->godot_nativescript_set_type_tag(godot::_RegisterState::nativescript_handle, T::___get_type_name(), (const void *)typeid(T).hash_code());
  167. T::_register_methods();
  168. }
  169. // method registering
  170. typedef godot_variant (*__godot_wrapper_method)(godot_object *, void *, void *, int, godot_variant **);
  171. template <class T, class R, class... args>
  172. const char *___get_method_class_name(R (T::*p)(args... a)) {
  173. return T::___get_type_name();
  174. }
  175. template <class T, class R, class... args>
  176. const char *___get_method_class_name(R (T::*p)(args... a) const) {
  177. return T::___get_type_name();
  178. }
  179. // Okay, time for some template magic.
  180. // Many thanks to manpat from the GDL Discord Server.
  181. // This is stuff that's available in C++14 I think, but whatever.
  182. template <int... I>
  183. struct __Sequence {};
  184. template <int N, int... I>
  185. struct __construct_sequence {
  186. using type = typename __construct_sequence<N - 1, N - 1, I...>::type;
  187. };
  188. template <int... I>
  189. struct __construct_sequence<0, I...> {
  190. using type = __Sequence<I...>;
  191. };
  192. // Now the wrapping part.
  193. template <class T, class R, class... As>
  194. struct _WrappedMethod {
  195. R(T::*f)
  196. (As...);
  197. template <int... I>
  198. void apply(Variant *ret, T *obj, Variant **args, __Sequence<I...>) {
  199. *ret = (obj->*f)(_ArgCast<As>::_arg_cast(*args[I])...);
  200. }
  201. };
  202. template <class T, class... As>
  203. struct _WrappedMethod<T, void, As...> {
  204. void (T::*f)(As...);
  205. template <int... I>
  206. void apply(Variant *ret, T *obj, Variant **args, __Sequence<I...>) {
  207. (obj->*f)(_ArgCast<As>::_arg_cast(*args[I])...);
  208. }
  209. };
  210. template <class T, class R, class... As>
  211. godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args) {
  212. godot_variant v;
  213. godot::api->godot_variant_new_nil(&v);
  214. T *obj = (T *)user_data;
  215. _WrappedMethod<T, R, As...> *method = (_WrappedMethod<T, R, As...> *)method_data;
  216. Variant *var = (Variant *)&v;
  217. Variant **arg = (Variant **)args;
  218. method->apply(var, obj, arg, typename __construct_sequence<sizeof...(As)>::type{});
  219. return v;
  220. }
  221. template <class T, class R, class... As>
  222. void *___make_wrapper_function(R (T::*f)(As...)) {
  223. using MethodType = _WrappedMethod<T, R, As...>;
  224. MethodType *p = (MethodType *)godot::api->godot_alloc(sizeof(MethodType));
  225. p->f = f;
  226. return (void *)p;
  227. }
  228. template <class T, class R, class... As>
  229. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(As...)) {
  230. return (__godot_wrapper_method)&__wrapped_method<T, R, As...>;
  231. }
  232. template <class T, class R, class... A>
  233. void *___make_wrapper_function(R (T::*f)(A...) const) {
  234. return ___make_wrapper_function((R(T::*)(A...))f);
  235. }
  236. template <class T, class R, class... A>
  237. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A...) const) {
  238. return ___get_wrapper_function((R(T::*)(A...))f);
  239. }
  240. template <class M>
  241. void register_method(const char *name, M method_ptr, godot_method_rpc_mode rpc_type = GODOT_METHOD_RPC_MODE_DISABLED) {
  242. godot_instance_method method = {};
  243. method.method_data = ___make_wrapper_function(method_ptr);
  244. method.free_func = godot::api->godot_free;
  245. method.method = (__godot_wrapper_method)___get_wrapper_function(method_ptr);
  246. godot_method_attributes attr = {};
  247. attr.rpc_type = rpc_type;
  248. godot::nativescript_api->godot_nativescript_register_method(godot::_RegisterState::nativescript_handle, ___get_method_class_name(method_ptr), name, attr, method);
  249. }
  250. // User can specify a derived class D to register the method for, instead of it being inferred.
  251. template <class D, class B, class R, class... As>
  252. void register_method_explicit(const char *name, R (B::*method_ptr)(As...), godot_method_rpc_mode rpc_type = GODOT_METHOD_RPC_MODE_DISABLED) {
  253. static_assert(std::is_base_of<B, D>::value, "Explicit class must derive from method class");
  254. register_method(name, static_cast<R (D::*)(As...)>(method_ptr), rpc_type);
  255. }
  256. template <class T, class P>
  257. struct _PropertySetFunc {
  258. void (T::*f)(P);
  259. static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant *value) {
  260. _PropertySetFunc<T, P> *set_func = (_PropertySetFunc<T, P> *)method_data;
  261. T *obj = (T *)user_data;
  262. Variant *v = (Variant *)value;
  263. (obj->*(set_func->f))(_ArgCast<P>::_arg_cast(*v));
  264. }
  265. };
  266. template <class T, class P>
  267. struct _PropertyGetFunc {
  268. P(T::*f)
  269. ();
  270. static godot_variant _wrapped_getter(godot_object *object, void *method_data, void *user_data) {
  271. _PropertyGetFunc<T, P> *get_func = (_PropertyGetFunc<T, P> *)method_data;
  272. T *obj = (T *)user_data;
  273. godot_variant var;
  274. godot::api->godot_variant_new_nil(&var);
  275. Variant *v = (Variant *)&var;
  276. *v = (obj->*(get_func->f))();
  277. return var;
  278. }
  279. };
  280. template <class T, class P>
  281. struct _PropertyDefaultSetFunc {
  282. P(T::*f);
  283. static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant *value) {
  284. _PropertyDefaultSetFunc<T, P> *set_func = (_PropertyDefaultSetFunc<T, P> *)method_data;
  285. T *obj = (T *)user_data;
  286. Variant *v = (Variant *)value;
  287. (obj->*(set_func->f)) = _ArgCast<P>::_arg_cast(*v);
  288. }
  289. };
  290. template <class T, class P>
  291. struct _PropertyDefaultGetFunc {
  292. P(T::*f);
  293. static godot_variant _wrapped_getter(godot_object *object, void *method_data, void *user_data) {
  294. _PropertyDefaultGetFunc<T, P> *get_func = (_PropertyDefaultGetFunc<T, P> *)method_data;
  295. T *obj = (T *)user_data;
  296. godot_variant var;
  297. godot::api->godot_variant_new_nil(&var);
  298. Variant *v = (Variant *)&var;
  299. *v = (obj->*(get_func->f));
  300. return var;
  301. }
  302. };
  303. template <class T, class P>
  304. void register_property(const char *name, P(T::*var), P default_value, godot_method_rpc_mode rpc_mode = GODOT_METHOD_RPC_MODE_DISABLED, godot_property_usage_flags usage = GODOT_PROPERTY_USAGE_DEFAULT, godot_property_hint hint = GODOT_PROPERTY_HINT_NONE, String hint_string = "") {
  305. Variant def_val = default_value;
  306. usage = (godot_property_usage_flags)((int)usage | GODOT_PROPERTY_USAGE_SCRIPT_VARIABLE);
  307. if (def_val.get_type() == Variant::OBJECT) {
  308. Object *o = detail::get_wrapper<Object>(def_val.operator godot_object *());
  309. if (o && o->is_class("Resource")) {
  310. hint = (godot_property_hint)((int)hint | GODOT_PROPERTY_HINT_RESOURCE_TYPE);
  311. hint_string = o->get_class();
  312. }
  313. }
  314. godot_string *_hint_string = (godot_string *)&hint_string;
  315. godot_property_attributes attr = {};
  316. if (def_val.get_type() == Variant::NIL) {
  317. attr.type = Variant::OBJECT;
  318. } else {
  319. attr.type = def_val.get_type();
  320. attr.default_value = *(godot_variant *)&def_val;
  321. }
  322. attr.hint = hint;
  323. attr.rset_type = rpc_mode;
  324. attr.usage = usage;
  325. attr.hint_string = *_hint_string;
  326. _PropertyDefaultSetFunc<T, P> *wrapped_set = (_PropertyDefaultSetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertyDefaultSetFunc<T, P>));
  327. wrapped_set->f = var;
  328. _PropertyDefaultGetFunc<T, P> *wrapped_get = (_PropertyDefaultGetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertyDefaultGetFunc<T, P>));
  329. wrapped_get->f = var;
  330. godot_property_set_func set_func = {};
  331. set_func.method_data = (void *)wrapped_set;
  332. set_func.free_func = godot::api->godot_free;
  333. set_func.set_func = &_PropertyDefaultSetFunc<T, P>::_wrapped_setter;
  334. godot_property_get_func get_func = {};
  335. get_func.method_data = (void *)wrapped_get;
  336. get_func.free_func = godot::api->godot_free;
  337. get_func.get_func = &_PropertyDefaultGetFunc<T, P>::_wrapped_getter;
  338. godot::nativescript_api->godot_nativescript_register_property(godot::_RegisterState::nativescript_handle, T::___get_type_name(), name, &attr, set_func, get_func);
  339. }
  340. template <class T, class P>
  341. void register_property(const char *name, void (T::*setter)(P), P (T::*getter)(), P default_value, godot_method_rpc_mode rpc_mode = GODOT_METHOD_RPC_MODE_DISABLED, godot_property_usage_flags usage = GODOT_PROPERTY_USAGE_DEFAULT, godot_property_hint hint = GODOT_PROPERTY_HINT_NONE, String hint_string = "") {
  342. Variant def_val = default_value;
  343. godot_string *_hint_string = (godot_string *)&hint_string;
  344. godot_property_attributes attr = {};
  345. if (def_val.get_type() == Variant::NIL) {
  346. attr.type = Variant::OBJECT;
  347. } else {
  348. attr.type = def_val.get_type();
  349. attr.default_value = *(godot_variant *)&def_val;
  350. }
  351. attr.hint = hint;
  352. attr.rset_type = rpc_mode;
  353. attr.usage = usage;
  354. attr.hint_string = *_hint_string;
  355. _PropertySetFunc<T, P> *wrapped_set = (_PropertySetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertySetFunc<T, P>));
  356. wrapped_set->f = setter;
  357. _PropertyGetFunc<T, P> *wrapped_get = (_PropertyGetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertyGetFunc<T, P>));
  358. wrapped_get->f = getter;
  359. godot_property_set_func set_func = {};
  360. set_func.method_data = (void *)wrapped_set;
  361. set_func.free_func = godot::api->godot_free;
  362. set_func.set_func = &_PropertySetFunc<T, P>::_wrapped_setter;
  363. godot_property_get_func get_func = {};
  364. get_func.method_data = (void *)wrapped_get;
  365. get_func.free_func = godot::api->godot_free;
  366. get_func.get_func = &_PropertyGetFunc<T, P>::_wrapped_getter;
  367. godot::nativescript_api->godot_nativescript_register_property(godot::_RegisterState::nativescript_handle, T::___get_type_name(), name, &attr, set_func, get_func);
  368. }
  369. template <class T, class P>
  370. void register_property(const char *name, void (T::*setter)(P), P (T::*getter)() const, P default_value, godot_method_rpc_mode rpc_mode = GODOT_METHOD_RPC_MODE_DISABLED, godot_property_usage_flags usage = GODOT_PROPERTY_USAGE_DEFAULT, godot_property_hint hint = GODOT_PROPERTY_HINT_NONE, String hint_string = "") {
  371. register_property(name, setter, (P(T::*)())getter, default_value, rpc_mode, usage, hint, hint_string);
  372. }
  373. template <class T>
  374. void register_signal(String name, Dictionary args = Dictionary()) {
  375. godot_signal signal = {};
  376. signal.name = *(godot_string *)&name;
  377. signal.num_args = args.size();
  378. signal.num_default_args = 0;
  379. // Need to check because malloc(0) is platform-dependent. Zero arguments will leave args to nullptr.
  380. if (signal.num_args != 0) {
  381. signal.args = (godot_signal_argument *)godot::api->godot_alloc(sizeof(godot_signal_argument) * signal.num_args);
  382. memset((void *)signal.args, 0, sizeof(godot_signal_argument) * signal.num_args);
  383. }
  384. for (int i = 0; i < signal.num_args; i++) {
  385. // Array entry = args[i];
  386. // String name = entry[0];
  387. String name = args.keys()[i];
  388. godot_string *_key = (godot_string *)&name;
  389. godot::api->godot_string_new_copy(&signal.args[i].name, _key);
  390. // if (entry.size() > 1) {
  391. // signal.args[i].type = entry[1];
  392. // }
  393. signal.args[i].type = args.values()[i];
  394. }
  395. godot::nativescript_api->godot_nativescript_register_signal(godot::_RegisterState::nativescript_handle, T::___get_type_name(), &signal);
  396. for (int i = 0; i < signal.num_args; i++) {
  397. godot::api->godot_string_destroy(&signal.args[i].name);
  398. }
  399. if (signal.args) {
  400. godot::api->godot_free(signal.args);
  401. }
  402. }
  403. template <class T, class... Args>
  404. void register_signal(String name, Args... varargs) {
  405. register_signal<T>(name, Dictionary::make(varargs...));
  406. }
  407. #ifndef GODOT_CPP_NO_OBJECT_CAST
  408. template <class T>
  409. T *Object::cast_to(const Object *obj) {
  410. if (!obj)
  411. return nullptr;
  412. if (T::___CLASS_IS_SCRIPT) {
  413. size_t have_tag = (size_t)godot::nativescript_1_1_api->godot_nativescript_get_type_tag(obj->_owner);
  414. if (have_tag) {
  415. if (!godot::_TagDB::is_type_known((size_t)have_tag)) {
  416. have_tag = 0;
  417. }
  418. }
  419. if (!have_tag) {
  420. have_tag = obj->_type_tag;
  421. }
  422. if (godot::_TagDB::is_type_compatible(T::___get_id(), have_tag)) {
  423. return detail::get_custom_class_instance<T>(obj);
  424. }
  425. } else {
  426. if (godot::core_1_2_api->godot_object_cast_to(obj->_owner, (void *)T::___get_id())) {
  427. return (T *)obj;
  428. }
  429. }
  430. return nullptr;
  431. }
  432. #endif
  433. } // namespace godot
  434. #endif // GODOT_H