Godot.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. #ifndef GODOT_H
  2. #define GODOT_H
  3. #include <cstdlib>
  4. #include <godot.h>
  5. #include <godot_cpp/core/CoreTypes.hpp>
  6. #include <godot_cpp/core/Variant.hpp>
  7. namespace godot {
  8. #if !defined(_WIN32)
  9. #define GD_EXPORT
  10. #else
  11. #define GD_EXPORT __declspec(dllexport)
  12. #endif
  13. #define GODOT_DLSCRIPT_INIT(arg) extern "C" void GD_EXPORT godot_dlscript_init(arg)
  14. #define GODOT_DLSCRIPT_TERMINATE(arg) extern "C" void GD_EXPORT godot_dlscript_terminate(arg)
  15. #define GODOT_CLASS(Name, Base) \
  16. public: inline static char *___get_type_name() { return (char *) #Name; } \
  17. inline static char *___get_base_type_name() { return (char *) #Base; } \
  18. Base *self; \
  19. inline Name(godot_object *o) { self = (Base *) o; } \
  20. private:
  21. // inline Name(Object o) { this = (Name *) godot_dlinstance_get_userdata(o.__core_object);\
  22. // memcpy(this, p, sizeof(Name)); } \
  23. // inline Name(const Variant& obj) { __core_object = ((Object) obj).__core_object; } \
  24. template<class T>
  25. T *as(Object *obj)
  26. {
  27. return (T *) godot_dlinstance_get_userdata(obj);
  28. }
  29. // instance and destroy funcs
  30. template<class T>
  31. void *_godot_class_instance_func(godot_object *p, void *method_data)
  32. {
  33. T *d = new T(p);
  34. d->_init();
  35. return d;
  36. }
  37. template<class T>
  38. void _godot_class_destroy_func(godot_object *p, void *method_data, void *data)
  39. {
  40. T *d = (T *) data;
  41. delete d;
  42. }
  43. template<class T>
  44. void register_class()
  45. {
  46. godot_instance_create_func create = {};
  47. create.create_func = _godot_class_instance_func<T>;
  48. godot_instance_destroy_func destroy = {};
  49. destroy.destroy_func = _godot_class_destroy_func<T>;
  50. godot_script_register_class(T::___get_type_name(), T::___get_base_type_name(), create, destroy);
  51. T::_register_methods();
  52. }
  53. template<class T>
  54. void register_tool_class()
  55. {
  56. godot_instance_create_func create = {};
  57. create.create_func = _godot_class_instance_func<T>;
  58. godot_instance_destroy_func destroy = {};
  59. destroy.destroy_func = _godot_class_destroy_func<T>;
  60. godot_script_register_tool_class(T::___get_type_name(), T::___get_base_type_name(), create, destroy);
  61. T::_register_methods();
  62. }
  63. // method registering
  64. typedef godot_variant (*__godot_wrapper_method)(godot_object *, void *, void *, int, godot_variant **);
  65. template<class T, class R, class ...args>
  66. char *___get_method_class_name(R (T::*p)(args... a))
  67. {
  68. return T::___get_type_name();
  69. }
  70. // wohooo, let the fun begin.
  71. template<class T, class R>
  72. struct _WrappedMethod0 {
  73. R (T::*f)();
  74. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  75. {
  76. godot_variant v;
  77. godot_variant_new_nil(&v);
  78. T *obj = (T *) user_data;
  79. _WrappedMethod0<T, R> *method = (_WrappedMethod0<T, R>*) method_data;
  80. Variant *var = (Variant *) &v;
  81. *var = (obj->*(method->f))();
  82. return v;
  83. }
  84. };
  85. template<class T>
  86. struct _WrappedMethod0<T, void> {
  87. void (T::*f)();
  88. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  89. {
  90. godot_variant v;
  91. godot_variant_new_nil(&v);
  92. T *obj = (T *) user_data;
  93. _WrappedMethod0<T, void> *method = (_WrappedMethod0<T, void>*) method_data;
  94. (obj->*(method->f))();
  95. return v;
  96. }
  97. };
  98. template<class T, class R>
  99. void *___make_wrapper_function(R (T::*f)())
  100. {
  101. _WrappedMethod0<T, R> *p = (_WrappedMethod0<T, R> *) malloc(sizeof(_WrappedMethod0<T, R>));
  102. p->f = f;
  103. return (void *) p;
  104. }
  105. template<class T, class R>
  106. __godot_wrapper_method ___get_wrapper_function(R (T::*f)())
  107. {
  108. return (__godot_wrapper_method) &_WrappedMethod0<T, R>::__wrapped_method;
  109. }
  110. template<class T, class R, class A0>
  111. struct _WrappedMethod1 {
  112. R (T::*f)(A0);
  113. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  114. {
  115. godot_variant v;
  116. godot_variant_new_nil(&v);
  117. T *obj = (T *) user_data;
  118. _WrappedMethod1<T, R, A0> *method = (_WrappedMethod1<T, R, A0>*) method_data;
  119. Variant *var = (Variant *) &v;
  120. Variant **arg = (Variant **) args;
  121. *var = (obj->*(method->f))(*arg[0]);
  122. return v;
  123. }
  124. };
  125. template<class T, class A0>
  126. struct _WrappedMethod1<T, void, A0> {
  127. void (T::*f)(A0);
  128. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  129. {
  130. godot_variant v;
  131. godot_variant_new_nil(&v);
  132. T *obj = (T *) user_data;
  133. _WrappedMethod1<T, void, A0> *method = (_WrappedMethod1<T, void, A0>*) method_data;
  134. Variant **arg = (Variant **) args;
  135. (obj->*(method->f))(*arg[0]);
  136. return v;
  137. }
  138. };
  139. template<class T, class R, class A0>
  140. void *___make_wrapper_function(R (T::*f)(A0))
  141. {
  142. _WrappedMethod1<T, R, A0> *p = (_WrappedMethod1<T, R, A0> *) malloc(sizeof(_WrappedMethod1<T, R, A0>));
  143. p->f = f;
  144. return (void *) p;
  145. }
  146. template<class T, class R, class A0>
  147. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0))
  148. {
  149. return (__godot_wrapper_method) &_WrappedMethod1<T, R, A0>::__wrapped_method;
  150. }
  151. template<class T, class R, class A0, class A1>
  152. struct _WrappedMethod2 {
  153. R (T::*f)(A0, A1);
  154. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  155. {
  156. godot_variant v;
  157. godot_variant_new_nil(&v);
  158. T *obj = (T *) user_data;
  159. _WrappedMethod2<T, R, A0, A1> *method = (_WrappedMethod2<T, R, A0, A1>*) method_data;
  160. Variant *var = (Variant *) &v;
  161. Variant **arg = (Variant **) args;
  162. *var = (obj->*(method->f))(*arg[0], *arg[1]);
  163. return v;
  164. }
  165. };
  166. template<class T, class A0, class A1>
  167. struct _WrappedMethod2<T, void, A0, A1> {
  168. void (T::*f)(A0, A1);
  169. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  170. {
  171. godot_variant v;
  172. godot_variant_new_nil(&v);
  173. T *obj = (T *) user_data;
  174. _WrappedMethod2<T, void, A0, A1> *method = (_WrappedMethod2<T, void, A0, A1>*) method_data;
  175. Variant **arg = (Variant **) args;
  176. (obj->*(method->f))(*arg[0], *arg[1]);
  177. return v;
  178. }
  179. };
  180. template<class T, class R, class A0, class A1>
  181. void *___make_wrapper_function(R (T::*f)(A0, A1))
  182. {
  183. _WrappedMethod2<T, R, A0, A1> *p = (_WrappedMethod2<T, R, A0, A1> *) malloc(sizeof(_WrappedMethod2<T, R, A0, A1>));
  184. p->f = f;
  185. return (void *) p;
  186. }
  187. template<class T, class R, class A0, class A1>
  188. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1))
  189. {
  190. return (__godot_wrapper_method) &_WrappedMethod2<T, R, A0, A1>::__wrapped_method;
  191. }
  192. template<class T, class R, class A0, class A1, class A2>
  193. struct _WrappedMethod3 {
  194. R (T::*f)(A0, A1, A2);
  195. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  196. {
  197. godot_variant v;
  198. godot_variant_new_nil(&v);
  199. T *obj = (T *) user_data;
  200. _WrappedMethod3<T, R, A0, A1, A2> *method = (_WrappedMethod3<T, R, A0, A1, A2>*) method_data;
  201. Variant *var = (Variant *) &v;
  202. Variant **arg = (Variant **) args;
  203. *var = (obj->*(method->f))(*arg[0], *arg[1], *arg[2]);
  204. return v;
  205. }
  206. };
  207. template<class T, class A0, class A1, class A2>
  208. struct _WrappedMethod3<T, void, A0, A1, A2> {
  209. void (T::*f)(A0, A1, A2);
  210. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  211. {
  212. godot_variant v;
  213. godot_variant_new_nil(&v);
  214. T *obj = (T *) user_data;
  215. _WrappedMethod3<T, void, A0, A1, A2> *method = (_WrappedMethod3<T, void, A0, A1, A2>*) method_data;
  216. Variant **arg = (Variant **) args;
  217. (obj->*(method->f))(*arg[0], *arg[1], *arg[2]);
  218. return v;
  219. }
  220. };
  221. template<class T, class R, class A0, class A1, class A2>
  222. void *___make_wrapper_function(R (T::*f)(A0, A1, A2))
  223. {
  224. _WrappedMethod3<T, R, A0, A1, A2> *p = (_WrappedMethod3<T, R, A0, A1, A2> *) malloc(sizeof(_WrappedMethod3<T, R, A0, A1, A2>));
  225. p->f = f;
  226. return (void *) p;
  227. }
  228. template<class T, class R, class A0, class A1, class A2>
  229. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1, A2))
  230. {
  231. return (__godot_wrapper_method) &_WrappedMethod3<T, R, A0, A1, A2>::__wrapped_method;
  232. }
  233. template<class T, class R, class A0, class A1, class A2, class A3>
  234. struct _WrappedMethod4 {
  235. R (T::*f)(A0, A1, A2, A3);
  236. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  237. {
  238. godot_variant v;
  239. godot_variant_new_nil(&v);
  240. T *obj = (T *) user_data;
  241. _WrappedMethod4<T, R, A0, A1, A2, A3> *method = (_WrappedMethod4<T, R, A0, A1, A2, A3>*) method_data;
  242. Variant *var = (Variant *) &v;
  243. Variant **arg = (Variant **) args;
  244. *var = (obj->*(method->f))(*arg[0], *arg[1], *arg[2], *arg[3]);
  245. return v;
  246. }
  247. };
  248. template<class T, class A0, class A1, class A2, class A3>
  249. struct _WrappedMethod4<T, void, A0, A1, A2, A3> {
  250. void (T::*f)(A0, A1, A2, A3);
  251. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  252. {
  253. godot_variant v;
  254. godot_variant_new_nil(&v);
  255. T *obj = (T *) user_data;
  256. _WrappedMethod4<T, void, A0, A1, A2, A3> *method = (_WrappedMethod4<T, void, A0, A1, A2, A3>*) method_data;
  257. Variant **arg = (Variant **) args;
  258. (obj->*(method->f))(*arg[0], *arg[1], *arg[2], *arg[3]);
  259. return v;
  260. }
  261. };
  262. template<class T, class R, class A0, class A1, class A2, class A3>
  263. void *___make_wrapper_function(R (T::*f)(A0, A1, A2, A3))
  264. {
  265. _WrappedMethod4<T, R, A0, A1, A2, A3> *p = (_WrappedMethod4<T, R, A0, A1, A2, A3> *) malloc(sizeof(_WrappedMethod4<T, R, A0, A1, A2, A3>));
  266. p->f = f;
  267. return (void *) p;
  268. }
  269. template<class T, class R, class A0, class A1, class A2, class A3>
  270. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1, A2, A3))
  271. {
  272. return (__godot_wrapper_method) &_WrappedMethod4<T, R, A0, A1, A2, A3>::__wrapped_method;
  273. }
  274. template<class T, class R, class A0, class A1, class A2, class A3, class A4>
  275. struct _WrappedMethod5 {
  276. R (T::*f)(A0, A1, A2, A3, A4);
  277. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  278. {
  279. godot_variant v;
  280. godot_variant_new_nil(&v);
  281. T *obj = (T *) user_data;
  282. _WrappedMethod5<T, R, A0, A1, A2, A3, A4> *method = (_WrappedMethod5<T, R, A0, A1, A2, A3, A4>*) method_data;
  283. Variant *var = (Variant *) &v;
  284. Variant **arg = (Variant **) args;
  285. *var = (obj->*(method->f))(*arg[0], *arg[1], *arg[2], *arg[3], *arg[4]);
  286. return v;
  287. }
  288. };
  289. template<class T, class A0, class A1, class A2, class A3, class A4>
  290. struct _WrappedMethod5<T, void, A0, A1, A2, A3, A4> {
  291. void (T::*f)(A0, A1, A2, A3, A4);
  292. static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args)
  293. {
  294. godot_variant v;
  295. godot_variant_new_nil(&v);
  296. T *obj = (T *) user_data;
  297. _WrappedMethod5<T, void, A0, A1, A2, A3, A4> *method = (_WrappedMethod5<T, void, A0, A1, A2, A3, A4>*) method_data;
  298. Variant **arg = (Variant **) args;
  299. (obj->*(method->f))(*arg[0], *arg[1], *arg[2], *arg[3], *arg[4]);
  300. return v;
  301. }
  302. };
  303. template<class T, class R, class A0, class A1, class A2, class A3, class A4>
  304. void *___make_wrapper_function(R (T::*f)(A0, A1, A2, A3, A4))
  305. {
  306. _WrappedMethod5<T, R, A0, A1, A2, A3, A4> *p = (_WrappedMethod5<T, R, A0, A1, A2, A3, A4> *) malloc(sizeof(_WrappedMethod5<T, R, A0, A1, A2, A3, A4>));
  307. p->f = f;
  308. return (void *) p;
  309. }
  310. template<class T, class R, class A0, class A1, class A2, class A3, class A4>
  311. __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1, A2, A3, A4))
  312. {
  313. return (__godot_wrapper_method) &_WrappedMethod5<T, R, A0, A1, A2, A3, A4>::__wrapped_method;
  314. }
  315. template<class M>
  316. void register_method(char *name, M method_ptr, godot_method_rpc_mode rpc_type = GODOT_METHOD_RPC_MODE_DISABLED)
  317. {
  318. godot_instance_method method = {};
  319. method.method_data = ___make_wrapper_function(method_ptr);
  320. method.free_func = free;
  321. method.method = (__godot_wrapper_method) ___get_wrapper_function(method_ptr);
  322. godot_method_attributes attr = {};
  323. attr.rpc_type = rpc_type;
  324. godot_script_register_method(___get_method_class_name(method_ptr), name, attr, method);
  325. }
  326. template<class T, class P>
  327. struct _PropertySetFunc {
  328. void (T::*f)(P);
  329. static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant value)
  330. {
  331. _PropertySetFunc<T, P> *set_func = (_PropertySetFunc<T, P> *) method_data;
  332. T *obj = (T *) user_data;
  333. Variant *v = (Variant *) &value;
  334. (obj->*(set_func->f))(*v);
  335. }
  336. };
  337. template<class T, class P>
  338. struct _PropertyGetFunc {
  339. P (T::*f)();
  340. static godot_variant _wrapped_getter(godot_object *object, void *method_data, void *user_data)
  341. {
  342. _PropertyGetFunc<T, P> *get_func = (_PropertyGetFunc<T, P> *) method_data;
  343. T *obj = (T *) user_data;
  344. godot_variant var;
  345. godot_variant_new_nil(&var);
  346. Variant *v = (Variant *) &var;
  347. *v = (obj->*(get_func->f))();
  348. return var;
  349. }
  350. };
  351. template<class T, class P>
  352. void register_property(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)
  353. {
  354. Variant def_val = default_value;
  355. godot_property_attributes attr = {};
  356. attr.type = def_val.get_type();
  357. attr.default_value = *(godot_variant *) &def_val;
  358. attr.hint = hint;
  359. attr.rset_type = rpc_mode;
  360. attr.usage = usage;
  361. _PropertySetFunc<T, P> *wrapped_set = (_PropertySetFunc<T, P> *) malloc(sizeof(_PropertySetFunc<T, P>));
  362. wrapped_set->f = setter;
  363. _PropertyGetFunc<T, P> *wrapped_get = (_PropertyGetFunc<T, P> *) malloc(sizeof(_PropertyGetFunc<T, P>));
  364. wrapped_get->f = getter;
  365. godot_property_set_func set_func = {};
  366. set_func.method_data = (void *) wrapped_set;
  367. set_func.free_func = free;
  368. set_func.set_func = &_PropertySetFunc<T, P>::_wrapped_setter;
  369. godot_property_get_func get_func = {};
  370. get_func.method_data = (void *) wrapped_get;
  371. get_func.free_func = free;
  372. get_func.get_func = &_PropertyGetFunc<T, P>::_wrapped_getter;
  373. godot_script_register_property(T::___get_type_name(), name, &attr, set_func, get_func);
  374. }
  375. }
  376. #endif // GODOT_H