Skip to content

Commit

Permalink
Patch by Simon McVittie to improve dynamic library loading (#4700)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thilo Schulz committed Feb 4, 2011
1 parent d2f8b9f commit 7bb5906
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 39 deletions.
26 changes: 26 additions & 0 deletions code/qcommon/files.c
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions code/qcommon/qcommon.h
Expand Up @@ -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);

Expand Down
48 changes: 9 additions & 39 deletions code/sys/sys_main.c
Expand Up @@ -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
Expand All @@ -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;
}

Expand Down

0 comments on commit 7bb5906

Please sign in to comment.