diff --git a/SConstruct b/SConstruct index b028676f25..f235eb05e1 100644 --- a/SConstruct +++ b/SConstruct @@ -173,6 +173,14 @@ opts.Add( ) ) +opts.Add( + BoolVariable( + key="use_hot_reload", + help="Enable the extra accounting required to support hot reload.", + default=(env.get("target", "template_debug") != "template_release"), + ) +) + # Add platform options tools = {} for pl in platforms: @@ -238,6 +246,9 @@ if env["arch"] == "": print("Unsupported CPU architecture: " + host_machine) Exit() +if env["use_hot_reload"]: + env.Append(CPPDEFINES=["HOT_RELOAD_ENABLED"]) + tool = Tool(env["platform"], toolpath=["tools"]) if tool is None or not tool.exists(env): diff --git a/gdextension/gdextension_interface.h b/gdextension/gdextension_interface.h index 6c05f3988b..7719e9efc5 100644 --- a/gdextension/gdextension_interface.h +++ b/gdextension/gdextension_interface.h @@ -264,6 +264,7 @@ typedef void (*GDExtensionClassReference)(GDExtensionClassInstancePtr p_instance typedef void (*GDExtensionClassUnreference)(GDExtensionClassInstancePtr p_instance); typedef void (*GDExtensionClassCallVirtual)(GDExtensionClassInstancePtr p_instance, const GDExtensionConstTypePtr *p_args, GDExtensionTypePtr r_ret); typedef GDExtensionObjectPtr (*GDExtensionClassCreateInstance)(void *p_userdata); +typedef GDExtensionClassInstancePtr (*GDExtensionClassRecreateInstance)(void *p_userdata, GDExtensionObjectPtr p_object); typedef void (*GDExtensionClassFreeInstance)(void *p_userdata, GDExtensionClassInstancePtr p_instance); typedef GDExtensionClassCallVirtual (*GDExtensionClassGetVirtual)(void *p_userdata, GDExtensionConstStringNamePtr p_name); @@ -285,6 +286,7 @@ typedef struct { GDExtensionClassGetVirtual get_virtual_func; // Queries a virtual function by name and returns a callback to invoke the requested virtual function. GDExtensionClassGetRID get_rid_func; void *class_userdata; // Per-class user data, later accessible in instance bindings. + GDExtensionClassRecreateInstance recreate_instance_func; } GDExtensionClassCreationInfo; typedef void *GDExtensionClassLibraryPtr; diff --git a/include/godot_cpp/classes/wrapped.hpp b/include/godot_cpp/classes/wrapped.hpp index f2efbd091e..8f566c9667 100644 --- a/include/godot_cpp/classes/wrapped.hpp +++ b/include/godot_cpp/classes/wrapped.hpp @@ -51,6 +51,15 @@ class Wrapped { friend void postinitialize_handler(Wrapped *); protected: +#ifdef HOT_RELOAD_ENABLED + struct RecreateInstance { + GDExtensionClassInstancePtr wrapper; + GDExtensionObjectPtr owner; + RecreateInstance *next; + }; + inline static RecreateInstance *recreate_instance = nullptr; +#endif + virtual const StringName *_get_extension_class_name() const; // This is needed to retrieve the class name before the godot object has its _extension and _extension_instance members assigned. virtual const GDExtensionInstanceBindingCallbacks *_get_bindings_callbacks() const = 0; @@ -104,6 +113,17 @@ void free_c_property_list(GDExtensionPropertyInfo *plist); } // namespace godot +#ifdef HOT_RELOAD_ENABLED +#define _GDCLASS_RECREATE(m_class, m_inherits) \ + m_class *new_instance = (m_class *)memalloc(sizeof(m_class)); \ + Wrapped::RecreateInstance recreate_data = { new_instance, obj, Wrapped::recreate_instance }; \ + Wrapped::recreate_instance = &recreate_data; \ + memnew_placement(new_instance, m_class); \ + return new_instance; +#else +#define _GDCLASS_RECREATE(m_class, m_inherits) return nullptr; +#endif + // Use this on top of your own classes. // Note: the trail of `***` is to keep sane diffs in PRs, because clang-format otherwise moves every `\` which makes // every line of the macro different @@ -187,6 +207,10 @@ public: return new_object->_owner; \ } \ \ + static GDExtensionClassInstancePtr recreate(void *data, GDExtensionObjectPtr obj) { \ + _GDCLASS_RECREATE(m_class, m_inherits); \ + } \ + \ static void notification_bind(GDExtensionClassInstancePtr p_instance, int32_t p_what) { \ if (p_instance && m_class::_get_notification()) { \ if (m_class::_get_notification() != m_inherits::_get_notification()) { \ diff --git a/include/godot_cpp/core/class_db.hpp b/include/godot_cpp/core/class_db.hpp index 0802a45dcb..c1946e3474 100644 --- a/include/godot_cpp/core/class_db.hpp +++ b/include/godot_cpp/core/class_db.hpp @@ -192,6 +192,7 @@ void ClassDB::_register_class(bool p_virtual) { &ClassDB::get_virtual_func, // GDExtensionClassGetVirtual get_virtual_func; nullptr, // GDExtensionClassGetRID get_rid; (void *)&T::get_class_static(), // void *class_userdata; + T::recreate, }; internal::gdextension_interface_classdb_register_extension_class(internal::library, cl.name._native_ptr(), cl.parent_name._native_ptr(), &class_info); diff --git a/src/classes/wrapped.cpp b/src/classes/wrapped.cpp index 1e9239cbad..cac16c5dd2 100644 --- a/src/classes/wrapped.cpp +++ b/src/classes/wrapped.cpp @@ -49,6 +49,23 @@ void Wrapped::_postinitialize() { } Wrapped::Wrapped(const StringName p_godot_class) { +#ifdef HOT_RELOAD_ENABLED + if (unlikely(Wrapped::recreate_instance)) { + RecreateInstance *recreate_data = Wrapped::recreate_instance; + RecreateInstance *previous = nullptr; + while (recreate_data) { + if (recreate_data->wrapper == this) { + _owner = recreate_data->owner; + if (previous) { + previous->next = recreate_data->next; + } + return; + } + previous = recreate_data; + recreate_data = recreate_data->next; + } + } +#endif _owner = godot::internal::gdextension_interface_classdb_construct_object(reinterpret_cast(p_godot_class._native_ptr())); } diff --git a/src/core/class_db.cpp b/src/core/class_db.cpp index 159c03142d..6fb2199758 100644 --- a/src/core/class_db.cpp +++ b/src/core/class_db.cpp @@ -354,6 +354,9 @@ void ClassDB::deinitialize(GDExtensionInitializationLevel p_level) { for (auto method : cl.method_map) { memdelete(method.second); } + + classes.erase(*i); + class_register_order.erase((i + 1).base()); } } diff --git a/tools/linux.py b/tools/linux.py index cb48ae5864..823b66e127 100644 --- a/tools/linux.py +++ b/tools/linux.py @@ -14,6 +14,9 @@ def generate(env): if env["use_llvm"]: clang.generate(env) clangxx.generate(env) + elif env["use_hot_reload"]: + # Required for extensions to truly unload. + env.Append(CXXFLAGS=["-fno-gnu-unique"]) env.Append(CCFLAGS=["-fPIC", "-Wwrite-strings"]) env.Append(LINKFLAGS=["-Wl,-R,'$$ORIGIN'"])