summaryrefslogtreecommitdiffstats
path: root/core/extension
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2023-05-26 11:00:32 +0200
committerRémi Verschelde <rverschelde@gmail.com>2023-05-26 11:00:32 +0200
commit699b66b62d34d17d72dced139d6691aab64b8180 (patch)
tree5e9f89717fa088cef360c7a14264ea90d19f574d /core/extension
parentcb711a995061e7a7e097dcb0ed5760bcba3f23d1 (diff)
parent300716321072c719dc5c3f8a19126fe753747a60 (diff)
downloadredot-engine-699b66b62d34d17d72dced139d6691aab64b8180.tar.gz
Merge pull request #77010 from dsnopek/gdextension-editor-plugins
Allow GDExtensions to add editor plugins
Diffstat (limited to 'core/extension')
-rw-r--r--core/extension/gdextension.cpp22
-rw-r--r--core/extension/gdextension.h24
-rw-r--r--core/extension/gdextension_interface.cpp16
-rw-r--r--core/extension/gdextension_interface.h20
4 files changed, 82 insertions, 0 deletions
diff --git a/core/extension/gdextension.cpp b/core/extension/gdextension.cpp
index 0cbcf58882..8bdea01ae6 100644
--- a/core/extension/gdextension.cpp
+++ b/core/extension/gdextension.cpp
@@ -678,3 +678,25 @@ String GDExtensionResourceLoader::get_resource_type(const String &p_path) const
}
return "";
}
+
+#ifdef TOOLS_ENABLED
+Vector<StringName> 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 95811820cf..49f1cf1d8e 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<StringName> 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<StringName> &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 12ef1772e3..21d34b6e0c 100644
--- a/core/extension/gdextension_interface.cpp
+++ b/core/extension/gdextension_interface.cpp
@@ -1071,6 +1071,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<const StringName *>(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<const StringName *>(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() {
@@ -1199,6 +1213,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 a5ea3918df..3aa41f28da 100644
--- a/core/extension/gdextension_interface.h
+++ b/core/extension/gdextension_interface.h
@@ -2141,6 +2141,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