/*************************************************************************/ /* class_db.hpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #ifndef CLASS_DB_HPP #define CLASS_DB_HPP #include #include #include #include #include #include #include #include #include #include namespace godot { #define DEFVAL(m_defval) (m_defval) struct MethodDefinition { const char *name = nullptr; std::list args; MethodDefinition() {} MethodDefinition(const char *p_name) : name(p_name) {} }; MethodDefinition D_METHOD(const char *p_name); MethodDefinition D_METHOD(const char *p_name, const char *p_arg1); template MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, Args... args) { MethodDefinition md = D_METHOD(p_name, args...); md.args.push_front(p_arg1); return md; } class ClassDB { static GDNativeInitializationLevel current_level; friend class godot::GDExtensionBinding; public: struct PropertySetGet { int index; const char *setter; const char *getter; MethodBind *_setptr; MethodBind *_getptr; Variant::Type type; }; struct ClassInfo { const char *name = nullptr; const char *parent_name = nullptr; GDNativeInitializationLevel level = GDNATIVE_INITIALIZATION_SCENE; std::unordered_map method_map; std::set signal_names; std::unordered_map virtual_methods; std::set property_names; std::set constant_names; // Pointer to the parent custom class, if any. Will be null if the parent class is a Godot class. ClassInfo *parent_ptr = nullptr; }; private: // This may only contain custom classes, not Godot classes static std::unordered_map classes; static MethodBind *bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const void **p_defs, int p_defcount); static void initialize_class(const ClassInfo &cl); static void bind_method_godot(const char *p_class_name, MethodBind *p_method); static _FORCE_INLINE_ char *_alloc_and_copy_cstr(const char *p_str) { size_t size = strlen(p_str) + 1; char *ret = reinterpret_cast(memalloc(size)); memcpy(ret, p_str, size); return ret; } public: template static void register_class(); template static MethodBind *bind_method(N p_method_name, M p_method, VarArgs... p_args); template static MethodBind *bind_static_method(const char *p_class, N p_method_name, M p_method, VarArgs... p_args); template static MethodBind *bind_vararg_method(uint32_t p_flags, const char *p_name, M p_method, const MethodInfo &p_info = MethodInfo(), const std::vector &p_default_args = std::vector{}, bool p_return_nil_is_variant = true); static void add_property_group(const char *p_class, const char *p_name, const char *p_prefix); static void add_property_subgroup(const char *p_class, const char *p_name, const char *p_prefix); static void add_property(const char *p_class, const PropertyInfo &p_pinfo, const char *p_setter, const char *p_getter, int p_index = -1); static void add_signal(const char *p_class, const MethodInfo &p_signal); static void bind_integer_constant(const char *p_class_name, const char *p_enum_name, const char *p_constant_name, GDNativeInt p_constant_value, bool p_is_bitfield = false); static void bind_virtual_method(const char *p_class, const char *p_method, GDNativeExtensionClassCallVirtual p_call); static MethodBind *get_method(const char *p_class, const char *p_method); static GDNativeExtensionClassCallVirtual get_virtual_func(void *p_userdata, const char *p_name); static void initialize(GDNativeInitializationLevel p_level); static void deinitialize(GDNativeInitializationLevel p_level); }; #define BIND_CONSTANT(m_constant) \ godot::ClassDB::bind_integer_constant(get_class_static(), "", #m_constant, m_constant); #define BIND_ENUM_CONSTANT(m_constant) \ godot::ClassDB::bind_integer_constant(get_class_static(), godot::__constant_get_enum_name(m_constant, #m_constant), #m_constant, m_constant); #define BIND_BITFIELD_FLAG(m_constant) \ godot::ClassDB::bind_integer_constant(get_class_static(), godot::__constant_get_bitfield_name(m_constant, #m_constant), #m_constant, m_constant, true); #define BIND_VIRTUAL_METHOD(m_class, m_method) \ { \ auto ___call##m_method = [](GDNativeObjectPtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr p_ret) -> void { \ call_with_ptr_args(reinterpret_cast(p_instance), &m_class::m_method, p_args, p_ret); \ }; \ godot::ClassDB::bind_virtual_method(m_class::get_class_static(), #m_method, ___call##m_method); \ } template void ClassDB::register_class() { // Register this class within our plugin ClassInfo cl; cl.name = T::get_class_static(); cl.parent_name = T::get_parent_class_static(); cl.level = current_level; std::unordered_map::iterator parent_it = classes.find(cl.parent_name); if (parent_it != classes.end()) { // Assign parent if it is also a custom class cl.parent_ptr = &parent_it->second; } classes[cl.name] = cl; // Register this class with Godot GDNativeExtensionClassCreationInfo class_info = { T::set_bind, // GDNativeExtensionClassSet set_func; T::get_bind, // GDNativeExtensionClassGet get_func; T::get_property_list_bind, // GDNativeExtensionClassGetPropertyList get_property_list_func; T::free_property_list_bind, // GDNativeExtensionClassFreePropertyList free_property_list_func; T::property_can_revert_bind, // GDNativeExtensionClassPropertyCanRevert property_can_revert_func; T::property_get_revert_bind, // GDNativeExtensionClassPropertyGetRevert property_get_revert_func; T::notification_bind, // GDNativeExtensionClassNotification notification_func; T::to_string_bind, // GDNativeExtensionClassToString to_string_func; nullptr, // GDNativeExtensionClassReference reference_func; nullptr, // GDNativeExtensionClassUnreference unreference_func; T::create, // GDNativeExtensionClassCreateInstance create_instance_func; /* this one is mandatory */ T::free, // GDNativeExtensionClassFreeInstance free_instance_func; /* this one is mandatory */ &ClassDB::get_virtual_func, // GDNativeExtensionClassGetVirtual get_virtual_func; nullptr, // GDNativeExtensionClassGetRID get_rid; (void *)cl.name, // void *class_userdata; }; internal::gdn_interface->classdb_register_extension_class(internal::library, cl.name, cl.parent_name, &class_info); // call bind_methods etc. to register all members of the class T::initialize_class(); // now register our class within ClassDB within Godot initialize_class(classes[cl.name]); } template MethodBind *ClassDB::bind_method(N p_method_name, M p_method, VarArgs... p_args) { Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported. const Variant *argptrs[sizeof...(p_args) + 1]; for (uint32_t i = 0; i < sizeof...(p_args); i++) { argptrs[i] = &args[i]; } MethodBind *bind = create_method_bind(p_method); return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, sizeof...(p_args) == 0 ? nullptr : (const void **)argptrs, sizeof...(p_args)); } template MethodBind *ClassDB::bind_static_method(const char *p_class, N p_method_name, M p_method, VarArgs... p_args) { Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported. const Variant *argptrs[sizeof...(p_args) + 1]; for (uint32_t i = 0; i < sizeof...(p_args); i++) { argptrs[i] = &args[i]; } MethodBind *bind = create_static_method_bind(p_method); bind->set_instance_class(p_class); return bind_methodfi(0, bind, p_method_name, sizeof...(p_args) == 0 ? nullptr : (const void **)argptrs, sizeof...(p_args)); } template MethodBind *ClassDB::bind_vararg_method(uint32_t p_flags, const char *p_name, M p_method, const MethodInfo &p_info, const std::vector &p_default_args, bool p_return_nil_is_variant) { MethodBind *bind = create_vararg_method_bind(p_method, p_info, p_return_nil_is_variant); ERR_FAIL_COND_V(!bind, nullptr); bind->set_name(p_name); bind->set_default_arguments(p_default_args); const char *instance_type = bind->get_instance_class(); std::unordered_map::iterator type_it = classes.find(instance_type); if (type_it == classes.end()) { memdelete(bind); ERR_FAIL_V_MSG(nullptr, String("Class '{0}' doesn't exist.").format(instance_type)); } ClassInfo &type = type_it->second; if (type.method_map.find(p_name) != type.method_map.end()) { memdelete(bind); ERR_FAIL_V_MSG(nullptr, String("Binding duplicate method: {0}::{1}.").format(Array::make(instance_type, p_method))); } // register our method bind within our plugin type.method_map[p_name] = bind; // and register with godot bind_method_godot(type.name, bind); return bind; } #define GDREGISTER_CLASS(m_class) ClassDB::register_class(); } // namespace godot #endif // ! CLASS_DB_HPP