Godot.hpp 18 KB

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