From b22186b2f11857d36c10be604746895c1cf07c5a Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sat, 2 Aug 2025 09:43:48 +0500 Subject: [PATCH] engine: platform: android: strip library path when loading libraries from APK --- engine/platform/android/lib_android.c | 47 ++++++++++++--------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/engine/platform/android/lib_android.c b/engine/platform/android/lib_android.c index cf78a7d4..2bd2f69f 100644 --- a/engine/platform/android/lib_android.c +++ b/engine/platform/android/lib_android.c @@ -19,44 +19,37 @@ GNU General Public License for more details. #include "platform/android/lib_android.h" #include "platform/android/dlsym-weak.h" // Android < 5.0 -void *ANDROID_LoadLibrary( const char *dllname ) +void *ANDROID_LoadLibrary( const char *path ) { - char path[MAX_SYSPATH]; - const char *libdir[2]; - int i; - void *pHandle = NULL; + const char *libdir[2], *name = COM_FileWithoutPath( path ); + char fullpath[MAX_SYSPATH]; + void *handle; - libdir[0] = getenv("XASH3D_GAMELIBDIR"); - libdir[1] = getenv("XASH3D_ENGLIBDIR"); + libdir[0] = getenv( "XASH3D_GAMELIBDIR" ); // TODO: remove this once distributing games from APKs will be deprecated + libdir[1] = NULL; // TODO: put here data directory where libraries will be downloaded to - for( i = 0; i < 2; i++ ) + for( int i = 0; i < ARRAYSIZE( libdir ); i++ ) { + // this is an APK directory, get base path + const char *p = i == 0 ? name : path; + if( !libdir[i] ) continue; - Q_snprintf( path, MAX_SYSPATH, "%s/%s."OS_LIB_EXT, libdir[i], dllname ); - pHandle = dlopen( path, RTLD_NOW ); - if( pHandle ) - return pHandle; + Q_snprintf( fullpath, sizeof( fullpath ), "%s/%s", libdir[i], p ); + + handle = dlopen( fullpath, RTLD_NOW ); + + if( handle ) + return handle; COM_PushLibraryError( dlerror() ); } - // HACKHACK: keep old behaviour for compatibility - if( Q_strstr( dllname, "." OS_LIB_EXT ) || Q_strstr( dllname, "/" )) - { - pHandle = dlopen( dllname, RTLD_NOW ); - if( pHandle ) - return pHandle; - } - else - { - Q_snprintf( path, MAX_SYSPATH, "%s."OS_LIB_EXT, dllname ); - pHandle = dlopen( path, RTLD_NOW ); - if( pHandle ) - return pHandle; - } - + // find in system search path, that includes our APK + handle = dlopen( name, RTLD_NOW ); + if( handle ) + return handle; COM_PushLibraryError( dlerror() ); return NULL;