18#include "SDL_stdinc.h"
30#include "glue_macos.h"
50 static char *primarypath = NULL;
51 if ( primarypath == NULL ) {
52 char **search_path = PHYSFS_getSearchPath();
54 for (
char **p = search_path; *p != NULL; p++ )
56 for (
int i = n - 1; i >= 0; i-- ) {
58 primarypath = strdup( search_path[i] );
62 PHYSFS_freeList( search_path );
76 return PHYSFS_exists(
"VERSION" ) && PHYSFS_exists( START_DATA_PATH );
90 snprintf( err,
sizeof( err ),
91 _(
"Unable to find game data. You may need to install, specify "
92 "a datapath, or run using %s (if developing)." ),
94#if SDL_VERSION_ATLEAST( 3, 0, 0 )
95 SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_ERROR,
96 _(
"Naev Critical Error" ), err,
104 for (
size_t i = 0; i <
MIN( size,
PATH_MAX - 1 ); i++ )
109 WARN( _(
"ndata version inconsistency with this version of Naev!" ) );
110 WARN( _(
"Expected ndata version %s got %s." ),
naev_version( 0 ), cbuf );
111 if (
ABS( diff ) > 2 ) {
113 snprintf( err,
sizeof( err ),
114 _(
"Please get a compatible ndata version!" ) );
115#if SDL_VERSION_ATLEAST( 3, 0, 0 )
116 SDL_ShowSimpleMessageBox( SDL_MESSAGEBOX_ERROR,
117 _(
"Naev Critical Error" ), err,
122 if (
ABS( diff ) > 1 )
123 WARN( _(
"Naev will probably crash now as the versions are probably "
124 "not compatible." ) );
135 if ( conf.datapath ) {
136 PHYSFS_setWriteDir( conf.datapath );
142 PHYSFS_setWriteDir( PHYSFS_getPrefDir(
".",
"org.naev.Naev" ) );
144 PHYSFS_setWriteDir( PHYSFS_getPrefDir(
".",
"naev" ) );
146 if ( PHYSFS_getWriteDir() == NULL ) {
147 WARN( _(
"Cannot determine data path, using current directory." ) );
148 PHYSFS_setWriteDir(
"./naev/" );
159 if ( conf.ndata != NULL && PHYSFS_mount( conf.ndata, NULL, 1 ) )
160 LOG( _(
"Added datapath from conf.lua file: %s" ), conf.ndata );
164 macos_resourcesPath( buf,
PATH_MAX - 4 ) >= 0 &&
165 strncat( buf,
"/dat", 4 ) ) {
166 LOG( _(
"Trying default datapath: %s" ), buf );
167 PHYSFS_mount( buf, NULL, 1 );
173 nfile_concatPaths( buf,
PATH_MAX, env.appdir, PKGDATADIR,
"dat" ) >=
175 LOG( _(
"Trying default datapath: %s" ), buf );
176 PHYSFS_mount( buf, NULL, 1 );
181 nfile_concatPaths( buf,
PATH_MAX, PKGDATADIR,
"dat" ) >= 0 ) {
182 LOG( _(
"Trying default datapath: %s" ), buf );
183 PHYSFS_mount( buf, NULL, 1 );
187 nfile_concatPaths( buf,
PATH_MAX, PHYSFS_getBaseDir(),
"dat" ) >= 0 ) {
188 LOG( _(
"Trying default datapath: %s" ), buf );
189 PHYSFS_mount( buf, NULL, 1 );
192 PHYSFS_mount( PHYSFS_getWriteDir(), NULL, 0 );
211 PHYSFS_sint64 len, n;
212 PHYSFS_Stat path_stat;
214 if ( !PHYSFS_stat( path, &path_stat ) ) {
215 WARN( _(
"Error occurred while opening '%s': %s" ), path,
216 _( PHYSFS_getErrorByCode( PHYSFS_getLastErrorCode() ) ) );
220 if ( path_stat.filetype != PHYSFS_FILETYPE_REGULAR ) {
221 WARN( _(
"Error occurred while opening '%s': It is not a regular file" ),
228 file = PHYSFS_openRead( path );
229 if ( file == NULL ) {
230 WARN( _(
"Error occurred while opening '%s': %s" ), path,
231 _( PHYSFS_getErrorByCode( PHYSFS_getLastErrorCode() ) ) );
237 len = PHYSFS_fileLength( file );
239 WARN( _(
"Error occurred while seeking '%s': %s" ), path,
240 _( PHYSFS_getErrorByCode( PHYSFS_getLastErrorCode() ) ) );
241 PHYSFS_close( file );
247 buf = malloc( len + 1 );
249 WARN( _(
"Out of Memory" ) );
250 PHYSFS_close( file );
259 size_t pos = PHYSFS_readBytes( file, &buf[n], len - n );
261 WARN( _(
"Error occurred while reading '%s': %s" ), path,
262 _( PHYSFS_getErrorByCode( PHYSFS_getLastErrorCode() ) ) );
263 PHYSFS_close( file );
272 PHYSFS_close( file );
293 for (
int i = 0; i + 1 <
array_size( files ); i++ )
294 if ( strcmp( files[i], files[i + 1] ) == 0 ) {
313 dir_len = strlen( origdir );
314 fmt = dir_len && origdir[dir_len - 1] ==
'/' ?
"%s%s" :
"%s/%s";
315 SDL_asprintf( &path, fmt, origdir, fname );
316 if ( !PHYSFS_stat( path, &stat ) ) {
317 WARN( _(
"PhysicsFS: Cannot stat %s: %s" ), path,
318 _( PHYSFS_getErrorByCode( PHYSFS_getLastErrorCode() ) ) );
320 }
else if ( stat.filetype == PHYSFS_FILETYPE_REGULAR )
322 else if ( stat.filetype == PHYSFS_FILETYPE_DIRECTORY ) {
327 return PHYSFS_ENUM_OK;
343 if ( !PHYSFS_exists( path ) )
346 snprintf( backup,
sizeof( backup ),
"%s.backup", path );
360 PHYSFS_File *f_in, *f_out;
362 PHYSFS_sint64 lr, lw;
368 if ( !PHYSFS_exists( file1 ) )
372 f_in = PHYSFS_openRead( file1 );
373 f_out = PHYSFS_openWrite( file2 );
374 if ( ( f_in == NULL ) || ( f_out == NULL ) ) {
375 WARN( _(
"Failure to copy '%s' to '%s': %s" ), file1, file2,
376 _( PHYSFS_getErrorByCode( PHYSFS_getLastErrorCode() ) ) );
378 PHYSFS_close( f_in );
384 lr = PHYSFS_readBytes( f_in, buf,
sizeof( buf ) );
388 if ( PHYSFS_eof( f_in ) )
393 lw = PHYSFS_writeBytes( f_out, buf, lr );
399 PHYSFS_close( f_in );
400 PHYSFS_close( f_out );
405 WARN( _(
"Failure to copy '%s' to '%s': %s" ), file1, file2,
406 _( PHYSFS_getErrorByCode( PHYSFS_getLastErrorCode() ) ) );
407 PHYSFS_close( f_in );
408 PHYSFS_close( f_out );
424 for ( i = strlen( path ) - 1; i > 0; i-- )
425 if ( path[i] ==
'.' )
429 return strcmp( &path[i + 1], ext ) == 0;
443 const char *filename )
445 PHYSFS_Stat path_stat;
446 snprintf( path, len,
"%s%s", default_path, filename );
447 if ( PHYSFS_stat( path, &path_stat ) &&
448 ( path_stat.filetype == PHYSFS_FILETYPE_REGULAR ) )
450 snprintf( path, len,
"%s", filename );
451 if ( PHYSFS_stat( path, &path_stat ) &&
452 ( path_stat.filetype == PHYSFS_FILETYPE_REGULAR ) )
Provides macros to work with dynamic arrays.
#define array_erase(ptr_array, first, last)
Erases elements in interval [first, last).
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
#define array_push_back(ptr_array, element)
Adds a new element at the end of the array.
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
int macos_isBundle(void)
Determine if we're running from inside an app bundle.
int naev_versionCompare(const char *version)
Compares the version against the current naev version.
Header file with generic functions and naev-specifics.
const char * naev_version(int long_version)
Returns the version in a human readable string.
int ndata_backupIfExists(const char *path)
Backup a file, if it exists.
void ndata_setupReadDirs(void)
Sets up the PhysicsFS search path.
static void ndata_testVersion(void)
Test version to see if it matches.
int ndata_copyIfExists(const char *file1, const char *file2)
Copy a file, if it exists.
void ndata_setupWriteDir(void)
Gets Naev's data path (for user data such as saves and screenshots)
static int ndata_found(void)
Checks to see if the physfs search path is enough to find game data.
void * ndata_read(const char *path, size_t *filesize)
Reads a file from the ndata (will be NUL terminated).
int ndata_matchExt(const char *path, const char *ext)
Sees if a file matches an extension.
static int ndata_enumerateCallback(void *data, const char *origdir, const char *fname)
The PHYSFS_EnumerateCallback for ndata_listRecursive.
char ** ndata_listRecursive(const char *path)
Lists all the visible files in a directory, at any depth.
const char * ndata_primaryPath(void)
Gets the primary path for where the data is.
int ndata_getPathDefault(char *path, int len, const char *default_path, const char *filename)
Tries to see if a file is in a default path before seeing if it is an absolute path.
int nfile_dirExists(const char *path)
Checks to see if a directory exists.
int strsort(const void *p1, const void *p2)
Sort function for sorting strings with qsort().
int plugin_init(void)
Initialize and loads all the available plugins.