From c6d7670ed439522ba8be3959a0315cb8c4039a7f Mon Sep 17 00:00:00 2001 From: cloudwalk Date: Sat, 6 Mar 2021 12:03:22 +0000 Subject: [PATCH] sys: Simplify Sys_LoadLibrary. Implement Sys_LoadDependency and move bloat to it git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@13096 d7cf8633-e32d-0410-b094-e92efae38249 --- av_backend_libav.c | 4 +-- cap_ogg.c | 8 +++--- cl_video_libavw.c | 2 +- crypto.c | 4 +-- fs.c | 8 +++--- ft2.c | 2 +- image_png.c | 4 +-- jpeg.c | 2 +- libcurl.c | 2 +- snd_ogg.c | 2 +- snd_xmp.c | 2 +- sys.h | 12 +++++++-- sys_shared.c | 63 ++++++++++++++++++++++++++++++---------------- vid_shared.c | 2 +- world.c | 2 +- 15 files changed, 74 insertions(+), 45 deletions(-) diff --git a/av_backend_libav.c b/av_backend_libav.c index db9f5609..653c9b42 100644 --- a/av_backend_libav.c +++ b/av_backend_libav.c @@ -289,9 +289,9 @@ qbool LibAV_LoadLibrary(void) return false; // Load the DLL - if (Sys_LoadLibrary (dllnames_libavcodec, &libavcodec_dll, libavcodecfuncs)) + if (Sys_LoadDependency (dllnames_libavcodec, &libavcodec_dll, libavcodecfuncs)) { - if(Sys_LoadLibrary(dllnames_libavformat, &libavformat_dll, libavformatfuncs)) + if(Sys_LoadDependency(dllnames_libavformat, &libavformat_dll, libavformatfuncs)) return true; Con_Printf(CON_ERROR "Failed to load the libavformat library. Cannot use libavcodec without it\n"); Sys_FreeLibrary(&libavcodec_dll); diff --git a/cap_ogg.c b/cap_ogg.c index 0a0c1634..6218e8ad 100644 --- a/cap_ogg.c +++ b/cap_ogg.c @@ -565,13 +565,13 @@ static qbool SCR_CaptureVideo_Ogg_OpenLibrary(void) }; return - Sys_LoadLibrary (dllnames_og, &og_dll, oggfuncs) + Sys_LoadDependency (dllnames_og, &og_dll, oggfuncs) && - Sys_LoadLibrary (dllnames_th, &th_dll, theorafuncs) + Sys_LoadDependency (dllnames_th, &th_dll, theorafuncs) && - Sys_LoadLibrary (dllnames_vo, &vo_dll, vorbisfuncs) + Sys_LoadDependency (dllnames_vo, &vo_dll, vorbisfuncs) && - Sys_LoadLibrary (dllnames_ve, &ve_dll, vorbisencfuncs); + Sys_LoadDependency (dllnames_ve, &ve_dll, vorbisencfuncs); } void SCR_CaptureVideo_Ogg_Init(void) diff --git a/cl_video_libavw.c b/cl_video_libavw.c index f85148a8..83f4921c 100644 --- a/cl_video_libavw.c +++ b/cl_video_libavw.c @@ -364,7 +364,7 @@ qbool LibAvW_OpenLibrary(void) return false; // load DLL's - Sys_LoadLibrary(dllnames_libavw, &libavw_dll, libavwfuncs); + Sys_LoadDependency(dllnames_libavw, &libavw_dll, libavwfuncs); if (!libavw_dll) return false; diff --git a/crypto.c b/crypto.c index af34b385..72fcd2d3 100644 --- a/crypto.c +++ b/crypto.c @@ -279,7 +279,7 @@ static qbool Crypto_OpenLibrary (void) return true; // Load the DLL - return Sys_LoadLibrary (dllnames, &d0_blind_id_dll, d0_blind_id_funcs); + return Sys_LoadDependency (dllnames, &d0_blind_id_dll, d0_blind_id_funcs); } static void Crypto_CloseLibrary (void) @@ -348,7 +348,7 @@ static qbool Crypto_Rijndael_OpenLibrary (void) return true; // Load the DLL - return Sys_LoadLibrary (dllnames, &d0_rijndael_dll, d0_rijndael_funcs); + return Sys_LoadDependency (dllnames, &d0_rijndael_dll, d0_rijndael_funcs); } static void Crypto_Rijndael_CloseLibrary (void) diff --git a/fs.c b/fs.c index 58e9de4b..74733bfb 100644 --- a/fs.c +++ b/fs.c @@ -535,7 +535,7 @@ static qbool PK3_OpenLibrary (void) return true; // Load the DLL - return Sys_LoadLibrary (dllnames, &zlib_dll, zlibfuncs); + return Sys_LoadDependency (dllnames, &zlib_dll, zlibfuncs); #endif } @@ -1823,7 +1823,7 @@ static int FS_ChooseUserDir(userdirmode_t userdirmode, char *userdir, size_t use break; case USERDIRMODE_MYGAMES: if (!shfolder_dll) - Sys_LoadLibrary(shfolderdllnames, &shfolder_dll, shfolderfuncs); + Sys_LoadDependency(shfolderdllnames, &shfolder_dll, shfolderfuncs); mydocsdir[0] = 0; if (qSHGetFolderPath && qSHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, mydocsdir) == S_OK) { @@ -1849,9 +1849,9 @@ static int FS_ChooseUserDir(userdirmode_t userdirmode, char *userdir, size_t use return -1; case USERDIRMODE_SAVEDGAMES: if (!shell32_dll) - Sys_LoadLibrary(shell32dllnames, &shell32_dll, shell32funcs); + Sys_LoadDependency(shell32dllnames, &shell32_dll, shell32funcs); if (!ole32_dll) - Sys_LoadLibrary(ole32dllnames, &ole32_dll, ole32funcs); + Sys_LoadDependency(ole32dllnames, &ole32_dll, ole32funcs); if (qSHGetKnownFolderPath && qCoInitializeEx && qCoTaskMemFree && qCoUninitialize) { savedgamesdir[0] = 0; diff --git a/ft2.c b/ft2.c index 2df99bcb..b7421b21 100644 --- a/ft2.c +++ b/ft2.c @@ -367,7 +367,7 @@ qbool Font_OpenLibrary (void) return true; // Load the DLL - if (!Sys_LoadLibrary (dllnames, &ft2_dll, ft2funcs)) + if (!Sys_LoadDependency (dllnames, &ft2_dll, ft2funcs)) return false; #endif return true; diff --git a/image_png.c b/image_png.c index f7db48aa..6b226af0 100644 --- a/image_png.c +++ b/image_png.c @@ -172,10 +172,10 @@ qbool PNG_OpenLibrary (void) return true; // Load the DLL - if(!Sys_LoadLibrary (dllnames, &png_dll, pngfuncs)) + if(!Sys_LoadDependency (dllnames, &png_dll, pngfuncs)) return false; if(qpng_access_version_number() / 100 >= 104) - if(!Sys_LoadLibrary (dllnames, &png14_dll, png14funcs)) + if(!Sys_LoadDependency (dllnames, &png14_dll, png14funcs)) { Sys_FreeLibrary (&png_dll); return false; diff --git a/jpeg.c b/jpeg.c index 1e247a66..37914807 100644 --- a/jpeg.c +++ b/jpeg.c @@ -523,7 +523,7 @@ qbool JPEG_OpenLibrary (void) #endif // Load the DLL - return Sys_LoadLibrary (dllnames, &jpeg_dll, jpegfuncs); + return Sys_LoadDependency (dllnames, &jpeg_dll, jpegfuncs); #endif } diff --git a/libcurl.c b/libcurl.c index cd6d8279..cd106e7e 100644 --- a/libcurl.c +++ b/libcurl.c @@ -377,7 +377,7 @@ static qbool CURL_OpenLibrary (void) return true; // Load the DLL - return Sys_LoadLibrary (dllnames, &curl_dll, curlfuncs); + return Sys_LoadDependency (dllnames, &curl_dll, curlfuncs); } diff --git a/snd_ogg.c b/snd_ogg.c index 39c09b5b..09d20c8e 100644 --- a/snd_ogg.c +++ b/snd_ogg.c @@ -306,7 +306,7 @@ qbool OGG_OpenLibrary (void) // Load the DLLs // We need to load both by hand because some OSes seem to not load // the vorbis DLL automatically when loading the VorbisFile DLL - return Sys_LoadLibrary (dllnames_vo, &vo_dll, vorbisfuncs) && Sys_LoadLibrary (dllnames_vf, &vf_dll, vorbisfilefuncs); + return Sys_LoadDependency (dllnames_vo, &vo_dll, vorbisfuncs) && Sys_LoadDependency (dllnames_vf, &vf_dll, vorbisfilefuncs); } diff --git a/snd_xmp.c b/snd_xmp.c index 6be0bfe5..9876803d 100644 --- a/snd_xmp.c +++ b/snd_xmp.c @@ -371,7 +371,7 @@ qbool XMP_OpenLibrary (void) return false; // Load the DLL - if (Sys_LoadLibrary (dllnames_xmp, &xmp_dll, xmpfuncs)) + if (Sys_LoadDependency (dllnames_xmp, &xmp_dll, xmpfuncs)) { if (*qxmp_vercode < 0x040200) { diff --git a/sys.h b/sys.h index b606315d..ea711f66 100644 --- a/sys.h +++ b/sys.h @@ -173,12 +173,20 @@ dllfunction_t; qbool Sys_LoadSelf(dllhandle_t *handle); -/*! Loads a library. +/*! Loads a dependency library. * \param dllnames a NULL terminated array of possible names for the DLL you want to load. * \param handle * \param fcts */ -qbool Sys_LoadLibrary (const char** dllnames, dllhandle_t* handle, const dllfunction_t *fcts); +qbool Sys_LoadDependency (const char** dllnames, dllhandle_t* handle, const dllfunction_t *fcts); + +/*! Loads a library. + * \param name a string of the library filename + * \param handle + * \return true if library was loaded successfully + */ +qbool Sys_LoadLibrary(const char *name, dllhandle_t *handle); + void Sys_FreeLibrary (dllhandle_t* handle); void* Sys_GetProcAddress (dllhandle_t handle, const char* name); diff --git a/sys_shared.c b/sys_shared.c index 985af903..e849a9bc 100644 --- a/sys_shared.c +++ b/sys_shared.c @@ -91,7 +91,7 @@ DLL MANAGEMENT =============================================================================== */ -static qbool Sys_LoadLibraryFunctions(dllhandle_t dllhandle, const dllfunction_t *fcts, qbool complain, qbool has_next) +static qbool Sys_LoadDependencyFunctions(dllhandle_t dllhandle, const dllfunction_t *fcts, qbool complain, qbool has_next) { const dllfunction_t *func; if(dllhandle) @@ -131,7 +131,7 @@ qbool Sys_LoadSelf(dllhandle_t *handle) return true; } -qbool Sys_LoadLibrary (const char** dllnames, dllhandle_t* handle, const dllfunction_t *fcts) +qbool Sys_LoadDependency (const char** dllnames, dllhandle_t* handle, const dllfunction_t *fcts) { #ifdef SUPPORTDLL const dllfunction_t *func; @@ -144,7 +144,7 @@ qbool Sys_LoadLibrary (const char** dllnames, dllhandle_t* handle, const dllfunc #ifndef WIN32 #ifdef PREFER_PRELOAD dllhandle = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL); - if(Sys_LoadLibraryFunctions(dllhandle, fcts, false, false)) + if(Sys_LoadDependencyFunctions(dllhandle, fcts, false, false)) { Con_DPrintf ("All of %s's functions were already linked in! Not loading dynamically...\n", dllnames[0]); *handle = dllhandle; @@ -173,15 +173,14 @@ notfound: SetDllDirectory("bin32"); # endif # endif - dllhandle = LoadLibrary (dllnames[i]); - // no need to unset this - we want ALL dlls to be loaded from there, anyway -#else - dllhandle = dlopen (dllnames[i], RTLD_LAZY | RTLD_GLOBAL); #endif - if (Sys_LoadLibraryFunctions(dllhandle, fcts, true, (dllnames[i+1] != NULL) || (strrchr(sys.argv[0], '/')))) - break; - else - Sys_FreeLibrary (&dllhandle); + if(Sys_LoadLibrary(dllnames[i], &dllhandle)) + { + if (Sys_LoadDependencyFunctions(dllhandle, fcts, true, (dllnames[i+1] != NULL) || (strrchr(sys.argv[0], '/')))) + break; + else + Sys_FreeLibrary (&dllhandle); + } } // see if the names can be loaded relative to the executable path @@ -197,15 +196,14 @@ notfound: strlcpy(temp, path, sizeof(temp)); strlcat(temp, dllnames[i], sizeof(temp)); Con_DPrintf (" \"%s\"", temp); -#ifdef WIN32 - dllhandle = LoadLibrary (temp); -#else - dllhandle = dlopen (temp, RTLD_LAZY | RTLD_GLOBAL); -#endif - if (Sys_LoadLibraryFunctions(dllhandle, fcts, true, dllnames[i+1] != NULL)) - break; - else - Sys_FreeLibrary (&dllhandle); + + if(Sys_LoadLibrary(temp, &dllhandle)) + { + if (Sys_LoadDependencyFunctions(dllhandle, fcts, true, (dllnames[i+1] != NULL) || (strrchr(sys.argv[0], '/')))) + break; + else + Sys_FreeLibrary (&dllhandle); + } } } @@ -226,6 +224,27 @@ notfound: #endif } +qbool Sys_LoadLibrary(const char *name, dllhandle_t *handle) +{ + dllhandle_t dllhandle = 0; + + if(handle == NULL) + return false; + +#ifdef SUPPORTDLL +# ifdef WIN32 + dllhandle = LoadLibrary (name); +# else + dllhandle = dlopen (name, RTLD_LAZY | RTLD_GLOBAL); +# endif +#endif + if(!dllhandle) + return false; + + *handle = dllhandle; + return true; +} + void Sys_FreeLibrary (dllhandle_t* handle) { #ifdef SUPPORTDLL @@ -282,8 +301,9 @@ void* Sys_GetProcAddress (dllhandle_t handle, const char* name) # define HAVE_USLEEP 1 #endif -// this one is referenced elsewhere +// these are referenced elsewhere cvar_t sys_usenoclockbutbenchmark = {CF_CLIENT | CF_SERVER | CF_ARCHIVE, "sys_usenoclockbutbenchmark", "0", "don't use ANY real timing, and simulate a clock (for benchmarking); the game then runs as fast as possible. Run a QC mod with bots that does some stuff, then does a quit at the end, to benchmark a server. NEVER do this on a public server."}; +cvar_t sys_libdir = {CF_READONLY | CF_CLIENT | CF_SERVER, "sys_libdir", "", "Default engine library directory"}; // these are not static cvar_t sys_debugsleep = {CF_CLIENT | CF_SERVER, "sys_debugsleep", "0", "write requested and attained sleep times to standard output, to be used with gnuplot"}; @@ -325,6 +345,7 @@ void Sys_Init_Commands (void) { Cvar_RegisterVariable(&sys_debugsleep); Cvar_RegisterVariable(&sys_usenoclockbutbenchmark); + Cvar_RegisterVariable(&sys_libdir); #if HAVE_TIMEGETTIME || HAVE_QUERYPERFORMANCECOUNTER || HAVE_CLOCKGETTIME || HAVE_GETTIMEOFDAY if(sys_supportsdlgetticks) { diff --git a/vid_shared.c b/vid_shared.c index df8662d5..bc4c9855 100644 --- a/vid_shared.c +++ b/vid_shared.c @@ -1360,7 +1360,7 @@ void VID_Shared_Init(void) //Cvar_RegisterVariable(&joy_x360_sensitivityroll); #ifdef WIN32 - Sys_LoadLibrary(xinputdllnames, &xinputdll_dll, xinputdllfuncs); + Sys_LoadDependency(xinputdllnames, &xinputdll_dll, xinputdllfuncs); #endif Cmd_AddCommand(CF_CLIENT, "force_centerview", Force_CenterView_f, "recenters view (stops looking up/down)"); diff --git a/world.c b/world.c index 76ef3a7a..6c833975 100644 --- a/world.c +++ b/world.c @@ -1522,7 +1522,7 @@ static void World_Physics_Init(void) #ifndef LINK_TO_LIBODE // Load the DLL - if (Sys_LoadLibrary (dllnames, &ode_dll, odefuncs)) + if (Sys_LoadDependency (dllnames, &ode_dll, odefuncs)) #endif { dInitODE(); -- 2.39.2