diff --git a/include/godot_cpp/core/class_db.hpp b/include/godot_cpp/core/class_db.hpp index aea95a77..57109910 100644 --- a/include/godot_cpp/core/class_db.hpp +++ b/include/godot_cpp/core/class_db.hpp @@ -39,6 +39,7 @@ #include #include +#include #include #include #include @@ -82,16 +83,10 @@ public: const char *parent_name = nullptr; GDNativeInitializationLevel level = GDNATIVE_INITIALIZATION_SCENE; std::unordered_map method_map; - std::unordered_map signal_map; - std::list method_order; - GDExtensionClassInstancePtr (*constructor)(void *data); + std::set signal_names; std::unordered_map virtual_methods; - void (*destructor)(void *data, GDExtensionClassInstancePtr ptr); - void (*object_instance)(GDExtensionClassInstancePtr p_instance, GDNativeObjectPtr p_object_instance); - std::unordered_map property_setget; - std::list property_list; - std::unordered_map> constant_map; // String in pair is enum name. - std::list constant_order; + std::set property_names; + std::set constant_names; ClassInfo *parent_ptr = nullptr; }; @@ -99,6 +94,8 @@ private: 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); public: template @@ -112,7 +109,7 @@ public: 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, const char *p_enum, const char *p_name, GDNativeInt p_constant); + static void bind_integer_constant(const char *p_class_name, const char *p_enum_name, const char *p_constant_name, GDNativeInt p_constant_value); 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); @@ -139,18 +136,40 @@ public: 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; - cl.constructor = T::create; - cl.destructor = T::free; - cl.object_instance = T::set_object_instance; classes[cl.name] = cl; if (classes.find(cl.parent_name) != classes.end()) { cl.parent_ptr = &classes[cl.parent_name]; } + + // Register this class with Godot + GDNativeExtensionClassCreationInfo class_info = { + nullptr, // GDNativeExtensionClassSet set_func; + nullptr, // GDNativeExtensionClassGet get_func; + nullptr, // GDNativeExtensionClassGetPropertyList get_property_list_func; + nullptr, // GDNativeExtensionClassFreePropertyList free_property_list_func; + nullptr, // GDNativeExtensionClassNotification notification_func; + nullptr, // GDNativeExtensionClassToString to_string_func; + nullptr, // GDNativeExtensionClassReference reference_func; + nullptr, // GDNativeExtensionClassUnreference + T::create, // GDNativeExtensionClassCreateInstance create_instance_func; /* this one is mandatory */ + T::free, // GDNativeExtensionClassFreeInstance free_instance_func; /* this one is mandatory */ + T::set_object_instance, // GDNativeExtensionClassObjectInstance object_instance_func; /* this one is mandatory */ + &ClassDB::get_virtual_func, // GDNativeExtensionClassGetVirtual get_virtual_func; + (void *)cl.name, //void *class_userdata; + }; + + internal::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 @@ -183,8 +202,11 @@ MethodBind *ClassDB::bind_vararg_method(uint32_t p_flags, const char *p_name, M ERR_FAIL_V_MSG(nullptr, "Binding duplicate method."); } + // register our method bind within our plugin type.method_map[p_name] = bind; - type.method_order.push_back(bind); + + // and register with godot + bind_method_godot(type.name, bind); return bind; } diff --git a/src/core/class_db.cpp b/src/core/class_db.cpp index 51510802..ec8a1074 100644 --- a/src/core/class_db.cpp +++ b/src/core/class_db.cpp @@ -55,17 +55,13 @@ MethodDefinition D_METHOD(const char *p_name, const char *p_arg1) { void ClassDB::add_property_group(const char *p_class, const char *p_name, const char *p_prefix) { ERR_FAIL_COND_MSG(classes.find(p_class) == classes.end(), "Trying to add property to non-existing class."); - ClassInfo &info = classes[p_class]; - - info.property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, p_prefix, PROPERTY_USAGE_GROUP)); + internal::interface->classdb_register_extension_class_property_group(internal::library, p_class, p_name, p_prefix); } void ClassDB::add_property_subgroup(const char *p_class, const char *p_name, const char *p_prefix) { ERR_FAIL_COND_MSG(classes.find(p_class) == classes.end(), "Trying to add property to non-existing class."); - ClassInfo &info = classes[p_class]; - - info.property_list.push_back(PropertyInfo(Variant::NIL, p_name, PROPERTY_HINT_NONE, p_prefix, PROPERTY_USAGE_SUBGROUP)); + internal::interface->classdb_register_extension_class_property_subgroup(internal::library, p_class, p_name, p_prefix); } void ClassDB::add_property(const char *p_class, const PropertyInfo &p_pinfo, const char *p_setter, const char *p_getter, int p_index) { @@ -73,7 +69,7 @@ void ClassDB::add_property(const char *p_class, const PropertyInfo &p_pinfo, con ClassInfo &info = classes[p_class]; - ERR_FAIL_COND_MSG(info.property_setget.find(p_pinfo.name) != info.property_setget.end(), "Property already exists in class."); + ERR_FAIL_COND_MSG(info.property_names.find(p_pinfo.name) != info.property_names.end(), "Property already exists in class."); MethodBind *setter = nullptr; if (p_setter) { @@ -94,7 +90,18 @@ void ClassDB::add_property(const char *p_class, const PropertyInfo &p_pinfo, con ERR_FAIL_COND_MSG(exp_args != getter->get_argument_count(), "Getter method must not take any argument."); } - info.property_list.push_back(p_pinfo); + // register property with plugin + info.property_names.insert(p_pinfo.name); + + // register with Godot + GDNativePropertyInfo prop_info = { + (uint32_t)p_pinfo.type, //uint32_t type; + p_pinfo.name, //const char *name; + p_pinfo.class_name, //const char *class_name; + p_pinfo.hint, // NONE //uint32_t hint; + p_pinfo.hint_string, // const char *hint_string; + p_pinfo.usage, // DEFAULT //uint32_t usage; + }; PropertySetGet setget; setget.setter = p_setter; @@ -104,7 +111,7 @@ void ClassDB::add_property(const char *p_class, const PropertyInfo &p_pinfo, con setget.index = p_index; setget.type = p_pinfo.type; - info.property_setget[p_pinfo.name] = setget; + internal::interface->classdb_register_extension_class_property(internal::library, info.name, &prop_info, setget.setter, setget.getter); } MethodBind *ClassDB::get_method(const char *p_class, const char *p_method) { @@ -162,38 +169,83 @@ MethodBind *ClassDB::bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const M p_bind->set_argument_names(args); - type.method_order.push_back(p_bind); + // register our method bind within our plugin type.method_map[method_name.name] = p_bind; + // and register with godot + bind_method_godot(type.name, p_bind); + return p_bind; } +void ClassDB::bind_method_godot(const char *p_class_name, MethodBind *p_method) { + GDNativeExtensionClassMethodInfo method_info = { + p_method->get_name(), //const char *name; + p_method, //void *method_userdata; + MethodBind::bind_call, //GDNativeExtensionClassMethodCall call_func; + MethodBind::bind_ptrcall, //GDNativeExtensionClassMethodPtrCall ptrcall_func; + GDNATIVE_EXTENSION_METHOD_FLAGS_DEFAULT, //uint32_t method_flags; /* GDNativeExtensionClassMethodFlags */ + (uint32_t)p_method->get_argument_count(), //uint32_t argument_count; + (GDNativeBool)p_method->has_return(), //GDNativeBool has_return_value; + MethodBind::bind_get_argument_type, //(GDNativeExtensionClassMethodGetArgumentType) get_argument_type_func; + MethodBind::bind_get_argument_info, //GDNativeExtensionClassMethodGetArgumentInfo get_argument_info_func; /* name and hint information for the argument can be omitted in release builds. Class name should always be present if it applies. */ + MethodBind::bind_get_argument_metadata, //GDNativeExtensionClassMethodGetArgumentMetadata get_argument_metadata_func; + p_method->get_hint_flags(), //uint32_t default_argument_count; + nullptr, //GDNativeVariantPtr *default_arguments; + }; + internal::interface->classdb_register_extension_class_method(internal::library, p_class_name, &method_info); +} + void ClassDB::add_signal(const char *p_class, const MethodInfo &p_signal) { std::unordered_map::iterator type_it = classes.find(p_class); ERR_FAIL_COND_MSG(type_it == classes.end(), "Class doesn't exist."); - ClassInfo &base = type_it->second; - ClassInfo *check = &base; + ClassInfo &cl = type_it->second; + + // Check if this signal is already register + ClassInfo *check = &cl; while (check) { - ERR_FAIL_COND_MSG(check->signal_map.find(p_signal.name) != check->signal_map.end(), String("Class '" + String(p_class) + "' already has signal '" + String(p_signal.name) + "'.").utf8().get_data()); + ERR_FAIL_COND_MSG(check->signal_names.find(p_signal.name) != check->signal_names.end(), String("Class '" + String(p_class) + "' already has signal '" + String(p_signal.name) + "'.").utf8().get_data()); check = check->parent_ptr; } - base.signal_map[p_signal.name] = p_signal; + // register our signal in our plugin + cl.signal_names.insert(p_signal.name); + + // register our signal in godot + std::vector parameters; + parameters.reserve(p_signal.arguments.size()); + + for (const PropertyInfo &par : p_signal.arguments) { + parameters.push_back(GDNativePropertyInfo{ + static_cast(par.type), // uint32_t type; + par.name, // const char *name; + par.class_name, // const char *class_name; + par.hint, // uint32_t hint; + par.hint_string, // const char *hint_string; + par.usage, // uint32_t usage; + }); + } + + internal::interface->classdb_register_extension_class_signal(internal::library, cl.name, p_signal.name, parameters.data(), parameters.size()); } -void ClassDB::bind_integer_constant(const char *p_class, const char *p_enum, const char *p_name, GDNativeInt p_constant) { - std::unordered_map::iterator type_it = classes.find(p_class); +void ClassDB::bind_integer_constant(const char *p_class_name, const char *p_enum_name, const char *p_constant_name, GDNativeInt p_constant_value) { + std::unordered_map::iterator type_it = classes.find(p_class_name); ERR_FAIL_COND_MSG(type_it == classes.end(), "Class doesn't exist."); ClassInfo &type = type_it->second; - ERR_FAIL_COND_MSG(type.constant_map.find(p_name) != type.constant_map.end(), "Constant already registered."); + // check if it already exists + ERR_FAIL_COND_MSG(type.constant_names.find(p_constant_name) != type.constant_names.end(), "Constant already registered."); - type.constant_map[p_name] = std::pair{ p_enum, p_constant }; - type.constant_order.push_back(p_name); + // register it with our plugin (purely to check for duplicates) + type.constant_names.insert(p_constant_name); + + // Register it with Godot + internal::interface->classdb_register_extension_class_integer_constant(internal::library, p_class_name, p_enum_name, p_constant_name, p_constant_value); } GDNativeExtensionClassCallVirtual ClassDB::get_virtual_func(void *p_userdata, const char *p_name) { @@ -225,6 +277,9 @@ void ClassDB::bind_virtual_method(const char *p_class, const char *p_method, GDN type.virtual_methods[p_method] = p_call; } +void ClassDB::initialize_class(const ClassInfo &p_cl) { +} + void ClassDB::initialize(GDNativeInitializationLevel p_level) { for (const std::pair pair : classes) { const ClassInfo &cl = pair.second; @@ -232,88 +287,7 @@ void ClassDB::initialize(GDNativeInitializationLevel p_level) { continue; } - GDNativeExtensionClassCreationInfo class_info = { - nullptr, // GDNativeExtensionClassSet set_func; - nullptr, // GDNativeExtensionClassGet get_func; - nullptr, // GDNativeExtensionClassGetPropertyList get_property_list_func; - nullptr, // GDNativeExtensionClassFreePropertyList free_property_list_func; - nullptr, // GDNativeExtensionClassNotification notification_func; - nullptr, // GDNativeExtensionClassToString to_string_func; - nullptr, // GDNativeExtensionClassReference reference_func; - nullptr, // GDNativeExtensionClassUnreference - cl.constructor, // GDNativeExtensionClassCreateInstance create_instance_func; /* this one is mandatory */ - cl.destructor, // GDNativeExtensionClassFreeInstance free_instance_func; /* this one is mandatory */ - cl.object_instance, // GDNativeExtensionClassObjectInstance object_instance_func; /* this one is mandatory */ - &ClassDB::get_virtual_func, // GDNativeExtensionClassGetVirtual get_virtual_func; - (void *)cl.name, //void *class_userdata; - }; - - internal::interface->classdb_register_extension_class(internal::library, cl.name, cl.parent_name, &class_info); - - for (MethodBind *method : cl.method_order) { - GDNativeExtensionClassMethodInfo method_info = { - method->get_name(), //const char *name; - method, //void *method_userdata; - MethodBind::bind_call, //GDNativeExtensionClassMethodCall call_func; - MethodBind::bind_ptrcall, //GDNativeExtensionClassMethodPtrCall ptrcall_func; - GDNATIVE_EXTENSION_METHOD_FLAGS_DEFAULT, //uint32_t method_flags; /* GDNativeExtensionClassMethodFlags */ - (uint32_t)method->get_argument_count(), //uint32_t argument_count; - (GDNativeBool)method->has_return(), //GDNativeBool has_return_value; - MethodBind::bind_get_argument_type, //(GDNativeExtensionClassMethodGetArgumentType) get_argument_type_func; - MethodBind::bind_get_argument_info, //GDNativeExtensionClassMethodGetArgumentInfo get_argument_info_func; /* name and hint information for the argument can be omitted in release builds. Class name should always be present if it applies. */ - MethodBind::bind_get_argument_metadata, //GDNativeExtensionClassMethodGetArgumentMetadata get_argument_metadata_func; - method->get_hint_flags(), //uint32_t default_argument_count; - nullptr, //GDNativeVariantPtr *default_arguments; - }; - internal::interface->classdb_register_extension_class_method(internal::library, cl.name, &method_info); - } - - for (const PropertyInfo &property : cl.property_list) { - GDNativePropertyInfo info = { - (uint32_t)property.type, //uint32_t type; - property.name, //const char *name; - property.class_name, //const char *class_name; - property.hint, // NONE //uint32_t hint; - property.hint_string, // const char *hint_string; - property.usage, // DEFAULT //uint32_t usage; - }; - - if (info.usage == PROPERTY_USAGE_GROUP) { - internal::interface->classdb_register_extension_class_property_group(internal::library, cl.name, info.name, info.hint_string); - } else if (info.usage == PROPERTY_USAGE_SUBGROUP) { - internal::interface->classdb_register_extension_class_property_subgroup(internal::library, cl.name, info.name, info.hint_string); - } else { - const PropertySetGet &setget = cl.property_setget.find(property.name)->second; - - internal::interface->classdb_register_extension_class_property(internal::library, cl.name, &info, setget.setter, setget.getter); - } - } - - for (const std::pair pair : cl.signal_map) { - const MethodInfo &signal = pair.second; - - std::vector parameters; - parameters.reserve(signal.arguments.size()); - - for (const PropertyInfo &par : signal.arguments) { - parameters.push_back(GDNativePropertyInfo{ - static_cast(par.type), // uint32_t type; - par.name, // const char *name; - par.class_name, // const char *class_name; - par.hint, // uint32_t hint; - par.hint_string, // const char *hint_string; - par.usage, // uint32_t usage; - }); - } - - internal::interface->classdb_register_extension_class_signal(internal::library, cl.name, pair.first.c_str(), parameters.data(), parameters.size()); - } - - for (std::string constant : cl.constant_order) { - const std::pair &def = cl.constant_map.find(constant)->second; - - internal::interface->classdb_register_extension_class_integer_constant(internal::library, cl.name, def.first.c_str(), constant.c_str(), def.second); - } + // Nothing to do here for now... } } @@ -326,8 +300,8 @@ void ClassDB::deinitialize(GDNativeInitializationLevel p_level) { internal::interface->classdb_unregister_extension_class(internal::library, cl.name); - for (MethodBind *method : cl.method_order) { - memdelete(method); + for (auto method : cl.method_map) { + memdelete(method.second); } } } diff --git a/test/demo/main.gd b/test/demo/main.gd index 81f45265..557e9019 100644 --- a/test/demo/main.gd +++ b/test/demo/main.gd @@ -1,10 +1,9 @@ extends Node func _ready(): - # Connect signals. - $Button.button_up.connect($Example.emit_custom_signal, ["Button", 42]) - $Example.custom_signal.connect(on_signal) - + # Bind signals + $Button.button_up.connect($Example.emit_custom_signal.bind("Button", 42)) + # Call methods. $Example.simple_func() ($Example as Example).simple_const_func() # Force use of ptrcall @@ -23,5 +22,5 @@ func _ready(): prints("ANSWER_TO_EVERYTHING", $Example.ANSWER_TO_EVERYTHING) prints("CONSTANT_WITHOUT_ENUM", $Example.CONSTANT_WITHOUT_ENUM) -func on_signal(name, value): +func _on_Example_custom_signal(name, value): prints("Example emitted:", name, value) diff --git a/test/demo/main.tscn b/test/demo/main.tscn index 4b211418..125eae03 100644 --- a/test/demo/main.tscn +++ b/test/demo/main.tscn @@ -15,6 +15,9 @@ offset_right = 234.0 offset_bottom = 21.0 structured_text_bidi_override_options = [] script = null +__meta__ = { +"_edit_use_anchors_": false +} [node name="Button" type="Button" parent="."] offset_right = 79.0 @@ -24,3 +27,5 @@ script = null __meta__ = { "_edit_use_anchors_": false } + +[connection signal="custom_signal" from="Example" to="." method="_on_Example_custom_signal"]