From 42a445da5c9d5675a5e0e9487e277f85cb431c49 Mon Sep 17 00:00:00 2001 From: David Snopek Date: Fri, 4 Aug 2023 11:02:57 -0500 Subject: [PATCH] Changes necessary for hot reload to work --- gdextension/gdextension_interface.h | 71 +++++++++++++++++++++++++++ include/godot_cpp/classes/wrapped.hpp | 24 +++++++++ include/godot_cpp/core/class_db.hpp | 1 + src/classes/wrapped.cpp | 19 +++++++ src/core/class_db.cpp | 3 ++ tools/godotcpp.py | 11 +++++ tools/linux.py | 3 ++ 7 files changed, 132 insertions(+) diff --git a/gdextension/gdextension_interface.h b/gdextension/gdextension_interface.h index eb4fdc55e6..a5c9cab985 100644 --- a/gdextension/gdextension_interface.h +++ b/gdextension/gdextension_interface.h @@ -267,6 +267,7 @@ typedef void (*GDExtensionClassUnreference)(GDExtensionClassInstancePtr p_instan typedef void (*GDExtensionClassCallVirtual)(GDExtensionClassInstancePtr p_instance, const GDExtensionConstTypePtr *p_args, GDExtensionTypePtr r_ret); typedef GDExtensionObjectPtr (*GDExtensionClassCreateInstance)(void *p_class_userdata); typedef void (*GDExtensionClassFreeInstance)(void *p_class_userdata, GDExtensionClassInstancePtr p_instance); +typedef GDExtensionClassInstancePtr (*GDExtensionClassRecreateInstance)(void *p_userdata, GDExtensionObjectPtr p_object); typedef GDExtensionClassCallVirtual (*GDExtensionClassGetVirtual)(void *p_class_userdata, GDExtensionConstStringNamePtr p_name); typedef void *(*GDExtensionClassGetVirtualCallData)(void *p_class_userdata, GDExtensionConstStringNamePtr p_name); typedef void (*GDExtensionClassCallVirtualWithData)(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, void *p_virtual_call_userdata, const GDExtensionConstTypePtr *p_args, GDExtensionTypePtr r_ret); @@ -308,6 +309,7 @@ typedef struct { GDExtensionClassUnreference unreference_func; GDExtensionClassCreateInstance create_instance_func; // (Default) constructor; mandatory. If the class is not instantiable, consider making it virtual or abstract. GDExtensionClassFreeInstance free_instance_func; // Destructor; mandatory. + GDExtensionClassRecreateInstance recreate_instance_func; // Queries a virtual function by name and returns a callback to invoke the requested virtual function. GDExtensionClassGetVirtual get_virtual_func; // Paired with `call_virtual_with_data_func`, this is an alternative to `get_virtual_func` for extensions that @@ -379,6 +381,47 @@ typedef struct { GDExtensionVariantPtr *default_arguments; } GDExtensionClassMethodInfo; +typedef void (*GDExtensionCallableCustomCall)(void *callable_userdata, const GDExtensionConstVariantPtr *p_args, GDExtensionInt p_argument_count, GDExtensionVariantPtr r_return, GDExtensionCallError *r_error); +typedef GDExtensionBool (*GDExtensionCallableCustomIsValid)(void *callable_userdata); +typedef void (*GDExtensionCallableCustomFree)(void *callable_userdata); + +typedef uint32_t (*GDExtensionCallableCustomHash)(void *callable_userdata); +typedef GDExtensionBool (*GDExtensionCallableCustomEqual)(void *callable_userdata_a, void *callable_userdata_b); +typedef GDExtensionBool (*GDExtensionCallableCustomLessThan)(void *callable_userdata_a, void *callable_userdata_b); + +typedef void (*GDExtensionCallableCustomToString)(void *callable_userdata, GDExtensionBool *r_is_valid, GDExtensionStringPtr r_out); + +typedef struct { + /* Only `call_func` and `token` are strictly required, however, `object` should be passed if its not a static method. + * + * `token` should point to an address that uniquely identifies the GDExtension (for example, the + * `GDExtensionClassLibraryPtr` passed to the entry symbol function. + * + * `hash_func`, `equal_func`, and `less_than_func` are optional. If not provided both `call_func` and + * `callable_userdata` together are used as the identity of the callable for hashing and comparison purposes. + * + * The hash returned by `hash_func` is cached, `hash_func` will not be called more than once per callable. + * + * `is_valid_func` is necessary if the validity of the callable can change before destruction. + * + * `free_func` is necessary if `callable_userdata` needs to be cleaned up when the callable is freed. + */ + void *callable_userdata; + void *token; + + GDExtensionObjectPtr object; + + GDExtensionCallableCustomCall call_func; + GDExtensionCallableCustomIsValid is_valid_func; + GDExtensionCallableCustomFree free_func; + + GDExtensionCallableCustomHash hash_func; + GDExtensionCallableCustomEqual equal_func; + GDExtensionCallableCustomLessThan less_than_func; + + GDExtensionCallableCustomToString to_string_func; +} GDExtensionCallableCustomInfo; + /* SCRIPT INSTANCE EXTENSION */ typedef void *GDExtensionScriptInstanceDataPtr; // Pointer to custom ScriptInstance native implementation. @@ -2262,6 +2305,34 @@ typedef void (*GDExtensionInterfacePlaceHolderScriptInstanceUpdate)(GDExtensionS */ typedef GDExtensionScriptInstanceDataPtr (*GDExtensionInterfaceObjectGetScriptInstance)(GDExtensionConstObjectPtr p_object, GDExtensionObjectPtr p_language); +/* INTERFACE: Callable */ + +/** + * @name callable_custom_create + * @since 4.2 + * + * Creates a custom Callable object from a function pointer. + * + * Provided struct can be safely freed once the function returns. + * + * @param r_callable A pointer that will receive the new Callable. + * @param p_callable_custom_info The info required to construct a Callable. + */ +typedef void (*GDExtensionInterfaceCallableCustomCreate)(GDExtensionUninitializedTypePtr r_callable, GDExtensionCallableCustomInfo *p_callable_custom_info); + +/** + * @name callable_custom_get_userdata + * @since 4.2 + * + * Retrieves the userdata pointer from a custom Callable. + * + * If the Callable is not a custom Callable or the token does not match the one provided to callable_custom_create() via GDExtensionCallableCustomInfo then NULL will be returned. + * + * @param p_callable A pointer to a Callable. + * @param p_token A pointer to an address that uniquely identifies the GDExtension. + */ +typedef void *(*GDExtensionInterfaceCallableCustomGetUserData)(GDExtensionConstTypePtr p_callable, void *p_token); + /* INTERFACE: ClassDB */ /** diff --git a/include/godot_cpp/classes/wrapped.hpp b/include/godot_cpp/classes/wrapped.hpp index 91e29eb552..879346bf74 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; @@ -106,6 +115,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 @@ -193,6 +213,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, GDExtensionBool p_reversed) { \ 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 af81037afb..075eb6b311 100644 --- a/include/godot_cpp/core/class_db.hpp +++ b/include/godot_cpp/core/class_db.hpp @@ -197,6 +197,7 @@ void ClassDB::_register_class(bool p_virtual, bool p_exposed) { nullptr, // GDExtensionClassUnreference unreference_func; T::create, // GDExtensionClassCreateInstance create_instance_func; /* this one is mandatory */ T::free, // GDExtensionClassFreeInstance free_instance_func; /* this one is mandatory */ + T::recreate, // GDExtensionClassRecreateInstance recreate_instance_func; &ClassDB::get_virtual_func, // GDExtensionClassGetVirtual get_virtual_func; nullptr, // GDExtensionClassGetVirtualCallData get_virtual_call_data_func; nullptr, // GDExtensionClassCallVirtualWithData call_virtual_func; diff --git a/src/classes/wrapped.cpp b/src/classes/wrapped.cpp index 1e9239cbad..5b425c7110 100644 --- a/src/classes/wrapped.cpp +++ b/src/classes/wrapped.cpp @@ -49,6 +49,25 @@ 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; + } else { + Wrapped::recreate_instance = 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 76abd2160f..6dc3804529 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/godotcpp.py b/tools/godotcpp.py index 969f8c411c..c89335d2e8 100644 --- a/tools/godotcpp.py +++ b/tools/godotcpp.py @@ -175,6 +175,14 @@ def options(opts, env): ) ) + 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 for pl in platforms: tool = Tool(pl, toolpath=["tools"]) @@ -231,6 +239,9 @@ def generate(env): print("Building for architecture " + env["arch"] + " on platform " + env["platform"]) + 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/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'"])