From 20be441026ce3e294bef110279233a8168d219c9 Mon Sep 17 00:00:00 2001 From: Kehom Date: Thu, 16 Feb 2023 11:43:26 -0300 Subject: [PATCH] Unregister custom classes in reverse registration order --- include/godot_cpp/core/class_db.hpp | 3 +++ src/core/class_db.cpp | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/godot_cpp/core/class_db.hpp b/include/godot_cpp/core/class_db.hpp index 425d5a21..b1625bf2 100644 --- a/include/godot_cpp/core/class_db.hpp +++ b/include/godot_cpp/core/class_db.hpp @@ -105,6 +105,8 @@ private: // This may only contain custom classes, not Godot classes static std::unordered_map classes; static std::unordered_map instance_binding_callbacks; + // Used to remember the custom class registration order. + static std::vector class_register_order; 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); @@ -178,6 +180,7 @@ void ClassDB::_register_class(bool p_virtual) { cl.parent_ptr = &parent_it->second; } classes[cl.name] = cl; + class_register_order.push_back(cl.name); // Register this class with Godot GDExtensionClassCreationInfo class_info = { diff --git a/src/core/class_db.cpp b/src/core/class_db.cpp index 551cb890..e8bb40c6 100644 --- a/src/core/class_db.cpp +++ b/src/core/class_db.cpp @@ -41,6 +41,7 @@ namespace godot { std::unordered_map ClassDB::classes; std::unordered_map ClassDB::instance_binding_callbacks; +std::vector ClassDB::class_register_order; GDExtensionInitializationLevel ClassDB::current_level = GDEXTENSION_INITIALIZATION_CORE; MethodDefinition D_METHOD(StringName p_name) { @@ -348,13 +349,15 @@ void ClassDB::initialize(GDExtensionInitializationLevel p_level) { } void ClassDB::deinitialize(GDExtensionInitializationLevel p_level) { - for (const std::pair pair : classes) { - const ClassInfo &cl = pair.second; + for (std::vector::reverse_iterator i = class_register_order.rbegin(); i != class_register_order.rend(); ++i) { + const StringName &name = *i; + const ClassInfo &cl = classes[name]; + if (cl.level != p_level) { continue; } - internal::gdextension_interface_classdb_unregister_extension_class(internal::library, cl.name._native_ptr()); + internal::gdextension_interface_classdb_unregister_extension_class(internal::library, name._native_ptr()); for (auto method : cl.method_map) { memdelete(method.second);