Godot.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*************************************************************************/
  2. /* Godot.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef GODOT_HPP
  31. #define GODOT_HPP
  32. #include <cstdlib>
  33. #include <cstring>
  34. #include <gdnative_api_struct.gen.h>
  35. #include <nativescript/godot_nativescript.h>
  36. #include <typeinfo>
  37. #include "CoreTypes.hpp"
  38. #include "Ref.hpp"
  39. #include "TagDB.hpp"
  40. #include "Variant.hpp"
  41. #include "Object.hpp"
  42. #include "GodotGlobal.hpp"
  43. #include <type_traits>
  44. namespace godot {
  45. namespace detail {
  46. // Godot classes are wrapped by heap-allocated instances mimicking them through the C API.
  47. // They all inherit `_Wrapped`.
  48. template <class T>
  49. T *get_wrapper(godot_object *obj) {
  50. return (T *)godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, obj);
  51. }
  52. // Custom class instances are not obtainable by just casting the pointer to the base class they inherit,
  53. // partly because in Godot, scripts are not instances of the classes themselves, they are only attached to them.
  54. // Yet we want to "fake" it as if they were the same entity.
  55. template <class T>
  56. T *get_custom_class_instance(const Object *obj) {
  57. return (obj) ? (T *)godot::nativescript_api->godot_nativescript_get_userdata(obj->_owner) : nullptr;
  58. }
  59. template <class T>
  60. inline T *create_custom_class_instance() {
  61. // Usually, script instances hold a reference to their NativeScript resource.
  62. // that resource is obtained from a `.gdns` file, which in turn exists because
  63. // of the resource system of Godot. We can't cleanly hardcode that here,
  64. // so the easiest for now (though not really clean) is to create new resource instances,
  65. // individually attached to the script instances.
  66. // We cannot use wrappers because of https://github.com/godotengine/godot/issues/39181
  67. // godot::NativeScript *script = godot::NativeScript::_new();
  68. // script->set_library(get_wrapper<godot::GDNativeLibrary>((godot_object *)godot::gdnlib));
  69. // script->set_class_name(T::___get_class_name());
  70. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  71. // So we use the C API directly.
  72. static godot_class_constructor script_constructor = godot::api->godot_get_class_constructor("NativeScript");
  73. static godot_method_bind *mb_set_library = godot::api->godot_method_bind_get_method("NativeScript", "set_library");
  74. static godot_method_bind *mb_set_class_name = godot::api->godot_method_bind_get_method("NativeScript", "set_class_name");
  75. godot_object *script = script_constructor();
  76. {
  77. const void *args[] = { godot::gdnlib };
  78. godot::api->godot_method_bind_ptrcall(mb_set_library, script, args, nullptr);
  79. }
  80. {
  81. const String class_name = T::___get_class_name();
  82. const void *args[] = { &class_name };
  83. godot::api->godot_method_bind_ptrcall(mb_set_class_name, script, args, nullptr);
  84. }
  85. // Now to instanciate T, we initially did this, however in case of Reference it returns a variant with refcount
  86. // already initialized, which woud cause inconsistent behavior compared to other classes (we still have to return a pointer).
  87. //Variant instance_variant = script->new_();
  88. //T *instance = godot::get_custom_class_instance<T>(instance_variant);
  89. // So we should do this instead, however while convenient, it uses unnecessary wrapper objects.
  90. // Object *base_obj = T::___new_godot_base();
  91. // base_obj->set_script(script);
  92. // return get_custom_class_instance<T>(base_obj);
  93. // Again using the C API to do exactly what we have to do.
  94. static godot_class_constructor base_constructor = godot::api->godot_get_class_constructor(T::___get_godot_class_name());
  95. static godot_method_bind *mb_set_script = godot::api->godot_method_bind_get_method("Object", "set_script");
  96. godot_object *base_obj = base_constructor();
  97. {
  98. const void *args[] = { script };
  99. godot::api->godot_method_bind_ptrcall(mb_set_script, base_obj, args, nullptr);
  100. }
  101. return (T *)godot::nativescript_api->godot_nativescript_get_userdata(base_obj);
  102. }
  103. } // namespace detail
  104. // Used in the definition of a custom class.
  105. //
  106. // Name: Name of your class, without namespace
  107. // Base: Name of the direct base class, with namespace if necessary
  108. //
  109. // ___get_class_name: Name of the class
  110. // ___get_godot_class_name: Name of the Godot base class this class inherits from (i.e not direct)
  111. // _new: Creates a new instance of the class
  112. // ___get_id: Gets the unique ID of the class. Godot and custom classes are both within that set.
  113. // ___get_base_id: Gets the ID of the direct base class, as returned by ___get_id
  114. // ___get_base_class_name: Name of the direct base class
  115. // ___get_from_variant: Converts a Variant into an Object*. Will be non-null if the class matches.
  116. #define GODOT_CLASS(Name, Base) \
  117. \
  118. public: \
  119. inline static const char *___get_class_name() { return #Name; } \
  120. enum { ___CLASS_IS_SCRIPT = 1 }; \
  121. inline static const char *___get_godot_class_name() { \
  122. return Base::___get_godot_class_name(); \
  123. } \
  124. inline static Name *_new() { \
  125. return godot::detail::create_custom_class_instance<Name>(); \
  126. } \
  127. inline static size_t ___get_id() { return typeid(Name).hash_code(); } \
  128. inline static size_t ___get_base_id() { return Base::___get_id(); } \
  129. inline static const char *___get_base_class_name() { return Base::___get_class_name(); } \
  130. inline static godot::Object *___get_from_variant(godot::Variant a) { \
  131. return (godot::Object *)godot::detail::get_custom_class_instance<Name>( \
  132. godot::Object::___get_from_variant(a)); \
  133. } \
  134. \
  135. private:
  136. // Legacy compatibility
  137. #define GODOT_SUBCLASS(Name, Base) GODOT_CLASS(Name, Base)
  138. template <class T>
  139. struct _ArgCast {
  140. static T _arg_cast(Variant a) {
  141. return a;
  142. }
  143. };
  144. template <class T>
  145. struct _ArgCast<T *> {
  146. static T *_arg_cast(Variant a) {
  147. return (T *)T::___get_from_variant(a);
  148. }
  149. };
  150. template <>
  151. struct _ArgCast<Variant> {
  152. static Variant _arg_cast(Variant a) {
  153. return a;
  154. }
  155. };
  156. // instance and destroy funcs
  157. template <class T>
  158. void *_godot_class_instance_func(godot_object *p, void * /*method_data*/) {
  159. T *d = new T();
  160. d->_owner = p;
  161. d->_type_tag = typeid(T).hash_code();
  162. d->_init();
  163. return d;
  164. }
  165. template <class T>
  166. void _godot_class_destroy_func(godot_object * /*p*/, void * /*method_data*/, void *data) {
  167. T *d = (T *)data;
  168. delete d;
  169. }
  170. template <class T>
  171. void register_class() {
  172. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  173. godot_instance_create_func create = {};
  174. create.create_func = _godot_class_instance_func<T>;
  175. godot_instance_destroy_func destroy = {};
  176. destroy.destroy_func = _godot_class_destroy_func<T>;
  177. _TagDB::register_type(T::___get_id(), T::___get_base_id());
  178. godot::nativescript_api->godot_nativescript_register_class(godot::_RegisterState::nativescript_handle,
  179. T::___get_class_name(), T::___get_base_class_name(), create, destroy);
  180. godot::nativescript_1_1_api->godot_nativescript_set_type_tag(godot::_RegisterState::nativescript_handle,
  181. T::___get_class_name(), (const void *)T::___get_id());
  182. T::_register_methods();
  183. }
  184. template <class T>
  185. void register_tool_class() {
  186. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  187. godot_instance_create_func create = {};
  188. create.create_func = _godot_class_instance_func<T>;
  189. godot_instance_destroy_func destroy = {};
  190. destroy.destroy_func = _godot_class_destroy_func<T>;
  191. _TagDB::register_type(T::___get_id(), T::___get_base_id());
  192. godot::nativescript_api->godot_nativescript_register_tool_class(godot::_RegisterState::nativescript_handle,
  193. T::___get_class_name(), T::___get_base_class_name(), create, destroy);
  194. godot::nativescript_1_1_api->godot_nativescript_set_type_tag(godot::_RegisterState::nativescript_handle,
  195. T::___get_class_name(), (const void *)T::___get_id());
  196. T::_register_methods();
  197. }
  198. // method registering
  199. typedef godot_variant (*__godot_wrapper_method)(godot_object *, void *, void *, int, godot_variant **);
  200. template <class T, class R, class... args>
  201. const char *___get_method_class_name(R (T::*p)(args... a)) {
  202. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  203. (void)p; // To avoid "unused parameter" warnings. `p` is required for template matching.
  204. return T::___get_class_name();
  205. }
  206. // This second version is also required to match constant functions
  207. template <class T, class R, class... args>
  208. const char *___get_method_class_name(R (T::*p)(args... a) const) {
  209. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  210. (void)p; // To avoid "unused parameter" warnings. `p` is required for template matching.
  211. return T::___get_class_name();
  212. }
  213. // Okay, time for some template magic.
  214. // Many thanks to manpat from the GDL Discord Server.
  215. // This is stuff that's available in C++14 I think, but whatever.
  216. template <int... I>
  217. struct __Sequence {};
  218. template <int N, int... I>
  219. struct __construct_sequence {
  220. using type = typename __construct_sequence<N - 1, N - 1, I...>::type;
  221. };
  222. template <int... I>
  223. struct __construct_sequence<0, I...> {
  224. using type = __Sequence<I...>;
  225. };
  226. // Now the wrapping part.
  227. template <class T, class R, class... As>
  228. struct _WrappedMethod {
  229. R(T::*f)
  230. (As...);
  231. template <int... I>
  232. void apply(Variant *ret, T *obj, Variant **args, __Sequence<I...>) {
  233. *ret = (obj->*f)(_ArgCast<As>::_arg_cast(*args[I])...);
  234. }
  235. };
  236. template <class T, class... As>
  237. struct _WrappedMethod<T, void, As...> {
  238. void (T::*f)(As...);
  239. template <int... I>
  240. void apply(Variant * /*ret*/, T *obj, Variant **args, __Sequence<I...>) {
  241. (obj->*f)(_ArgCast<As>::_arg_cast(*args[I])...);
  242. }
  243. };
  244. template <class T, class R, class... As>
  245. godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int /*num_args*/, godot_variant **args) {
  246. godot_variant v;
  247. godot::api->godot_variant_new_nil(&v);
  248. T *obj = (T *)user_data;
  249. _WrappedMethod<T, R, As...> *method = (_WrappedMethod<T, R, As...> *)method_data;
  250. Variant *var = (Variant *)&v;
  251. Variant **arg = (Variant **)args;
  252. method->apply(var, obj, arg, typename __construct_sequence<sizeof...(As)>::type{});
  253. return v;
  254. }
  255. template <class T, class R, class... As>
  256. void *___make_wrapper_function(R (T::*f)(As...)) {
  257. using MethodType = _WrappedMethod<T, R, As...>;
  258. MethodType *p = (MethodType *)godot::api->godot_alloc(sizeof(MethodType));
  259. p->f = f;
  260. return (void *)p;
  261. }
  262. template <class T, class R, class... As>
  263. __godot_wrapper_method ___get_wrapper_function(R (T::* /*f*/)(As...)) {
  264. return (__godot_wrapper_method)&__wrapped_method<T, R, As...>;
  265. }
  266. template <class T, class R, class... A>
  267. void *___make_wrapper_function(R (T::*f)(A...) const) {
  268. return ___make_wrapper_function((R(T::*)(A...))f);
  269. }
  270. template <class T, class R, class... A>
  271. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A...) const) {
  272. return ___get_wrapper_function((R(T::*)(A...))f);
  273. }
  274. template <class M>
  275. void register_method(const char *name, M method_ptr, godot_method_rpc_mode rpc_type = GODOT_METHOD_RPC_MODE_DISABLED) {
  276. godot_instance_method method = {};
  277. method.method_data = ___make_wrapper_function(method_ptr);
  278. method.free_func = godot::api->godot_free;
  279. method.method = (__godot_wrapper_method)___get_wrapper_function(method_ptr);
  280. godot_method_attributes attr = {};
  281. attr.rpc_type = rpc_type;
  282. godot::nativescript_api->godot_nativescript_register_method(godot::_RegisterState::nativescript_handle,
  283. ___get_method_class_name(method_ptr), name, attr, method);
  284. }
  285. // User can specify a derived class D to register the method for, instead of it being inferred.
  286. template <class D, class B, class R, class... As>
  287. void register_method_explicit(const char *name, R (B::*method_ptr)(As...),
  288. godot_method_rpc_mode rpc_type = GODOT_METHOD_RPC_MODE_DISABLED) {
  289. static_assert(std::is_base_of<B, D>::value, "Explicit class must derive from method class");
  290. register_method(name, static_cast<R (D::*)(As...)>(method_ptr), rpc_type);
  291. }
  292. template <class T, class P>
  293. struct _PropertySetFunc {
  294. void (T::*f)(P);
  295. static void _wrapped_setter(godot_object * /*object*/, void *method_data, void *user_data, godot_variant *value) {
  296. _PropertySetFunc<T, P> *set_func = (_PropertySetFunc<T, P> *)method_data;
  297. T *obj = (T *)user_data;
  298. Variant *v = (Variant *)value;
  299. (obj->*(set_func->f))(_ArgCast<P>::_arg_cast(*v));
  300. }
  301. };
  302. template <class T, class P>
  303. struct _PropertyGetFunc {
  304. P(T::*f)
  305. ();
  306. static godot_variant _wrapped_getter(godot_object * /*object*/, void *method_data, void *user_data) {
  307. _PropertyGetFunc<T, P> *get_func = (_PropertyGetFunc<T, P> *)method_data;
  308. T *obj = (T *)user_data;
  309. godot_variant var;
  310. godot::api->godot_variant_new_nil(&var);
  311. Variant *v = (Variant *)&var;
  312. *v = (obj->*(get_func->f))();
  313. return var;
  314. }
  315. };
  316. template <class T, class P>
  317. struct _PropertyDefaultSetFunc {
  318. P(T::*f);
  319. static void _wrapped_setter(godot_object * /*object*/, void *method_data, void *user_data, godot_variant *value) {
  320. _PropertyDefaultSetFunc<T, P> *set_func = (_PropertyDefaultSetFunc<T, P> *)method_data;
  321. T *obj = (T *)user_data;
  322. Variant *v = (Variant *)value;
  323. (obj->*(set_func->f)) = _ArgCast<P>::_arg_cast(*v);
  324. }
  325. };
  326. template <class T, class P>
  327. struct _PropertyDefaultGetFunc {
  328. P(T::*f);
  329. static godot_variant _wrapped_getter(godot_object * /*object*/, void *method_data, void *user_data) {
  330. _PropertyDefaultGetFunc<T, P> *get_func = (_PropertyDefaultGetFunc<T, P> *)method_data;
  331. T *obj = (T *)user_data;
  332. godot_variant var;
  333. godot::api->godot_variant_new_nil(&var);
  334. Variant *v = (Variant *)&var;
  335. *v = (obj->*(get_func->f));
  336. return var;
  337. }
  338. };
  339. template <class T, class P>
  340. void register_property(const char *name, P(T::*var), P default_value,
  341. godot_method_rpc_mode rpc_mode = GODOT_METHOD_RPC_MODE_DISABLED,
  342. godot_property_usage_flags usage = GODOT_PROPERTY_USAGE_DEFAULT,
  343. godot_property_hint hint = GODOT_PROPERTY_HINT_NONE, String hint_string = "") {
  344. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  345. Variant def_val = default_value;
  346. usage = (godot_property_usage_flags)((int)usage | GODOT_PROPERTY_USAGE_SCRIPT_VARIABLE);
  347. if (def_val.get_type() == Variant::OBJECT) {
  348. Object *o = detail::get_wrapper<Object>(def_val.operator godot_object *());
  349. if (o && o->is_class("Resource")) {
  350. hint = (godot_property_hint)((int)hint | GODOT_PROPERTY_HINT_RESOURCE_TYPE);
  351. hint_string = o->get_class();
  352. }
  353. }
  354. godot_string *_hint_string = (godot_string *)&hint_string;
  355. godot_property_attributes attr = {};
  356. if (def_val.get_type() == Variant::NIL) {
  357. attr.type = Variant::OBJECT;
  358. } else {
  359. attr.type = def_val.get_type();
  360. attr.default_value = *(godot_variant *)&def_val;
  361. }
  362. attr.hint = hint;
  363. attr.rset_type = rpc_mode;
  364. attr.usage = usage;
  365. attr.hint_string = *_hint_string;
  366. _PropertyDefaultSetFunc<T, P> *wrapped_set =
  367. (_PropertyDefaultSetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertyDefaultSetFunc<T, P>));
  368. wrapped_set->f = var;
  369. _PropertyDefaultGetFunc<T, P> *wrapped_get =
  370. (_PropertyDefaultGetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertyDefaultGetFunc<T, P>));
  371. wrapped_get->f = var;
  372. godot_property_set_func set_func = {};
  373. set_func.method_data = (void *)wrapped_set;
  374. set_func.free_func = godot::api->godot_free;
  375. set_func.set_func = &_PropertyDefaultSetFunc<T, P>::_wrapped_setter;
  376. godot_property_get_func get_func = {};
  377. get_func.method_data = (void *)wrapped_get;
  378. get_func.free_func = godot::api->godot_free;
  379. get_func.get_func = &_PropertyDefaultGetFunc<T, P>::_wrapped_getter;
  380. godot::nativescript_api->godot_nativescript_register_property(godot::_RegisterState::nativescript_handle,
  381. T::___get_class_name(), name, &attr, set_func, get_func);
  382. }
  383. template <class T, class P>
  384. void register_property(const char *name, void (T::*setter)(P), P (T::*getter)(), P default_value,
  385. godot_method_rpc_mode rpc_mode = GODOT_METHOD_RPC_MODE_DISABLED,
  386. godot_property_usage_flags usage = GODOT_PROPERTY_USAGE_DEFAULT,
  387. godot_property_hint hint = GODOT_PROPERTY_HINT_NONE, String hint_string = "") {
  388. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  389. Variant def_val = default_value;
  390. godot_string *_hint_string = (godot_string *)&hint_string;
  391. godot_property_attributes attr = {};
  392. if (def_val.get_type() == Variant::NIL) {
  393. attr.type = Variant::OBJECT;
  394. } else {
  395. attr.type = def_val.get_type();
  396. attr.default_value = *(godot_variant *)&def_val;
  397. }
  398. attr.hint = hint;
  399. attr.rset_type = rpc_mode;
  400. attr.usage = usage;
  401. attr.hint_string = *_hint_string;
  402. _PropertySetFunc<T, P> *wrapped_set = (_PropertySetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertySetFunc<T, P>));
  403. wrapped_set->f = setter;
  404. _PropertyGetFunc<T, P> *wrapped_get = (_PropertyGetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertyGetFunc<T, P>));
  405. wrapped_get->f = getter;
  406. godot_property_set_func set_func = {};
  407. set_func.method_data = (void *)wrapped_set;
  408. set_func.free_func = godot::api->godot_free;
  409. set_func.set_func = &_PropertySetFunc<T, P>::_wrapped_setter;
  410. godot_property_get_func get_func = {};
  411. get_func.method_data = (void *)wrapped_get;
  412. get_func.free_func = godot::api->godot_free;
  413. get_func.get_func = &_PropertyGetFunc<T, P>::_wrapped_getter;
  414. godot::nativescript_api->godot_nativescript_register_property(godot::_RegisterState::nativescript_handle,
  415. T::___get_class_name(), name, &attr, set_func, get_func);
  416. }
  417. template <class T, class P>
  418. void register_property(const char *name, void (T::*setter)(P), P (T::*getter)() const, P default_value,
  419. godot_method_rpc_mode rpc_mode = GODOT_METHOD_RPC_MODE_DISABLED,
  420. godot_property_usage_flags usage = GODOT_PROPERTY_USAGE_DEFAULT,
  421. godot_property_hint hint = GODOT_PROPERTY_HINT_NONE, String hint_string = "") {
  422. register_property(name, setter, (P(T::*)())getter, default_value, rpc_mode, usage, hint, hint_string);
  423. }
  424. template <class T>
  425. void register_signal(String name, Dictionary args) {
  426. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  427. godot_signal signal = {};
  428. signal.name = *(godot_string *)&name;
  429. signal.num_args = args.size();
  430. signal.num_default_args = 0;
  431. // Need to check because malloc(0) is platform-dependent. Zero arguments will leave args to nullptr.
  432. if (signal.num_args != 0) {
  433. signal.args = (godot_signal_argument *)godot::api->godot_alloc(sizeof(godot_signal_argument) * signal.num_args);
  434. memset((void *)signal.args, 0, sizeof(godot_signal_argument) * signal.num_args);
  435. }
  436. for (int i = 0; i < signal.num_args; i++) {
  437. // Array entry = args[i];
  438. // String name = entry[0];
  439. String name = args.keys()[i];
  440. godot_string *_key = (godot_string *)&name;
  441. godot::api->godot_string_new_copy(&signal.args[i].name, _key);
  442. // if (entry.size() > 1) {
  443. // signal.args[i].type = entry[1];
  444. // }
  445. signal.args[i].type = args.values()[i];
  446. }
  447. godot::nativescript_api->godot_nativescript_register_signal(godot::_RegisterState::nativescript_handle,
  448. T::___get_class_name(), &signal);
  449. for (int i = 0; i < signal.num_args; i++) {
  450. godot::api->godot_string_destroy(&signal.args[i].name);
  451. }
  452. if (signal.args) {
  453. godot::api->godot_free(signal.args);
  454. }
  455. }
  456. template <class T, class... Args>
  457. void register_signal(String name, Args... varargs) {
  458. register_signal<T>(name, Dictionary::make(varargs...));
  459. }
  460. template <class T>
  461. void register_signal(String name) {
  462. static_assert(T::___CLASS_IS_SCRIPT, "This function must only be used on custom classes");
  463. godot_signal signal = {};
  464. signal.name = *(godot_string *)&name;
  465. godot::nativescript_api->godot_nativescript_register_signal(godot::_RegisterState::nativescript_handle,
  466. T::___get_class_name(), &signal);
  467. }
  468. #ifndef GODOT_CPP_NO_OBJECT_CAST
  469. template <class T>
  470. T *Object::cast_to(const Object *obj) {
  471. if (!obj)
  472. return nullptr;
  473. if (T::___CLASS_IS_SCRIPT) {
  474. size_t have_tag = (size_t)godot::nativescript_1_1_api->godot_nativescript_get_type_tag(obj->_owner);
  475. if (have_tag) {
  476. if (!godot::_TagDB::is_type_known((size_t)have_tag)) {
  477. have_tag = 0;
  478. }
  479. }
  480. if (!have_tag) {
  481. have_tag = obj->_type_tag;
  482. }
  483. if (godot::_TagDB::is_type_compatible(T::___get_id(), have_tag)) {
  484. return detail::get_custom_class_instance<T>(obj);
  485. }
  486. } else {
  487. if (godot::core_1_2_api->godot_object_cast_to(obj->_owner, (void *)T::___get_id())) {
  488. return (T *)obj;
  489. }
  490. }
  491. return nullptr;
  492. }
  493. #endif
  494. } // namespace godot
  495. #endif // GODOT_HPP