Skip to content

Commit 102867a

Browse files
committed
Changes necessary for hot reload to work
1 parent 5eebc6b commit 102867a

File tree

7 files changed

+61
-0
lines changed

7 files changed

+61
-0
lines changed

SConstruct

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ opts.Add(
173173
)
174174
)
175175

176+
opts.Add(
177+
BoolVariable(
178+
key="use_hot_reload",
179+
help="Enable the extra accounting required to support hot reload.",
180+
default=(env.get("target", "template_debug") != "template_release"),
181+
)
182+
)
183+
176184
# Add platform options
177185
tools = {}
178186
for pl in platforms:
@@ -238,6 +246,9 @@ if env["arch"] == "":
238246
print("Unsupported CPU architecture: " + host_machine)
239247
Exit()
240248

249+
if env["use_hot_reload"]:
250+
env.Append(CPPDEFINES=["HOT_RELOAD_ENABLED"])
251+
241252
tool = Tool(env["platform"], toolpath=["tools"])
242253

243254
if tool is None or not tool.exists(env):

gdextension/gdextension_interface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ typedef void (*GDExtensionClassReference)(GDExtensionClassInstancePtr p_instance
264264
typedef void (*GDExtensionClassUnreference)(GDExtensionClassInstancePtr p_instance);
265265
typedef void (*GDExtensionClassCallVirtual)(GDExtensionClassInstancePtr p_instance, const GDExtensionConstTypePtr *p_args, GDExtensionTypePtr r_ret);
266266
typedef GDExtensionObjectPtr (*GDExtensionClassCreateInstance)(void *p_userdata);
267+
typedef GDExtensionClassInstancePtr (*GDExtensionClassRecreateInstance)(void *p_userdata, GDExtensionObjectPtr p_object);
267268
typedef void (*GDExtensionClassFreeInstance)(void *p_userdata, GDExtensionClassInstancePtr p_instance);
268269
typedef GDExtensionClassCallVirtual (*GDExtensionClassGetVirtual)(void *p_userdata, GDExtensionConstStringNamePtr p_name);
269270

@@ -285,6 +286,7 @@ typedef struct {
285286
GDExtensionClassGetVirtual get_virtual_func; // Queries a virtual function by name and returns a callback to invoke the requested virtual function.
286287
GDExtensionClassGetRID get_rid_func;
287288
void *class_userdata; // Per-class user data, later accessible in instance bindings.
289+
GDExtensionClassRecreateInstance recreate_instance_func;
288290
} GDExtensionClassCreationInfo;
289291

290292
typedef void *GDExtensionClassLibraryPtr;

include/godot_cpp/classes/wrapped.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ class Wrapped {
5151
friend void postinitialize_handler(Wrapped *);
5252

5353
protected:
54+
#ifdef HOT_RELOAD_ENABLED
55+
struct RecreateInstance {
56+
GDExtensionClassInstancePtr wrapper;
57+
GDExtensionObjectPtr owner;
58+
RecreateInstance *next;
59+
};
60+
inline static RecreateInstance *recreate_instance = nullptr;
61+
#endif
62+
5463
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.
5564
virtual const GDExtensionInstanceBindingCallbacks *_get_bindings_callbacks() const = 0;
5665

@@ -104,6 +113,17 @@ void free_c_property_list(GDExtensionPropertyInfo *plist);
104113

105114
} // namespace godot
106115

116+
#ifdef HOT_RELOAD_ENABLED
117+
#define _GDCLASS_RECREATE(m_class, m_inherits) \
118+
m_class *new_instance = (m_class *)memalloc(sizeof(m_class)); \
119+
Wrapped::RecreateInstance recreate_data = { new_instance, obj, Wrapped::recreate_instance }; \
120+
Wrapped::recreate_instance = &recreate_data; \
121+
memnew_placement(new_instance, m_class); \
122+
return new_instance;
123+
#else
124+
#define _GDCLASS_RECREATE(m_class, m_inherits) return nullptr;
125+
#endif
126+
107127
// Use this on top of your own classes.
108128
// Note: the trail of `***` is to keep sane diffs in PRs, because clang-format otherwise moves every `\` which makes
109129
// every line of the macro different
@@ -187,6 +207,10 @@ public:
187207
return new_object->_owner; \
188208
} \
189209
\
210+
static GDExtensionClassInstancePtr recreate(void *data, GDExtensionObjectPtr obj) { \
211+
_GDCLASS_RECREATE(m_class, m_inherits); \
212+
} \
213+
\
190214
static void notification_bind(GDExtensionClassInstancePtr p_instance, int32_t p_what) { \
191215
if (p_instance && m_class::_get_notification()) { \
192216
if (m_class::_get_notification() != m_inherits::_get_notification()) { \

include/godot_cpp/core/class_db.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ void ClassDB::_register_class(bool p_virtual) {
192192
&ClassDB::get_virtual_func, // GDExtensionClassGetVirtual get_virtual_func;
193193
nullptr, // GDExtensionClassGetRID get_rid;
194194
(void *)&T::get_class_static(), // void *class_userdata;
195+
T::recreate,
195196
};
196197

197198
internal::gdextension_interface_classdb_register_extension_class(internal::library, cl.name._native_ptr(), cl.parent_name._native_ptr(), &class_info);

src/classes/wrapped.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ void Wrapped::_postinitialize() {
4949
}
5050

5151
Wrapped::Wrapped(const StringName p_godot_class) {
52+
#ifdef HOT_RELOAD_ENABLED
53+
if (unlikely(Wrapped::recreate_instance)) {
54+
RecreateInstance *recreate_data = Wrapped::recreate_instance;
55+
RecreateInstance *previous = nullptr;
56+
while (recreate_data) {
57+
if (recreate_data->wrapper == this) {
58+
_owner = recreate_data->owner;
59+
if (previous) {
60+
previous->next = recreate_data->next;
61+
}
62+
return;
63+
}
64+
previous = recreate_data;
65+
recreate_data = recreate_data->next;
66+
}
67+
}
68+
#endif
5269
_owner = godot::internal::gdextension_interface_classdb_construct_object(reinterpret_cast<GDExtensionConstStringNamePtr>(p_godot_class._native_ptr()));
5370
}
5471

src/core/class_db.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,9 @@ void ClassDB::deinitialize(GDExtensionInitializationLevel p_level) {
354354
for (auto method : cl.method_map) {
355355
memdelete(method.second);
356356
}
357+
358+
classes.erase(*i);
359+
class_register_order.erase((i + 1).base());
357360
}
358361
}
359362

tools/linux.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ def generate(env):
1414
if env["use_llvm"]:
1515
clang.generate(env)
1616
clangxx.generate(env)
17+
elif env["use_hot_reload"]:
18+
# Required for extensions to truly unload.
19+
env.Append(CXXFLAGS=["-fno-gnu-unique"])
1720

1821
env.Append(CCFLAGS=["-fPIC", "-Wwrite-strings"])
1922
env.Append(LINKFLAGS=["-Wl,-R,'$$ORIGIN'"])

0 commit comments

Comments
 (0)