diff --git a/engine/common/common.h b/engine/common/common.h index 146909c1..adc46be9 100644 --- a/engine/common/common.h +++ b/engine/common/common.h @@ -37,8 +37,6 @@ GNU General Public License for more details. #include "com_model.h" #include "com_strings.h" #include "crtlib.h" -#define FSCALLBACK_OVERRIDE_MALLOC_LIKE -#include "fscallback.h" #include "cvar.h" #include "con_nprint.h" #include "crclib.h" diff --git a/engine/platform/ios/lib_ios.c b/engine/platform/ios/lib_ios.c index dfe9af01..66ebba17 100644 --- a/engine/platform/ios/lib_ios.c +++ b/engine/platform/ios/lib_ios.c @@ -13,63 +13,83 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ #include +#include #include #include +#include #include "crtlib.h" #include "library.h" #include "platform/ios/lib_ios.h" #include "common.h" -#define EXT_LENGTH 5 +const char *g_szLibrarySuffix; static void *IOS_LoadLibraryInternal( const char *dllname ) { void *pHandle; - string errorstring = ""; char path[MAX_SYSPATH]; - Q_snprintf( path, MAX_SYSPATH, "%s%s", SDL_GetBasePath(), dllname ); + Q_snprintf( path, sizeof( path ), "%s%s", SDL_GetBasePath(), dllname ); pHandle = dlopen( path, RTLD_LAZY ); if( !pHandle ) - { - COM_PushLibraryError(errorstring); - COM_PushLibraryError(dlerror()); - } + COM_PushLibraryError(dlerror( )); return pHandle; } -const char *g_szLibrarySuffix; + +static qboolean IOS_LibraryExistsInternal( const char *name ) +{ + struct stat buf; + char path[MAX_SYSPATH]; + + Q_snprintf( path, sizeof( path ), "%s%s", SDL_GetBasePath(), name ); + + return stat( path, &buf ) == 0; +} + +static const char *IOS_GetLibraryPostfix( void ) +{ + if( g_szLibrarySuffix ) + return g_szLibrarySuffix; + + return Q_strcmp( host.default_gamedir, FS_Gamedir( )) ? FS_Gamedir( ) : ""; +} + +static void IOS_PrepareGameLibraryPath( const char *dllname, char *out, size_t outsize ) +{ + string strippedname; + + Q_strncpy( strippedname, dllname, sizeof( strippedname )); + COM_StripExtension( strippedname ); + + Q_snprintf( out, outsize, "%s_%s.dylib", strippedname, IOS_GetLibraryPostfix( )); +} + void *IOS_LoadLibrary( const char *dllname ) { - //Immediately load if the library is filesystem_stdio.dylib or we will crash when accessing gamedir - if ( !Q_strcmp( dllname, "filesystem_stdio.dylib" ) ) - { + // filesystem_stdio is a special case, as engine won't work correctly without it + // but it's always located at known path + if( !Q_strcmp( dllname, "filesystem_stdio.dylib" )) return IOS_LoadLibraryInternal( dllname ); - } string name; - string strippedname; - const char *postfix = g_szLibrarySuffix; char *pHandle; - - if( !postfix ) - { - if ( Q_strcmp(FS_Gamedir(), host.default_gamedir ) ) - { - postfix = FS_Gamedir( ); - } - else postfix = ""; - } - Q_strncpy( strippedname, dllname, sizeof( strippedname ) ); - COM_StripExtension(strippedname); - - Q_snprintf( name, MAX_STRING, "%s_%s.dylib", strippedname, postfix ); + IOS_PrepareGameLibraryPath( dllname, name, sizeof( name )); pHandle = IOS_LoadLibraryInternal( name ); - if( pHandle ) - return pHandle; + if( !pHandle ) + pHandle = IOS_LoadLibraryInternal( dllname ); - return IOS_LoadLibraryInternal( dllname ); + return pHandle; +} + +qboolean IOS_LibraryExists( const char *dllname ) +{ + string name; + + IOS_PrepareGameLibraryPath( dllname, name, sizeof( name )); + + return IOS_LibraryExistsInternal( name ) || IOS_LibraryExistsInternal( dllname ); } diff --git a/engine/platform/ios/lib_ios.h b/engine/platform/ios/lib_ios.h index 33f222f4..67bd8bfa 100644 --- a/engine/platform/ios/lib_ios.h +++ b/engine/platform/ios/lib_ios.h @@ -20,6 +20,7 @@ GNU General Public License for more details. #define Platform_POSIX_LoadLibrary( x ) IOS_LoadLibrary(( x )) void *IOS_LoadLibrary( const char *dllname ); +qboolean IOS_LibraryExists( const char *name ); #endif // IOS_LIB_H #endif // TARGET_OS_IPHONE diff --git a/engine/platform/platform.h b/engine/platform/platform.h index 942cc217..0d214936 100644 --- a/engine/platform/platform.h +++ b/engine/platform/platform.h @@ -18,6 +18,8 @@ GNU General Public License for more details. #define PLATFORM_H #include +#define FSCALLBACK_OVERRIDE_MALLOC_LIKE +#include "fscallback.h" #include "common.h" #include "system.h" #include "defaults.h" @@ -43,6 +45,7 @@ qboolean Platform_DebuggerPresent( void ); int IOS_GetArgs( char ***argv ); const char *IOS_GetDocsDir( void ); void IOS_LaunchDialog( void ); +#include "platform/ios/lib_ios.h" #endif // TARGET_OS_IOS #if XASH_WIN32 || XASH_LINUX @@ -207,6 +210,18 @@ static inline void Sys_RestoreCrashHandler( void ) } #endif +static inline qboolean Platform_LibraryExists( const char *name, qboolean gamedironly ) +{ +#if XASH_IOS + return IOS_LibraryExists( name ); +#elif XASH_ANDROID + // sorry, unimplemented + return false; +#else + return g_fsapi.FileExists( name, gamedironly ); +#endif +} + /* ==============================================================================