Browse Source

Merge pull request #80284 from dsnopek/gdextension-hot-reload

Implement reloading of GDExtensions
Rémi Verschelde 1 year ago
parent
commit
55dfbd7761

+ 7 - 0
core/config/engine.h

@@ -82,6 +82,7 @@ private:
 
 
 	bool editor_hint = false;
 	bool editor_hint = false;
 	bool project_manager_hint = false;
 	bool project_manager_hint = false;
+	bool extension_reloading = false;
 
 
 	static Engine *singleton;
 	static Engine *singleton;
 
 
@@ -140,12 +141,18 @@ public:
 
 
 	_FORCE_INLINE_ void set_project_manager_hint(bool p_enabled) { project_manager_hint = p_enabled; }
 	_FORCE_INLINE_ void set_project_manager_hint(bool p_enabled) { project_manager_hint = p_enabled; }
 	_FORCE_INLINE_ bool is_project_manager_hint() const { return project_manager_hint; }
 	_FORCE_INLINE_ bool is_project_manager_hint() const { return project_manager_hint; }
+
+	_FORCE_INLINE_ void set_extension_reloading_enabled(bool p_enabled) { extension_reloading = p_enabled; }
+	_FORCE_INLINE_ bool is_extension_reloading_enabled() const { return extension_reloading; }
 #else
 #else
 	_FORCE_INLINE_ void set_editor_hint(bool p_enabled) {}
 	_FORCE_INLINE_ void set_editor_hint(bool p_enabled) {}
 	_FORCE_INLINE_ bool is_editor_hint() const { return false; }
 	_FORCE_INLINE_ bool is_editor_hint() const { return false; }
 
 
 	_FORCE_INLINE_ void set_project_manager_hint(bool p_enabled) {}
 	_FORCE_INLINE_ void set_project_manager_hint(bool p_enabled) {}
 	_FORCE_INLINE_ bool is_project_manager_hint() const { return false; }
 	_FORCE_INLINE_ bool is_project_manager_hint() const { return false; }
+
+	_FORCE_INLINE_ void set_extension_reloading_enabled(bool p_enabled) {}
+	_FORCE_INLINE_ bool is_extension_reloading_enabled() const { return false; }
 #endif
 #endif
 
 
 	Dictionary get_version_info() const;
 	Dictionary get_version_info() const;

+ 412 - 49
core/extension/gdextension.cpp

@@ -162,6 +162,14 @@ class GDExtensionMethodBind : public MethodBind {
 	List<PropertyInfo> arguments_info;
 	List<PropertyInfo> arguments_info;
 	List<GodotTypeInfo::Metadata> arguments_metadata;
 	List<GodotTypeInfo::Metadata> arguments_metadata;
 
 
+#ifdef TOOLS_ENABLED
+	friend class GDExtension;
+
+	StringName name;
+	bool is_reloading = false;
+	bool valid = true;
+#endif
+
 protected:
 protected:
 	virtual Variant::Type _gen_argument_type(int p_arg) const override {
 	virtual Variant::Type _gen_argument_type(int p_arg) const override {
 		if (p_arg < 0) {
 		if (p_arg < 0) {
@@ -179,6 +187,10 @@ protected:
 	}
 	}
 
 
 public:
 public:
+#ifdef TOOLS_ENABLED
+	virtual bool is_valid() const override { return valid; }
+#endif
+
 #ifdef DEBUG_METHODS_ENABLED
 #ifdef DEBUG_METHODS_ENABLED
 	virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const override {
 	virtual GodotTypeInfo::Metadata get_argument_meta(int p_arg) const override {
 		if (p_arg < 0) {
 		if (p_arg < 0) {
@@ -190,6 +202,9 @@ public:
 #endif
 #endif
 
 
 	virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override {
 	virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const override {
+#ifdef TOOLS_ENABLED
+		ERR_FAIL_COND_V_MSG(!valid, Variant(), vformat("Cannot call invalid GDExtension method bind '%s'. It's probably cached - you may need to restart Godot.", name));
+#endif
 		Variant ret;
 		Variant ret;
 		GDExtensionClassInstancePtr extension_instance = is_static() ? nullptr : p_object->_get_extension_instance();
 		GDExtensionClassInstancePtr extension_instance = is_static() ? nullptr : p_object->_get_extension_instance();
 		GDExtensionCallError ce{ GDEXTENSION_CALL_OK, 0, 0 };
 		GDExtensionCallError ce{ GDEXTENSION_CALL_OK, 0, 0 };
@@ -200,6 +215,9 @@ public:
 		return ret;
 		return ret;
 	}
 	}
 	virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override {
 	virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const override {
+#ifdef TOOLS_ENABLED
+		ERR_FAIL_COND_MSG(!valid, vformat("Cannot call invalid GDExtension method bind '%s'. It's probably cached - you may need to restart Godot.", name));
+#endif
 		ERR_FAIL_COND_MSG(vararg, "Validated methods don't have ptrcall support. This is most likely an engine bug.");
 		ERR_FAIL_COND_MSG(vararg, "Validated methods don't have ptrcall support. This is most likely an engine bug.");
 		GDExtensionClassInstancePtr extension_instance = is_static() ? nullptr : p_object->_get_extension_instance();
 		GDExtensionClassInstancePtr extension_instance = is_static() ? nullptr : p_object->_get_extension_instance();
 
 
@@ -234,6 +252,9 @@ public:
 	}
 	}
 
 
 	virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override {
 	virtual void ptrcall(Object *p_object, const void **p_args, void *r_ret) const override {
+#ifdef TOOLS_ENABLED
+		ERR_FAIL_COND_MSG(!valid, vformat("Cannot call invalid GDExtension method bind '%s'. It's probably cached - you may need to restart Godot.", name));
+#endif
 		ERR_FAIL_COND_MSG(vararg, "Vararg methods don't have ptrcall support. This is most likely an engine bug.");
 		ERR_FAIL_COND_MSG(vararg, "Vararg methods don't have ptrcall support. This is most likely an engine bug.");
 		GDExtensionClassInstancePtr extension_instance = p_object->_get_extension_instance();
 		GDExtensionClassInstancePtr extension_instance = p_object->_get_extension_instance();
 		ptrcall_func(method_userdata, extension_instance, reinterpret_cast<GDExtensionConstTypePtr *>(p_args), (GDExtensionTypePtr)r_ret);
 		ptrcall_func(method_userdata, extension_instance, reinterpret_cast<GDExtensionConstTypePtr *>(p_args), (GDExtensionTypePtr)r_ret);
@@ -243,7 +264,43 @@ public:
 		return false;
 		return false;
 	}
 	}
 
 
-	explicit GDExtensionMethodBind(const GDExtensionClassMethodInfo *p_method_info) {
+#ifdef TOOLS_ENABLED
+	bool try_update(const GDExtensionClassMethodInfo *p_method_info) {
+		if (is_static() != (bool)(p_method_info->method_flags & GDEXTENSION_METHOD_FLAG_STATIC)) {
+			return false;
+		}
+
+		if (vararg != (bool)(p_method_info->method_flags & GDEXTENSION_METHOD_FLAG_VARARG)) {
+			return false;
+		}
+
+		if (has_return() != (bool)p_method_info->has_return_value) {
+			return false;
+		}
+
+		if (has_return() && return_value_info.type != (Variant::Type)p_method_info->return_value_info->type) {
+			return false;
+		}
+
+		if (argument_count != p_method_info->argument_count) {
+			return false;
+		}
+
+		for (uint32_t i = 0; i < p_method_info->argument_count; i++) {
+			if (arguments_info[i].type != (Variant::Type)p_method_info->arguments_info[i].type) {
+				return false;
+			}
+		}
+
+		update(p_method_info);
+		return true;
+	}
+#endif
+
+	void update(const GDExtensionClassMethodInfo *p_method_info) {
+#ifdef TOOLS_ENABLED
+		name = *reinterpret_cast<StringName *>(p_method_info->name);
+#endif
 		method_userdata = p_method_info->method_userdata;
 		method_userdata = p_method_info->method_userdata;
 		call_func = p_method_info->call_func;
 		call_func = p_method_info->call_func;
 		validated_call_func = nullptr;
 		validated_call_func = nullptr;
@@ -255,6 +312,8 @@ public:
 			return_value_metadata = GodotTypeInfo::Metadata(p_method_info->return_value_metadata);
 			return_value_metadata = GodotTypeInfo::Metadata(p_method_info->return_value_metadata);
 		}
 		}
 
 
+		arguments_info.clear();
+		arguments_metadata.clear();
 		for (uint32_t i = 0; i < p_method_info->argument_count; i++) {
 		for (uint32_t i = 0; i < p_method_info->argument_count; i++) {
 			arguments_info.push_back(PropertyInfo(p_method_info->arguments_info[i]));
 			arguments_info.push_back(PropertyInfo(p_method_info->arguments_info[i]));
 			arguments_metadata.push_back(GodotTypeInfo::Metadata(p_method_info->arguments_metadata[i]));
 			arguments_metadata.push_back(GodotTypeInfo::Metadata(p_method_info->arguments_metadata[i]));
@@ -279,6 +338,10 @@ public:
 
 
 		set_default_arguments(defargs);
 		set_default_arguments(defargs);
 	}
 	}
+
+	explicit GDExtensionMethodBind(const GDExtensionClassMethodInfo *p_method_info) {
+		update(p_method_info);
+	}
 };
 };
 
 
 #ifndef DISABLE_DEPRECATED
 #ifndef DISABLE_DEPRECATED
@@ -300,6 +363,7 @@ void GDExtension::_register_extension_class(GDExtensionClassLibraryPtr p_library
 		p_extension_funcs->unreference_func, // GDExtensionClassUnreference unreference_func;
 		p_extension_funcs->unreference_func, // GDExtensionClassUnreference unreference_func;
 		p_extension_funcs->create_instance_func, // GDExtensionClassCreateInstance create_instance_func; /* this one is mandatory */
 		p_extension_funcs->create_instance_func, // GDExtensionClassCreateInstance create_instance_func; /* this one is mandatory */
 		p_extension_funcs->free_instance_func, // GDExtensionClassFreeInstance free_instance_func; /* this one is mandatory */
 		p_extension_funcs->free_instance_func, // GDExtensionClassFreeInstance free_instance_func; /* this one is mandatory */
+		nullptr, // GDExtensionClassRecreateInstance recreate_instance_func;
 		p_extension_funcs->get_virtual_func, // GDExtensionClassGetVirtual get_virtual_func;
 		p_extension_funcs->get_virtual_func, // GDExtensionClassGetVirtual get_virtual_func;
 		nullptr, // GDExtensionClassGetVirtualCallData get_virtual_call_data_func;
 		nullptr, // GDExtensionClassGetVirtualCallData get_virtual_call_data_func;
 		nullptr, // GDExtensionClassCallVirtualWithData call_virtual_func;
 		nullptr, // GDExtensionClassCallVirtualWithData call_virtual_func;
@@ -341,15 +405,33 @@ void GDExtension::_register_extension_class_internal(GDExtensionClassLibraryPtr
 		ERR_FAIL_MSG("Attempt to register an extension class '" + String(class_name) + "' using non-existing parent class '" + String(parent_class_name) + "'");
 		ERR_FAIL_MSG("Attempt to register an extension class '" + String(class_name) + "' using non-existing parent class '" + String(parent_class_name) + "'");
 	}
 	}
 
 
+#ifdef TOOLS_ENABLED
+	Extension *extension = nullptr;
+	if (self->is_reloading && self->extension_classes.has(class_name)) {
+		extension = &self->extension_classes[class_name];
+		if (!parent_extension && parent_class_name != extension->gdextension.parent_class_name) {
+			ERR_FAIL_MSG(vformat("GDExtension class '%s' attempt to change parent type from '%s' to '%s' on hot reload. Restart Godot for this change to take effect.", class_name, extension->gdextension.parent_class_name, parent_class_name));
+		}
+		extension->is_reloading = false;
+	} else {
+		self->extension_classes[class_name] = Extension();
+		extension = &self->extension_classes[class_name];
+	}
+#else
 	self->extension_classes[class_name] = Extension();
 	self->extension_classes[class_name] = Extension();
-
 	Extension *extension = &self->extension_classes[class_name];
 	Extension *extension = &self->extension_classes[class_name];
+#endif
 
 
 	if (parent_extension) {
 	if (parent_extension) {
 		extension->gdextension.parent = &parent_extension->gdextension;
 		extension->gdextension.parent = &parent_extension->gdextension;
 		parent_extension->gdextension.children.push_back(&extension->gdextension);
 		parent_extension->gdextension.children.push_back(&extension->gdextension);
 	}
 	}
 
 
+	if (self->reloadable && p_extension_funcs->recreate_instance_func == nullptr) {
+		ERR_PRINT(vformat("Extension marked as reloadable, but attempted to register class '%s' which doesn't support reloading. Perhaps your language binding don't support it? Reloading disabled for this extension.", class_name));
+		self->reloadable = false;
+	}
+
 	extension->gdextension.library = self;
 	extension->gdextension.library = self;
 	extension->gdextension.parent_class_name = parent_class_name;
 	extension->gdextension.parent_class_name = parent_class_name;
 	extension->gdextension.class_name = class_name;
 	extension->gdextension.class_name = class_name;
@@ -376,11 +458,25 @@ void GDExtension::_register_extension_class_internal(GDExtensionClassLibraryPtr
 	extension->gdextension.class_userdata = p_extension_funcs->class_userdata;
 	extension->gdextension.class_userdata = p_extension_funcs->class_userdata;
 	extension->gdextension.create_instance = p_extension_funcs->create_instance_func;
 	extension->gdextension.create_instance = p_extension_funcs->create_instance_func;
 	extension->gdextension.free_instance = p_extension_funcs->free_instance_func;
 	extension->gdextension.free_instance = p_extension_funcs->free_instance_func;
+	extension->gdextension.recreate_instance = p_extension_funcs->recreate_instance_func;
 	extension->gdextension.get_virtual = p_extension_funcs->get_virtual_func;
 	extension->gdextension.get_virtual = p_extension_funcs->get_virtual_func;
 	extension->gdextension.get_virtual_call_data = p_extension_funcs->get_virtual_call_data_func;
 	extension->gdextension.get_virtual_call_data = p_extension_funcs->get_virtual_call_data_func;
 	extension->gdextension.call_virtual_with_data = p_extension_funcs->call_virtual_with_data_func;
 	extension->gdextension.call_virtual_with_data = p_extension_funcs->call_virtual_with_data_func;
 	extension->gdextension.get_rid = p_extension_funcs->get_rid_func;
 	extension->gdextension.get_rid = p_extension_funcs->get_rid_func;
 
 
+	extension->gdextension.reloadable = self->reloadable;
+#ifdef TOOLS_ENABLED
+	if (extension->gdextension.reloadable) {
+		extension->gdextension.tracking_userdata = extension;
+		extension->gdextension.track_instance = &GDExtension::_track_instance;
+		extension->gdextension.untrack_instance = &GDExtension::_untrack_instance;
+	} else {
+		extension->gdextension.tracking_userdata = nullptr;
+		extension->gdextension.track_instance = nullptr;
+		extension->gdextension.untrack_instance = nullptr;
+	}
+#endif
+
 	ClassDB::register_extension_class(&extension->gdextension);
 	ClassDB::register_extension_class(&extension->gdextension);
 }
 }
 
 
@@ -391,10 +487,39 @@ void GDExtension::_register_extension_class_method(GDExtensionClassLibraryPtr p_
 	StringName method_name = *reinterpret_cast<const StringName *>(p_method_info->name);
 	StringName method_name = *reinterpret_cast<const StringName *>(p_method_info->name);
 	ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension method '" + String(method_name) + "' for unexisting class '" + class_name + "'.");
 	ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension method '" + String(method_name) + "' for unexisting class '" + class_name + "'.");
 
 
-	//Extension *extension = &self->extension_classes[class_name];
+#ifdef TOOLS_ENABLED
+	Extension *extension = &self->extension_classes[class_name];
+	GDExtensionMethodBind *method = nullptr;
 
 
+	// If the extension is still marked as reloading, that means it failed to register again.
+	if (extension->is_reloading) {
+		return;
+	}
+
+	if (self->is_reloading && extension->methods.has(method_name)) {
+		method = extension->methods[method_name];
+
+		// Try to update the method bind. If it doesn't work (because it's incompatible) then
+		// mark as invalid and create a new one.
+		if (!method->is_reloading || !method->try_update(p_method_info)) {
+			method->valid = false;
+			self->invalid_methods.push_back(method);
+
+			method = nullptr;
+		}
+	}
+
+	if (method == nullptr) {
+		method = memnew(GDExtensionMethodBind(p_method_info));
+		method->set_instance_class(class_name);
+		extension->methods[method_name] = method;
+	} else {
+		method->is_reloading = false;
+	}
+#else
 	GDExtensionMethodBind *method = memnew(GDExtensionMethodBind(p_method_info));
 	GDExtensionMethodBind *method = memnew(GDExtensionMethodBind(p_method_info));
 	method->set_instance_class(class_name);
 	method->set_instance_class(class_name);
+#endif
 
 
 	ClassDB::bind_method_custom(class_name, method);
 	ClassDB::bind_method_custom(class_name, method);
 }
 }
@@ -406,6 +531,14 @@ void GDExtension::_register_extension_class_integer_constant(GDExtensionClassLib
 	StringName constant_name = *reinterpret_cast<const StringName *>(p_constant_name);
 	StringName constant_name = *reinterpret_cast<const StringName *>(p_constant_name);
 	ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension constant '" + constant_name + "' for unexisting class '" + class_name + "'.");
 	ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension constant '" + constant_name + "' for unexisting class '" + class_name + "'.");
 
 
+#ifdef TOOLS_ENABLED
+	// If the extension is still marked as reloading, that means it failed to register again.
+	Extension *extension = &self->extension_classes[class_name];
+	if (extension->is_reloading) {
+		return;
+	}
+#endif
+
 	ClassDB::bind_integer_constant(class_name, enum_name, constant_name, p_constant_value, p_is_bitfield);
 	ClassDB::bind_integer_constant(class_name, enum_name, constant_name, p_constant_value, p_is_bitfield);
 }
 }
 
 
@@ -422,6 +555,14 @@ void GDExtension::_register_extension_class_property_indexed(GDExtensionClassLib
 	String property_name = *reinterpret_cast<const StringName *>(p_info->name);
 	String property_name = *reinterpret_cast<const StringName *>(p_info->name);
 	ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class property '" + property_name + "' for unexisting class '" + class_name + "'.");
 	ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class property '" + property_name + "' for unexisting class '" + class_name + "'.");
 
 
+#ifdef TOOLS_ENABLED
+	// If the extension is still marked as reloading, that means it failed to register again.
+	Extension *extension = &self->extension_classes[class_name];
+	if (extension->is_reloading) {
+		return;
+	}
+#endif
+
 	PropertyInfo pinfo(*p_info);
 	PropertyInfo pinfo(*p_info);
 
 
 	ClassDB::add_property(class_name, pinfo, setter, getter, p_index);
 	ClassDB::add_property(class_name, pinfo, setter, getter, p_index);
@@ -435,6 +576,14 @@ void GDExtension::_register_extension_class_property_group(GDExtensionClassLibra
 	String prefix = *reinterpret_cast<const String *>(p_prefix);
 	String prefix = *reinterpret_cast<const String *>(p_prefix);
 	ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class property group '" + group_name + "' for unexisting class '" + class_name + "'.");
 	ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class property group '" + group_name + "' for unexisting class '" + class_name + "'.");
 
 
+#ifdef TOOLS_ENABLED
+	// If the extension is still marked as reloading, that means it failed to register again.
+	Extension *extension = &self->extension_classes[class_name];
+	if (extension->is_reloading) {
+		return;
+	}
+#endif
+
 	ClassDB::add_property_group(class_name, group_name, prefix);
 	ClassDB::add_property_group(class_name, group_name, prefix);
 }
 }
 
 
@@ -446,6 +595,14 @@ void GDExtension::_register_extension_class_property_subgroup(GDExtensionClassLi
 	String prefix = *reinterpret_cast<const String *>(p_prefix);
 	String prefix = *reinterpret_cast<const String *>(p_prefix);
 	ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class property subgroup '" + subgroup_name + "' for unexisting class '" + class_name + "'.");
 	ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class property subgroup '" + subgroup_name + "' for unexisting class '" + class_name + "'.");
 
 
+#ifdef TOOLS_ENABLED
+	// If the extension is still marked as reloading, that means it failed to register again.
+	Extension *extension = &self->extension_classes[class_name];
+	if (extension->is_reloading) {
+		return;
+	}
+#endif
+
 	ClassDB::add_property_subgroup(class_name, subgroup_name, prefix);
 	ClassDB::add_property_subgroup(class_name, subgroup_name, prefix);
 }
 }
 
 
@@ -456,6 +613,14 @@ void GDExtension::_register_extension_class_signal(GDExtensionClassLibraryPtr p_
 	StringName signal_name = *reinterpret_cast<const StringName *>(p_signal_name);
 	StringName signal_name = *reinterpret_cast<const StringName *>(p_signal_name);
 	ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class signal '" + signal_name + "' for unexisting class '" + class_name + "'.");
 	ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to register extension class signal '" + signal_name + "' for unexisting class '" + class_name + "'.");
 
 
+#ifdef TOOLS_ENABLED
+	// If the extension is still marked as reloading, that means it failed to register again.
+	Extension *extension = &self->extension_classes[class_name];
+	if (extension->is_reloading) {
+		return;
+	}
+#endif
+
 	MethodInfo s;
 	MethodInfo s;
 	s.name = signal_name;
 	s.name = signal_name;
 	for (int i = 0; i < p_argument_count; i++) {
 	for (int i = 0; i < p_argument_count; i++) {
@@ -470,14 +635,32 @@ void GDExtension::_unregister_extension_class(GDExtensionClassLibraryPtr p_libra
 
 
 	StringName class_name = *reinterpret_cast<const StringName *>(p_class_name);
 	StringName class_name = *reinterpret_cast<const StringName *>(p_class_name);
 	ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to unregister unexisting extension class '" + class_name + "'.");
 	ERR_FAIL_COND_MSG(!self->extension_classes.has(class_name), "Attempt to unregister unexisting extension class '" + class_name + "'.");
+
 	Extension *ext = &self->extension_classes[class_name];
 	Extension *ext = &self->extension_classes[class_name];
+#ifdef TOOLS_ENABLED
+	if (ext->is_reloading) {
+		self->_clear_extension(ext);
+	}
+#endif
 	ERR_FAIL_COND_MSG(ext->gdextension.children.size(), "Attempt to unregister class '" + class_name + "' while other extension classes inherit from it.");
 	ERR_FAIL_COND_MSG(ext->gdextension.children.size(), "Attempt to unregister class '" + class_name + "' while other extension classes inherit from it.");
 
 
+#ifdef TOOLS_ENABLED
+	ClassDB::unregister_extension_class(class_name, !ext->is_reloading);
+#else
 	ClassDB::unregister_extension_class(class_name);
 	ClassDB::unregister_extension_class(class_name);
+#endif
+
 	if (ext->gdextension.parent != nullptr) {
 	if (ext->gdextension.parent != nullptr) {
 		ext->gdextension.parent->children.erase(&ext->gdextension);
 		ext->gdextension.parent->children.erase(&ext->gdextension);
 	}
 	}
+
+#ifdef TOOLS_ENABLED
+	if (!ext->is_reloading) {
+		self->extension_classes.erase(class_name);
+	}
+#else
 	self->extension_classes.erase(class_name);
 	self->extension_classes.erase(class_name);
+#endif
 }
 }
 
 
 void GDExtension::_get_library_path(GDExtensionClassLibraryPtr p_library, GDExtensionUninitializedStringPtr r_path) {
 void GDExtension::_get_library_path(GDExtensionClassLibraryPtr p_library, GDExtensionUninitializedStringPtr r_path) {
@@ -540,6 +723,11 @@ void GDExtension::close_library() {
 #endif
 #endif
 
 
 	library = nullptr;
 	library = nullptr;
+	class_icon_paths.clear();
+
+#ifdef TOOLS_ENABLED
+	instance_bindings.clear();
+#endif
 }
 }
 
 
 bool GDExtension::is_library_open() const {
 bool GDExtension::is_library_open() const {
@@ -590,6 +778,12 @@ GDExtension::~GDExtension() {
 	if (library != nullptr) {
 	if (library != nullptr) {
 		close_library();
 		close_library();
 	}
 	}
+#ifdef TOOLS_ENABLED
+	// If we have any invalid method binds still laying around, we can finally free them!
+	for (GDExtensionMethodBind *E : invalid_methods) {
+		memdelete(E);
+	}
+#endif
 }
 }
 
 
 void GDExtension::initialize_gdextensions() {
 void GDExtension::initialize_gdextensions() {
@@ -610,27 +804,22 @@ void GDExtension::initialize_gdextensions() {
 	register_interface_function("get_library_path", (GDExtensionInterfaceFunctionPtr)&GDExtension::_get_library_path);
 	register_interface_function("get_library_path", (GDExtensionInterfaceFunctionPtr)&GDExtension::_get_library_path);
 }
 }
 
 
-Ref<Resource> GDExtensionResourceLoader::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
+Error GDExtensionResourceLoader::load_gdextension_resource(const String &p_path, Ref<GDExtension> &p_extension) {
+	ERR_FAIL_COND_V_MSG(p_extension.is_valid() && p_extension->is_library_open(), ERR_ALREADY_IN_USE, "Cannot load GDExtension resource into already opened library.");
+
 	Ref<ConfigFile> config;
 	Ref<ConfigFile> config;
 	config.instantiate();
 	config.instantiate();
 
 
 	Error err = config->load(p_path);
 	Error err = config->load(p_path);
 
 
-	if (r_error) {
-		*r_error = err;
-	}
-
 	if (err != OK) {
 	if (err != OK) {
 		ERR_PRINT("Error loading GDExtension configuration file: " + p_path);
 		ERR_PRINT("Error loading GDExtension configuration file: " + p_path);
-		return Ref<Resource>();
+		return err;
 	}
 	}
 
 
 	if (!config->has_section_key("configuration", "entry_symbol")) {
 	if (!config->has_section_key("configuration", "entry_symbol")) {
-		if (r_error) {
-			*r_error = ERR_INVALID_DATA;
-		}
 		ERR_PRINT("GDExtension configuration file must contain a \"configuration/entry_symbol\" key: " + p_path);
 		ERR_PRINT("GDExtension configuration file must contain a \"configuration/entry_symbol\" key: " + p_path);
-		return Ref<Resource>();
+		return ERR_INVALID_DATA;
 	}
 	}
 
 
 	String entry_symbol = config->get_value("configuration", "entry_symbol");
 	String entry_symbol = config->get_value("configuration", "entry_symbol");
@@ -648,19 +837,13 @@ Ref<Resource> GDExtensionResourceLoader::load(const String &p_path, const String
 			}
 			}
 		}
 		}
 	} else {
 	} else {
-		if (r_error) {
-			*r_error = ERR_INVALID_DATA;
-		}
 		ERR_PRINT("GDExtension configuration file must contain a \"configuration/compatibility_minimum\" key: " + p_path);
 		ERR_PRINT("GDExtension configuration file must contain a \"configuration/compatibility_minimum\" key: " + p_path);
-		return Ref<Resource>();
+		return ERR_INVALID_DATA;
 	}
 	}
 
 
 	if (compatibility_minimum[0] < 4 || (compatibility_minimum[0] == 4 && compatibility_minimum[1] == 0)) {
 	if (compatibility_minimum[0] < 4 || (compatibility_minimum[0] == 4 && compatibility_minimum[1] == 0)) {
-		if (r_error) {
-			*r_error = ERR_INVALID_DATA;
-		}
 		ERR_PRINT(vformat("GDExtension's compatibility_minimum (%d.%d.%d) must be at least 4.1.0: %s", compatibility_minimum[0], compatibility_minimum[1], compatibility_minimum[2], p_path));
 		ERR_PRINT(vformat("GDExtension's compatibility_minimum (%d.%d.%d) must be at least 4.1.0: %s", compatibility_minimum[0], compatibility_minimum[1], compatibility_minimum[2], p_path));
-		return Ref<Resource>();
+		return ERR_INVALID_DATA;
 	}
 	}
 
 
 	bool compatible = true;
 	bool compatible = true;
@@ -673,42 +856,42 @@ Ref<Resource> GDExtensionResourceLoader::load(const String &p_path, const String
 		compatible = VERSION_PATCH >= compatibility_minimum[2];
 		compatible = VERSION_PATCH >= compatibility_minimum[2];
 	}
 	}
 	if (!compatible) {
 	if (!compatible) {
-		if (r_error) {
-			*r_error = ERR_INVALID_DATA;
-		}
 		ERR_PRINT(vformat("GDExtension only compatible with Godot version %d.%d.%d or later: %s", compatibility_minimum[0], compatibility_minimum[1], compatibility_minimum[2], p_path));
 		ERR_PRINT(vformat("GDExtension only compatible with Godot version %d.%d.%d or later: %s", compatibility_minimum[0], compatibility_minimum[1], compatibility_minimum[2], p_path));
-		return Ref<Resource>();
+		return ERR_INVALID_DATA;
 	}
 	}
 
 
 	String library_path = GDExtension::find_extension_library(p_path, config, [](String p_feature) { return OS::get_singleton()->has_feature(p_feature); });
 	String library_path = GDExtension::find_extension_library(p_path, config, [](String p_feature) { return OS::get_singleton()->has_feature(p_feature); });
 
 
 	if (library_path.is_empty()) {
 	if (library_path.is_empty()) {
-		if (r_error) {
-			*r_error = ERR_FILE_NOT_FOUND;
-		}
 		const String os_arch = OS::get_singleton()->get_name().to_lower() + "." + Engine::get_singleton()->get_architecture_name();
 		const String os_arch = OS::get_singleton()->get_name().to_lower() + "." + Engine::get_singleton()->get_architecture_name();
 		ERR_PRINT(vformat("No GDExtension library found for current OS and architecture (%s) in configuration file: %s", os_arch, p_path));
 		ERR_PRINT(vformat("No GDExtension library found for current OS and architecture (%s) in configuration file: %s", os_arch, p_path));
-		return Ref<Resource>();
+		return ERR_FILE_NOT_FOUND;
 	}
 	}
 
 
 	if (!library_path.is_resource_file() && !library_path.is_absolute_path()) {
 	if (!library_path.is_resource_file() && !library_path.is_absolute_path()) {
 		library_path = p_path.get_base_dir().path_join(library_path);
 		library_path = p_path.get_base_dir().path_join(library_path);
 	}
 	}
 
 
-	Ref<GDExtension> lib;
-	lib.instantiate();
-	String abs_path = ProjectSettings::get_singleton()->globalize_path(library_path);
+	if (p_extension.is_null()) {
+		p_extension.instantiate();
+	}
 
 
+#ifdef TOOLS_ENABLED
+	p_extension->set_reloadable(config->get_value("configuration", "reloadable", false) && Engine::get_singleton()->is_extension_reloading_enabled());
+
+	p_extension->update_last_modified_time(MAX(
+			FileAccess::get_modified_time(library_path),
+			FileAccess::get_modified_time(p_path)));
+#endif
+
+	String abs_path = ProjectSettings::get_singleton()->globalize_path(library_path);
 #if defined(WINDOWS_ENABLED) && defined(TOOLS_ENABLED)
 #if defined(WINDOWS_ENABLED) && defined(TOOLS_ENABLED)
 	// If running on the editor on Windows, we copy the library and open the copy.
 	// If running on the editor on Windows, we copy the library and open the copy.
 	// This is so the original file isn't locked and can be updated by a compiler.
 	// This is so the original file isn't locked and can be updated by a compiler.
 	if (Engine::get_singleton()->is_editor_hint()) {
 	if (Engine::get_singleton()->is_editor_hint()) {
 		if (!FileAccess::exists(abs_path)) {
 		if (!FileAccess::exists(abs_path)) {
-			if (r_error) {
-				*r_error = ERR_FILE_NOT_FOUND;
-			}
 			ERR_PRINT("GDExtension library not found: " + library_path);
 			ERR_PRINT("GDExtension library not found: " + library_path);
-			return Ref<Resource>();
+			return ERR_FILE_NOT_FOUND;
 		}
 		}
 
 
 		// Copy the file to the same directory as the original with a prefix in the name.
 		// Copy the file to the same directory as the original with a prefix in the name.
@@ -722,36 +905,29 @@ Ref<Resource> GDExtensionResourceLoader::load(const String &p_path, const String
 
 
 		Error copy_err = DirAccess::copy_absolute(abs_path, copy_path);
 		Error copy_err = DirAccess::copy_absolute(abs_path, copy_path);
 		if (copy_err) {
 		if (copy_err) {
-			if (r_error) {
-				*r_error = ERR_CANT_CREATE;
-			}
 			ERR_PRINT("Error copying GDExtension library: " + library_path);
 			ERR_PRINT("Error copying GDExtension library: " + library_path);
-			return Ref<Resource>();
+			return ERR_CANT_CREATE;
 		}
 		}
 		FileAccess::set_hidden_attribute(copy_path, true);
 		FileAccess::set_hidden_attribute(copy_path, true);
 
 
 		// Save the copied path so it can be deleted later.
 		// Save the copied path so it can be deleted later.
-		lib->set_temp_library_path(copy_path);
+		p_extension->set_temp_library_path(copy_path);
 
 
 		// Use the copy to open the library.
 		// Use the copy to open the library.
 		abs_path = copy_path;
 		abs_path = copy_path;
 	}
 	}
 #endif
 #endif
-	err = lib->open_library(abs_path, entry_symbol);
-
-	if (r_error) {
-		*r_error = err;
-	}
 
 
+	err = p_extension->open_library(abs_path, entry_symbol);
 	if (err != OK) {
 	if (err != OK) {
 #if defined(WINDOWS_ENABLED) && defined(TOOLS_ENABLED)
 #if defined(WINDOWS_ENABLED) && defined(TOOLS_ENABLED)
 		// If the DLL fails to load, make sure that temporary DLL copies are cleaned up.
 		// If the DLL fails to load, make sure that temporary DLL copies are cleaned up.
 		if (Engine::get_singleton()->is_editor_hint()) {
 		if (Engine::get_singleton()->is_editor_hint()) {
-			DirAccess::remove_absolute(lib->get_temp_library_path());
+			DirAccess::remove_absolute(p_extension->get_temp_library_path());
 		}
 		}
 #endif
 #endif
 		// Errors already logged in open_library()
 		// Errors already logged in open_library()
-		return Ref<Resource>();
+		return err;
 	}
 	}
 
 
 	// Handle icons if any are specified.
 	// Handle icons if any are specified.
@@ -759,10 +935,20 @@ Ref<Resource> GDExtensionResourceLoader::load(const String &p_path, const String
 		List<String> keys;
 		List<String> keys;
 		config->get_section_keys("icons", &keys);
 		config->get_section_keys("icons", &keys);
 		for (const String &key : keys) {
 		for (const String &key : keys) {
-			lib->class_icon_paths[key] = config->get_value("icons", key);
+			p_extension->class_icon_paths[key] = config->get_value("icons", key);
 		}
 		}
 	}
 	}
 
 
+	return OK;
+}
+
+Ref<Resource> GDExtensionResourceLoader::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
+	Ref<GDExtension> lib;
+	Error err = load_gdextension_resource(p_path, lib);
+	if (err != OK && r_error) {
+		// Errors already logged in load_gdextension_resource().
+		*r_error = err;
+	}
 	return lib;
 	return lib;
 }
 }
 
 
@@ -783,6 +969,183 @@ String GDExtensionResourceLoader::get_resource_type(const String &p_path) const
 }
 }
 
 
 #ifdef TOOLS_ENABLED
 #ifdef TOOLS_ENABLED
+bool GDExtension::has_library_changed() const {
+	if (FileAccess::get_modified_time(get_path()) > last_modified_time) {
+		return true;
+	}
+	if (FileAccess::get_modified_time(library_path) > last_modified_time) {
+		return true;
+	}
+	return false;
+}
+
+void GDExtension::prepare_reload() {
+	is_reloading = true;
+
+	for (KeyValue<StringName, Extension> &E : extension_classes) {
+		E.value.is_reloading = true;
+
+		for (KeyValue<StringName, GDExtensionMethodBind *> &M : E.value.methods) {
+			M.value->is_reloading = true;
+		}
+
+		for (const ObjectID &obj_id : E.value.instances) {
+			Object *obj = ObjectDB::get_instance(obj_id);
+			if (!obj) {
+				continue;
+			}
+
+			// Store instance state so it can be restored after reload.
+			List<Pair<String, Variant>> state;
+			List<PropertyInfo> prop_list;
+			obj->get_property_list(&prop_list);
+			for (const PropertyInfo &P : prop_list) {
+				if (!(P.usage & PROPERTY_USAGE_STORAGE)) {
+					continue;
+				}
+
+				Variant value = obj->get(P.name);
+				Variant default_value = ClassDB::class_get_default_property_value(obj->get_class_name(), P.name);
+
+				if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, value, default_value))) {
+					continue;
+				}
+
+				if (P.type == Variant::OBJECT && value.is_zero() && !(P.usage & PROPERTY_USAGE_STORE_IF_NULL)) {
+					continue;
+				}
+
+				state.push_back(Pair<String, Variant>(P.name, value));
+			}
+			E.value.instance_state[obj_id] = state;
+		}
+	}
+}
+
+void GDExtension::_clear_extension(Extension *p_extension) {
+	// Clear out hierarchy information because it may change.
+	p_extension->gdextension.parent = nullptr;
+	p_extension->gdextension.children.clear();
+
+	// Clear all objects of any GDExtension data. It will become its native parent class
+	// until the reload can reset the object with the new GDExtension data.
+	for (const ObjectID &obj_id : p_extension->instances) {
+		Object *obj = ObjectDB::get_instance(obj_id);
+		if (!obj) {
+			continue;
+		}
+
+		obj->clear_internal_extension();
+	}
+}
+
+void GDExtension::track_instance_binding(Object *p_object) {
+	instance_bindings.push_back(p_object->get_instance_id());
+}
+
+void GDExtension::untrack_instance_binding(Object *p_object) {
+	instance_bindings.erase(p_object->get_instance_id());
+}
+
+void GDExtension::clear_instance_bindings() {
+	for (ObjectID obj_id : instance_bindings) {
+		Object *obj = ObjectDB::get_instance(obj_id);
+		if (!obj) {
+			continue;
+		}
+
+		obj->free_instance_binding(this);
+	}
+	instance_bindings.clear();
+}
+
+void GDExtension::finish_reload() {
+	is_reloading = false;
+
+	// Clean up any classes or methods that didn't get re-added.
+	Vector<StringName> classes_to_remove;
+	for (KeyValue<StringName, Extension> &E : extension_classes) {
+		if (E.value.is_reloading) {
+			E.value.is_reloading = false;
+			classes_to_remove.push_back(E.key);
+		}
+
+		Vector<StringName> methods_to_remove;
+		for (KeyValue<StringName, GDExtensionMethodBind *> &M : E.value.methods) {
+			if (M.value->is_reloading) {
+				M.value->valid = false;
+				invalid_methods.push_back(M.value);
+
+				M.value->is_reloading = false;
+				methods_to_remove.push_back(M.key);
+			}
+		}
+		for (const StringName &method_name : methods_to_remove) {
+			E.value.methods.erase(method_name);
+		}
+	}
+	for (const StringName &class_name : classes_to_remove) {
+		extension_classes.erase(class_name);
+	}
+
+	// Reset any the extension on instances made from the classes that remain.
+	for (KeyValue<StringName, Extension> &E : extension_classes) {
+		// Loop over 'instance_state' rather than 'instance' because new instances
+		// may have been created when re-initializing the extension.
+		for (const KeyValue<ObjectID, List<Pair<String, Variant>>> &S : E.value.instance_state) {
+			Object *obj = ObjectDB::get_instance(S.key);
+			if (!obj) {
+				continue;
+			}
+
+			obj->reset_internal_extension(&E.value.gdextension);
+		}
+	}
+
+	// Now that all the classes are back, restore the state.
+	for (KeyValue<StringName, Extension> &E : extension_classes) {
+		for (const KeyValue<ObjectID, List<Pair<String, Variant>>> &S : E.value.instance_state) {
+			Object *obj = ObjectDB::get_instance(S.key);
+			if (!obj) {
+				continue;
+			}
+
+			for (const Pair<String, Variant> &state : S.value) {
+				obj->set(state.first, state.second);
+			}
+		}
+	}
+
+	// Finally, let the objects know that we are done reloading them.
+	for (KeyValue<StringName, Extension> &E : extension_classes) {
+		for (const KeyValue<ObjectID, List<Pair<String, Variant>>> &S : E.value.instance_state) {
+			Object *obj = ObjectDB::get_instance(S.key);
+			if (!obj) {
+				continue;
+			}
+
+			obj->notification(NOTIFICATION_EXTENSION_RELOADED);
+		}
+
+		// Clear the instance state, we're done looping.
+		E.value.instance_state.clear();
+	}
+}
+
+void GDExtension::_track_instance(void *p_user_data, void *p_instance) {
+	Extension *extension = reinterpret_cast<Extension *>(p_user_data);
+	Object *obj = reinterpret_cast<Object *>(p_instance);
+
+	extension->instances.insert(obj->get_instance_id());
+}
+
+void GDExtension::_untrack_instance(void *p_user_data, void *p_instance) {
+	Extension *extension = reinterpret_cast<Extension *>(p_user_data);
+	Object *obj = reinterpret_cast<Object *>(p_instance);
+
+	extension->instances.erase(obj->get_instance_id());
+}
+
 Vector<StringName> GDExtensionEditorPlugins::extension_classes;
 Vector<StringName> GDExtensionEditorPlugins::extension_classes;
 GDExtensionEditorPlugins::EditorPluginRegisterFunc GDExtensionEditorPlugins::editor_node_add_plugin = nullptr;
 GDExtensionEditorPlugins::EditorPluginRegisterFunc GDExtensionEditorPlugins::editor_node_add_plugin = nullptr;
 GDExtensionEditorPlugins::EditorPluginRegisterFunc GDExtensionEditorPlugins::editor_node_remove_plugin = nullptr;
 GDExtensionEditorPlugins::EditorPluginRegisterFunc GDExtensionEditorPlugins::editor_node_remove_plugin = nullptr;

+ 44 - 0
core/extension/gdextension.h

@@ -38,17 +38,29 @@
 #include "core/io/resource_loader.h"
 #include "core/io/resource_loader.h"
 #include "core/object/ref_counted.h"
 #include "core/object/ref_counted.h"
 
 
+class GDExtensionMethodBind;
+
 class GDExtension : public Resource {
 class GDExtension : public Resource {
 	GDCLASS(GDExtension, Resource)
 	GDCLASS(GDExtension, Resource)
 
 
+	friend class GDExtensionManager;
+
 	void *library = nullptr; // pointer if valid,
 	void *library = nullptr; // pointer if valid,
 	String library_path;
 	String library_path;
 #if defined(WINDOWS_ENABLED) && defined(TOOLS_ENABLED)
 #if defined(WINDOWS_ENABLED) && defined(TOOLS_ENABLED)
 	String temp_lib_path;
 	String temp_lib_path;
 #endif
 #endif
+	bool reloadable = false;
 
 
 	struct Extension {
 	struct Extension {
 		ObjectGDExtension gdextension;
 		ObjectGDExtension gdextension;
+
+#ifdef TOOLS_ENABLED
+		bool is_reloading = false;
+		HashMap<StringName, GDExtensionMethodBind *> methods;
+		HashSet<ObjectID> instances;
+		HashMap<ObjectID, List<Pair<String, Variant>>> instance_state;
+#endif
 	};
 	};
 
 
 	HashMap<StringName, Extension> extension_classes;
 	HashMap<StringName, Extension> extension_classes;
@@ -77,6 +89,23 @@ class GDExtension : public Resource {
 	GDExtensionInitialization initialization;
 	GDExtensionInitialization initialization;
 	int32_t level_initialized = -1;
 	int32_t level_initialized = -1;
 
 
+#ifdef TOOLS_ENABLED
+	uint64_t last_modified_time = 0;
+	bool is_reloading = false;
+	Vector<GDExtensionMethodBind *> invalid_methods;
+	Vector<ObjectID> instance_bindings;
+
+	static void _track_instance(void *p_user_data, void *p_instance);
+	static void _untrack_instance(void *p_user_data, void *p_instance);
+
+	void _clear_extension(Extension *p_extension);
+
+	// Only called by GDExtensionManager during the reload process.
+	void prepare_reload();
+	void finish_reload();
+	void clear_instance_bindings();
+#endif
+
 protected:
 protected:
 	static void _bind_methods();
 	static void _bind_methods();
 
 
@@ -103,6 +132,19 @@ public:
 
 
 	bool is_library_open() const;
 	bool is_library_open() const;
 
 
+#ifdef TOOLS_ENABLED
+	bool is_reloadable() const { return reloadable; }
+	void set_reloadable(bool p_reloadable) { reloadable = p_reloadable; }
+
+	bool has_library_changed() const;
+	void update_last_modified_time(uint64_t p_last_modified_time) {
+		last_modified_time = MAX(last_modified_time, p_last_modified_time);
+	}
+
+	void track_instance_binding(Object *p_object);
+	void untrack_instance_binding(Object *p_object);
+#endif
+
 	InitializationLevel get_minimum_library_initialization_level() const;
 	InitializationLevel get_minimum_library_initialization_level() const;
 	void initialize_library(InitializationLevel p_level);
 	void initialize_library(InitializationLevel p_level);
 	void deinitialize_library(InitializationLevel p_level);
 	void deinitialize_library(InitializationLevel p_level);
@@ -119,6 +161,8 @@ VARIANT_ENUM_CAST(GDExtension::InitializationLevel)
 
 
 class GDExtensionResourceLoader : public ResourceFormatLoader {
 class GDExtensionResourceLoader : public ResourceFormatLoader {
 public:
 public:
+	static Error load_gdextension_resource(const String &p_path, Ref<GDExtension> &p_extension);
+
 	virtual Ref<Resource> load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE);
 	virtual Ref<Resource> load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE);
 	virtual void get_recognized_extensions(List<String> *p_extensions) const;
 	virtual void get_recognized_extensions(List<String> *p_extensions) const;
 	virtual bool handles_type(const String &p_type) const;
 	virtual bool handles_type(const String &p_type) const;

+ 2 - 0
core/extension/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 void (*GDExtensionClassCallVirtual)(GDExtensionClassInstancePtr p_instance, const GDExtensionConstTypePtr *p_args, GDExtensionTypePtr r_ret);
 typedef GDExtensionObjectPtr (*GDExtensionClassCreateInstance)(void *p_class_userdata);
 typedef GDExtensionObjectPtr (*GDExtensionClassCreateInstance)(void *p_class_userdata);
 typedef void (*GDExtensionClassFreeInstance)(void *p_class_userdata, GDExtensionClassInstancePtr p_instance);
 typedef void (*GDExtensionClassFreeInstance)(void *p_class_userdata, GDExtensionClassInstancePtr p_instance);
+typedef GDExtensionClassInstancePtr (*GDExtensionClassRecreateInstance)(void *p_class_userdata, GDExtensionObjectPtr p_object);
 typedef GDExtensionClassCallVirtual (*GDExtensionClassGetVirtual)(void *p_class_userdata, GDExtensionConstStringNamePtr p_name);
 typedef GDExtensionClassCallVirtual (*GDExtensionClassGetVirtual)(void *p_class_userdata, GDExtensionConstStringNamePtr p_name);
 typedef void *(*GDExtensionClassGetVirtualCallData)(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);
 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;
 	GDExtensionClassUnreference unreference_func;
 	GDExtensionClassCreateInstance create_instance_func; // (Default) constructor; mandatory. If the class is not instantiable, consider making it virtual or abstract.
 	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.
 	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.
 	// Queries a virtual function by name and returns a callback to invoke the requested virtual function.
 	GDExtensionClassGetVirtual get_virtual_func;
 	GDExtensionClassGetVirtual get_virtual_func;
 	// Paired with `call_virtual_with_data_func`, this is an alternative to `get_virtual_func` for extensions that
 	// Paired with `call_virtual_with_data_func`, this is an alternative to `get_virtual_func` for extensions that

+ 140 - 25
core/extension/gdextension_manager.cpp

@@ -32,58 +32,117 @@
 
 
 #include "core/extension/gdextension_compat_hashes.h"
 #include "core/extension/gdextension_compat_hashes.h"
 #include "core/io/file_access.h"
 #include "core/io/file_access.h"
+#include "core/object/script_language.h"
 
 
-GDExtensionManager::LoadStatus GDExtensionManager::load_extension(const String &p_path) {
-	if (gdextension_map.has(p_path)) {
-		return LOAD_STATUS_ALREADY_LOADED;
-	}
-	Ref<GDExtension> extension = ResourceLoader::load(p_path);
-	if (extension.is_null()) {
-		return LOAD_STATUS_FAILED;
-	}
-
+GDExtensionManager::LoadStatus GDExtensionManager::_load_extension_internal(const Ref<GDExtension> &p_extension) {
 	if (level >= 0) { // Already initialized up to some level.
 	if (level >= 0) { // Already initialized up to some level.
-		int32_t minimum_level = extension->get_minimum_library_initialization_level();
+		int32_t minimum_level = p_extension->get_minimum_library_initialization_level();
 		if (minimum_level < MIN(level, GDExtension::INITIALIZATION_LEVEL_SCENE)) {
 		if (minimum_level < MIN(level, GDExtension::INITIALIZATION_LEVEL_SCENE)) {
 			return LOAD_STATUS_NEEDS_RESTART;
 			return LOAD_STATUS_NEEDS_RESTART;
 		}
 		}
 		// Initialize up to current level.
 		// Initialize up to current level.
 		for (int32_t i = minimum_level; i <= level; i++) {
 		for (int32_t i = minimum_level; i <= level; i++) {
-			extension->initialize_library(GDExtension::InitializationLevel(i));
+			p_extension->initialize_library(GDExtension::InitializationLevel(i));
 		}
 		}
 	}
 	}
 
 
-	for (const KeyValue<String, String> &kv : extension->class_icon_paths) {
+	for (const KeyValue<String, String> &kv : p_extension->class_icon_paths) {
 		gdextension_class_icon_paths[kv.key] = kv.value;
 		gdextension_class_icon_paths[kv.key] = kv.value;
 	}
 	}
 
 
+	return LOAD_STATUS_OK;
+}
+
+GDExtensionManager::LoadStatus GDExtensionManager::_unload_extension_internal(const Ref<GDExtension> &p_extension) {
+	if (level >= 0) { // Already initialized up to some level.
+		// Deinitialize down from current level.
+		for (int32_t i = level; i >= GDExtension::INITIALIZATION_LEVEL_CORE; i--) {
+			p_extension->deinitialize_library(GDExtension::InitializationLevel(i));
+		}
+	}
+
+	for (const KeyValue<String, String> &kv : p_extension->class_icon_paths) {
+		gdextension_class_icon_paths.erase(kv.key);
+	}
+
+	return LOAD_STATUS_OK;
+}
+
+GDExtensionManager::LoadStatus GDExtensionManager::load_extension(const String &p_path) {
+	if (gdextension_map.has(p_path)) {
+		return LOAD_STATUS_ALREADY_LOADED;
+	}
+	Ref<GDExtension> extension = ResourceLoader::load(p_path);
+	if (extension.is_null()) {
+		return LOAD_STATUS_FAILED;
+	}
+
+	LoadStatus status = _load_extension_internal(extension);
+	if (status != LOAD_STATUS_OK) {
+		return status;
+	}
+
 	gdextension_map[p_path] = extension;
 	gdextension_map[p_path] = extension;
 	return LOAD_STATUS_OK;
 	return LOAD_STATUS_OK;
 }
 }
 
 
 GDExtensionManager::LoadStatus GDExtensionManager::reload_extension(const String &p_path) {
 GDExtensionManager::LoadStatus GDExtensionManager::reload_extension(const String &p_path) {
-	return LOAD_STATUS_OK; //TODO
-}
-GDExtensionManager::LoadStatus GDExtensionManager::unload_extension(const String &p_path) {
+#ifndef TOOLS_ENABLED
+	ERR_FAIL_V_MSG(LOAD_STATUS_FAILED, "GDExtensions can only be reloaded in an editor build.");
+#else
+	ERR_FAIL_COND_V_MSG(!Engine::get_singleton()->is_extension_reloading_enabled(), LOAD_STATUS_FAILED, "GDExtension reloading is disabled.");
+
 	if (!gdextension_map.has(p_path)) {
 	if (!gdextension_map.has(p_path)) {
 		return LOAD_STATUS_NOT_LOADED;
 		return LOAD_STATUS_NOT_LOADED;
 	}
 	}
 
 
 	Ref<GDExtension> extension = gdextension_map[p_path];
 	Ref<GDExtension> extension = gdextension_map[p_path];
+	ERR_FAIL_COND_V_MSG(!extension->is_reloadable(), LOAD_STATUS_FAILED, vformat("This GDExtension is not marked as 'reloadable' or doesn't support reloading: %s.", p_path));
 
 
-	if (level >= 0) { // Already initialized up to some level.
-		int32_t minimum_level = extension->get_minimum_library_initialization_level();
-		if (minimum_level < MIN(level, GDExtension::INITIALIZATION_LEVEL_SCENE)) {
-			return LOAD_STATUS_NEEDS_RESTART;
-		}
-		// Deinitialize down to current level.
-		for (int32_t i = level; i >= minimum_level; i--) {
-			extension->deinitialize_library(GDExtension::InitializationLevel(i));
+	LoadStatus status;
+
+	extension->prepare_reload();
+
+	// Unload library if it's open. It may not be open if the developer made a
+	// change that broke loading in a previous hot-reload attempt.
+	if (extension->is_library_open()) {
+		status = _unload_extension_internal(extension);
+		if (status != LOAD_STATUS_OK) {
+			// We need to clear these no matter what.
+			extension->clear_instance_bindings();
+			return status;
 		}
 		}
+
+		extension->clear_instance_bindings();
+		extension->close_library();
 	}
 	}
 
 
-	for (const KeyValue<String, String> &kv : extension->class_icon_paths) {
-		gdextension_class_icon_paths.erase(kv.key);
+	Error err = GDExtensionResourceLoader::load_gdextension_resource(p_path, extension);
+	if (err != OK) {
+		return LOAD_STATUS_FAILED;
+	}
+
+	status = _load_extension_internal(extension);
+	if (status != LOAD_STATUS_OK) {
+		return status;
+	}
+
+	extension->finish_reload();
+
+	return LOAD_STATUS_OK;
+#endif
+}
+
+GDExtensionManager::LoadStatus GDExtensionManager::unload_extension(const String &p_path) {
+	if (!gdextension_map.has(p_path)) {
+		return LOAD_STATUS_NOT_LOADED;
+	}
+
+	Ref<GDExtension> extension = gdextension_map[p_path];
+
+	LoadStatus status = _unload_extension_internal(extension);
+	if (status != LOAD_STATUS_OK) {
+		return status;
 	}
 	}
 
 
 	gdextension_map.erase(p_path);
 	gdextension_map.erase(p_path);
@@ -136,6 +195,36 @@ void GDExtensionManager::deinitialize_extensions(GDExtension::InitializationLeve
 	level = int32_t(p_level) - 1;
 	level = int32_t(p_level) - 1;
 }
 }
 
 
+#ifdef TOOLS_ENABLED
+void GDExtensionManager::track_instance_binding(void *p_token, Object *p_object) {
+	for (KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
+		if (E.value.ptr() == p_token) {
+			if (E.value->is_reloadable()) {
+				E.value->track_instance_binding(p_object);
+				return;
+			}
+		}
+	}
+}
+
+void GDExtensionManager::untrack_instance_binding(void *p_token, Object *p_object) {
+	for (KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
+		if (E.value.ptr() == p_token) {
+			if (E.value->is_reloadable()) {
+				E.value->untrack_instance_binding(p_object);
+				return;
+			}
+		}
+	}
+}
+
+void GDExtensionManager::_reload_all_scripts() {
+	for (int i = 0; i < ScriptServer::get_language_count(); i++) {
+		ScriptServer::get_language(i)->reload_all_scripts();
+	}
+}
+#endif // TOOLS_ENABLED
+
 void GDExtensionManager::load_extensions() {
 void GDExtensionManager::load_extensions() {
 	Ref<FileAccess> f = FileAccess::open(GDExtension::get_extension_list_config_file(), FileAccess::READ);
 	Ref<FileAccess> f = FileAccess::open(GDExtension::get_extension_list_config_file(), FileAccess::READ);
 	while (f.is_valid() && !f->eof_reached()) {
 	while (f.is_valid() && !f->eof_reached()) {
@@ -149,9 +238,33 @@ void GDExtensionManager::load_extensions() {
 	OS::get_singleton()->load_platform_gdextensions();
 	OS::get_singleton()->load_platform_gdextensions();
 }
 }
 
 
+void GDExtensionManager::reload_extensions() {
+#ifdef TOOLS_ENABLED
+	bool reloaded = false;
+	for (const KeyValue<String, Ref<GDExtension>> &E : gdextension_map) {
+		if (!E.value->is_reloadable()) {
+			continue;
+		}
+
+		if (E.value->has_library_changed()) {
+			reloaded = true;
+			reload_extension(E.value->get_path());
+		}
+	}
+
+	if (reloaded) {
+		emit_signal("extensions_reloaded");
+
+		// Reload all scripts to clear out old references.
+		callable_mp_static(&GDExtensionManager::_reload_all_scripts).call_deferred();
+	}
+#endif
+}
+
 GDExtensionManager *GDExtensionManager::get_singleton() {
 GDExtensionManager *GDExtensionManager::get_singleton() {
 	return singleton;
 	return singleton;
 }
 }
+
 void GDExtensionManager::_bind_methods() {
 void GDExtensionManager::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("load_extension", "path"), &GDExtensionManager::load_extension);
 	ClassDB::bind_method(D_METHOD("load_extension", "path"), &GDExtensionManager::load_extension);
 	ClassDB::bind_method(D_METHOD("reload_extension", "path"), &GDExtensionManager::reload_extension);
 	ClassDB::bind_method(D_METHOD("reload_extension", "path"), &GDExtensionManager::reload_extension);
@@ -166,6 +279,8 @@ void GDExtensionManager::_bind_methods() {
 	BIND_ENUM_CONSTANT(LOAD_STATUS_ALREADY_LOADED);
 	BIND_ENUM_CONSTANT(LOAD_STATUS_ALREADY_LOADED);
 	BIND_ENUM_CONSTANT(LOAD_STATUS_NOT_LOADED);
 	BIND_ENUM_CONSTANT(LOAD_STATUS_NOT_LOADED);
 	BIND_ENUM_CONSTANT(LOAD_STATUS_NEEDS_RESTART);
 	BIND_ENUM_CONSTANT(LOAD_STATUS_NEEDS_RESTART);
+
+	ADD_SIGNAL(MethodInfo("extensions_reloaded"));
 }
 }
 
 
 GDExtensionManager *GDExtensionManager::singleton = nullptr;
 GDExtensionManager *GDExtensionManager::singleton = nullptr;

+ 15 - 0
core/extension/gdextension_manager.h

@@ -53,6 +53,15 @@ public:
 		LOAD_STATUS_NEEDS_RESTART,
 		LOAD_STATUS_NEEDS_RESTART,
 	};
 	};
 
 
+private:
+	LoadStatus _load_extension_internal(const Ref<GDExtension> &p_extension);
+	LoadStatus _unload_extension_internal(const Ref<GDExtension> &p_extension);
+
+#ifdef TOOLS_ENABLED
+	static void _reload_all_scripts();
+#endif
+
+public:
 	LoadStatus load_extension(const String &p_path);
 	LoadStatus load_extension(const String &p_path);
 	LoadStatus reload_extension(const String &p_path);
 	LoadStatus reload_extension(const String &p_path);
 	LoadStatus unload_extension(const String &p_path);
 	LoadStatus unload_extension(const String &p_path);
@@ -66,9 +75,15 @@ public:
 	void initialize_extensions(GDExtension::InitializationLevel p_level);
 	void initialize_extensions(GDExtension::InitializationLevel p_level);
 	void deinitialize_extensions(GDExtension::InitializationLevel p_level);
 	void deinitialize_extensions(GDExtension::InitializationLevel p_level);
 
 
+#ifdef TOOLS_ENABLED
+	void track_instance_binding(void *p_token, Object *p_object);
+	void untrack_instance_binding(void *p_token, Object *p_object);
+#endif
+
 	static GDExtensionManager *get_singleton();
 	static GDExtensionManager *get_singleton();
 
 
 	void load_extensions();
 	void load_extensions();
+	void reload_extensions();
 
 
 	GDExtensionManager();
 	GDExtensionManager();
 };
 };

+ 21 - 4
core/object/class_db.cpp

@@ -349,7 +349,13 @@ Object *ClassDB::instantiate(const StringName &p_class) {
 	}
 	}
 #endif
 #endif
 	if (ti->gdextension && ti->gdextension->create_instance) {
 	if (ti->gdextension && ti->gdextension->create_instance) {
-		return (Object *)ti->gdextension->create_instance(ti->gdextension->class_userdata);
+		Object *obj = (Object *)ti->gdextension->create_instance(ti->gdextension->class_userdata);
+#ifdef TOOLS_ENABLED
+		if (ti->gdextension->track_instance) {
+			ti->gdextension->track_instance(ti->gdextension->tracking_userdata, obj);
+		}
+#endif
+		return obj;
 	} else {
 	} else {
 		return ti->creation_func();
 		return ti->creation_func();
 	}
 	}
@@ -1555,6 +1561,14 @@ bool ClassDB::is_class_exposed(const StringName &p_class) {
 	return ti->exposed;
 	return ti->exposed;
 }
 }
 
 
+bool ClassDB::is_class_reloadable(const StringName &p_class) {
+	OBJTYPE_RLOCK;
+
+	ClassInfo *ti = classes.getptr(p_class);
+	ERR_FAIL_NULL_V_MSG(ti, false, "Cannot get class '" + String(p_class) + "'.");
+	return ti->reloadable;
+}
+
 void ClassDB::add_resource_base_extension(const StringName &p_extension, const StringName &p_class) {
 void ClassDB::add_resource_base_extension(const StringName &p_extension, const StringName &p_class) {
 	if (resource_base_extensions.has(p_extension)) {
 	if (resource_base_extensions.has(p_extension)) {
 		return;
 		return;
@@ -1693,15 +1707,18 @@ void ClassDB::register_extension_class(ObjectGDExtension *p_extension) {
 			parent = classes.getptr(parent->name);
 			parent = classes.getptr(parent->name);
 		}
 		}
 	}
 	}
+	c.reloadable = p_extension->reloadable;
 
 
 	classes[p_extension->class_name] = c;
 	classes[p_extension->class_name] = c;
 }
 }
 
 
-void ClassDB::unregister_extension_class(const StringName &p_class) {
+void ClassDB::unregister_extension_class(const StringName &p_class, bool p_free_method_binds) {
 	ClassInfo *c = classes.getptr(p_class);
 	ClassInfo *c = classes.getptr(p_class);
 	ERR_FAIL_NULL_MSG(c, "Class '" + String(p_class) + "' does not exist.");
 	ERR_FAIL_NULL_MSG(c, "Class '" + String(p_class) + "' does not exist.");
-	for (KeyValue<StringName, MethodBind *> &F : c->method_map) {
-		memdelete(F.value);
+	if (p_free_method_binds) {
+		for (KeyValue<StringName, MethodBind *> &F : c->method_map) {
+			memdelete(F.value);
+		}
 	}
 	}
 	classes.erase(p_class);
 	classes.erase(p_class);
 }
 }

+ 3 - 1
core/object/class_db.h

@@ -131,6 +131,7 @@ public:
 		StringName name;
 		StringName name;
 		bool disabled = false;
 		bool disabled = false;
 		bool exposed = false;
 		bool exposed = false;
+		bool reloadable = false;
 		bool is_virtual = false;
 		bool is_virtual = false;
 		Object *(*creation_func)() = nullptr;
 		Object *(*creation_func)() = nullptr;
 
 
@@ -228,7 +229,7 @@ public:
 	}
 	}
 
 
 	static void register_extension_class(ObjectGDExtension *p_extension);
 	static void register_extension_class(ObjectGDExtension *p_extension);
-	static void unregister_extension_class(const StringName &p_class);
+	static void unregister_extension_class(const StringName &p_class, bool p_free_method_binds = true);
 
 
 	template <class T>
 	template <class T>
 	static Object *_create_ptr_func() {
 	static Object *_create_ptr_func() {
@@ -426,6 +427,7 @@ public:
 	static bool is_class_enabled(const StringName &p_class);
 	static bool is_class_enabled(const StringName &p_class);
 
 
 	static bool is_class_exposed(const StringName &p_class);
 	static bool is_class_exposed(const StringName &p_class);
+	static bool is_class_reloadable(const StringName &p_class);
 
 
 	static void add_resource_base_extension(const StringName &p_extension, const StringName &p_class);
 	static void add_resource_base_extension(const StringName &p_extension, const StringName &p_class);
 	static void get_resource_base_extensions(List<String> *p_extensions);
 	static void get_resource_base_extensions(List<String> *p_extensions);

+ 14 - 0
core/object/make_virtuals.py

@@ -22,6 +22,7 @@ _FORCE_INLINE_ bool _gdvirtual_##m_name##_call($CALLARGS) $CONST { \\
         } else if (_get_extension()->get_virtual) {\\
         } else if (_get_extension()->get_virtual) {\\
             _gdvirtual_##m_name = (void *)_get_extension()->get_virtual(_get_extension()->class_userdata, &_gdvirtual_##m_name##_sn);\\
             _gdvirtual_##m_name = (void *)_get_extension()->get_virtual(_get_extension()->class_userdata, &_gdvirtual_##m_name##_sn);\\
         }\\
         }\\
+        GDVIRTUAL_TRACK(_gdvirtual_##m_name, _gdvirtual_##m_name##_initialized); \\
         _gdvirtual_##m_name##_initialized = true;\\
         _gdvirtual_##m_name##_initialized = true;\\
     }\\
     }\\
 	if (_gdvirtual_##m_name) {\\
 	if (_gdvirtual_##m_name) {\\
@@ -56,6 +57,7 @@ _FORCE_INLINE_ bool _gdvirtual_##m_name##_overridden() const { \\
         } else if (_get_extension()->get_virtual) {\\
         } else if (_get_extension()->get_virtual) {\\
              _gdvirtual_##m_name = (void *)_get_extension()->get_virtual(_get_extension()->class_userdata, &_gdvirtual_##m_name##_sn);\\
              _gdvirtual_##m_name = (void *)_get_extension()->get_virtual(_get_extension()->class_userdata, &_gdvirtual_##m_name##_sn);\\
         }\\
         }\\
+        GDVIRTUAL_TRACK(_gdvirtual_##m_name, _gdvirtual_##m_name##_initialized); \\
         _gdvirtual_##m_name##_initialized = true;\\
         _gdvirtual_##m_name##_initialized = true;\\
     }\\
     }\\
 	if (_gdvirtual_##m_name) {\\
 	if (_gdvirtual_##m_name) {\\
@@ -175,6 +177,18 @@ def run(target, source, env):
 
 
 #include "core/object/script_instance.h"
 #include "core/object/script_instance.h"
 
 
+#ifdef TOOLS_ENABLED
+#define GDVIRTUAL_TRACK(m_virtual, m_initialized) \\
+    if (_get_extension()->reloadable) {\\
+        VirtualMethodTracker *tracker = memnew(VirtualMethodTracker);\\
+        tracker->method = (void **)&m_virtual;\\
+        tracker->initialized = &m_initialized;\\
+        tracker->next = virtual_method_list;\\
+        virtual_method_list = tracker;\\
+    }
+#else
+#define GDVIRTUAL_TRACK(m_virtual, m_initialized)
+#endif
 
 
 """
 """
 
 

+ 4 - 0
core/object/method_bind.h

@@ -111,6 +111,10 @@ public:
 
 
 	_FORCE_INLINE_ int get_argument_count() const { return argument_count; };
 	_FORCE_INLINE_ int get_argument_count() const { return argument_count; };
 
 
+#ifdef TOOLS_ENABLED
+	virtual bool is_valid() const { return true; }
+#endif
+
 	virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const = 0;
 	virtual Variant call(Object *p_object, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) const = 0;
 	virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const = 0;
 	virtual void validated_call(Object *p_object, const Variant **p_args, Variant *r_ret) const = 0;
 
 

+ 95 - 6
core/object/object.cpp

@@ -31,6 +31,7 @@
 #include "object.h"
 #include "object.h"
 
 
 #include "core/core_string_names.h"
 #include "core/core_string_names.h"
+#include "core/extension/gdextension_manager.h"
 #include "core/io/resource.h"
 #include "core/io/resource.h"
 #include "core/object/class_db.h"
 #include "core/object/class_db.h"
 #include "core/object/message_queue.h"
 #include "core/object/message_queue.h"
@@ -1794,14 +1795,17 @@ StringName Object::get_class_name_for_extension(const GDExtension *p_library) co
 }
 }
 
 
 void Object::set_instance_binding(void *p_token, void *p_binding, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
 void Object::set_instance_binding(void *p_token, void *p_binding, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
-	// This is only meant to be used on creation by the binder.
-	ERR_FAIL_COND(_instance_bindings != nullptr);
-	_instance_bindings = (InstanceBinding *)memalloc(sizeof(InstanceBinding));
+	// This is only meant to be used on creation by the binder, but we also
+	// need to account for reloading (where the 'binding' will be cleared).
+	ERR_FAIL_COND(_instance_bindings != nullptr && _instance_bindings[0].binding != nullptr);
+	if (_instance_bindings == nullptr) {
+		_instance_bindings = (InstanceBinding *)memalloc(sizeof(InstanceBinding));
+		_instance_binding_count = 1;
+	}
 	_instance_bindings[0].binding = p_binding;
 	_instance_bindings[0].binding = p_binding;
 	_instance_bindings[0].free_callback = p_callbacks->free_callback;
 	_instance_bindings[0].free_callback = p_callbacks->free_callback;
 	_instance_bindings[0].reference_callback = p_callbacks->reference_callback;
 	_instance_bindings[0].reference_callback = p_callbacks->reference_callback;
 	_instance_bindings[0].token = p_token;
 	_instance_bindings[0].token = p_token;
-	_instance_binding_count = 1;
 }
 }
 
 
 void *Object::get_instance_binding(void *p_token, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
 void *Object::get_instance_binding(void *p_token, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
@@ -1828,6 +1832,12 @@ void *Object::get_instance_binding(void *p_token, const GDExtensionInstanceBindi
 		binding = p_callbacks->create_callback(p_token, this);
 		binding = p_callbacks->create_callback(p_token, this);
 		_instance_bindings[_instance_binding_count].binding = binding;
 		_instance_bindings[_instance_binding_count].binding = binding;
 
 
+#ifdef TOOLS_ENABLED
+		if (!_extension && Engine::get_singleton()->is_extension_reloading_enabled()) {
+			GDExtensionManager::get_singleton()->track_instance_binding(p_token, this);
+		}
+#endif
+
 		_instance_binding_count++;
 		_instance_binding_count++;
 	}
 	}
 
 
@@ -1851,6 +1861,71 @@ bool Object::has_instance_binding(void *p_token) {
 	return found;
 	return found;
 }
 }
 
 
+#ifdef TOOLS_ENABLED
+void Object::free_instance_binding(void *p_token) {
+	bool found = false;
+	_instance_binding_mutex.lock();
+	for (uint32_t i = 0; i < _instance_binding_count; i++) {
+		if (!found && _instance_bindings[i].token == p_token) {
+			if (_instance_bindings[i].free_callback) {
+				_instance_bindings[i].free_callback(_instance_bindings[i].token, this, _instance_bindings[i].binding);
+			}
+			found = true;
+		}
+		if (found) {
+			if (i + 1 < _instance_binding_count) {
+				_instance_bindings[i] = _instance_bindings[i + 1];
+			} else {
+				_instance_bindings[i] = { nullptr };
+			}
+		}
+	}
+	if (found) {
+		_instance_binding_count--;
+	}
+	_instance_binding_mutex.unlock();
+}
+
+void Object::clear_internal_extension() {
+	ERR_FAIL_NULL(_extension);
+
+	// Free the instance inside the GDExtension.
+	if (_extension->free_instance) {
+		_extension->free_instance(_extension->class_userdata, _extension_instance);
+	}
+	_extension = nullptr;
+	_extension_instance = nullptr;
+
+	// Clear the instance bindings.
+	_instance_binding_mutex.lock();
+	if (_instance_bindings[0].free_callback) {
+		_instance_bindings[0].free_callback(_instance_bindings[0].token, this, _instance_bindings[0].binding);
+	}
+	_instance_bindings[0].binding = nullptr;
+	_instance_bindings[0].token = nullptr;
+	_instance_bindings[0].free_callback = nullptr;
+	_instance_bindings[0].reference_callback = nullptr;
+	_instance_binding_mutex.unlock();
+
+	// Clear the virtual methods.
+	while (virtual_method_list) {
+		(*virtual_method_list->method) = nullptr;
+		(*virtual_method_list->initialized) = false;
+		virtual_method_list = virtual_method_list->next;
+	}
+}
+
+void Object::reset_internal_extension(ObjectGDExtension *p_extension) {
+	ERR_FAIL_COND(_extension != nullptr);
+
+	if (p_extension) {
+		_extension_instance = p_extension->recreate_instance ? p_extension->recreate_instance(p_extension->class_userdata, (GDExtensionObjectPtr)this) : nullptr;
+		ERR_FAIL_NULL_MSG(_extension_instance, "Unable to recreate GDExtension instance - does this extension support hot reloading?");
+		_extension = p_extension;
+	}
+}
+#endif
+
 void Object::_construct_object(bool p_reference) {
 void Object::_construct_object(bool p_reference) {
 	type_is_reference = p_reference;
 	type_is_reference = p_reference;
 	_instance_id = ObjectDB::add_instance(this);
 	_instance_id = ObjectDB::add_instance(this);
@@ -1881,11 +1956,25 @@ Object::~Object() {
 	}
 	}
 	script_instance = nullptr;
 	script_instance = nullptr;
 
 
-	if (_extension && _extension->free_instance) {
-		_extension->free_instance(_extension->class_userdata, _extension_instance);
+	if (_extension) {
+#ifdef TOOLS_ENABLED
+		if (_extension->untrack_instance) {
+			_extension->untrack_instance(_extension->tracking_userdata, this);
+		}
+#endif
+		if (_extension->free_instance) {
+			_extension->free_instance(_extension->class_userdata, _extension_instance);
+		}
 		_extension = nullptr;
 		_extension = nullptr;
 		_extension_instance = nullptr;
 		_extension_instance = nullptr;
 	}
 	}
+#ifdef TOOLS_ENABLED
+	else if (_instance_bindings != nullptr && Engine::get_singleton()->is_extension_reloading_enabled()) {
+		for (uint32_t i = 0; i < _instance_binding_count; i++) {
+			GDExtensionManager::get_singleton()->untrack_instance_binding(_instance_bindings[i].token, this);
+		}
+	}
+#endif
 
 
 	if (_emitting) {
 	if (_emitting) {
 		//@todo this may need to actually reach the debugger prioritarily somehow because it may crash before
 		//@todo this may need to actually reach the debugger prioritarily somehow because it may crash before

+ 26 - 1
core/object/object.h

@@ -313,6 +313,7 @@ struct ObjectGDExtension {
 	StringName parent_class_name;
 	StringName parent_class_name;
 	StringName class_name;
 	StringName class_name;
 	bool editor_class = false;
 	bool editor_class = false;
+	bool reloadable = false;
 	bool is_virtual = false;
 	bool is_virtual = false;
 	bool is_abstract = false;
 	bool is_abstract = false;
 	bool is_exposed = true;
 	bool is_exposed = true;
@@ -349,6 +350,13 @@ struct ObjectGDExtension {
 	GDExtensionClassGetVirtual get_virtual;
 	GDExtensionClassGetVirtual get_virtual;
 	GDExtensionClassGetVirtualCallData get_virtual_call_data;
 	GDExtensionClassGetVirtualCallData get_virtual_call_data;
 	GDExtensionClassCallVirtualWithData call_virtual_with_data;
 	GDExtensionClassCallVirtualWithData call_virtual_with_data;
+	GDExtensionClassRecreateInstance recreate_instance;
+
+#ifdef TOOLS_ENABLED
+	void *tracking_userdata = nullptr;
+	void (*track_instance)(void *p_userdata, void *p_instance);
+	void (*untrack_instance)(void *p_userdata, void *p_instance);
+#endif
 };
 };
 
 
 #define GDVIRTUAL_CALL(m_name, ...) _gdvirtual_##m_name##_call<false>(__VA_ARGS__)
 #define GDVIRTUAL_CALL(m_name, ...) _gdvirtual_##m_name##_call<false>(__VA_ARGS__)
@@ -750,6 +758,16 @@ protected:
 
 
 	bool _disconnect(const StringName &p_signal, const Callable &p_callable, bool p_force = false);
 	bool _disconnect(const StringName &p_signal, const Callable &p_callable, bool p_force = false);
 
 
+#ifdef TOOLS_ENABLED
+	struct VirtualMethodTracker {
+		void **method;
+		bool *initialized;
+		VirtualMethodTracker *next;
+	};
+
+	mutable VirtualMethodTracker *virtual_method_list = nullptr;
+#endif
+
 public: // Should be protected, but bug in clang++.
 public: // Should be protected, but bug in clang++.
 	static void initialize_class();
 	static void initialize_class();
 	_FORCE_INLINE_ static void register_custom_data_to_otdb() {}
 	_FORCE_INLINE_ static void register_custom_data_to_otdb() {}
@@ -781,7 +799,8 @@ public:
 
 
 	enum {
 	enum {
 		NOTIFICATION_POSTINITIALIZE = 0,
 		NOTIFICATION_POSTINITIALIZE = 0,
-		NOTIFICATION_PREDELETE = 1
+		NOTIFICATION_PREDELETE = 1,
+		NOTIFICATION_EXTENSION_RELOADED = 2,
 	};
 	};
 
 
 	/* TYPE API */
 	/* TYPE API */
@@ -952,6 +971,12 @@ public:
 	void set_instance_binding(void *p_token, void *p_binding, const GDExtensionInstanceBindingCallbacks *p_callbacks);
 	void set_instance_binding(void *p_token, void *p_binding, const GDExtensionInstanceBindingCallbacks *p_callbacks);
 	bool has_instance_binding(void *p_token);
 	bool has_instance_binding(void *p_token);
 
 
+#ifdef TOOLS_ENABLED
+	void free_instance_binding(void *p_token);
+	void clear_internal_extension();
+	void reset_internal_extension(ObjectGDExtension *p_extension);
+#endif
+
 	void clear_internal_resource_paths();
 	void clear_internal_resource_paths();
 
 
 	_ALWAYS_INLINE_ bool is_ref_counted() const { return type_is_reference; }
 	_ALWAYS_INLINE_ bool is_ref_counted() const { return type_is_reference; }

+ 7 - 0
doc/classes/GDExtensionManager.xml

@@ -43,6 +43,13 @@
 			</description>
 			</description>
 		</method>
 		</method>
 	</methods>
 	</methods>
+	<signals>
+		<signal name="extensions_reloaded">
+			<description>
+				Emitted after the editor has automatically reloaded any extensions.
+			</description>
+		</signal>
+	</signals>
 	<constants>
 	<constants>
 		<constant name="LOAD_STATUS_OK" value="0" enum="LoadStatus">
 		<constant name="LOAD_STATUS_OK" value="0" enum="LoadStatus">
 		</constant>
 		</constant>

+ 11 - 1
editor/editor_node.cpp

@@ -31,6 +31,7 @@
 #include "editor_node.h"
 #include "editor_node.h"
 
 
 #include "core/config/project_settings.h"
 #include "core/config/project_settings.h"
+#include "core/extension/gdextension_manager.h"
 #include "core/input/input.h"
 #include "core/input/input.h"
 #include "core/io/config_file.h"
 #include "core/io/config_file.h"
 #include "core/io/file_access.h"
 #include "core/io/file_access.h"
@@ -435,6 +436,11 @@ void EditorNode::_update_from_settings() {
 #endif // DEBUG_ENABLED
 #endif // DEBUG_ENABLED
 }
 }
 
 
+void EditorNode::_gdextensions_reloaded() {
+	// In case the developer is inspecting an object that will be changed by the reload.
+	InspectorDock::get_inspector_singleton()->update_tree();
+}
+
 void EditorNode::_select_default_main_screen_plugin() {
 void EditorNode::_select_default_main_screen_plugin() {
 	if (EDITOR_3D < main_editor_buttons.size() && main_editor_buttons[EDITOR_3D]->is_visible()) {
 	if (EDITOR_3D < main_editor_buttons.size() && main_editor_buttons[EDITOR_3D]->is_visible()) {
 		// If the 3D editor is enabled, use this as the default.
 		// If the 3D editor is enabled, use this as the default.
@@ -714,6 +720,9 @@ void EditorNode::_notification(int p_what) {
 
 
 			EditorFileSystem::get_singleton()->scan_changes();
 			EditorFileSystem::get_singleton()->scan_changes();
 			_scan_external_changes();
 			_scan_external_changes();
+
+			GDExtensionManager *gdextension_manager = GDExtensionManager::get_singleton();
+			callable_mp(gdextension_manager, &GDExtensionManager::reload_extensions).call_deferred();
 		} break;
 		} break;
 
 
 		case NOTIFICATION_APPLICATION_FOCUS_OUT: {
 		case NOTIFICATION_APPLICATION_FOCUS_OUT: {
@@ -3269,7 +3278,7 @@ void EditorNode::remove_extension_editor_plugin(const StringName &p_class_name)
 
 
 	EditorPlugin *plugin = singleton->editor_data.get_extension_editor_plugin(p_class_name);
 	EditorPlugin *plugin = singleton->editor_data.get_extension_editor_plugin(p_class_name);
 	remove_editor_plugin(plugin);
 	remove_editor_plugin(plugin);
-	memfree(plugin);
+	memdelete(plugin);
 	singleton->editor_data.remove_extension_editor_plugin(p_class_name);
 	singleton->editor_data.remove_extension_editor_plugin(p_class_name);
 }
 }
 
 
@@ -6710,6 +6719,7 @@ EditorNode::EditorNode() {
 	EditorUndoRedoManager::get_singleton()->connect("version_changed", callable_mp(this, &EditorNode::_update_undo_redo_allowed));
 	EditorUndoRedoManager::get_singleton()->connect("version_changed", callable_mp(this, &EditorNode::_update_undo_redo_allowed));
 	EditorUndoRedoManager::get_singleton()->connect("history_changed", callable_mp(this, &EditorNode::_update_undo_redo_allowed));
 	EditorUndoRedoManager::get_singleton()->connect("history_changed", callable_mp(this, &EditorNode::_update_undo_redo_allowed));
 	ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &EditorNode::_update_from_settings));
 	ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &EditorNode::_update_from_settings));
+	GDExtensionManager::get_singleton()->connect("extensions_reloaded", callable_mp(this, &EditorNode::_gdextensions_reloaded));
 
 
 	TranslationServer::get_singleton()->set_enabled(false);
 	TranslationServer::get_singleton()->set_enabled(false);
 	// Load settings.
 	// Load settings.

+ 1 - 0
editor/editor_node.h

@@ -597,6 +597,7 @@ private:
 	void _add_dropped_files_recursive(const Vector<String> &p_files, String to_path);
 	void _add_dropped_files_recursive(const Vector<String> &p_files, String to_path);
 
 
 	void _update_from_settings();
 	void _update_from_settings();
+	void _gdextensions_reloaded();
 
 
 	void _renderer_selected(int);
 	void _renderer_selected(int);
 	void _update_renderer_color();
 	void _update_renderer_color();

+ 1 - 0
main/main.cpp

@@ -1605,6 +1605,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
 #ifdef TOOLS_ENABLED
 #ifdef TOOLS_ENABLED
 	if (editor) {
 	if (editor) {
 		Engine::get_singleton()->set_editor_hint(true);
 		Engine::get_singleton()->set_editor_hint(true);
+		Engine::get_singleton()->set_extension_reloading_enabled(true);
 	}
 	}
 #endif
 #endif