Skip to content

Commit 130644c

Browse files
committed
Merge pull request #1138 from dsnopek/editor-plugins-deinitialize
Automatically remove editor plugins when deinitializing GDExtension
2 parents ca78bcf + d28a3cb commit 130644c

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

binding_generator.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,9 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
12361236
else:
12371237
result.append(f"#include <godot_cpp/{get_include_path(included)}>")
12381238

1239+
if class_name == "EditorPlugin":
1240+
result.append("#include <godot_cpp/templates/vector.hpp>")
1241+
12391242
if len(fully_used_classes) > 0:
12401243
result.append("")
12411244

@@ -1385,17 +1388,23 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
13851388

13861389
if class_name == "EditorPlugin":
13871390
result.append("class EditorPlugins {")
1391+
result.append("private:")
1392+
result.append("\tstatic Vector<StringName> plugin_classes;")
1393+
result.append("")
13881394
result.append("public:")
1395+
result.append("\tstatic void add_plugin_class(const StringName &p_class_name);")
1396+
result.append("\tstatic void remove_plugin_class(const StringName &p_class_name);")
1397+
result.append("\tstatic void deinitialize(GDExtensionInitializationLevel p_level);")
13891398
result.append("")
13901399

13911400
result.append("\ttemplate <class T>")
13921401
result.append("\tstatic void add_by_type() {")
1393-
result.append("\t\tinternal::gdextension_interface_editor_add_plugin(T::get_class_static()._native_ptr());")
1402+
result.append("\t\tadd_plugin_class(T::get_class_static());")
13941403
result.append("\t}")
13951404

13961405
result.append("\ttemplate <class T>")
13971406
result.append("\tstatic void remove_by_type() {")
1398-
result.append("\t\tinternal::gdextension_interface_editor_remove_plugin(T::get_class_static()._native_ptr());")
1407+
result.append("\t\tremove_plugin_class(T::get_class_static());")
13991408
result.append("\t}")
14001409

14011410
result.append("};")

src/classes/editor_plugin.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**************************************************************************/
2+
/* editor_plugin.cpp */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#include <godot_cpp/classes/editor_plugin.hpp>
32+
33+
#include <godot_cpp/variant/string_name.hpp>
34+
35+
namespace godot {
36+
37+
Vector<StringName> EditorPlugins::plugin_classes;
38+
39+
void EditorPlugins::add_plugin_class(const StringName &p_class_name) {
40+
ERR_FAIL_COND_MSG(plugin_classes.find(p_class_name) != -1, vformat("Editor plugin already registered: %s", p_class_name));
41+
plugin_classes.push_back(p_class_name);
42+
internal::gdextension_interface_editor_add_plugin(p_class_name._native_ptr());
43+
}
44+
45+
void EditorPlugins::remove_plugin_class(const StringName &p_class_name) {
46+
int index = plugin_classes.find(p_class_name);
47+
ERR_FAIL_COND_MSG(index == -1, vformat("Editor plugin is not registered: %s", p_class_name));
48+
plugin_classes.remove_at(index);
49+
internal::gdextension_interface_editor_remove_plugin(p_class_name._native_ptr());
50+
}
51+
52+
void EditorPlugins::deinitialize(GDExtensionInitializationLevel p_level) {
53+
if (p_level == GDEXTENSION_INITIALIZATION_EDITOR) {
54+
for (const StringName &class_name : plugin_classes) {
55+
internal::gdextension_interface_editor_remove_plugin(class_name._native_ptr());
56+
}
57+
plugin_classes.clear();
58+
}
59+
}
60+
61+
} // namespace godot

src/godot.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include <godot_cpp/godot.hpp>
3232

33+
#include <godot_cpp/classes/editor_plugin.hpp>
3334
#include <godot_cpp/classes/wrapped.hpp>
3435
#include <godot_cpp/core/class_db.hpp>
3536
#include <godot_cpp/core/memory.hpp>
@@ -401,6 +402,7 @@ void GDExtensionBinding::deinitialize_level(void *userdata, GDExtensionInitializ
401402
terminate_callback(static_cast<ModuleInitializationLevel>(p_level));
402403
}
403404

405+
EditorPlugins::deinitialize(p_level);
404406
ClassDB::deinitialize(p_level);
405407
}
406408
GDExtensionBinding::InitObject::InitObject(GDExtensionInterfaceGetProcAddress p_get_proc_address, GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) {

0 commit comments

Comments
 (0)