From 7bb59065997544061162d39a9906b87505b86da8 Mon Sep 17 00:00:00 2001 From: Thilo Schulz Date: Fri, 4 Feb 2011 17:47:57 +0000 Subject: [PATCH] Patch by Simon McVittie to improve dynamic library loading (#4700) --- code/qcommon/files.c | 26 +++++++++++++++++++++++ code/qcommon/qcommon.h | 3 +++ code/sys/sys_main.c | 48 ++++++++---------------------------------- 3 files changed, 38 insertions(+), 39 deletions(-) diff --git a/code/qcommon/files.c b/code/qcommon/files.c index c2202657..8c719d0c 100644 --- a/code/qcommon/files.c +++ b/code/qcommon/files.c @@ -1220,6 +1220,32 @@ int FS_FOpenFileRead( const char *filename, fileHandle_t *file, qboolean uniqueF } +char *FS_FindDll( const char *filename ) { + searchpath_t *search; + directory_t *dir; + + if ( !fs_searchpaths ) { + Com_Error( ERR_FATAL, "Filesystem call made without initialization\n" ); + } + + for ( search = fs_searchpaths ; search ; search = search->next ) { + if ( search->dir ) { + FILE *f; + char *netpath; + + dir = search->dir; + netpath = FS_BuildOSPath( dir->path, dir->gamedir, filename ); + f = fopen( netpath, "rb" ); + if (f) { + fclose( f ); + return netpath; + } + } + } + + return NULL; +} + /* ================= FS_Read diff --git a/code/qcommon/qcommon.h b/code/qcommon/qcommon.h index 3a04ba94..04d41e1d 100644 --- a/code/qcommon/qcommon.h +++ b/code/qcommon/qcommon.h @@ -606,6 +606,9 @@ void FS_FreeFileList( char **list ); qboolean FS_FileExists( const char *file ); qboolean FS_CreatePath (char *OSPath); + +char *FS_FindDll( const char *filename ); + char *FS_BuildOSPath( const char *base, const char *game, const char *qpath ); qboolean FS_CompareZipChecksum(const char *zipfile); diff --git a/code/sys/sys_main.c b/code/sys/sys_main.c index 1030bcb9..65dff8b0 100644 --- a/code/sys/sys_main.c +++ b/code/sys/sys_main.c @@ -405,34 +405,6 @@ void Sys_UnloadDll( void *dllHandle ) Sys_UnloadLibrary(dllHandle); } -/* -================= -Sys_TryLibraryLoad -================= -*/ -static void* Sys_TryLibraryLoad(const char* base, const char* gamedir, const char* fname, char* fqpath ) -{ - void* libHandle; - char* fn; - - *fqpath = 0; - - fn = FS_BuildOSPath( base, gamedir, fname ); - Com_Printf( "Sys_LoadDll(%s)... \n", fn ); - - libHandle = Sys_LoadLibrary(fn); - - if(!libHandle) { - Com_Printf( "Sys_LoadDll(%s) failed:\n\"%s\"\n", fn, Sys_LibraryError() ); - return NULL; - } - - Com_Printf ( "Sys_LoadDll(%s): succeeded ...\n", fn ); - Q_strncpyz ( fqpath , fn , MAX_QPATH ) ; - - return libHandle; -} - /* ================= Sys_LoadDll @@ -449,26 +421,24 @@ void *Sys_LoadDll( const char *name, char *fqpath , void *libHandle; void (*dllEntry)( intptr_t (*syscallptr)(intptr_t, ...) ); char fname[MAX_OSPATH]; - char *basepath; - char *homepath; - char *gamedir; + char *netpath; assert( name ); Q_snprintf (fname, sizeof(fname), "%s" ARCH_STRING DLL_EXT, name); - // TODO: use fs_searchpaths from files.c - basepath = Cvar_VariableString( "fs_basepath" ); - homepath = Cvar_VariableString( "fs_homepath" ); - gamedir = Cvar_VariableString( "fs_game" ); + netpath = FS_FindDll(fname); - libHandle = Sys_TryLibraryLoad(homepath, gamedir, fname, fqpath); + if(!netpath) { + Com_Printf( "Sys_LoadDll(%s) could not find it\n", fname ); + return NULL; + } - if(!libHandle && basepath) - libHandle = Sys_TryLibraryLoad(basepath, gamedir, fname, fqpath); + Com_Printf( "Loading DLL file: %s\n", netpath); + libHandle = Sys_LoadLibrary(netpath); if(!libHandle) { - Com_Printf ( "Sys_LoadDll(%s) failed to load library\n", name ); + Com_Printf( "Sys_LoadDll(%s) failed:\n\"%s\"\n", netpath, Sys_LibraryError() ); return NULL; }