#ifndef GODOT_HPP #define GODOT_HPP #include #include #include #include #include #include namespace godot { template class GodotScript { public: T *owner; // GodotScript() {} void _init() {} static char *___get_base_type_name() { return T::___get_class_name(); } static void _register_methods() {} }; #if !defined(_WIN32) #define GD_EXPORT #else #define GD_EXPORT __declspec(dllexport) #endif #define GODOT_NATIVE_INIT(arg) extern "C" void GD_EXPORT godot_native_init(arg) #define GODOT_NATIVE_TERMINATE(arg) extern "C" void GD_EXPORT godot_native_terminate(arg) #define GODOT_CLASS(Name) \ public: inline static char *___get_type_name() { return (char *) #Name; } \ private: #define GODOT_SUBCLASS(Name, Base) \ public: inline static char *___get_type_name() { return (char *) #Name; } \ inline static char *___get_base_type_name() { return (char *) #Base; } \ inline Name(godot_object *o) : Base(o) {} \ private: template struct _ArgCast { static T _arg_cast(Variant a) { return (T) a; } }; template struct _ArgCast { static T *_arg_cast(Variant a) { return (T *) ((Object *) a); } }; template T *as(Object *obj) { return (T *) godot_native_get_userdata(obj); } // instance and destroy funcs template void *_godot_class_instance_func(godot_object *p, void *method_data) { T *d = new T(); *(godot_object **) &d->owner = p; d->_init(); return d; } template void _godot_class_destroy_func(godot_object *p, void *method_data, void *data) { T *d = (T *) data; delete d; } template void register_class() { godot_instance_create_func create = {}; create.create_func = _godot_class_instance_func; godot_instance_destroy_func destroy = {}; destroy.destroy_func = _godot_class_destroy_func; godot_script_register_class(T::___get_type_name(), T::___get_base_type_name(), create, destroy); T::_register_methods(); } template void register_tool_class() { godot_instance_create_func create = {}; create.create_func = _godot_class_instance_func; godot_instance_destroy_func destroy = {}; destroy.destroy_func = _godot_class_destroy_func; godot_script_register_tool_class(T::___get_type_name(), T::___get_base_type_name(), create, destroy); T::_register_methods(); } // method registering typedef godot_variant (*__godot_wrapper_method)(godot_object *, void *, void *, int, godot_variant **); template char *___get_method_class_name(R (T::*p)(args... a)) { return T::___get_type_name(); } // wohooo, let the fun begin. template struct _WrappedMethod0 { R (T::*f)(); static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args) { godot_variant v; godot_variant_new_nil(&v); T *obj = (T *) user_data; _WrappedMethod0 *method = (_WrappedMethod0*) method_data; Variant *var = (Variant *) &v; *var = (obj->*(method->f))(); return v; } }; template struct _WrappedMethod0 { void (T::*f)(); static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args) { godot_variant v; godot_variant_new_nil(&v); T *obj = (T *) user_data; _WrappedMethod0 *method = (_WrappedMethod0*) method_data; (obj->*(method->f))(); return v; } }; template void *___make_wrapper_function(R (T::*f)()) { _WrappedMethod0 *p = (_WrappedMethod0 *) malloc(sizeof(_WrappedMethod0)); p->f = f; return (void *) p; } template __godot_wrapper_method ___get_wrapper_function(R (T::*f)()) { return (__godot_wrapper_method) &_WrappedMethod0::__wrapped_method; } template struct _WrappedMethod1 { R (T::*f)(A0); static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args) { godot_variant v; godot_variant_new_nil(&v); T *obj = (T *) user_data; _WrappedMethod1 *method = (_WrappedMethod1*) method_data; Variant *var = (Variant *) &v; Variant **arg = (Variant **) args; *var = (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0])); return v; } }; template struct _WrappedMethod1 { void (T::*f)(A0); static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args) { godot_variant v; godot_variant_new_nil(&v); T *obj = (T *) user_data; _WrappedMethod1 *method = (_WrappedMethod1*) method_data; Variant **arg = (Variant **) args; (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0])); return v; } }; template void *___make_wrapper_function(R (T::*f)(A0)) { _WrappedMethod1 *p = (_WrappedMethod1 *) malloc(sizeof(_WrappedMethod1)); p->f = f; return (void *) p; } template __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0)) { return (__godot_wrapper_method) &_WrappedMethod1::__wrapped_method; } template struct _WrappedMethod2 { R (T::*f)(A0, A1); static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args) { godot_variant v; godot_variant_new_nil(&v); T *obj = (T *) user_data; _WrappedMethod2 *method = (_WrappedMethod2*) method_data; Variant *var = (Variant *) &v; Variant **arg = (Variant **) args; *var = (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0]), _ArgCast::_arg_cast(*arg[1])); return v; } }; template struct _WrappedMethod2 { void (T::*f)(A0, A1); static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args) { godot_variant v; godot_variant_new_nil(&v); T *obj = (T *) user_data; _WrappedMethod2 *method = (_WrappedMethod2*) method_data; Variant **arg = (Variant **) args; (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0]), _ArgCast::_arg_cast(*arg[1])); return v; } }; template void *___make_wrapper_function(R (T::*f)(A0, A1)) { _WrappedMethod2 *p = (_WrappedMethod2 *) malloc(sizeof(_WrappedMethod2)); p->f = f; return (void *) p; } template __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1)) { return (__godot_wrapper_method) &_WrappedMethod2::__wrapped_method; } template struct _WrappedMethod3 { R (T::*f)(A0, A1, A2); static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args) { godot_variant v; godot_variant_new_nil(&v); T *obj = (T *) user_data; _WrappedMethod3 *method = (_WrappedMethod3*) method_data; Variant *var = (Variant *) &v; Variant **arg = (Variant **) args; *var = (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0]), _ArgCast::_arg_cast(*arg[1]), _ArgCast::_arg_cast(*arg[2])); return v; } }; template struct _WrappedMethod3 { void (T::*f)(A0, A1, A2); static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args) { godot_variant v; godot_variant_new_nil(&v); T *obj = (T *) user_data; _WrappedMethod3 *method = (_WrappedMethod3*) method_data; Variant **arg = (Variant **) args; (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0]), _ArgCast::_arg_cast(*arg[1]), _ArgCast::_arg_cast(*arg[2])); return v; } }; template void *___make_wrapper_function(R (T::*f)(A0, A1, A2)) { _WrappedMethod3 *p = (_WrappedMethod3 *) malloc(sizeof(_WrappedMethod3)); p->f = f; return (void *) p; } template __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1, A2)) { return (__godot_wrapper_method) &_WrappedMethod3::__wrapped_method; } template struct _WrappedMethod4 { R (T::*f)(A0, A1, A2, A3); static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args) { godot_variant v; godot_variant_new_nil(&v); T *obj = (T *) user_data; _WrappedMethod4 *method = (_WrappedMethod4*) method_data; Variant *var = (Variant *) &v; Variant **arg = (Variant **) args; *var = (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0]), _ArgCast::_arg_cast(*arg[1]), _ArgCast::_arg_cast(*arg[2]), _ArgCast::_arg_cast(*arg[3])); return v; } }; template struct _WrappedMethod4 { void (T::*f)(A0, A1, A2, A3); static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args) { godot_variant v; godot_variant_new_nil(&v); T *obj = (T *) user_data; _WrappedMethod4 *method = (_WrappedMethod4*) method_data; Variant **arg = (Variant **) args; (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0]), _ArgCast::_arg_cast(*arg[1]), _ArgCast::_arg_cast(*arg[2]), _ArgCast::_arg_cast(*arg[3])); return v; } }; template void *___make_wrapper_function(R (T::*f)(A0, A1, A2, A3)) { _WrappedMethod4 *p = (_WrappedMethod4 *) malloc(sizeof(_WrappedMethod4)); p->f = f; return (void *) p; } template __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1, A2, A3)) { return (__godot_wrapper_method) &_WrappedMethod4::__wrapped_method; } template struct _WrappedMethod5 { R (T::*f)(A0, A1, A2, A3, A4); static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args) { godot_variant v; godot_variant_new_nil(&v); T *obj = (T *) user_data; _WrappedMethod5 *method = (_WrappedMethod5*) method_data; Variant *var = (Variant *) &v; Variant **arg = (Variant **) args; *var = (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0]), _ArgCast::_arg_cast(*arg[1]), _ArgCast::_arg_cast(*arg[2]), _ArgCast::_arg_cast(*arg[3]), _ArgCast::_arg_cast(*arg[4])); return v; } }; template struct _WrappedMethod5 { void (T::*f)(A0, A1, A2, A3, A4); static godot_variant __wrapped_method(godot_object *, void *method_data, void *user_data, int num_args, godot_variant **args) { godot_variant v; godot_variant_new_nil(&v); T *obj = (T *) user_data; _WrappedMethod5 *method = (_WrappedMethod5*) method_data; Variant **arg = (Variant **) args; (obj->*(method->f))(_ArgCast::_arg_cast(*arg[0]), _ArgCast::_arg_cast(*arg[1]), _ArgCast::_arg_cast(*arg[2]), _ArgCast::_arg_cast(*arg[3]), _ArgCast::_arg_cast(*arg[4])); return v; } }; template void *___make_wrapper_function(R (T::*f)(A0, A1, A2, A3, A4)) { _WrappedMethod5 *p = (_WrappedMethod5 *) malloc(sizeof(_WrappedMethod5)); p->f = f; return (void *) p; } template __godot_wrapper_method ___get_wrapper_function(R (T::*f)(A0, A1, A2, A3, A4)) { return (__godot_wrapper_method) &_WrappedMethod5::__wrapped_method; } template void register_method(char *name, M method_ptr, godot_method_rpc_mode rpc_type = GODOT_METHOD_RPC_MODE_DISABLED) { godot_instance_method method = {}; method.method_data = ___make_wrapper_function(method_ptr); method.free_func = free; method.method = (__godot_wrapper_method) ___get_wrapper_function(method_ptr); godot_method_attributes attr = {}; attr.rpc_type = rpc_type; godot_script_register_method(___get_method_class_name(method_ptr), name, attr, method); } template struct _PropertySetFunc { void (T::*f)(P); static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant value) { _PropertySetFunc *set_func = (_PropertySetFunc *) method_data; T *obj = (T *) user_data; Variant *v = (Variant *) &value; (obj->*(set_func->f))(_ArgCast

::_arg_cast(*v)); } }; template struct _PropertyGetFunc { P (T::*f)(); static godot_variant _wrapped_getter(godot_object *object, void *method_data, void *user_data) { _PropertyGetFunc *get_func = (_PropertyGetFunc *) method_data; T *obj = (T *) user_data; godot_variant var; godot_variant_new_nil(&var); Variant *v = (Variant *) &var; *v = (obj->*(get_func->f))(); return var; } }; template struct _PropertyDefaultSetFunc { P (T::*f); static void _wrapped_setter(godot_object *object, void *method_data, void *user_data, godot_variant value) { _PropertyDefaultSetFunc *set_func = (_PropertyDefaultSetFunc *) method_data; T *obj = (T *) user_data; Variant *v = (Variant *) &value; (obj->*(set_func->f)) = _ArgCast

::_arg_cast(*v); } }; template struct _PropertyDefaultGetFunc { P (T::*f); static godot_variant _wrapped_getter(godot_object *object, void *method_data, void *user_data) { _PropertyDefaultGetFunc *get_func = (_PropertyDefaultGetFunc *) method_data; T *obj = (T *) user_data; godot_variant var; godot_variant_new_nil(&var); Variant *v = (Variant *) &var; *v = (obj->*(get_func->f)); return var; } }; template void register_property(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 = "") { Variant def_val = default_value; usage = (godot_property_usage_flags) ((int) usage | GODOT_PROPERTY_USAGE_SCRIPT_VARIABLE); if (def_val.get_type() == Variant::OBJECT) { Object *o = def_val; if (o && o->is_class("Resource")) { hint = (godot_property_hint) ((int) hint | GODOT_PROPERTY_HINT_RESOURCE_TYPE); hint_string = o->get_class(); } } godot_string *_hint_string = (godot_string*) &hint_string; godot_property_attributes attr = {}; attr.type = def_val.get_type(); attr.default_value = *(godot_variant *) &def_val; attr.hint = hint; attr.rset_type = rpc_mode; attr.usage = usage; attr.hint_string = *_hint_string; _PropertyDefaultSetFunc *wrapped_set = (_PropertyDefaultSetFunc *) malloc(sizeof(_PropertyDefaultSetFunc)); wrapped_set->f = var; _PropertyDefaultGetFunc *wrapped_get = (_PropertyDefaultGetFunc *) malloc(sizeof(_PropertyDefaultGetFunc)); wrapped_get->f = var; godot_property_set_func set_func = {}; set_func.method_data = (void *) wrapped_set; set_func.free_func = free; set_func.set_func = &_PropertyDefaultSetFunc::_wrapped_setter; godot_property_get_func get_func = {}; get_func.method_data = (void *) wrapped_get; get_func.free_func = free; get_func.get_func = &_PropertyDefaultGetFunc::_wrapped_getter; godot_script_register_property(T::___get_type_name(), name, &attr, set_func, get_func); } template 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, String hint_string = "") { Variant def_val = default_value; godot_property_attributes attr = {}; attr.type = def_val.get_type(); attr.default_value = *(godot_variant *) &def_val; attr.hint = hint; attr.rset_type = rpc_mode; attr.usage = usage; _PropertySetFunc *wrapped_set = (_PropertySetFunc *) malloc(sizeof(_PropertySetFunc)); wrapped_set->f = setter; _PropertyGetFunc *wrapped_get = (_PropertyGetFunc *) malloc(sizeof(_PropertyGetFunc)); wrapped_get->f = getter; godot_property_set_func set_func = {}; set_func.method_data = (void *) wrapped_set; set_func.free_func = free; set_func.set_func = &_PropertySetFunc::_wrapped_setter; godot_property_get_func get_func = {}; get_func.method_data = (void *) wrapped_get; get_func.free_func = free; get_func.get_func = &_PropertyGetFunc::_wrapped_getter; godot_script_register_property(T::___get_type_name(), name, &attr, set_func, get_func); } template void register_signal(String name, Dictionary args = Dictionary()) { godot_signal signal = {}; signal.name = *(godot_string *)&name; signal.num_args = args.size(); signal.num_default_args = 0; signal.args = (godot_signal_argument*) godot_alloc(sizeof(godot_signal_argument) * signal.num_args); memset((void *) signal.args, 0, sizeof(godot_signal_argument) * signal.num_args); for (int i = 0; i < signal.num_args; i++) { // Array entry = args[i]; // String name = entry[0]; String name = args.keys()[i]; godot_string *_key = (godot_string *)&name; godot_string_new(&signal.args[i].name); godot_string_copy_string(&signal.args[i].name, _key); // if (entry.size() > 1) { // signal.args[i].type = entry[1]; // } signal.args[i].type = args.values()[i]; } godot_script_register_signal(T::___get_type_name(), &signal); for (int i = 0; i < signal.num_args; i++) { godot_string_destroy(&signal.args[i].name); } } } #endif // GODOT_H