Skip to content

Commit

Permalink
GDExtension: Copy DLL to a temp file before opening
Browse files Browse the repository at this point in the history
This is done only in the editor and only on Windows, to avoid a file
lock that prevents the original library being updated (e.g. by a
compiler).

When the game runs it will load the original DLL and pick up any
changes, only the editor will stay with the copy (until it is restarted
and create a new copy).

The copy is done in place by prepending a `~` to the original file name,
so dependencies that are loaded with a relative file path still work.
When the library is unloaded the copy file is deleted.
  • Loading branch information
vnen committed Aug 2, 2023
1 parent dcd187d commit 07bca3d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
48 changes: 48 additions & 0 deletions core/extension/gdextension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ extern GDExtensionInterfaceFunctionPtr gdextension_get_proc_address(const char *

typedef GDExtensionBool (*GDExtensionLegacyInitializationFunction)(void *p_interface, GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization);

#ifdef WINDOWS_ENABLED
// Prefix for temporary copies of libraries.
#define TEMP_COPY_PREFIX "~"
#endif

String GDExtension::get_extension_list_config_file() {
return ProjectSettings::get_singleton()->get_project_data_path().path_join("extension_list.cfg");
}
Expand Down Expand Up @@ -485,6 +490,13 @@ void GDExtension::close_library() {
ERR_FAIL_COND(library == nullptr);
OS::get_singleton()->close_dynamic_library(library);

#if defined(TOOLS_ENABLED) && defined(WINDOWS_ENABLED)
// Delete temporary copy of library if it exists.
if (!temp_lib_path.is_empty() && Engine::get_singleton()->is_editor_hint()) {
DirAccess::remove_absolute(temp_lib_path);
}
#endif

library = nullptr;
}

Expand Down Expand Up @@ -640,6 +652,38 @@ Ref<Resource> GDExtensionResourceLoader::load(const String &p_path, const String
Ref<GDExtension> lib;
lib.instantiate();
String abs_path = ProjectSettings::get_singleton()->globalize_path(library_path);

#if defined(WINDOWS_ENABLED) && defined(TOOLS_ENABLED)
// 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.
if (Engine::get_singleton()->is_editor_hint()) {
if (!FileAccess::exists(abs_path)) {
if (r_error) {
*r_error = ERR_FILE_NOT_FOUND;
}
ERR_PRINT("GDExtension library not found: " + library_path);
return Ref<Resource>();
}

// Copy the file to the same directory as the original. This is so relative path to dependencies are satisfied.
String copy_path = abs_path.get_base_dir().path_join(TEMP_COPY_PREFIX + abs_path.get_file());

Error copy_err = DirAccess::copy_absolute(abs_path, copy_path);
if (copy_err) {
if (r_error) {
*r_error = ERR_CANT_CREATE;
}
ERR_PRINT("Error copying GDExtension library: " + library_path);
return Ref<Resource>();
}

// Save the copied path so it can be deleted later.
lib->set_temp_library_path(copy_path);

// Use the copy to open the library.
abs_path = copy_path;
}
#endif
err = lib->open_library(abs_path, entry_symbol);

if (r_error) {
Expand Down Expand Up @@ -700,3 +744,7 @@ void GDExtensionEditorPlugins::remove_extension_class(const StringName &p_class_
}
}
#endif // TOOLS_ENABLED

#ifdef TEMP_COPY_PREFIX
#undef TEMP_COPY_PREFIX
#endif
7 changes: 7 additions & 0 deletions core/extension/gdextension.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class GDExtension : public Resource {

void *library = nullptr; // pointer if valid,
String library_path;
#if defined(WINDOWS_ENABLED) && defined(TOOLS_ENABLED)
String temp_lib_path;
#endif

struct Extension {
ObjectGDExtension gdextension;
Expand Down Expand Up @@ -76,6 +79,10 @@ class GDExtension : public Resource {
Error open_library(const String &p_path, const String &p_entry_symbol);
void close_library();

#if defined(WINDOWS_ENABLED) && defined(TOOLS_ENABLED)
void set_temp_library_path(const String &p_path) { temp_lib_path = p_path; }
#endif

enum InitializationLevel {
INITIALIZATION_LEVEL_CORE = GDEXTENSION_INITIALIZATION_CORE,
INITIALIZATION_LEVEL_SERVERS = GDEXTENSION_INITIALIZATION_SERVERS,
Expand Down
7 changes: 6 additions & 1 deletion main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,12 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
}
}

#ifdef TOOLS_ENABLED
if (editor) {
Engine::get_singleton()->set_editor_hint(true);
}
#endif

// Initialize user data dir.
OS::get_singleton()->ensure_user_data_dir();

Expand Down Expand Up @@ -1549,7 +1555,6 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
#ifdef TOOLS_ENABLED
if (editor) {
packed_data->set_disabled(true);
Engine::get_singleton()->set_editor_hint(true);
main_args.push_back("--editor");
if (!init_windowed) {
init_maximized = true;
Expand Down

0 comments on commit 07bca3d

Please sign in to comment.