Add renaming of PDB files to avoid blocking them

This commit is contained in:
DmitriySalnikov
2024-01-11 15:33:35 +03:00
parent f6a78f83aa
commit b73e740786
18 changed files with 411 additions and 77 deletions

View File

@ -34,6 +34,7 @@
#include "joypad_windows.h"
#include "lang_table.h"
#include "windows_terminal_logger.h"
#include "windows_utils.h"
#include "core/debugger/engine_debugger.h"
#include "core/debugger/script_debugger.h"
@ -269,6 +270,10 @@ void OS_Windows::finalize() {
}
void OS_Windows::finalize_core() {
while (!temp_libraries.is_empty()) {
_remove_temp_library(temp_libraries.last()->key);
}
FileAccessWindows::finalize();
timeEndPeriod(1);
@ -354,7 +359,7 @@ void debug_dynamic_library_check_dependencies(const String &p_root_path, const S
}
#endif
Error OS_Windows::open_dynamic_library(const String &p_path, void *&p_library_handle, bool p_also_set_library_path, String *r_resolved_path) {
Error OS_Windows::open_dynamic_library(const String &p_path, void *&p_library_handle, bool p_also_set_library_path, String *r_resolved_path, bool p_generate_temp_files) {
String path = p_path.replace("/", "\\");
if (!FileAccess::exists(path)) {
@ -364,6 +369,35 @@ Error OS_Windows::open_dynamic_library(const String &p_path, void *&p_library_ha
ERR_FAIL_COND_V(!FileAccess::exists(path), ERR_FILE_NOT_FOUND);
// Here we want a copy to be loaded.
// This is so the original file isn't locked and can be updated by a compiler.
if (p_generate_temp_files) {
// Copy the file to the same directory as the original with a prefix in the name.
// This is so relative path to dependencies are satisfied.
String copy_path = path.get_base_dir().path_join("~" + path.get_file());
// If there's a left-over copy (possibly from a crash) then delete it first.
if (FileAccess::exists(copy_path)) {
DirAccess::remove_absolute(copy_path);
}
Error copy_err = DirAccess::copy_absolute(path, copy_path);
if (copy_err) {
ERR_PRINT("Error copying library: " + path);
return ERR_CANT_CREATE;
}
FileAccess::set_hidden_attribute(copy_path, true);
// Save the copied path so it can be deleted later.
path = copy_path;
Error pdb_err = WindowsUtils::copy_and_rename_pdb(path);
if (pdb_err != OK && pdb_err != ERR_SKIP) {
WARN_PRINT(vformat("Failed to rename the PDB file. The original PDB file for '%s' will be loaded.", path));
}
}
typedef DLL_DIRECTORY_COOKIE(WINAPI * PAddDllDirectory)(PCWSTR);
typedef BOOL(WINAPI * PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE);
@ -378,8 +412,12 @@ Error OS_Windows::open_dynamic_library(const String &p_path, void *&p_library_ha
}
p_library_handle = (void *)LoadLibraryExW((LPCWSTR)(path.utf16().get_data()), nullptr, (p_also_set_library_path && has_dll_directory_api) ? LOAD_LIBRARY_SEARCH_DEFAULT_DIRS : 0);
#ifdef DEBUG_ENABLED
if (!p_library_handle) {
if (p_generate_temp_files) {
DirAccess::remove_absolute(path);
}
#ifdef DEBUG_ENABLED
DWORD err_code = GetLastError();
HashSet<String> checekd_libs;
@ -397,8 +435,10 @@ Error OS_Windows::open_dynamic_library(const String &p_path, void *&p_library_ha
} else {
ERR_FAIL_V_MSG(ERR_CANT_OPEN, vformat("Can't open dynamic library: %s. Error: %s.", p_path, format_error_message(err_code)));
}
#endif
}
#else
#ifndef DEBUG_ENABLED
ERR_FAIL_NULL_V_MSG(p_library_handle, ERR_CANT_OPEN, vformat("Can't open dynamic library: %s. Error: %s.", p_path, format_error_message(GetLastError())));
#endif
@ -410,6 +450,10 @@ Error OS_Windows::open_dynamic_library(const String &p_path, void *&p_library_ha
*r_resolved_path = path;
}
if (p_generate_temp_files) {
temp_libraries[p_library_handle] = path;
}
return OK;
}
@ -417,9 +461,22 @@ Error OS_Windows::close_dynamic_library(void *p_library_handle) {
if (!FreeLibrary((HMODULE)p_library_handle)) {
return FAILED;
}
// Delete temporary copy of library if it exists.
_remove_temp_library(p_library_handle);
return OK;
}
void OS_Windows::_remove_temp_library(void *p_library_handle) {
if (temp_libraries.has(p_library_handle)) {
String path = temp_libraries[p_library_handle];
DirAccess::remove_absolute(path);
WindowsUtils::remove_temp_pdbs(path);
temp_libraries.erase(p_library_handle);
}
}
Error OS_Windows::get_dynamic_library_symbol_handle(void *p_library_handle, const String &p_name, void *&p_symbol_handle, bool p_optional) {
p_symbol_handle = (void *)GetProcAddress((HMODULE)p_library_handle, p_name.utf8().get_data());
if (!p_symbol_handle) {