Godot.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 "CoreTypes.hpp"
  8. #include "Variant.hpp"
  9. #include "Ref.hpp"
  10. #include "Object.hpp"
  11. #include "GodotGlobal.hpp"
  12. namespace godot {
  13. template<class T>
  14. T *as(Object *obj)
  15. {
  16. return (T *) godot::nativescript_api->godot_nativescript_get_userdata(obj);
  17. }
  18. template<class T>
  19. class GodotScript {
  20. public:
  21. T *owner;
  22. // GodotScript() {}
  23. void _init() {}
  24. static const char *___get_base_type_name()
  25. {
  26. return T::___get_class_name();
  27. }
  28. static GodotScript<T> *___get_from_variant(Variant a)
  29. {
  30. return as<GodotScript<T> >((Object *) a);
  31. }
  32. static void _register_methods() {}
  33. };
  34. #define GODOT_CLASS(Name) \
  35. public: inline static const char *___get_type_name() { return static_cast<const char *>(#Name); } \
  36. private:
  37. #define GODOT_SUBCLASS(Name, Base) \
  38. public: inline static const char *___get_type_name() { return static_cast<const char *>(#Name); } \
  39. inline static const char *___get_base_type_name() { return static_cast<const char *>(#Base); } \
  40. private:
  41. template<class T>
  42. struct _ArgCast {
  43. static T _arg_cast(Variant a)
  44. {
  45. return a;
  46. }
  47. };
  48. template<class T>
  49. struct _ArgCast<T*> {
  50. static T *_arg_cast(Variant a)
  51. {
  52. return (T *) T::___get_from_variant(a);
  53. }
  54. };
  55. template<>
  56. struct _ArgCast<Variant> {
  57. static Variant _arg_cast(Variant a)
  58. {
  59. return a;
  60. }
  61. };
  62. // instance and destroy funcs
  63. template<class T>
  64. void *_godot_class_instance_func(godot_object *p, void *method_data)
  65. {
  66. T *d = new T();
  67. *(godot_object **) &d->owner = p;
  68. d->_init();
  69. return d;
  70. }
  71. template<class T>
  72. void _godot_class_destroy_func(godot_object *p, void *method_data, void *data)
  73. {
  74. T *d = (T *) data;
  75. delete d;
  76. }
  77. template<class T>
  78. void register_class()
  79. {
  80. godot_instance_create_func create = {};
  81. create.create_func = _godot_class_instance_func<T>;
  82. godot_instance_destroy_func destroy = {};
  83. destroy.destroy_func = _godot_class_destroy_func<T>;
  84. godot::nativescript_api->godot_nativescript_register_class(godot::_RegisterState::nativescript_handle, T::___get_type_name(), T::___get_base_type_name(), create, destroy);
  85. T::_register_methods();
  86. }
  87. template<class T>
  88. void register_tool_class()
  89. {
  90. godot_instance_create_func create = {};
  91. create.create_func = _godot_class_instance_func<T>;
  92. godot_instance_destroy_func destroy = {};
  93. destroy.destroy_func = _godot_class_destroy_func<T>;
  94. godot::nativescript_api->godot_nativescript_register_tool_class(godot::_RegisterState::nativescript_handle, T::___get_type_name(), T::___get_base_type_name(), create, destroy);
  95. T::_register_methods();
  96. }
  97. // method registering
  98. typedef godot_variant (*__godot_wrapper_method)(godot_object *, void *, void *, int, godot_variant **);
  99. template<class T, class R, class ...args>
  100. const char *___get_method_class_name(R (T::*p)(args... a))
  101. {
  102. return T::___get_type_name();
  103. }
  104. template<class T, class R, class ...args>
  105. const char *___get_method_class_name(R (T::*p)(args... a) const)
  106. {
  107. return T::___get_type_name();
  108. }
  109. // Okay, time for some template magic.
  110. // Many thanks to manpat from the GDL Discord Server.
  111. // This is stuff that's available in C++14 I think, but whatever.
  112. template<int... I>
  113. struct __Sequence{};
  114. template<int N, int... I>
  115. struct __construct_sequence {
  116. using type = typename __construct_sequence<N-1, N-1, I...>::type;
  117. };
  118. template<int... I>
  119. struct __construct_sequence<0, I...> {
  120. using type = __Sequence<I...>;
  121. };
  122. // Now the wrapping part.
  123. template<class T, class R, class... As>
  124. struct _WrappedMethod {
  125. R (T::*f)(As...);
  126. template<int... I>
  127. void apply(Variant* ret, T* obj, Variant** args, __Sequence<I...>) {
  128. *ret = (obj->*f)( _ArgCast<As>::_arg_cast(*args[I])... );
  129. }
  130. };
  131. template<class T, class... As>
  132. struct _WrappedMethod<T, void, As...> {
  133. void (T::*f)(As...);
  134. template<int... I>
  135. void apply(Variant* ret, T* obj, Variant** args, __Sequence<I...>) {
  136. (obj->*f)( _ArgCast<As>::_arg_cast(*args[I])... );
  137. }
  138. };
  139. template<class T, class R, class... As>
  140. godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  141. {
  142. godot_variant v;
  143. godot::api->godot_variant_new_nil(&v);
  144. T *obj = (T *) user_data;
  145. _WrappedMethod<T, R, As...> *method = (_WrappedMethod<T, R, As...>*) method_data;
  146. Variant *var = (Variant *) &v;
  147. Variant **arg = (Variant **) args;
  148. method->apply(var, obj, arg, typename __construct_sequence<sizeof...(As)>::type {});
  149. return v;
  150. }
  151. template<class T, class R, class... As>
  152. void *___make_wrapper_function(R (T::*f)(As...))
  153. {
  154. using MethodType = _WrappedMethod<T, R, As...>;
  155. MethodType *p = (MethodType *) godot::api->godot_alloc(sizeof(MethodType));
  156. p->f = f;
  157. return (void *) p;
  158. }
  159. template<class T, class R, class... As>
  160. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(As...))
  161. {
  162. return (__godot_wrapper_method) &__wrapped_method<T, R, As...>;
  163. }
  164. template<class T, class R, class ...A>
  165. void *___make_wrapper_function(R (T::*f)(A...) const)
  166. {
  167. return ___make_wrapper_function((R (T::*)(A...)) f);
  168. }
  169. template<class T, class R, class ...A>
  170. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A...) const)
  171. {
  172. return ___get_wrapper_function((R (T::*)(A...)) f);
  173. }
  174. template<class M>
  175. void register_method(const char *name, M method_ptr, godot_method_rpc_mode rpc_type = GODOT_METHOD_RPC_MODE_DISABLED)
  176. {
  177. godot_instance_method method = {};
  178. method.method_data = ___make_wrapper_function(method_ptr);
  179. method.free_func = godot::api->godot_free;
  180. method.method = (__godot_wrapper_method) ___get_wrapper_function(method_ptr);
  181. godot_method_attributes attr = {};
  182. attr.rpc_type = rpc_type;
  183. godot::nativescript_api->godot_nativescript_register_method(godot::_RegisterState::nativescript_handle, ___get_method_class_name(method_ptr), name, attr, method);
  184. }
  185. template<class T, class P>
  186. struct _PropertySetFunc {
  187. void (T::*f)(P);
  188. static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant *value)
  189. {
  190. _PropertySetFunc<T, P> *set_func = (_PropertySetFunc<T, P> *) method_data;
  191. T *obj = (T *) user_data;
  192. Variant *v = (Variant *) value;
  193. (obj->*(set_func->f))(_ArgCast<P>::_arg_cast(*v));
  194. }
  195. };
  196. template<class T, class P>
  197. struct _PropertyGetFunc {
  198. P (T::*f)();
  199. static godot_variant _wrapped_getter(godot_object *object, void *method_data, void *user_data)
  200. {
  201. _PropertyGetFunc<T, P> *get_func = (_PropertyGetFunc<T, P> *) method_data;
  202. T *obj = (T *) user_data;
  203. godot_variant var;
  204. godot::api->godot_variant_new_nil(&var);
  205. Variant *v = (Variant *) &var;
  206. *v = (obj->*(get_func->f))();
  207. return var;
  208. }
  209. };
  210. template<class T, class P>
  211. struct _PropertyDefaultSetFunc {
  212. P (T::*f);
  213. static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant *value)
  214. {
  215. _PropertyDefaultSetFunc<T, P> *set_func = (_PropertyDefaultSetFunc<T, P> *) method_data;
  216. T *obj = (T *) user_data;
  217. Variant *v = (Variant *) value;
  218. (obj->*(set_func->f)) = _ArgCast<P>::_arg_cast(*v);
  219. }
  220. };
  221. template<class T, class P>
  222. struct _PropertyDefaultGetFunc {
  223. P (T::*f);
  224. static godot_variant _wrapped_getter(godot_object *object, void *method_data, void *user_data)
  225. {
  226. _PropertyDefaultGetFunc<T, P> *get_func = (_PropertyDefaultGetFunc<T, P> *) method_data;
  227. T *obj = (T *) user_data;
  228. godot_variant var;
  229. godot::api->godot_variant_new_nil(&var);
  230. Variant *v = (Variant *) &var;
  231. *v = (obj->*(get_func->f));
  232. return var;
  233. }
  234. };
  235. template<class T, class P>
  236. 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 = "")
  237. {
  238. Variant def_val = default_value;
  239. usage = (godot_property_usage_flags) ((int) usage | GODOT_PROPERTY_USAGE_SCRIPT_VARIABLE);
  240. if (def_val.get_type() == Variant::OBJECT) {
  241. Object *o = def_val;
  242. if (o && o->is_class("Resource")) {
  243. hint = (godot_property_hint) ((int) hint | GODOT_PROPERTY_HINT_RESOURCE_TYPE);
  244. hint_string = o->get_class();
  245. }
  246. }
  247. godot_string *_hint_string = (godot_string*) &hint_string;
  248. godot_property_attributes attr = {};
  249. attr.type = def_val.get_type();
  250. attr.default_value = *(godot_variant *) &def_val;
  251. attr.hint = hint;
  252. attr.rset_type = rpc_mode;
  253. attr.usage = usage;
  254. attr.hint_string = *_hint_string;
  255. _PropertyDefaultSetFunc<T, P> *wrapped_set = (_PropertyDefaultSetFunc<T, P> *)godot::api->godot_alloc(sizeof(_PropertyDefaultSetFunc<T, P>));
  256. wrapped_set->f = var;
  257. _PropertyDefaultGetFunc<T, P> *wrapped_get = (_PropertyDefaultGetFunc<T, P> *) godot::api->godot_alloc(sizeof(_PropertyDefaultGetFunc<T, P>));
  258. wrapped_get->f = var;
  259. godot_property_set_func set_func = {};
  260. set_func.method_data = (void *) wrapped_set;
  261. set_func.free_func = godot::api->godot_free;
  262. set_func.set_func = &_PropertyDefaultSetFunc<T, P>::_wrapped_setter;
  263. godot_property_get_func get_func = {};
  264. get_func.method_data = (void *) wrapped_get;
  265. get_func.free_func = godot::api->godot_free;
  266. get_func.get_func = &_PropertyDefaultGetFunc<T, P>::_wrapped_getter;
  267. godot::nativescript_api->godot_nativescript_register_property(godot::_RegisterState::nativescript_handle, T::___get_type_name(), name, &attr, set_func, get_func);
  268. }
  269. template<class T, class P>
  270. 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 = "")
  271. {
  272. Variant def_val = default_value;
  273. godot_property_attributes attr = {};
  274. attr.type = def_val.get_type();
  275. attr.default_value = *(godot_variant *) &def_val;
  276. attr.hint = hint;
  277. attr.rset_type = rpc_mode;
  278. attr.usage = usage;
  279. _PropertySetFunc<T, P> *wrapped_set = (_PropertySetFunc<T, P> *) godot::api->godot_alloc(sizeof(_PropertySetFunc<T, P>));
  280. wrapped_set->f = setter;
  281. _PropertyGetFunc<T, P> *wrapped_get = (_PropertyGetFunc<T, P> *) godot::api->godot_alloc(sizeof(_PropertyGetFunc<T, P>));
  282. wrapped_get->f = getter;
  283. godot_property_set_func set_func = {};
  284. set_func.method_data = (void *) wrapped_set;
  285. set_func.free_func = godot::api->godot_free;
  286. set_func.set_func = &_PropertySetFunc<T, P>::_wrapped_setter;
  287. godot_property_get_func get_func = {};
  288. get_func.method_data = (void *) wrapped_get;
  289. get_func.free_func = godot::api->godot_free;
  290. get_func.get_func = &_PropertyGetFunc<T, P>::_wrapped_getter;
  291. godot::nativescript_api->godot_nativescript_register_property(godot::_RegisterState::nativescript_handle, T::___get_type_name(), name, &attr, set_func, get_func);
  292. }
  293. template<class T, class P>
  294. 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 = "")
  295. {
  296. register_property(name, setter, (P (T::*)()) getter, default_value, rpc_mode, usage, hint, hint_string);
  297. }
  298. template<class T>
  299. void register_signal(String name, Dictionary args = Dictionary())
  300. {
  301. godot_signal signal = {};
  302. signal.name = *(godot_string *)&name;
  303. signal.num_args = args.size();
  304. signal.num_default_args = 0;
  305. // Need to check because malloc(0) is platform-dependent. Zero arguments will leave args to nullptr.
  306. if(signal.num_args != 0) {
  307. signal.args = (godot_signal_argument*) godot::api->godot_alloc(sizeof(godot_signal_argument) * signal.num_args);
  308. memset((void *) signal.args, 0, sizeof(godot_signal_argument) * signal.num_args);
  309. }
  310. for (int i = 0; i < signal.num_args; i++) {
  311. // Array entry = args[i];
  312. // String name = entry[0];
  313. String name = args.keys()[i];
  314. godot_string *_key = (godot_string *)&name;
  315. godot::api->godot_string_new_copy(&signal.args[i].name, _key);
  316. // if (entry.size() > 1) {
  317. // signal.args[i].type = entry[1];
  318. // }
  319. signal.args[i].type = args.values()[i];
  320. }
  321. godot::nativescript_api->godot_nativescript_register_signal(godot::_RegisterState::nativescript_handle, T::___get_type_name(), &signal);
  322. for (int i = 0; i < signal.num_args; i++) {
  323. godot::api->godot_string_destroy(&signal.args[i].name);
  324. }
  325. if(signal.args) {
  326. godot::api->godot_free(signal.args);
  327. }
  328. }
  329. template<class T, class... Args>
  330. void register_signal(String name, Args... varargs)
  331. {
  332. register_signal<T>(name, Dictionary::make(varargs...));
  333. }
  334. }
  335. #endif // GODOT_H