From 300716321072c719dc5c3f8a19126fe753747a60 Mon Sep 17 00:00:00 2001 From: David Snopek Date: Thu, 11 May 2023 16:46:53 -0500 Subject: [PATCH] Allow GDExtensions to add editor plugins --- core/extension/gdextension.cpp | 22 +++++++++++++++++ core/extension/gdextension.h | 24 +++++++++++++++++++ core/extension/gdextension_interface.cpp | 16 +++++++++++++ core/extension/gdextension_interface.h | 20 ++++++++++++++++ editor/editor_data.cpp | 18 ++++++++++++++ editor/editor_data.h | 6 +++++ editor/editor_interface.h | 4 ++++ editor/editor_node.cpp | 30 ++++++++++++++++++++++++ editor/editor_node.h | 3 +++ 9 files changed, 143 insertions(+) diff --git a/core/extension/gdextension.cpp b/core/extension/gdextension.cpp index 942ec956788..56da4c9174e 100644 --- a/core/extension/gdextension.cpp +++ b/core/extension/gdextension.cpp @@ -677,3 +677,25 @@ String GDExtensionResourceLoader::get_resource_type(const String &p_path) const } return ""; } + +#ifdef TOOLS_ENABLED +Vector GDExtensionEditorPlugins::extension_classes; +GDExtensionEditorPlugins::EditorPluginRegisterFunc GDExtensionEditorPlugins::editor_node_add_plugin = nullptr; +GDExtensionEditorPlugins::EditorPluginRegisterFunc GDExtensionEditorPlugins::editor_node_remove_plugin = nullptr; + +void GDExtensionEditorPlugins::add_extension_class(const StringName &p_class_name) { + if (editor_node_add_plugin) { + editor_node_add_plugin(p_class_name); + } else { + extension_classes.push_back(p_class_name); + } +} + +void GDExtensionEditorPlugins::remove_extension_class(const StringName &p_class_name) { + if (editor_node_remove_plugin) { + editor_node_remove_plugin(p_class_name); + } else { + extension_classes.erase(p_class_name); + } +} +#endif // TOOLS_ENABLED diff --git a/core/extension/gdextension.h b/core/extension/gdextension.h index 95811820cf4..49f1cf1d8ea 100644 --- a/core/extension/gdextension.h +++ b/core/extension/gdextension.h @@ -107,4 +107,28 @@ public: virtual String get_resource_type(const String &p_path) const; }; +#ifdef TOOLS_ENABLED +class GDExtensionEditorPlugins { +private: + static Vector extension_classes; + +protected: + friend class EditorNode; + + // Since this in core, we can't directly reference EditorNode, so it will + // set these function pointers in its constructor. + typedef void (*EditorPluginRegisterFunc)(const StringName &p_class_name); + static EditorPluginRegisterFunc editor_node_add_plugin; + static EditorPluginRegisterFunc editor_node_remove_plugin; + +public: + static void add_extension_class(const StringName &p_class_name); + static void remove_extension_class(const StringName &p_class_name); + + static const Vector &get_extension_classes() { + return extension_classes; + } +}; +#endif // TOOLS_ENABLED + #endif // GDEXTENSION_H diff --git a/core/extension/gdextension_interface.cpp b/core/extension/gdextension_interface.cpp index c661bfa8c76..159c2e87df9 100644 --- a/core/extension/gdextension_interface.cpp +++ b/core/extension/gdextension_interface.cpp @@ -1058,6 +1058,20 @@ static void *gdextension_classdb_get_class_tag(GDExtensionConstStringNamePtr p_c return class_info ? class_info->class_ptr : nullptr; } +static void gdextension_editor_add_plugin(GDExtensionConstStringNamePtr p_classname) { +#ifdef TOOLS_ENABLED + const StringName classname = *reinterpret_cast(p_classname); + GDExtensionEditorPlugins::add_extension_class(classname); +#endif +} + +static void gdextension_editor_remove_plugin(GDExtensionConstStringNamePtr p_classname) { +#ifdef TOOLS_ENABLED + const StringName classname = *reinterpret_cast(p_classname); + GDExtensionEditorPlugins::remove_extension_class(classname); +#endif +} + #define REGISTER_INTERFACE_FUNC(m_name) GDExtension::register_interface_function(#m_name, (GDExtensionInterfaceFunctionPtr)&gdextension_##m_name) void gdextension_setup_interface() { @@ -1185,6 +1199,8 @@ void gdextension_setup_interface() { REGISTER_INTERFACE_FUNC(classdb_construct_object); REGISTER_INTERFACE_FUNC(classdb_get_method_bind); REGISTER_INTERFACE_FUNC(classdb_get_class_tag); + REGISTER_INTERFACE_FUNC(editor_add_plugin); + REGISTER_INTERFACE_FUNC(editor_remove_plugin); } #undef REGISTER_INTERFACE_FUNCTION diff --git a/core/extension/gdextension_interface.h b/core/extension/gdextension_interface.h index 4e4f300f5dc..27cfb88332d 100644 --- a/core/extension/gdextension_interface.h +++ b/core/extension/gdextension_interface.h @@ -2128,6 +2128,26 @@ typedef void (*GDExtensionInterfaceClassdbUnregisterExtensionClass)(GDExtensionC */ typedef void (*GDExtensionInterfaceGetLibraryPath)(GDExtensionClassLibraryPtr p_library, GDExtensionUninitializedStringPtr r_path); +/** + * @name editor_add_plugin + * + * Adds an editor plugin. + * + * It's safe to call during initialization. + * + * @param p_class_name A pointer to a StringName with the name of a class (descending from EditorPlugin) which is already registered with ClassDB. + */ +typedef void (*GDExtensionInterfaceEditorAddPlugin)(GDExtensionConstStringNamePtr p_class_name); + +/** + * @name editor_remove_plugin + * + * Removes an editor plugin. + * + * @param p_class_name A pointer to a StringName with the name of a class that was previously added as an editor plugin. + */ +typedef void (*GDExtensionInterfaceEditorRemovePlugin)(GDExtensionConstStringNamePtr p_class_name); + #ifdef __cplusplus } #endif diff --git a/editor/editor_data.cpp b/editor/editor_data.cpp index 5d3037b4ec9..596a2dfac16 100644 --- a/editor/editor_data.cpp +++ b/editor/editor_data.cpp @@ -485,6 +485,24 @@ EditorPlugin *EditorData::get_editor_plugin(int p_idx) { return editor_plugins[p_idx]; } +void EditorData::add_extension_editor_plugin(const StringName &p_class_name, EditorPlugin *p_plugin) { + ERR_FAIL_COND(extension_editor_plugins.has(p_class_name)); + extension_editor_plugins.insert(p_class_name, p_plugin); +} + +void EditorData::remove_extension_editor_plugin(const StringName &p_class_name) { + extension_editor_plugins.erase(p_class_name); +} + +bool EditorData::has_extension_editor_plugin(const StringName &p_class_name) { + return extension_editor_plugins.has(p_class_name); +} + +EditorPlugin *EditorData::get_extension_editor_plugin(const StringName &p_class_name) { + EditorPlugin **plugin = extension_editor_plugins.getptr(p_class_name); + return plugin == nullptr ? nullptr : *plugin; +} + void EditorData::add_custom_type(const String &p_type, const String &p_inherits, const Ref