From 262f32b00218a79f7c32e5e2ed26a043624228bd Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Thu, 7 May 2026 20:22:08 +0500 Subject: [PATCH] engine: platform: android: implement library loading from VFS paths on Android --- engine/platform/android/lib_android.c | 58 ++++++++++++++++++++------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/engine/platform/android/lib_android.c b/engine/platform/android/lib_android.c index 74730877..30162d6b 100644 --- a/engine/platform/android/lib_android.c +++ b/engine/platform/android/lib_android.c @@ -22,41 +22,69 @@ GNU General Public License for more details. void *ANDROID_LoadLibrary( const char *path ) { - const char *libdir[2], *name = COM_FileWithoutPath( path ); - char fullpath[MAX_SYSPATH]; + const char *name = COM_FileWithoutPath( path ); void *handle; - 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 + Con_Reportf( "%s: loading \"%s\" (name: \"%s\")\n", __func__, path, name ); - for( int i = 0; i < ARRAYSIZE( libdir ); i++ ) + // TODO: remove this once distributing games from APKs will be deprecated + const char *gamelibdir = getenv( "XASH3D_GAMELIBDIR" ); + if( !COM_StringEmptyOrNULL( gamelibdir )) { - // this is an APK directory, get base path - const char *p = i == 0 ? name : path; + char fullpath[MAX_SYSPATH]; + Q_snprintf( fullpath, sizeof( fullpath ), "%s/%s", gamelibdir, name ); - if( !libdir[i] ) - continue; - - Q_snprintf( fullpath, sizeof( fullpath ), "%s/%s", libdir[i], p ); + Con_Reportf( "%s: trying APK path \"%s\"\n", __func__, fullpath ); handle = dlopen( fullpath, RTLD_NOW ); - if( handle ) { - Con_Reportf( "%s: loading library %s successful\n", __func__, fullpath ); + Con_Reportf( "%s: loaded from APK path\n", __func__ ); return handle; } + Con_Reportf( "%s: APK path failed: %s\n", __func__, dlerror() ); COM_PushLibraryError( dlerror() ); } - // find in system search path, that includes our APK + // try VFS + dll_user_t *hInst = FS_FindLibrary( path, false ); + if( hInst ) + { + Con_Reportf( "%s: VFS found \"%s\"\n", __func__, hInst->fullPath ); + if( !hInst->custom_loader ) + { + char libpath[MAX_SYSPATH]; + Q_strncpy( libpath, hInst->fullPath, sizeof( libpath )); + Mem_Free( hInst ); + + handle = dlopen( libpath, RTLD_NOW ); + if( handle ) + { + Con_Reportf( "%s: loaded from VFS path\n", __func__ ); + return handle; + } + Con_Reportf( "%s: VFS dlopen failed: %s\n", __func__, dlerror() ); + COM_PushLibraryError( dlerror() ); + } + else + { + Con_Reportf( "%s: VFS entry has custom loader, skipping\n", __func__ ); + COM_PushLibraryError( "custom loader not available on Android" ); + Mem_Free( hInst ); + } + } + + // find in system search path (APK's LD_LIBRARY_PATH) + Con_Reportf( "%s: trying LD_LIBRARY_PATH for \"%s\"\n", __func__, name ); handle = dlopen( name, RTLD_NOW ); if( handle ) { - Con_Reportf( "%s: loading library %s from LD_LIBRARY_PATH successful\n", __func__, name ); + Con_Reportf( "%s: loaded from LD_LIBRARY_PATH\n", __func__ ); return handle; } + + Con_Reportf( "%s: all paths failed for \"%s\"\n", __func__, path ); COM_PushLibraryError( dlerror() ); return NULL;