Skip to content

Commit

Permalink
add option to activate add-dll-dir search paths
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Nov 2, 2024
1 parent 07a8a3a commit bb598d6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
38 changes: 21 additions & 17 deletions src/core/chuck_compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1520,33 +1520,36 @@ static void logChuginLoad( const string & name, t_CKINT logLevel )
// name: import_chugin_opt()
// desc: load chugin module by path, with options
//-----------------------------------------------------------------------------
t_CKBOOL Chuck_Compiler::import_chugin_opt( const string & path, const string & name, string & errorStr )
t_CKBOOL Chuck_Compiler::import_chugin_opt( const string & thePath, const string & name, string & errorStr )
{
// get env
Chuck_Env * env = this->env();
// platform-specific path (below: will use the appropriate '/' vs '\\')
string path = thePath;

// NOTE this (verbose >= 5) is more informative if the chugin crashes, we can see the name
EM_log( CK_LOG_INFO, "@import loading [chugin] %s...", name.c_str() );

// create chuck DLL data structure
Chuck_DLL * dll = new Chuck_DLL( this->carrier(), name != "" ? name.c_str() : (extract_filepath_file(path)).c_str() );
Chuck_DLL * dll = new Chuck_DLL( this->carrier(), name != "" ? name.c_str() : (extract_filepath_file(thePath)).c_str() );
t_CKBOOL query_failed = FALSE;

// clear error string
errorStr = "";

#if defined(__PLATFORM_WINDOWS__)
// get dll path to add
string dll_path = extract_filepath_dir( path );
// replace '/' with '\\'
std::replace( dll_path.begin(), dll_path.end(), '/', '\\' );
// add in _deps
string dll_deps_path = dll_path + "_deps";
std::replace( path.begin(), path.end(), '/', '\\' );
// the dll search path to add
string dll_path = extract_filepath_dir( path );
// the relateive _deps directory
string dll_deps_path = dll_path + "_deps\\";
// convert to wchar
wstring dll_pathw = wstring( dll_path.begin(), dll_path.end() );
wstring dll_deps_pathw = wstring( dll_deps_path.begin(), dll_deps_path.end() );
// add directory
// add to the dll search path, for resolving the chugin's own DLL dependencies
DLL_DIRECTORY_COOKIE cookie_path = AddDllDirectory( dll_pathw.c_str() );
// add the relateive _deps directory to the search path as well
DLL_DIRECTORY_COOKIE cookie_deps_path = AddDllDirectory( dll_deps_pathw.c_str() );
#endif

Expand Down Expand Up @@ -1612,10 +1615,6 @@ t_CKBOOL Chuck_Compiler::import_chugin_opt( const string & path, const string &
goto error;
}

#if defined(__PLATFORM_WINDOWS__)
// RemoveDllDirectory( cookie_path );
// RemoveDllDirectory( cookie_deps_path );
#endif
// print `[chugin] X.chug`
logChuginLoad( name, CK_LOG_HERALD );
// print success status
Expand All @@ -1624,19 +1623,24 @@ t_CKBOOL Chuck_Compiler::import_chugin_opt( const string & path, const string &
m_importRegistry.commit( dll );
// commit operator overloads | 1.5.1.5
env->op_registry.preserve();
#if defined(__PLATFORM_WINDOWS__)
// undo the AddDllDirectory()
if( cookie_path ) RemoveDllDirectory( cookie_path );
if( cookie_deps_path ) RemoveDllDirectory( cookie_deps_path );
#endif
// return home successful
return TRUE;

error:
#if defined(__PLATFORM_WINDOWS__)
// RemoveDllDirectory( cookie_path );
// RemoveDllDirectory( cookie_deps_path );
#endif
// clean up
CK_SAFE_DELETE( dll );
// rollback operator overloads | 1.5.1.5
env->op_registry.reset2local();

#if defined(__PLATFORM_WINDOWS__)
// undo the AddDllDirectory()
if( cookie_path ) RemoveDllDirectory( cookie_path );
if( cookie_deps_path ) RemoveDllDirectory( cookie_deps_path );
#endif
return FALSE;
}

Expand Down
9 changes: 7 additions & 2 deletions src/core/chuck_dl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3011,11 +3011,16 @@ extern "C"
void *dlopen( const char *path, int mode )
{
#ifndef __CHUNREAL_ENGINE__
return (void *)LoadLibrary(path);
// 1.5.4.0 (ge) change from LoadLibrary to LoadLibraryEx to specific
// LOAD_LIBRARY_SEARCH_DEFAULT_DIRS -- this is needed, apparently,
// to take directories added by AddDllDirectory() into account, which
// is needed to load DLL dependencies of chugins...
// https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-adddlldirectory
return (void *)LoadLibraryEx( path, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS );
#else
// 1.5.0.0 (ge) | #chunreal; explicitly call ASCII version
// the build envirnment seems to force UNICODE
return (void *)LoadLibraryA(path);
return (void *)LoadLibraryExA( path, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS );
#endif
}

Expand Down

0 comments on commit bb598d6

Please sign in to comment.