2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
filesystem.c - game filesystem based on DP fs
|
|
|
|
Copyright (C) 2007 Uncle Mike
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2019-11-24 01:52:08 +01:00
|
|
|
#include "build.h"
|
2018-04-13 18:23:45 +02:00
|
|
|
#include <fcntl.h>
|
2023-01-04 16:10:21 +01:00
|
|
|
#include <sys/types.h>
|
2018-04-13 18:23:45 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <time.h>
|
2019-11-24 01:52:08 +01:00
|
|
|
#if XASH_WIN32
|
2018-04-13 19:44:39 +02:00
|
|
|
#include <direct.h>
|
|
|
|
#include <io.h>
|
2020-02-08 16:50:30 +01:00
|
|
|
#elif XASH_DOS4GW
|
|
|
|
#include <direct.h>
|
|
|
|
#include <errno.h>
|
2018-04-13 19:44:39 +02:00
|
|
|
#else
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
2022-07-01 18:37:21 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2022-09-10 21:55:07 +02:00
|
|
|
#if XASH_LINUX
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
#endif
|
2022-07-01 18:37:21 +02:00
|
|
|
#include "port.h"
|
|
|
|
#include "const.h"
|
|
|
|
#include "crtlib.h"
|
|
|
|
#include "crclib.h"
|
2018-04-13 18:23:45 +02:00
|
|
|
#include "filesystem.h"
|
2022-07-01 18:37:21 +02:00
|
|
|
#include "filesystem_internal.h"
|
2020-02-10 11:07:06 +01:00
|
|
|
#include "xash3d_mathlib.h"
|
2022-07-01 18:37:21 +02:00
|
|
|
#include "common/com_strings.h"
|
|
|
|
#include "common/protocol.h"
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
#define FILE_COPY_SIZE (1024 * 1024)
|
2022-07-11 02:58:59 +02:00
|
|
|
|
|
|
|
fs_globals_t FI;
|
2022-07-01 18:37:21 +02:00
|
|
|
qboolean fs_ext_path = false; // attempt to read\write from ./ or ../ pathes
|
|
|
|
poolhandle_t fs_mempool;
|
|
|
|
searchpath_t *fs_searchpaths = NULL; // chain
|
|
|
|
char fs_rodir[MAX_SYSPATH];
|
|
|
|
char fs_rootdir[MAX_SYSPATH];
|
2022-12-19 15:22:30 +01:00
|
|
|
searchpath_t *fs_writepath;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2020-02-08 16:50:30 +01:00
|
|
|
static char fs_basedir[MAX_SYSPATH]; // base game directory
|
|
|
|
static char fs_gamedir[MAX_SYSPATH]; // game current directory
|
2019-11-24 01:52:08 +01:00
|
|
|
#if !XASH_WIN32
|
2020-02-08 16:50:30 +01:00
|
|
|
static qboolean fs_caseinsensitive = true; // try to search missing files
|
2018-04-13 19:44:39 +02:00
|
|
|
#endif
|
2018-10-04 09:27:14 +02:00
|
|
|
|
2020-02-08 16:50:30 +01:00
|
|
|
#ifdef XASH_REDUCE_FD
|
|
|
|
static file_t *fs_last_readfile;
|
|
|
|
static zip_t *fs_last_zip;
|
|
|
|
|
|
|
|
static void FS_EnsureOpenFile( file_t *file )
|
|
|
|
{
|
|
|
|
if( fs_last_readfile == file )
|
|
|
|
return;
|
|
|
|
|
2020-02-14 15:48:26 +01:00
|
|
|
if( file && !file->backup_path )
|
2020-02-08 16:50:30 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
if( fs_last_readfile && (fs_last_readfile->handle != -1) )
|
|
|
|
{
|
|
|
|
fs_last_readfile->backup_position = lseek( fs_last_readfile->handle, 0, SEEK_CUR );
|
|
|
|
close( fs_last_readfile->handle );
|
|
|
|
fs_last_readfile->handle = -1;
|
|
|
|
}
|
|
|
|
fs_last_readfile = file;
|
|
|
|
if( file && (file->handle == -1) )
|
|
|
|
{
|
|
|
|
file->handle = open( file->backup_path, file->backup_options );
|
|
|
|
lseek( file->handle, file->backup_position, SEEK_SET );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FS_BackupFileName( file_t *file, const char *path, uint options )
|
|
|
|
{
|
|
|
|
if( path == NULL )
|
|
|
|
{
|
|
|
|
if( file->backup_path )
|
2020-02-08 18:29:56 +01:00
|
|
|
Mem_Free( (void*)file->backup_path );
|
2020-02-08 16:50:30 +01:00
|
|
|
if( file == fs_last_readfile )
|
|
|
|
FS_EnsureOpenFile( NULL );
|
|
|
|
}
|
|
|
|
else if( options == O_RDONLY || options == (O_RDONLY|O_BINARY) )
|
|
|
|
{
|
|
|
|
file->backup_path = copystring( path );
|
|
|
|
file->backup_options = options;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static void FS_EnsureOpenFile( file_t *file ) {}
|
|
|
|
static void FS_BackupFileName( file_t *file, const char *path, uint options ) {}
|
|
|
|
#endif
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
static void FS_InitMemory( void );
|
|
|
|
static void FS_Purge( file_t* file );
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============================================================================
|
|
|
|
|
|
|
|
FILEMATCH COMMON SYSTEM
|
|
|
|
|
|
|
|
=============================================================================
|
|
|
|
*/
|
2022-11-18 14:35:21 +01:00
|
|
|
void stringlistinit( stringlist_t *list )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
memset( list, 0, sizeof( *list ));
|
|
|
|
}
|
|
|
|
|
2022-11-18 14:35:21 +01:00
|
|
|
void stringlistfreecontents( stringlist_t *list )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for( i = 0; i < list->numstrings; i++ )
|
|
|
|
{
|
|
|
|
if( list->strings[i] )
|
|
|
|
Mem_Free( list->strings[i] );
|
|
|
|
list->strings[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( list->strings )
|
|
|
|
Mem_Free( list->strings );
|
|
|
|
|
|
|
|
list->numstrings = 0;
|
|
|
|
list->maxstrings = 0;
|
|
|
|
list->strings = NULL;
|
|
|
|
}
|
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
void stringlistappend( stringlist_t *list, char *text )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
size_t textlen;
|
|
|
|
|
2020-11-22 07:49:39 +01:00
|
|
|
if( !Q_strcmp( text, "." ) || !Q_strcmp( text, ".." ))
|
2018-04-13 18:23:45 +02:00
|
|
|
return; // ignore the virtual directories
|
|
|
|
|
|
|
|
if( list->numstrings >= list->maxstrings )
|
|
|
|
{
|
|
|
|
list->maxstrings += 4096;
|
|
|
|
list->strings = Mem_Realloc( fs_mempool, list->strings, list->maxstrings * sizeof( *list->strings ));
|
|
|
|
}
|
|
|
|
|
|
|
|
textlen = Q_strlen( text ) + 1;
|
2018-06-09 00:28:35 +02:00
|
|
|
list->strings[list->numstrings] = Mem_Calloc( fs_mempool, textlen );
|
2018-04-13 18:23:45 +02:00
|
|
|
memcpy( list->strings[list->numstrings], text, textlen );
|
|
|
|
list->numstrings++;
|
|
|
|
}
|
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
void stringlistsort( stringlist_t *list )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
char *temp;
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
// this is a selection sort (finds the best entry for each slot)
|
|
|
|
for( i = 0; i < list->numstrings - 1; i++ )
|
|
|
|
{
|
|
|
|
for( j = i + 1; j < list->numstrings; j++ )
|
|
|
|
{
|
|
|
|
if( Q_strcmp( list->strings[i], list->strings[j] ) > 0 )
|
|
|
|
{
|
|
|
|
temp = list->strings[i];
|
|
|
|
list->strings[i] = list->strings[j];
|
|
|
|
list->strings[j] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:10:21 +01:00
|
|
|
#if XASH_DOS4GW
|
|
|
|
// convert names to lowercase because dos doesn't care, but pattern matching code often does
|
2018-04-13 18:23:45 +02:00
|
|
|
static void listlowercase( stringlist_t *list )
|
|
|
|
{
|
|
|
|
char *c;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for( i = 0; i < list->numstrings; i++ )
|
|
|
|
{
|
|
|
|
for( c = list->strings[i]; *c; c++ )
|
|
|
|
*c = Q_tolower( *c );
|
|
|
|
}
|
|
|
|
}
|
2023-01-04 16:10:21 +01:00
|
|
|
#endif
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
void listdirectory( stringlist_t *list, const char *path )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2019-11-24 01:52:08 +01:00
|
|
|
#if XASH_WIN32
|
2018-04-13 19:44:39 +02:00
|
|
|
char pattern[4096];
|
2018-04-13 18:23:45 +02:00
|
|
|
struct _finddata_t n_file;
|
2021-02-18 08:01:33 +01:00
|
|
|
intptr_t hFile;
|
2018-04-13 19:44:39 +02:00
|
|
|
#else
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
#endif
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2019-11-24 01:52:08 +01:00
|
|
|
#if XASH_WIN32
|
2023-01-04 20:33:21 +01:00
|
|
|
Q_snprintf( pattern, sizeof( pattern ), "%s/*", path );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
// ask for the directory listing handle
|
|
|
|
hFile = _findfirst( pattern, &n_file );
|
|
|
|
if( hFile == -1 ) return;
|
|
|
|
|
|
|
|
// start a new chain with the the first name
|
|
|
|
stringlistappend( list, n_file.name );
|
|
|
|
// iterate through the directory
|
|
|
|
while( _findnext( hFile, &n_file ) == 0 )
|
|
|
|
stringlistappend( list, n_file.name );
|
|
|
|
_findclose( hFile );
|
2018-04-13 19:44:39 +02:00
|
|
|
#else
|
|
|
|
if( !( dir = opendir( path ) ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// iterate through the directory
|
|
|
|
while( ( entry = readdir( dir ) ))
|
|
|
|
stringlistappend( list, entry->d_name );
|
|
|
|
closedir( dir );
|
|
|
|
#endif
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2020-02-08 16:50:30 +01:00
|
|
|
#if XASH_DOS4GW
|
|
|
|
// convert names to lowercase because 8.3 always in CAPS
|
|
|
|
listlowercase( list );
|
2018-04-22 12:10:16 +02:00
|
|
|
#endif
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============================================================================
|
|
|
|
|
|
|
|
OTHER PRIVATE FUNCTIONS
|
|
|
|
|
|
|
|
=============================================================================
|
|
|
|
*/
|
2020-02-08 16:50:30 +01:00
|
|
|
|
2021-10-03 08:08:03 +02:00
|
|
|
#if XASH_WIN32
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_PathToWideChar
|
|
|
|
|
|
|
|
Converts input UTF-8 string to wide char string.
|
|
|
|
====================
|
|
|
|
*/
|
2022-07-01 18:37:21 +02:00
|
|
|
static const wchar_t *FS_PathToWideChar( const char *path )
|
2021-10-03 08:08:03 +02:00
|
|
|
{
|
|
|
|
static wchar_t pathBuffer[MAX_PATH];
|
|
|
|
MultiByteToWideChar( CP_UTF8, 0, path, -1, pathBuffer, MAX_PATH );
|
|
|
|
return pathBuffer;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
============
|
|
|
|
FS_CreatePath
|
|
|
|
|
|
|
|
Only used for FS_Open.
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void FS_CreatePath( char *path )
|
|
|
|
{
|
|
|
|
char *ofs, save;
|
|
|
|
|
|
|
|
for( ofs = path + 1; *ofs; ofs++ )
|
|
|
|
{
|
|
|
|
if( *ofs == '/' || *ofs == '\\' )
|
|
|
|
{
|
|
|
|
// create the directory
|
|
|
|
save = *ofs;
|
|
|
|
*ofs = 0;
|
|
|
|
_mkdir( path );
|
|
|
|
*ofs = save;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 22:03:27 +02:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
/*
|
2022-07-01 18:37:21 +02:00
|
|
|
================
|
|
|
|
FS_AddArchive_Fullpath
|
|
|
|
================
|
2018-04-13 18:23:45 +02:00
|
|
|
*/
|
2022-07-01 18:37:21 +02:00
|
|
|
static qboolean FS_AddArchive_Fullpath( const char *file, qboolean *already_loaded, int flags )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-07-01 18:37:21 +02:00
|
|
|
const char *ext = COM_FileExtension( file );
|
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
if( !Q_stricmp( ext, "pk3" ))
|
2022-07-01 18:37:21 +02:00
|
|
|
return FS_AddZip_Fullpath( file, already_loaded, flags );
|
|
|
|
else if ( !Q_stricmp( ext, "pak" ))
|
|
|
|
return FS_AddPak_Fullpath( file, already_loaded, flags );
|
|
|
|
|
|
|
|
// skip wads, this function only meant to be used for extras
|
|
|
|
return false;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-07-01 18:37:21 +02:00
|
|
|
================
|
|
|
|
FS_AddGameDirectory
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
Sets fs_writepath, adds the directory to the head of the path,
|
2022-07-01 18:37:21 +02:00
|
|
|
then loads and adds pak1.pak pak2.pak ...
|
|
|
|
================
|
2018-04-13 18:23:45 +02:00
|
|
|
*/
|
2022-07-01 18:37:21 +02:00
|
|
|
void FS_AddGameDirectory( const char *dir, uint flags )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-12-19 15:22:30 +01:00
|
|
|
stringlist_t list;
|
|
|
|
searchpath_t *search;
|
|
|
|
char fullpath[MAX_SYSPATH];
|
|
|
|
int i;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
stringlistinit( &list );
|
2022-12-26 12:39:51 +01:00
|
|
|
listdirectory( &list, dir );
|
2022-07-01 18:37:21 +02:00
|
|
|
stringlistsort( &list );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2023-01-15 11:11:45 +01:00
|
|
|
// add archives in specific order PAK -> PK3 -> WAD
|
|
|
|
// so raw WADs takes precedence over WADs included into PAKs and PK3s
|
2022-07-01 18:37:21 +02:00
|
|
|
for( i = 0; i < list.numstrings; i++ )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-12-19 15:22:30 +01:00
|
|
|
const char *ext = COM_FileExtension( list.strings[i] );
|
|
|
|
|
|
|
|
if( !Q_stricmp( ext, "pak" ))
|
2022-07-01 18:37:21 +02:00
|
|
|
{
|
2022-12-19 15:22:30 +01:00
|
|
|
Q_snprintf( fullpath, sizeof( fullpath ), "%s%s", dir, list.strings[i] );
|
2022-07-01 18:37:21 +02:00
|
|
|
FS_AddPak_Fullpath( fullpath, NULL, flags );
|
|
|
|
}
|
2023-01-15 11:11:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < list.numstrings; i++ )
|
|
|
|
{
|
|
|
|
const char *ext = COM_FileExtension( list.strings[i] );
|
|
|
|
|
|
|
|
if( !Q_stricmp( ext, "pk3" ))
|
2022-07-01 18:37:21 +02:00
|
|
|
{
|
2022-12-19 15:22:30 +01:00
|
|
|
Q_snprintf( fullpath, sizeof( fullpath ), "%s%s", dir, list.strings[i] );
|
2022-07-01 18:37:21 +02:00
|
|
|
FS_AddZip_Fullpath( fullpath, NULL, flags );
|
|
|
|
}
|
2023-01-15 11:11:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < list.numstrings; i++ )
|
|
|
|
{
|
|
|
|
const char *ext = COM_FileExtension( list.strings[i] );
|
|
|
|
|
|
|
|
if( !Q_stricmp( ext, "wad" ))
|
2022-07-01 18:37:21 +02:00
|
|
|
{
|
2022-12-19 15:22:30 +01:00
|
|
|
FS_AllowDirectPaths( true );
|
|
|
|
Q_snprintf( fullpath, sizeof( fullpath ), "%s%s", dir, list.strings[i] );
|
2022-07-01 18:37:21 +02:00
|
|
|
FS_AddWad_Fullpath( fullpath, NULL, flags );
|
2022-12-19 15:22:30 +01:00
|
|
|
FS_AllowDirectPaths( false );
|
2022-07-01 18:37:21 +02:00
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
2023-01-15 11:11:45 +01:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
stringlistfreecontents( &list );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
// add the directory to the search path
|
|
|
|
// (unpacked files have the priority over packed files)
|
2022-12-19 15:22:30 +01:00
|
|
|
search = FS_AddDir_Fullpath( dir, NULL, flags );
|
|
|
|
if( !FBitSet( flags, FS_NOWRITE_PATH ))
|
|
|
|
fs_writepath = search;
|
2022-07-01 18:37:21 +02:00
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
/*
|
|
|
|
================
|
|
|
|
FS_ClearSearchPath
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
void FS_ClearSearchPath( void )
|
|
|
|
{
|
|
|
|
while( fs_searchpaths )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-07-01 18:37:21 +02:00
|
|
|
searchpath_t *search = fs_searchpaths;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( !search ) break;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( FBitSet( search->flags, FS_STATIC_PATH ))
|
|
|
|
{
|
|
|
|
// skip read-only pathes
|
|
|
|
if( search->next )
|
|
|
|
fs_searchpaths = search->next->next;
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
else fs_searchpaths = search->next;
|
2020-02-08 16:50:30 +01:00
|
|
|
|
2022-12-14 23:06:20 +01:00
|
|
|
search->pfnClose( search );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
Mem_Free( search );
|
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2020-11-29 16:18:04 +01:00
|
|
|
/*
|
2022-07-01 18:37:21 +02:00
|
|
|
====================
|
|
|
|
FS_CheckNastyPath
|
|
|
|
Return true if the path should be rejected due to one of the following:
|
|
|
|
1: path elements that are non-portable
|
|
|
|
2: path elements that would allow access to files outside the game directory,
|
|
|
|
or are just not a good idea for a mod to be using.
|
|
|
|
====================
|
2020-11-29 16:18:04 +01:00
|
|
|
*/
|
2022-07-01 18:37:21 +02:00
|
|
|
int FS_CheckNastyPath (const char *path, qboolean isgamedir)
|
2020-11-29 16:18:04 +01:00
|
|
|
{
|
2022-07-01 18:37:21 +02:00
|
|
|
// all: never allow an empty path, as for gamedir it would access the parent directory and a non-gamedir path it is just useless
|
|
|
|
if( !COM_CheckString( path )) return 2;
|
2020-11-29 16:18:04 +01:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( fs_ext_path ) return 0; // allow any path
|
2019-05-24 18:13:03 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
// Mac: don't allow Mac-only filenames - : is a directory separator
|
|
|
|
// instead of /, but we rely on / working already, so there's no reason to
|
|
|
|
// support a Mac-only path
|
|
|
|
// Amiga and Windows: : tries to go to root of drive
|
|
|
|
if( Q_strchr( path, ':' )) return 1; // non-portable attempt to go to root of drive
|
2019-05-24 18:13:03 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
// Amiga: // is parent directory
|
|
|
|
if( Q_strstr( path, "//")) return 1; // non-portable attempt to go to parent directory
|
2019-05-27 20:02:38 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
// all: don't allow going to parent directory (../ or /../)
|
|
|
|
if( Q_strstr( path, "..")) return 2; // attempt to go outside the game directory
|
2019-05-27 20:02:38 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
// Windows and UNIXes: don't allow absolute paths
|
|
|
|
if( path[0] == '/') return 2; // attempt to go outside the game directory
|
2019-05-24 18:13:03 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
// all: forbid trailing slash on gamedir
|
|
|
|
if( isgamedir && path[Q_strlen(path)-1] == '/' ) return 2;
|
2020-02-08 16:50:30 +01:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
// all: forbid leading dot on any filename for any reason
|
|
|
|
if( Q_strstr(path, "/.")) return 2; // attempt to go outside the game directory
|
2019-05-27 20:02:38 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
// after all these checks we're pretty sure it's a / separated filename
|
|
|
|
// and won't do much if any harm
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-27 20:02:38 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
/*
|
|
|
|
================
|
|
|
|
FS_WriteGameInfo
|
2019-05-25 20:24:34 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
assume GameInfo is valid
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
static void FS_WriteGameInfo( const char *filepath, gameinfo_t *GameInfo )
|
|
|
|
{
|
|
|
|
file_t *f = FS_Open( filepath, "w", false ); // we in binary-mode
|
|
|
|
int i, write_ambients = false;
|
2020-02-08 16:50:30 +01:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( !f ) Sys_Error( "FS_WriteGameInfo: can't write %s\n", filepath ); // may be disk-space is out?
|
2019-05-24 18:13:03 +02:00
|
|
|
|
2022-12-04 17:04:42 +01:00
|
|
|
FS_Printf( f, "// generated by " XASH_ENGINE_NAME " " XASH_VERSION "-%s (%s-%s)\n\n\n", Q_buildcommit(), Q_buildos(), Q_buildarch() );
|
2019-05-27 20:02:38 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( COM_CheckStringEmpty( GameInfo->basedir ) )
|
|
|
|
FS_Printf( f, "basedir\t\t\"%s\"\n", GameInfo->basedir );
|
2019-05-27 20:02:38 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
// DEPRECATED: gamedir key isn't supported by FWGS fork
|
|
|
|
// but write it anyway to keep compability with original Xash3D
|
|
|
|
if( COM_CheckStringEmpty( GameInfo->gamefolder ) )
|
|
|
|
FS_Printf( f, "gamedir\t\t\"%s\"\n", GameInfo->gamefolder );
|
2019-05-26 17:12:41 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( COM_CheckStringEmpty( GameInfo->falldir ) )
|
|
|
|
FS_Printf( f, "fallback_dir\t\"%s\"\n", GameInfo->falldir );
|
2019-05-27 20:02:38 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( COM_CheckStringEmpty( GameInfo->title ) )
|
|
|
|
FS_Printf( f, "title\t\t\"%s\"\n", GameInfo->title );
|
2019-05-27 20:02:38 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( COM_CheckStringEmpty( GameInfo->startmap ) )
|
|
|
|
FS_Printf( f, "startmap\t\t\"%s\"\n", GameInfo->startmap );
|
2019-05-24 18:13:03 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( COM_CheckStringEmpty( GameInfo->trainmap ) )
|
|
|
|
FS_Printf( f, "trainmap\t\t\"%s\"\n", GameInfo->trainmap );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
if( GameInfo->version != 0.0f )
|
|
|
|
FS_Printf( f, "version\t\t%g\n", GameInfo->version );
|
|
|
|
|
|
|
|
if( GameInfo->size != 0 )
|
2019-10-19 15:47:16 +02:00
|
|
|
FS_Printf( f, "size\t\t%lu\n", GameInfo->size );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2020-11-22 07:49:39 +01:00
|
|
|
if( COM_CheckStringEmpty( GameInfo->game_url ) )
|
2018-04-13 18:23:45 +02:00
|
|
|
FS_Printf( f, "url_info\t\t\"%s\"\n", GameInfo->game_url );
|
|
|
|
|
2020-11-22 07:49:39 +01:00
|
|
|
if( COM_CheckStringEmpty( GameInfo->update_url ) )
|
2018-04-13 18:23:45 +02:00
|
|
|
FS_Printf( f, "url_update\t\t\"%s\"\n", GameInfo->update_url );
|
|
|
|
|
2020-11-22 07:49:39 +01:00
|
|
|
if( COM_CheckStringEmpty( GameInfo->type ) )
|
2018-04-13 18:23:45 +02:00
|
|
|
FS_Printf( f, "type\t\t\"%s\"\n", GameInfo->type );
|
|
|
|
|
2020-11-22 07:49:39 +01:00
|
|
|
if( COM_CheckStringEmpty( GameInfo->date ) )
|
2018-04-13 18:23:45 +02:00
|
|
|
FS_Printf( f, "date\t\t\"%s\"\n", GameInfo->date );
|
|
|
|
|
2020-11-22 07:49:39 +01:00
|
|
|
if( COM_CheckStringEmpty( GameInfo->dll_path ) )
|
2021-01-03 02:28:45 +01:00
|
|
|
FS_Printf( f, "dllpath\t\t\"%s\"\n", GameInfo->dll_path );
|
2018-04-24 04:50:10 +02:00
|
|
|
|
2020-11-22 07:49:39 +01:00
|
|
|
if( COM_CheckStringEmpty( GameInfo->game_dll ) )
|
2018-04-13 18:23:45 +02:00
|
|
|
FS_Printf( f, "gamedll\t\t\"%s\"\n", GameInfo->game_dll );
|
|
|
|
|
2020-11-22 07:49:39 +01:00
|
|
|
if( COM_CheckStringEmpty( GameInfo->game_dll_linux ) )
|
2018-04-24 04:50:10 +02:00
|
|
|
FS_Printf( f, "gamedll_linux\t\t\"%s\"\n", GameInfo->game_dll_linux );
|
|
|
|
|
2020-11-22 07:49:39 +01:00
|
|
|
if( COM_CheckStringEmpty( GameInfo->game_dll_osx ) )
|
2018-04-24 04:50:10 +02:00
|
|
|
FS_Printf( f, "gamedll_osx\t\t\"%s\"\n", GameInfo->game_dll_osx );
|
|
|
|
|
2020-11-22 07:49:39 +01:00
|
|
|
if( COM_CheckStringEmpty( GameInfo->iconpath ))
|
2018-04-13 18:23:45 +02:00
|
|
|
FS_Printf( f, "icon\t\t\"%s\"\n", GameInfo->iconpath );
|
|
|
|
|
|
|
|
switch( GameInfo->gamemode )
|
|
|
|
{
|
|
|
|
case 1: FS_Print( f, "gamemode\t\t\"singleplayer_only\"\n" ); break;
|
|
|
|
case 2: FS_Print( f, "gamemode\t\t\"multiplayer_only\"\n" ); break;
|
|
|
|
}
|
|
|
|
|
2020-11-22 07:49:39 +01:00
|
|
|
if( COM_CheckStringEmpty( GameInfo->sp_entity ))
|
2018-04-13 18:23:45 +02:00
|
|
|
FS_Printf( f, "sp_entity\t\t\"%s\"\n", GameInfo->sp_entity );
|
2020-11-22 07:49:39 +01:00
|
|
|
if( COM_CheckStringEmpty( GameInfo->mp_entity ))
|
2018-04-13 18:23:45 +02:00
|
|
|
FS_Printf( f, "mp_entity\t\t\"%s\"\n", GameInfo->mp_entity );
|
2020-11-22 07:49:39 +01:00
|
|
|
if( COM_CheckStringEmpty( GameInfo->mp_filter ))
|
2018-10-27 22:31:55 +02:00
|
|
|
FS_Printf( f, "mp_filter\t\t\"%s\"\n", GameInfo->mp_filter );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
if( GameInfo->secure )
|
|
|
|
FS_Printf( f, "secure\t\t\"%i\"\n", GameInfo->secure );
|
|
|
|
|
|
|
|
if( GameInfo->nomodels )
|
|
|
|
FS_Printf( f, "nomodels\t\t\"%i\"\n", GameInfo->nomodels );
|
|
|
|
|
|
|
|
if( GameInfo->max_edicts > 0 )
|
|
|
|
FS_Printf( f, "max_edicts\t%i\n", GameInfo->max_edicts );
|
|
|
|
if( GameInfo->max_tents > 0 )
|
|
|
|
FS_Printf( f, "max_tempents\t%i\n", GameInfo->max_tents );
|
|
|
|
if( GameInfo->max_beams > 0 )
|
|
|
|
FS_Printf( f, "max_beams\t\t%i\n", GameInfo->max_beams );
|
|
|
|
if( GameInfo->max_particles > 0 )
|
|
|
|
FS_Printf( f, "max_particles\t%i\n", GameInfo->max_particles );
|
|
|
|
|
2018-10-27 22:31:55 +02:00
|
|
|
for( i = 0; i < NUM_AMBIENTS; i++ )
|
|
|
|
{
|
|
|
|
if( *GameInfo->ambientsound[i] )
|
|
|
|
{
|
|
|
|
if( !write_ambients )
|
|
|
|
{
|
|
|
|
FS_Print( f, "\n" );
|
|
|
|
write_ambients = true;
|
|
|
|
}
|
|
|
|
FS_Printf( f, "ambient%i\t\t%s\n", i, GameInfo->ambientsound[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 05:34:05 +01:00
|
|
|
if( GameInfo->noskills )
|
|
|
|
FS_Printf( f, "noskills\t\t\"%i\"\n", GameInfo->nomodels );
|
|
|
|
|
|
|
|
// always expose our extensions :)
|
|
|
|
FS_Printf( f, "internal_vgui_support\t\t%s\n", GameInfo->internal_vgui_support ? "1" : "0" );
|
|
|
|
FS_Printf( f, "render_picbutton_text\t\t%s\n", GameInfo->render_picbutton_text ? "1" : "0" );
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
FS_Print( f, "\n\n\n" );
|
|
|
|
FS_Close( f ); // all done
|
|
|
|
}
|
|
|
|
|
2018-04-17 02:46:08 +02:00
|
|
|
void FS_InitGameInfo( gameinfo_t *GameInfo, const char *gamedir )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2018-04-17 02:46:08 +02:00
|
|
|
memset( GameInfo, 0, sizeof( *GameInfo ));
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-04-17 02:46:08 +02:00
|
|
|
// filesystem info
|
|
|
|
Q_strncpy( GameInfo->gamefolder, gamedir, sizeof( GameInfo->gamefolder ));
|
|
|
|
Q_strncpy( GameInfo->basedir, "valve", sizeof( GameInfo->basedir ));
|
|
|
|
GameInfo->falldir[0] = 0;
|
|
|
|
Q_strncpy( GameInfo->startmap, "c0a0", sizeof( GameInfo->startmap ));
|
|
|
|
Q_strncpy( GameInfo->trainmap, "t0a0", sizeof( GameInfo->trainmap ));
|
|
|
|
Q_strncpy( GameInfo->title, "New Game", sizeof( GameInfo->title ));
|
|
|
|
GameInfo->version = 1.0f;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-04-17 02:46:08 +02:00
|
|
|
// .dll pathes
|
|
|
|
Q_strncpy( GameInfo->dll_path, "cl_dlls", sizeof( GameInfo->dll_path ));
|
|
|
|
Q_strncpy( GameInfo->game_dll, "dlls/hl.dll", sizeof( GameInfo->game_dll ));
|
|
|
|
Q_strncpy( GameInfo->game_dll_linux, "dlls/hl.so", sizeof( GameInfo->game_dll_linux ));
|
|
|
|
Q_strncpy( GameInfo->game_dll_osx, "dlls/hl.dylib", sizeof( GameInfo->game_dll_osx ));
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-04-17 02:46:08 +02:00
|
|
|
// .ico path
|
|
|
|
Q_strncpy( GameInfo->iconpath, "game.ico", sizeof( GameInfo->iconpath ));
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
Q_strncpy( GameInfo->sp_entity, "info_player_start", sizeof( GameInfo->sp_entity ));
|
|
|
|
Q_strncpy( GameInfo->mp_entity, "info_player_deathmatch", sizeof( GameInfo->mp_entity ));
|
|
|
|
|
2018-04-17 02:46:08 +02:00
|
|
|
GameInfo->max_edicts = 900; // default value if not specified
|
|
|
|
GameInfo->max_tents = 500;
|
|
|
|
GameInfo->max_beams = 128;
|
|
|
|
GameInfo->max_particles = 4096;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FS_ParseGenericGameInfo( gameinfo_t *GameInfo, const char *buf, const qboolean isGameInfo )
|
|
|
|
{
|
|
|
|
char *pfile = (char*) buf;
|
|
|
|
qboolean found_linux = false, found_osx = false;
|
|
|
|
string token;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2021-10-01 19:40:36 +02:00
|
|
|
while(( pfile = COM_ParseFile( pfile, token, sizeof( token ))) != NULL )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2018-04-17 02:46:08 +02:00
|
|
|
// different names in liblist/gameinfo
|
|
|
|
if( !Q_stricmp( token, isGameInfo ? "title" : "game" ))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, GameInfo->title, sizeof( GameInfo->title ));
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
2018-04-17 02:46:08 +02:00
|
|
|
// valid for both
|
|
|
|
else if( !Q_stricmp( token, "fallback_dir" ))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, GameInfo->falldir, sizeof( GameInfo->falldir ));
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
2018-04-17 02:46:08 +02:00
|
|
|
// valid for both
|
2018-04-13 18:23:45 +02:00
|
|
|
else if( !Q_stricmp( token, "startmap" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, GameInfo->startmap, sizeof( GameInfo->startmap ));
|
2018-04-13 18:23:45 +02:00
|
|
|
COM_StripExtension( GameInfo->startmap ); // HQ2:Amen has extension .bsp
|
|
|
|
}
|
2018-04-17 02:46:08 +02:00
|
|
|
// only trainmap is valid for gameinfo
|
|
|
|
else if( !Q_stricmp( token, "trainmap" ) ||
|
|
|
|
(!isGameInfo && !Q_stricmp( token, "trainingmap" )))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, GameInfo->trainmap, sizeof( GameInfo->trainmap ));
|
2018-04-13 18:23:45 +02:00
|
|
|
COM_StripExtension( GameInfo->trainmap ); // HQ2:Amen has extension .bsp
|
|
|
|
}
|
2018-04-17 02:46:08 +02:00
|
|
|
// valid for both
|
2018-04-13 18:23:45 +02:00
|
|
|
else if( !Q_stricmp( token, "url_info" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, GameInfo->game_url, sizeof( GameInfo->game_url ));
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
2018-04-17 02:46:08 +02:00
|
|
|
// different names
|
|
|
|
else if( !Q_stricmp( token, isGameInfo ? "url_update" : "url_dl" ))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, GameInfo->update_url, sizeof( GameInfo->update_url ));
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
2018-04-17 02:46:08 +02:00
|
|
|
// valid for both
|
2018-04-13 18:23:45 +02:00
|
|
|
else if( !Q_stricmp( token, "gamedll" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, GameInfo->game_dll, sizeof( GameInfo->game_dll ));
|
2018-04-13 18:23:45 +02:00
|
|
|
COM_FixSlashes( GameInfo->game_dll );
|
|
|
|
}
|
2018-04-17 02:46:08 +02:00
|
|
|
// valid for both
|
|
|
|
else if( !Q_stricmp( token, "gamedll_linux" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, GameInfo->game_dll_linux, sizeof( GameInfo->game_dll_linux ));
|
2018-04-17 02:46:08 +02:00
|
|
|
found_linux = true;
|
|
|
|
}
|
|
|
|
// valid for both
|
|
|
|
else if( !Q_stricmp( token, "gamedll_osx" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, GameInfo->game_dll_osx, sizeof( GameInfo->game_dll_osx ));
|
2018-04-17 02:46:08 +02:00
|
|
|
found_osx = true;
|
|
|
|
}
|
|
|
|
// valid for both
|
2018-04-13 18:23:45 +02:00
|
|
|
else if( !Q_stricmp( token, "icon" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, GameInfo->iconpath, sizeof( GameInfo->iconpath ));
|
2018-04-13 18:23:45 +02:00
|
|
|
COM_FixSlashes( GameInfo->iconpath );
|
|
|
|
COM_DefaultExtension( GameInfo->iconpath, ".ico" );
|
|
|
|
}
|
|
|
|
else if( !Q_stricmp( token, "type" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, token, sizeof( token ));
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-04-17 02:46:08 +02:00
|
|
|
if( !isGameInfo && !Q_stricmp( token, "singleplayer_only" ))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2018-04-17 02:46:08 +02:00
|
|
|
// TODO: Remove this ugly hack too.
|
|
|
|
// This was made because Half-Life has multiplayer,
|
|
|
|
// but for some reason it's marked as singleplayer_only.
|
|
|
|
// Old WON version is fine.
|
|
|
|
if( !Q_stricmp( GameInfo->gamefolder, "valve") )
|
|
|
|
GameInfo->gamemode = GAME_NORMAL;
|
|
|
|
else
|
|
|
|
GameInfo->gamemode = GAME_SINGLEPLAYER_ONLY;
|
2018-04-13 18:23:45 +02:00
|
|
|
Q_strncpy( GameInfo->type, "Single", sizeof( GameInfo->type ));
|
|
|
|
}
|
2018-04-17 02:46:08 +02:00
|
|
|
else if( !isGameInfo && !Q_stricmp( token, "multiplayer_only" ))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2018-04-17 02:46:08 +02:00
|
|
|
GameInfo->gamemode = GAME_MULTIPLAYER_ONLY;
|
2018-04-13 18:23:45 +02:00
|
|
|
Q_strncpy( GameInfo->type, "Multiplayer", sizeof( GameInfo->type ));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// pass type without changes
|
2018-04-17 02:46:08 +02:00
|
|
|
if( !isGameInfo )
|
|
|
|
GameInfo->gamemode = GAME_NORMAL;
|
2018-04-13 18:23:45 +02:00
|
|
|
Q_strncpy( GameInfo->type, token, sizeof( GameInfo->type ));
|
|
|
|
}
|
|
|
|
}
|
2018-04-17 02:46:08 +02:00
|
|
|
// valid for both
|
2018-04-13 18:23:45 +02:00
|
|
|
else if( !Q_stricmp( token, "version" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, token, sizeof( token ));
|
2018-04-13 18:23:45 +02:00
|
|
|
GameInfo->version = Q_atof( token );
|
|
|
|
}
|
2018-04-17 02:46:08 +02:00
|
|
|
// valid for both
|
2018-04-13 18:23:45 +02:00
|
|
|
else if( !Q_stricmp( token, "size" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, token, sizeof( token ));
|
2018-04-13 18:23:45 +02:00
|
|
|
GameInfo->size = Q_atoi( token );
|
|
|
|
}
|
2018-10-04 08:08:48 +02:00
|
|
|
else if( !Q_stricmp( token, "edicts" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, token, sizeof( token ));
|
2018-10-04 08:08:48 +02:00
|
|
|
GameInfo->max_edicts = Q_atoi( token );
|
|
|
|
}
|
2019-10-04 10:10:12 +02:00
|
|
|
else if( !Q_stricmp( token, isGameInfo ? "mp_entity" : "mpentity" ))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, GameInfo->mp_entity, sizeof( GameInfo->mp_entity ));
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
2018-10-27 23:39:29 +02:00
|
|
|
else if( !Q_stricmp( token, isGameInfo ? "mp_filter" : "mpfilter" ))
|
2018-10-27 22:31:55 +02:00
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, GameInfo->mp_filter, sizeof( GameInfo->mp_filter ));
|
2018-10-27 22:31:55 +02:00
|
|
|
}
|
2018-04-17 02:46:08 +02:00
|
|
|
// valid for both
|
2018-04-13 18:23:45 +02:00
|
|
|
else if( !Q_stricmp( token, "secure" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, token, sizeof( token ));
|
2018-04-13 18:23:45 +02:00
|
|
|
GameInfo->secure = Q_atoi( token );
|
|
|
|
}
|
2018-04-17 02:46:08 +02:00
|
|
|
// valid for both
|
2018-04-13 18:23:45 +02:00
|
|
|
else if( !Q_stricmp( token, "nomodels" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, token, sizeof( token ));
|
2018-04-13 18:23:45 +02:00
|
|
|
GameInfo->nomodels = Q_atoi( token );
|
|
|
|
}
|
2018-04-17 02:46:08 +02:00
|
|
|
else if( !Q_stricmp( token, isGameInfo ? "max_edicts" : "edicts" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, token, sizeof( token ));
|
2019-11-02 06:46:11 +01:00
|
|
|
GameInfo->max_edicts = bound( MIN_EDICTS, Q_atoi( token ), MAX_EDICTS );
|
2018-04-17 02:46:08 +02:00
|
|
|
}
|
|
|
|
// only for gameinfo
|
|
|
|
else if( isGameInfo )
|
|
|
|
{
|
|
|
|
if( !Q_stricmp( token, "basedir" ))
|
|
|
|
{
|
|
|
|
string fs_path;
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, fs_path, sizeof( fs_path ));
|
2018-04-17 02:46:08 +02:00
|
|
|
if( Q_stricmp( fs_path, GameInfo->basedir ) || Q_stricmp( fs_path, GameInfo->gamefolder ))
|
|
|
|
Q_strncpy( GameInfo->basedir, fs_path, sizeof( GameInfo->basedir ));
|
|
|
|
}
|
|
|
|
else if( !Q_stricmp( token, "sp_entity" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, GameInfo->sp_entity, sizeof( GameInfo->sp_entity ));
|
2018-04-17 02:46:08 +02:00
|
|
|
}
|
|
|
|
else if( isGameInfo && !Q_stricmp( token, "dllpath" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, GameInfo->dll_path, sizeof( GameInfo->dll_path ));
|
2018-04-17 02:46:08 +02:00
|
|
|
}
|
|
|
|
else if( isGameInfo && !Q_stricmp( token, "date" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, GameInfo->date, sizeof( GameInfo->date ));
|
2018-04-17 02:46:08 +02:00
|
|
|
}
|
|
|
|
else if( !Q_stricmp( token, "max_tempents" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, token, sizeof( token ));
|
2018-04-17 02:46:08 +02:00
|
|
|
GameInfo->max_tents = bound( 300, Q_atoi( token ), 2048 );
|
|
|
|
}
|
|
|
|
else if( !Q_stricmp( token, "max_beams" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, token, sizeof( token ));
|
2018-04-17 02:46:08 +02:00
|
|
|
GameInfo->max_beams = bound( 64, Q_atoi( token ), 512 );
|
|
|
|
}
|
|
|
|
else if( !Q_stricmp( token, "max_particles" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, token, sizeof( token ));
|
2018-04-26 01:53:24 +02:00
|
|
|
GameInfo->max_particles = bound( 4096, Q_atoi( token ), 32768 );
|
2018-04-17 02:46:08 +02:00
|
|
|
}
|
|
|
|
else if( !Q_stricmp( token, "gamemode" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, token, sizeof( token ));
|
2018-04-17 02:46:08 +02:00
|
|
|
// TODO: Remove this ugly hack too.
|
|
|
|
// This was made because Half-Life has multiplayer,
|
|
|
|
// but for some reason it's marked as singleplayer_only.
|
|
|
|
// Old WON version is fine.
|
|
|
|
if( !Q_stricmp( token, "singleplayer_only" ) && Q_stricmp( GameInfo->gamefolder, "valve") )
|
|
|
|
GameInfo->gamemode = GAME_SINGLEPLAYER_ONLY;
|
|
|
|
else if( !Q_stricmp( token, "multiplayer_only" ))
|
|
|
|
GameInfo->gamemode = GAME_MULTIPLAYER_ONLY;
|
|
|
|
}
|
|
|
|
else if( !Q_strnicmp( token, "ambient", 7 ))
|
|
|
|
{
|
|
|
|
int ambientNum = Q_atoi( token + 7 );
|
|
|
|
|
|
|
|
if( ambientNum < 0 || ambientNum > ( NUM_AMBIENTS - 1 ))
|
2018-10-27 23:39:29 +02:00
|
|
|
ambientNum = 0;
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, GameInfo->ambientsound[ambientNum],
|
|
|
|
sizeof( GameInfo->ambientsound[ambientNum] ));
|
2018-10-27 23:39:29 +02:00
|
|
|
}
|
|
|
|
else if( !Q_stricmp( token, "noskills" ))
|
|
|
|
{
|
2021-10-01 19:40:36 +02:00
|
|
|
pfile = COM_ParseFile( pfile, token, sizeof( token ));
|
2018-10-27 23:39:29 +02:00
|
|
|
GameInfo->noskills = Q_atoi( token );
|
2018-04-17 02:46:08 +02:00
|
|
|
}
|
2022-02-10 00:49:14 +01:00
|
|
|
else if( !Q_stricmp( token, "render_picbutton_text" ))
|
|
|
|
{
|
|
|
|
pfile = COM_ParseFile( pfile, token, sizeof( token ));
|
|
|
|
GameInfo->render_picbutton_text = Q_atoi( token );
|
|
|
|
}
|
2022-11-10 14:38:33 +01:00
|
|
|
else if( !Q_stricmp( token, "internal_vgui_support" ))
|
|
|
|
{
|
|
|
|
pfile = COM_ParseFile( pfile, token, sizeof( token ));
|
|
|
|
GameInfo->internal_vgui_support = Q_atoi( token );
|
|
|
|
}
|
2018-04-17 02:46:08 +02:00
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2018-04-17 02:46:08 +02:00
|
|
|
if( !found_linux || !found_osx )
|
|
|
|
{
|
|
|
|
// just replace extension from dll to so/dylib
|
|
|
|
char gamedll[64];
|
|
|
|
Q_strncpy( gamedll, GameInfo->game_dll, sizeof( gamedll ));
|
|
|
|
COM_StripExtension( gamedll );
|
|
|
|
|
|
|
|
if( !found_linux )
|
|
|
|
Q_snprintf( GameInfo->game_dll_linux, sizeof( GameInfo->game_dll_linux ), "%s.so", gamedll );
|
|
|
|
|
|
|
|
if( !found_osx )
|
|
|
|
Q_snprintf( GameInfo->game_dll_osx, sizeof( GameInfo->game_dll_osx ), "%s.dylib", gamedll );
|
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-04-17 02:46:08 +02:00
|
|
|
// make sure what gamedir is really exist
|
2023-01-04 20:33:21 +01:00
|
|
|
if( !FS_SysFolderExists( va( "%s/%s", fs_rootdir, GameInfo->falldir )))
|
2018-04-13 18:23:45 +02:00
|
|
|
GameInfo->falldir[0] = '\0';
|
2018-04-17 02:46:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
FS_CreateDefaultGameInfo
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
void FS_CreateDefaultGameInfo( const char *filename )
|
|
|
|
{
|
|
|
|
gameinfo_t defGI;
|
|
|
|
|
|
|
|
FS_InitGameInfo( &defGI, fs_basedir );
|
|
|
|
|
|
|
|
// make simple gameinfo.txt
|
|
|
|
FS_WriteGameInfo( filename, &defGI );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
FS_ParseLiblistGam
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
static qboolean FS_ParseLiblistGam( const char *filename, const char *gamedir, gameinfo_t *GameInfo )
|
|
|
|
{
|
2018-04-21 01:51:14 +02:00
|
|
|
char *afile;
|
2018-04-17 02:46:08 +02:00
|
|
|
|
2021-01-03 02:28:45 +01:00
|
|
|
if( !GameInfo ) return false;
|
2021-03-11 19:42:35 +01:00
|
|
|
afile = (char *)FS_LoadDirectFile( filename, NULL );
|
2018-04-17 02:46:08 +02:00
|
|
|
if( !afile ) return false;
|
|
|
|
|
|
|
|
FS_InitGameInfo( GameInfo, gamedir );
|
|
|
|
|
|
|
|
FS_ParseGenericGameInfo( GameInfo, afile, false );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
Mem_Free( afile );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
FS_ConvertGameInfo
|
|
|
|
================
|
|
|
|
*/
|
2018-07-10 22:03:27 +02:00
|
|
|
static qboolean FS_ConvertGameInfo( const char *gamedir, const char *gameinfo_path, const char *liblist_path )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
gameinfo_t GameInfo;
|
|
|
|
|
|
|
|
memset( &GameInfo, 0, sizeof( GameInfo ));
|
|
|
|
|
|
|
|
if( FS_ParseLiblistGam( liblist_path, gamedir, &GameInfo ))
|
|
|
|
{
|
|
|
|
Con_DPrintf( "Convert %s to %s\n", liblist_path, gameinfo_path );
|
|
|
|
FS_WriteGameInfo( gameinfo_path, &GameInfo );
|
2018-07-10 22:03:27 +02:00
|
|
|
|
|
|
|
return true;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
2018-07-10 22:03:27 +02:00
|
|
|
|
|
|
|
return false;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
FS_ReadGameInfo
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
static qboolean FS_ReadGameInfo( const char *filepath, const char *gamedir, gameinfo_t *GameInfo )
|
|
|
|
{
|
2018-04-21 01:51:14 +02:00
|
|
|
char *afile;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2019-07-13 22:25:03 +02:00
|
|
|
afile = (char *)FS_LoadFile( filepath, NULL, false );
|
2023-01-04 20:33:21 +01:00
|
|
|
if( !afile )
|
|
|
|
return false;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-04-17 02:46:08 +02:00
|
|
|
FS_InitGameInfo( GameInfo, gamedir );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-04-21 01:51:14 +02:00
|
|
|
FS_ParseGenericGameInfo( GameInfo, afile, true );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-04-17 02:46:08 +02:00
|
|
|
Mem_Free( afile );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
FS_CheckForGameDir
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
static qboolean FS_CheckForGameDir( const char *gamedir )
|
|
|
|
{
|
|
|
|
// if directory contain config.cfg it's 100% gamedir
|
|
|
|
if( FS_FileExists( va( "%s/config.cfg", gamedir ), false ))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// if directory contain progs.dat it's 100% gamedir
|
|
|
|
if( FS_FileExists( va( "%s/progs.dat", gamedir ), false ))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// quake mods probably always archived but can missed config.cfg before first running
|
|
|
|
if( FS_FileExists( va( "%s/pak0.pak", gamedir ), false ))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// NOTE; adds here some additional checks if you wished
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
FS_ParseGameInfo
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
static qboolean FS_ParseGameInfo( const char *gamedir, gameinfo_t *GameInfo )
|
|
|
|
{
|
|
|
|
string liblist_path, gameinfo_path;
|
|
|
|
string default_gameinfo_path;
|
2018-07-10 22:03:27 +02:00
|
|
|
qboolean haveUpdate = false;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
Q_snprintf( default_gameinfo_path, sizeof( default_gameinfo_path ), "%s/gameinfo.txt", fs_basedir );
|
|
|
|
Q_snprintf( gameinfo_path, sizeof( gameinfo_path ), "%s/gameinfo.txt", gamedir );
|
|
|
|
Q_snprintf( liblist_path, sizeof( liblist_path ), "%s/liblist.gam", gamedir );
|
|
|
|
|
2018-07-10 22:03:27 +02:00
|
|
|
// here goes some RoDir magic...
|
2023-01-04 20:33:21 +01:00
|
|
|
if( COM_CheckStringEmpty( fs_rodir ))
|
2018-07-10 22:03:27 +02:00
|
|
|
{
|
2023-01-04 20:33:21 +01:00
|
|
|
string gameinfo_ro, liblist_ro;
|
2018-07-10 22:03:27 +02:00
|
|
|
fs_offset_t roLibListTime, roGameInfoTime, rwGameInfoTime;
|
|
|
|
|
2021-03-11 19:42:35 +01:00
|
|
|
FS_AllowDirectPaths( true );
|
|
|
|
|
2023-01-04 20:33:21 +01:00
|
|
|
Q_snprintf( gameinfo_ro, sizeof( gameinfo_ro ), "%s/%s/gameinfo.txt", fs_rodir, gamedir );
|
2022-07-01 18:37:21 +02:00
|
|
|
Q_snprintf( liblist_ro, sizeof( liblist_ro ), "%s/%s/liblist.gam", fs_rodir, gamedir );
|
2018-07-10 22:03:27 +02:00
|
|
|
|
|
|
|
roLibListTime = FS_SysFileTime( liblist_ro );
|
2023-01-04 20:33:21 +01:00
|
|
|
roGameInfoTime = FS_SysFileTime( gameinfo_ro );
|
2018-07-10 22:03:27 +02:00
|
|
|
rwGameInfoTime = FS_SysFileTime( gameinfo_path );
|
|
|
|
|
|
|
|
if( roLibListTime > rwGameInfoTime )
|
|
|
|
{
|
|
|
|
haveUpdate = FS_ConvertGameInfo( gamedir, gameinfo_path, liblist_ro );
|
|
|
|
}
|
|
|
|
else if( roGameInfoTime > rwGameInfoTime )
|
|
|
|
{
|
2023-01-04 20:33:21 +01:00
|
|
|
fs_offset_t len;
|
|
|
|
char *afile_ro = (char *)FS_LoadDirectFile( gameinfo_ro, &len );
|
2018-07-10 22:03:27 +02:00
|
|
|
|
|
|
|
if( afile_ro )
|
|
|
|
{
|
2023-01-04 20:33:21 +01:00
|
|
|
Con_DPrintf( "Copy rodir %s to rwdir %s\n", gameinfo_ro, gameinfo_path );
|
2018-07-10 22:03:27 +02:00
|
|
|
haveUpdate = true;
|
2023-01-04 20:33:21 +01:00
|
|
|
FS_WriteFile( gameinfo_path, afile_ro, len );
|
2018-07-10 22:03:27 +02:00
|
|
|
Mem_Free( afile_ro );
|
|
|
|
}
|
|
|
|
}
|
2021-03-11 19:42:35 +01:00
|
|
|
|
|
|
|
FS_AllowDirectPaths( false );
|
2018-07-10 22:03:27 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
// if user change liblist.gam update the gameinfo.txt
|
|
|
|
if( FS_FileTime( liblist_path, false ) > FS_FileTime( gameinfo_path, false ))
|
|
|
|
FS_ConvertGameInfo( gamedir, gameinfo_path, liblist_path );
|
|
|
|
|
|
|
|
// force to create gameinfo for specified game if missing
|
|
|
|
if(( FS_CheckForGameDir( gamedir ) || !Q_stricmp( fs_gamedir, gamedir )) && !FS_FileExists( gameinfo_path, false ))
|
|
|
|
{
|
2023-01-04 20:33:21 +01:00
|
|
|
gameinfo_t tmpGameInfo;
|
2018-04-13 18:23:45 +02:00
|
|
|
memset( &tmpGameInfo, 0, sizeof( tmpGameInfo ));
|
|
|
|
|
|
|
|
if( FS_ReadGameInfo( default_gameinfo_path, gamedir, &tmpGameInfo ))
|
|
|
|
{
|
2022-07-01 18:37:21 +02:00
|
|
|
// now we have copy of game info from basedir but needs to change gamedir
|
|
|
|
Con_DPrintf( "Convert %s to %s\n", default_gameinfo_path, gameinfo_path );
|
|
|
|
Q_strncpy( tmpGameInfo.gamefolder, gamedir, sizeof( tmpGameInfo.gamefolder ));
|
|
|
|
FS_WriteGameInfo( gameinfo_path, &tmpGameInfo );
|
|
|
|
}
|
|
|
|
else FS_CreateDefaultGameInfo( gameinfo_path );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !GameInfo || !FS_FileExists( gameinfo_path, false ))
|
|
|
|
return false; // no dest
|
|
|
|
|
2023-01-04 20:33:21 +01:00
|
|
|
return FS_ReadGameInfo( gameinfo_path, gamedir, GameInfo );
|
2022-07-01 18:37:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
FS_AddGameHierarchy
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
void FS_AddGameHierarchy( const char *dir, uint flags )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
qboolean isGameDir = flags & FS_GAMEDIR_PATH;
|
|
|
|
|
|
|
|
GI->added = true;
|
|
|
|
|
|
|
|
if( !COM_CheckString( dir ))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// add the common game directory
|
|
|
|
|
|
|
|
// recursive gamedirs
|
|
|
|
// for example, czeror->czero->cstrike->valve
|
|
|
|
for( i = 0; i < FI.numgames; i++ )
|
|
|
|
{
|
|
|
|
if( !Q_strnicmp( FI.games[i]->gamefolder, dir, 64 ))
|
|
|
|
{
|
2022-12-14 22:52:09 +01:00
|
|
|
Con_Reportf( "FS_AddGameHierarchy: adding recursive basedir %s\n", FI.games[i]->basedir );
|
2022-07-01 18:37:21 +02:00
|
|
|
if( !FI.games[i]->added && Q_stricmp( FI.games[i]->gamefolder, FI.games[i]->basedir ))
|
|
|
|
{
|
|
|
|
FI.games[i]->added = true;
|
|
|
|
FS_AddGameHierarchy( FI.games[i]->basedir, flags & (~FS_GAMEDIR_PATH) );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( COM_CheckStringEmpty( fs_rodir ) )
|
|
|
|
{
|
|
|
|
// append new flags to rodir, except FS_GAMEDIR_PATH and FS_CUSTOM_PATH
|
|
|
|
uint newFlags = FS_NOWRITE_PATH | (flags & (~FS_GAMEDIR_PATH|FS_CUSTOM_PATH));
|
|
|
|
if( isGameDir )
|
|
|
|
newFlags |= FS_GAMERODIR_PATH;
|
|
|
|
|
|
|
|
FS_AllowDirectPaths( true );
|
|
|
|
FS_AddGameDirectory( va( "%s/%s/", fs_rodir, dir ), newFlags );
|
|
|
|
FS_AllowDirectPaths( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( isGameDir )
|
|
|
|
FS_AddGameDirectory( va( "%s/downloaded/", dir ), FS_NOWRITE_PATH | FS_CUSTOM_PATH );
|
|
|
|
FS_AddGameDirectory( va( "%s/", dir ), flags );
|
|
|
|
if( isGameDir )
|
|
|
|
FS_AddGameDirectory( va( "%s/custom/", dir ), FS_NOWRITE_PATH | FS_CUSTOM_PATH );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
FS_Rescan
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
void FS_Rescan( void )
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
const int extrasFlags = FS_NOWRITE_PATH | FS_CUSTOM_PATH;
|
|
|
|
Con_Reportf( "FS_Rescan( %s )\n", GI->title );
|
|
|
|
|
|
|
|
FS_ClearSearchPath();
|
|
|
|
|
|
|
|
#if XASH_IOS
|
|
|
|
{
|
|
|
|
FS_AddPak_Fullpath( va( "%sextras.pak", SDL_GetBasePath() ), NULL, extrasFlags );
|
|
|
|
FS_AddPak_Fullpath( va( "%sextras_%s.pak", SDL_GetBasePath(), GI->gamefolder ), NULL, extrasFlags );
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
str = getenv( "XASH3D_EXTRAS_PAK1" );
|
|
|
|
if( COM_CheckString( str ))
|
|
|
|
FS_AddArchive_Fullpath( str, NULL, extrasFlags );
|
|
|
|
|
|
|
|
str = getenv( "XASH3D_EXTRAS_PAK2" );
|
|
|
|
if( COM_CheckString( str ))
|
|
|
|
FS_AddArchive_Fullpath( str, NULL, extrasFlags );
|
|
|
|
#endif
|
|
|
|
|
2022-12-14 22:52:09 +01:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( Q_stricmp( GI->basedir, GI->gamefolder ))
|
|
|
|
FS_AddGameHierarchy( GI->basedir, 0 );
|
|
|
|
if( Q_stricmp( GI->basedir, GI->falldir ) && Q_stricmp( GI->gamefolder, GI->falldir ))
|
|
|
|
FS_AddGameHierarchy( GI->falldir, 0 );
|
|
|
|
FS_AddGameHierarchy( GI->gamefolder, FS_GAMEDIR_PATH );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
FS_LoadGameInfo
|
|
|
|
|
|
|
|
can be passed null arg
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
void FS_LoadGameInfo( const char *rootfolder )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// lock uplevel of gamedir for read\write
|
|
|
|
fs_ext_path = false;
|
|
|
|
|
|
|
|
if( rootfolder ) Q_strcpy( fs_gamedir, rootfolder );
|
|
|
|
Con_Reportf( "FS_LoadGameInfo( %s )\n", fs_gamedir );
|
|
|
|
|
|
|
|
// clear any old pathes
|
|
|
|
FS_ClearSearchPath();
|
|
|
|
|
|
|
|
// validate gamedir
|
|
|
|
for( i = 0; i < FI.numgames; i++ )
|
|
|
|
{
|
|
|
|
if( !Q_stricmp( FI.games[i]->gamefolder, fs_gamedir ))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( i == FI.numgames )
|
|
|
|
Sys_Error( "Couldn't find game directory '%s'\n", fs_gamedir );
|
|
|
|
|
|
|
|
FI.GameInfo = FI.games[i];
|
|
|
|
|
|
|
|
FS_Rescan(); // create new filesystem
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
FS_CheckForCrypt
|
|
|
|
|
|
|
|
return true if library is crypted
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
static qboolean FS_CheckForCrypt( const char *dllname )
|
|
|
|
{
|
|
|
|
file_t *f;
|
|
|
|
int key;
|
|
|
|
|
2022-12-04 04:15:44 +01:00
|
|
|
// this encryption is specific to DLLs
|
|
|
|
if( Q_stricmp( COM_FileExtension( dllname ), "dll" ))
|
|
|
|
return false;
|
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
f = FS_Open( dllname, "rb", false );
|
|
|
|
if( !f ) return false;
|
|
|
|
|
|
|
|
FS_Seek( f, 64, SEEK_SET ); // skip first 64 bytes
|
|
|
|
FS_Read( f, &key, sizeof( key ));
|
|
|
|
FS_Close( f );
|
|
|
|
|
|
|
|
return ( key == 0x12345678 ) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
FS_FindLibrary
|
|
|
|
|
|
|
|
search for library, assume index is valid
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
static qboolean FS_FindLibrary( const char *dllname, qboolean directpath, fs_dllinfo_t *dllInfo )
|
|
|
|
{
|
|
|
|
searchpath_t *search;
|
2022-12-19 15:22:30 +01:00
|
|
|
int index, start = 0, i, len;
|
2022-07-01 18:37:21 +02:00
|
|
|
|
|
|
|
fs_ext_path = directpath;
|
|
|
|
|
|
|
|
// check for bad exports
|
|
|
|
if( !COM_CheckString( dllname ))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// HACKHACK remove absoulte path to valve folder
|
|
|
|
if( !Q_strnicmp( dllname, "..\\valve\\", 9 ) || !Q_strnicmp( dllname, "../valve/", 9 ))
|
|
|
|
start += 9;
|
|
|
|
|
|
|
|
// replace all backward slashes
|
|
|
|
len = Q_strlen( dllname );
|
|
|
|
|
|
|
|
for( i = 0; i < len; i++ )
|
|
|
|
{
|
|
|
|
if( dllname[i+start] == '\\' ) dllInfo->shortPath[i] = '/';
|
|
|
|
else dllInfo->shortPath[i] = Q_tolower( dllname[i+start] );
|
|
|
|
}
|
|
|
|
dllInfo->shortPath[i] = '\0';
|
|
|
|
|
|
|
|
COM_DefaultExtension( dllInfo->shortPath, "."OS_LIB_EXT ); // apply ext if forget
|
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
search = FS_FindFile( dllInfo->shortPath, &index, NULL, 0, false );
|
2022-07-01 18:37:21 +02:00
|
|
|
|
|
|
|
if( !search && !directpath )
|
|
|
|
{
|
|
|
|
fs_ext_path = false;
|
|
|
|
|
|
|
|
// trying check also 'bin' folder for indirect paths
|
|
|
|
Q_strncpy( dllInfo->shortPath, dllname, sizeof( dllInfo->shortPath ));
|
2022-12-19 15:22:30 +01:00
|
|
|
search = FS_FindFile( dllInfo->shortPath, &index, NULL, 0, false );
|
2022-07-01 18:37:21 +02:00
|
|
|
if( !search ) return false; // unable to find
|
|
|
|
}
|
|
|
|
|
|
|
|
dllInfo->encrypted = FS_CheckForCrypt( dllInfo->shortPath );
|
|
|
|
|
2022-11-18 14:35:21 +01:00
|
|
|
if( index >= 0 && !dllInfo->encrypted && search )
|
2022-07-01 18:37:21 +02:00
|
|
|
{
|
|
|
|
Q_snprintf( dllInfo->fullPath, sizeof( dllInfo->fullPath ),
|
|
|
|
"%s%s", search->filename, dllInfo->shortPath );
|
|
|
|
dllInfo->custom_loader = false; // we can loading from disk and use normal debugging
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// NOTE: if search is NULL let the OS found library himself
|
|
|
|
Q_strncpy( dllInfo->fullPath, dllInfo->shortPath, sizeof( dllInfo->fullPath ));
|
|
|
|
|
|
|
|
if( search && search->type != SEARCHPATH_PLAIN )
|
|
|
|
{
|
|
|
|
#if XASH_WIN32 && XASH_X86 // a1ba: custom loader is non-portable (I just don't want to touch it)
|
|
|
|
Con_Printf( S_WARN "%s: loading libraries from packs is deprecated "
|
|
|
|
"and will be removed in the future\n", __FUNCTION__ );
|
2022-08-01 02:12:35 +02:00
|
|
|
dllInfo->custom_loader = true;
|
2022-07-01 18:37:21 +02:00
|
|
|
#else
|
|
|
|
Con_Printf( S_WARN "%s: loading libraries from packs is unsupported on "
|
|
|
|
"this platform\n", __FUNCTION__ );
|
|
|
|
dllInfo->custom_loader = false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dllInfo->custom_loader = false;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 18:37:21 +02:00
|
|
|
fs_ext_path = false; // always reset direct paths
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
return true;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
poolhandle_t _Mem_AllocPool( const char *name, const char *filename, int fileline )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-12-20 14:34:23 +01:00
|
|
|
return (poolhandle_t)0xDEADC0DE;
|
2022-07-01 18:37:21 +02:00
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
void _Mem_FreePool( poolhandle_t *poolptr, const char *filename, int fileline )
|
|
|
|
{
|
|
|
|
// stub
|
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
void* _Mem_Alloc( poolhandle_t poolptr, size_t size, qboolean clear, const char *filename, int fileline )
|
|
|
|
{
|
|
|
|
if( clear ) return calloc( 1, size );
|
|
|
|
return malloc( size );
|
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
void* _Mem_Realloc( poolhandle_t poolptr, void *memptr, size_t size, qboolean clear, const char *filename, int fileline )
|
|
|
|
{
|
|
|
|
return realloc( memptr, size );
|
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
void _Mem_Free( void *data, const char *filename, int fileline )
|
|
|
|
{
|
|
|
|
free( data );
|
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
void _Con_Printf( const char *fmt, ... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
2019-07-01 04:50:04 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
va_start( ap, fmt );
|
|
|
|
vprintf( fmt, ap );
|
|
|
|
va_end( ap );
|
|
|
|
}
|
2019-07-01 04:50:04 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
void _Sys_Error( const char *fmt, ... )
|
|
|
|
{
|
|
|
|
va_list ap;
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
va_start( ap, fmt );
|
|
|
|
vfprintf( stderr, fmt, ap );
|
|
|
|
va_end( ap );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
exit( 1 );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
================
|
|
|
|
FS_Init
|
|
|
|
================
|
|
|
|
*/
|
2022-07-01 18:37:21 +02:00
|
|
|
qboolean FS_InitStdio( qboolean caseinsensitive, const char *rootdir, const char *basedir, const char *gamedir, const char *rodir )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
stringlist_t dirs;
|
|
|
|
qboolean hasBaseDir = false;
|
|
|
|
qboolean hasGameDir = false;
|
|
|
|
int i;
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
FS_InitMemory();
|
2022-08-01 02:12:35 +02:00
|
|
|
#if !XASH_WIN32
|
2022-07-01 18:37:21 +02:00
|
|
|
fs_caseinsensitive = caseinsensitive;
|
2022-08-01 02:12:35 +02:00
|
|
|
#endif
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
Q_strncpy( fs_rootdir, rootdir, sizeof( fs_rootdir ));
|
|
|
|
Q_strncpy( fs_gamedir, gamedir, sizeof( fs_gamedir ));
|
|
|
|
Q_strncpy( fs_basedir, basedir, sizeof( fs_basedir ));
|
|
|
|
Q_strncpy( fs_rodir, rodir, sizeof( fs_rodir ));
|
2018-07-10 22:03:27 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
// add readonly directories first
|
|
|
|
if( COM_CheckStringEmpty( fs_rodir ))
|
2018-07-10 22:03:27 +02:00
|
|
|
{
|
2022-07-01 18:37:21 +02:00
|
|
|
if( !Q_stricmp( fs_rodir, fs_rootdir ))
|
2018-07-10 22:03:27 +02:00
|
|
|
{
|
|
|
|
Sys_Error( "RoDir and default rootdir can't point to same directory!" );
|
2022-07-01 18:37:21 +02:00
|
|
|
return false;
|
2018-07-10 22:03:27 +02:00
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-07-10 22:03:27 +02:00
|
|
|
stringlistinit( &dirs );
|
2022-12-26 12:39:51 +01:00
|
|
|
listdirectory( &dirs, fs_rodir );
|
2018-07-10 22:03:27 +02:00
|
|
|
stringlistsort( &dirs );
|
|
|
|
|
|
|
|
for( i = 0; i < dirs.numstrings; i++ )
|
|
|
|
{
|
2022-12-19 15:22:30 +01:00
|
|
|
char roPath[MAX_SYSPATH];
|
|
|
|
char rwPath[MAX_SYSPATH];
|
|
|
|
|
2023-01-04 20:33:21 +01:00
|
|
|
Q_snprintf( roPath, sizeof( roPath ), "%s/%s/", fs_rodir, dirs.strings[i] );
|
|
|
|
Q_snprintf( rwPath, sizeof( rwPath ), "%s/%s/", fs_rootdir, dirs.strings[i] );
|
2021-12-18 03:56:38 +01:00
|
|
|
|
|
|
|
// check if it's a directory
|
|
|
|
if( !FS_SysFolderExists( roPath ))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// no need to check folders here, FS_CreatePath will not fail
|
|
|
|
FS_CreatePath( rwPath );
|
2018-07-10 22:03:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stringlistfreecontents( &dirs );
|
|
|
|
}
|
|
|
|
|
2018-06-09 00:28:35 +02:00
|
|
|
// validate directories
|
2018-07-10 22:03:27 +02:00
|
|
|
stringlistinit( &dirs );
|
2022-12-26 12:39:51 +01:00
|
|
|
listdirectory( &dirs, "./" );
|
2018-07-10 22:03:27 +02:00
|
|
|
stringlistsort( &dirs );
|
|
|
|
|
2018-06-09 00:28:35 +02:00
|
|
|
for( i = 0; i < dirs.numstrings; i++ )
|
|
|
|
{
|
|
|
|
if( !Q_stricmp( fs_basedir, dirs.strings[i] ))
|
|
|
|
hasBaseDir = true;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-06-09 00:28:35 +02:00
|
|
|
if( !Q_stricmp( fs_gamedir, dirs.strings[i] ))
|
|
|
|
hasGameDir = true;
|
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-06-09 00:28:35 +02:00
|
|
|
if( !hasGameDir )
|
|
|
|
{
|
|
|
|
Con_Printf( S_ERROR "game directory \"%s\" not exist\n", fs_gamedir );
|
|
|
|
if( hasBaseDir ) Q_strncpy( fs_gamedir, fs_basedir, sizeof( fs_gamedir ));
|
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-06-09 00:28:35 +02:00
|
|
|
// build list of game directories here
|
2022-12-19 15:22:30 +01:00
|
|
|
if( COM_CheckStringEmpty( fs_rodir ))
|
|
|
|
FS_AddGameDirectory( va( "%s/", fs_rodir ), FS_STATIC_PATH|FS_NOWRITE_PATH );
|
2022-11-02 02:44:11 +01:00
|
|
|
FS_AddGameDirectory( "./", FS_STATIC_PATH );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-06-09 00:28:35 +02:00
|
|
|
for( i = 0; i < dirs.numstrings; i++ )
|
|
|
|
{
|
2023-01-04 20:33:21 +01:00
|
|
|
if( !FS_SysFolderExists( dirs.strings[i] ))
|
2018-06-09 00:28:35 +02:00
|
|
|
continue;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( FI.games[FI.numgames] == NULL )
|
|
|
|
FI.games[FI.numgames] = (gameinfo_t *)Mem_Calloc( fs_mempool, sizeof( gameinfo_t ));
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( FS_ParseGameInfo( dirs.strings[i], FI.games[FI.numgames] ))
|
|
|
|
FI.numgames++; // added
|
2018-06-09 00:28:35 +02:00
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-06-09 00:28:35 +02:00
|
|
|
stringlistfreecontents( &dirs );
|
2018-04-19 22:11:24 +02:00
|
|
|
Con_Reportf( "FS_Init: done\n" );
|
2022-07-01 18:37:21 +02:00
|
|
|
|
|
|
|
return true;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void FS_AllowDirectPaths( qboolean enable )
|
|
|
|
{
|
|
|
|
fs_ext_path = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
================
|
|
|
|
FS_Shutdown
|
|
|
|
================
|
|
|
|
*/
|
2022-07-01 18:37:21 +02:00
|
|
|
void FS_ShutdownStdio( void )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-07-01 18:37:21 +02:00
|
|
|
int i;
|
2018-04-13 18:23:45 +02:00
|
|
|
// release gamedirs
|
2022-07-01 18:37:21 +02:00
|
|
|
for( i = 0; i < FI.numgames; i++ )
|
|
|
|
if( FI.games[i] ) Mem_Free( FI.games[i] );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
FS_ClearSearchPath(); // release all wad files too
|
|
|
|
Mem_FreePool( &fs_mempool );
|
|
|
|
}
|
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
/*
|
|
|
|
============
|
|
|
|
FS_Path_f
|
|
|
|
|
|
|
|
debug info
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void FS_Path_f( void )
|
|
|
|
{
|
|
|
|
searchpath_t *s;
|
|
|
|
|
|
|
|
Con_Printf( "Current search path:\n" );
|
|
|
|
|
|
|
|
for( s = fs_searchpaths; s; s = s->next )
|
|
|
|
{
|
|
|
|
string info;
|
|
|
|
|
2022-12-14 23:06:20 +01:00
|
|
|
s->pfnPrintInfo( s, info, sizeof(info) );
|
2022-07-01 18:37:21 +02:00
|
|
|
|
|
|
|
Con_Printf( "%s", info );
|
|
|
|
|
|
|
|
if( s->flags & FS_GAMERODIR_PATH ) Con_Printf( " ^2rodir^7" );
|
|
|
|
if( s->flags & FS_GAMEDIR_PATH ) Con_Printf( " ^2gamedir^7" );
|
|
|
|
if( s->flags & FS_CUSTOM_PATH ) Con_Printf( " ^2custom^7" );
|
|
|
|
if( s->flags & FS_NOWRITE_PATH ) Con_Printf( " ^2nowrite^7" );
|
|
|
|
if( s->flags & FS_STATIC_PATH ) Con_Printf( " ^2static^7" );
|
|
|
|
|
|
|
|
Con_Printf( "\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_SysFileTime
|
|
|
|
|
|
|
|
Internal function used to determine filetime
|
|
|
|
====================
|
|
|
|
*/
|
2022-07-01 18:37:21 +02:00
|
|
|
int FS_SysFileTime( const char *filename )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2023-01-04 20:33:21 +01:00
|
|
|
#if XASH_WIN32
|
|
|
|
struct _stat buf;
|
|
|
|
if( _wstat( FS_PathToWideChar( filename ), &buf ) < 0 )
|
|
|
|
#else
|
2018-04-13 18:23:45 +02:00
|
|
|
struct stat buf;
|
2023-01-04 20:33:21 +01:00
|
|
|
if( stat( filename, &buf ) < 0 )
|
|
|
|
#endif
|
2018-04-13 18:23:45 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return buf.st_mtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_SysOpen
|
|
|
|
|
|
|
|
Internal function used to create a file_t and open the relevant non-packed file on disk
|
|
|
|
====================
|
|
|
|
*/
|
2022-07-01 18:37:21 +02:00
|
|
|
file_t *FS_SysOpen( const char *filepath, const char *mode )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
file_t *file;
|
|
|
|
int mod, opt;
|
|
|
|
uint ind;
|
|
|
|
|
|
|
|
// Parse the mode string
|
|
|
|
switch( mode[0] )
|
|
|
|
{
|
|
|
|
case 'r': // read
|
|
|
|
mod = O_RDONLY;
|
|
|
|
opt = 0;
|
|
|
|
break;
|
|
|
|
case 'w': // write
|
|
|
|
mod = O_WRONLY;
|
|
|
|
opt = O_CREAT | O_TRUNC;
|
|
|
|
break;
|
|
|
|
case 'a': // append
|
|
|
|
mod = O_WRONLY;
|
|
|
|
opt = O_CREAT | O_APPEND;
|
|
|
|
break;
|
|
|
|
case 'e': // edit
|
|
|
|
mod = O_WRONLY;
|
|
|
|
opt = O_CREAT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ind = 1; mode[ind] != '\0'; ind++ )
|
|
|
|
{
|
|
|
|
switch( mode[ind] )
|
|
|
|
{
|
|
|
|
case '+':
|
|
|
|
mod = O_RDWR;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
opt |= O_BINARY;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-09 00:28:35 +02:00
|
|
|
file = (file_t *)Mem_Calloc( fs_mempool, sizeof( *file ));
|
2018-04-13 18:23:45 +02:00
|
|
|
file->filetime = FS_SysFileTime( filepath );
|
|
|
|
file->ungetc = EOF;
|
|
|
|
|
2021-10-03 08:08:03 +02:00
|
|
|
#if XASH_WIN32
|
2023-01-04 16:10:21 +01:00
|
|
|
file->handle = _wopen( FS_PathToWideChar( filepath ), mod | opt, 0666 );
|
2021-10-03 08:08:03 +02:00
|
|
|
#else
|
2018-04-13 18:23:45 +02:00
|
|
|
file->handle = open( filepath, mod|opt, 0666 );
|
2021-10-03 08:08:03 +02:00
|
|
|
#endif
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2019-11-24 01:52:08 +01:00
|
|
|
#if !XASH_WIN32
|
2018-04-13 19:44:39 +02:00
|
|
|
if( file->handle < 0 )
|
2020-02-08 16:50:30 +01:00
|
|
|
FS_BackupFileName( file, filepath, mod|opt );
|
2018-04-13 19:44:39 +02:00
|
|
|
#endif
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
if( file->handle < 0 )
|
|
|
|
{
|
|
|
|
Mem_Free( file );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-02-08 16:50:30 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
file->real_length = lseek( file->handle, 0, SEEK_END );
|
|
|
|
|
2020-02-08 16:50:30 +01:00
|
|
|
// uncomment do disable write
|
|
|
|
//if( opt & O_CREAT )
|
|
|
|
// return NULL;
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
// For files opened in append mode, we start at the end of the file
|
2020-02-08 16:50:30 +01:00
|
|
|
if( opt & O_APPEND ) file->position = file->real_length;
|
2018-04-13 18:23:45 +02:00
|
|
|
else lseek( file->handle, 0, SEEK_SET );
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
2020-02-08 16:50:30 +01:00
|
|
|
/*
|
|
|
|
static int FS_DuplicateHandle( const char *filename, int handle, fs_offset_t pos )
|
|
|
|
{
|
|
|
|
#ifdef HAVE_DUP
|
|
|
|
return dup( handle );
|
|
|
|
#else
|
|
|
|
int newhandle = open( filename, O_RDONLY|O_BINARY );
|
|
|
|
lseek( newhandle, pos, SEEK_SET );
|
|
|
|
return newhandle;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
file_t *FS_OpenHandle( const char *syspath, int handle, fs_offset_t offset, fs_offset_t len )
|
2020-02-08 16:50:30 +01:00
|
|
|
{
|
|
|
|
file_t *file = (file_t *)Mem_Calloc( fs_mempool, sizeof( file_t ));
|
|
|
|
#ifndef XASH_REDUCE_FD
|
|
|
|
#ifdef HAVE_DUP
|
|
|
|
file->handle = dup( handle );
|
|
|
|
#else
|
|
|
|
file->handle = open( syspath, O_RDONLY|O_BINARY );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if( lseek( file->handle, offset, SEEK_SET ) == -1 )
|
|
|
|
{
|
|
|
|
Mem_Free( file );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
file->backup_position = offset;
|
|
|
|
file->backup_path = copystring( syspath );
|
|
|
|
file->backup_options = O_RDONLY|O_BINARY;
|
|
|
|
file->handle = -1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
file->real_length = len;
|
|
|
|
file->offset = offset;
|
|
|
|
file->position = 0;
|
|
|
|
file->ungetc = EOF;
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2023-01-04 16:10:21 +01:00
|
|
|
#if !defined( S_ISREG )
|
|
|
|
#define S_ISREG( m ) ( FBitSet( m, S_IFMT ) == S_IFREG )
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined( S_ISDIR )
|
|
|
|
#define S_ISDIR( m ) ( FBitSet( m, S_IFMT ) == S_IFDIR )
|
|
|
|
#endif
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
==================
|
|
|
|
FS_SysFileExists
|
|
|
|
|
|
|
|
Look for a file in the filesystem only
|
|
|
|
==================
|
|
|
|
*/
|
2023-01-04 16:10:21 +01:00
|
|
|
qboolean FS_SysFileExists( const char *path )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2021-10-03 08:08:03 +02:00
|
|
|
#if XASH_WIN32
|
2023-01-04 20:33:21 +01:00
|
|
|
struct _stat buf;
|
2023-01-04 16:10:21 +01:00
|
|
|
if( _wstat( FS_PathToWideChar( path ), &buf ) < 0 )
|
2021-10-03 08:08:03 +02:00
|
|
|
#else
|
2023-01-04 20:33:21 +01:00
|
|
|
struct stat buf;
|
2023-01-04 16:10:21 +01:00
|
|
|
if( stat( path, &buf ) < 0 )
|
2021-10-03 08:08:03 +02:00
|
|
|
#endif
|
2023-01-04 16:10:21 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return S_ISREG( buf.st_mode );
|
2021-10-03 08:08:03 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
==================
|
|
|
|
FS_SysFolderExists
|
|
|
|
|
|
|
|
Look for a existing folder
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
qboolean FS_SysFolderExists( const char *path )
|
|
|
|
{
|
2023-01-04 16:10:21 +01:00
|
|
|
#if XASH_WIN32
|
2023-01-04 20:33:21 +01:00
|
|
|
struct _stat buf;
|
2023-01-04 16:10:21 +01:00
|
|
|
if( _wstat( FS_PathToWideChar( path ), &buf ) < 0 )
|
|
|
|
#else
|
2023-01-04 20:33:21 +01:00
|
|
|
struct stat buf;
|
2022-05-05 04:03:47 +02:00
|
|
|
if( stat( path, &buf ) < 0 )
|
2023-01-04 16:10:21 +01:00
|
|
|
#endif
|
2018-04-13 19:44:39 +02:00
|
|
|
return false;
|
2022-05-05 04:03:47 +02:00
|
|
|
|
|
|
|
return S_ISDIR( buf.st_mode );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
/*
|
|
|
|
==============
|
|
|
|
FS_SysFileOrFolderExists
|
|
|
|
|
|
|
|
Check if filesystem entry exists at all, don't mind the type
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
qboolean FS_SysFileOrFolderExists( const char *path )
|
|
|
|
{
|
2023-01-04 16:10:21 +01:00
|
|
|
#if XASH_WIN32
|
2023-01-04 20:33:21 +01:00
|
|
|
struct _stat buf;
|
2023-01-04 16:10:21 +01:00
|
|
|
return _wstat( FS_PathToWideChar( path ), &buf ) >= 0;
|
|
|
|
#else
|
2023-01-04 20:33:21 +01:00
|
|
|
struct stat buf;
|
2022-12-19 15:22:30 +01:00
|
|
|
return stat( path, &buf ) >= 0;
|
2023-01-04 16:10:21 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
FS_SetCurrentDirectory
|
|
|
|
|
|
|
|
Sets current directory, path should be in UTF-8 encoding
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
int FS_SetCurrentDirectory( const char *path )
|
|
|
|
{
|
|
|
|
#if XASH_WIN32
|
|
|
|
return SetCurrentDirectoryW( FS_PathToWideChar( path ));
|
|
|
|
#elif XASH_POSIX
|
|
|
|
return !chdir( path );
|
2022-12-19 15:22:30 +01:00
|
|
|
#else
|
|
|
|
#error
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_FindFile
|
|
|
|
|
|
|
|
Look for a file in the packages and in the filesystem
|
|
|
|
|
|
|
|
Return the searchpath where the file was found (or NULL)
|
|
|
|
and the file index in the package if relevant
|
|
|
|
====================
|
|
|
|
*/
|
2022-12-19 15:22:30 +01:00
|
|
|
searchpath_t *FS_FindFile( const char *name, int *index, char *fixedname, size_t len, qboolean gamedironly )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
searchpath_t *search;
|
|
|
|
|
|
|
|
// search through the path, one element at a time
|
|
|
|
for( search = fs_searchpaths; search; search = search->next )
|
|
|
|
{
|
2022-11-18 14:35:21 +01:00
|
|
|
int pack_ind;
|
|
|
|
|
2018-07-10 22:03:27 +02:00
|
|
|
if( gamedironly & !FBitSet( search->flags, FS_GAMEDIRONLY_SEARCH_FLAGS ))
|
2018-04-13 18:23:45 +02:00
|
|
|
continue;
|
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
pack_ind = search->pfnFindFile( search, name, fixedname, len );
|
2022-11-18 14:35:21 +01:00
|
|
|
if( pack_ind >= 0 )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2023-01-04 20:33:21 +01:00
|
|
|
if( index )
|
|
|
|
*index = pack_ind;
|
2022-11-18 14:35:21 +01:00
|
|
|
return search;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 02:46:08 +02:00
|
|
|
if( fs_ext_path )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-12-14 22:52:09 +01:00
|
|
|
char netpath[MAX_SYSPATH], dirpath[MAX_SYSPATH];
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2023-01-04 20:33:21 +01:00
|
|
|
Q_snprintf( dirpath, sizeof( dirpath ), "%s/", fs_rootdir );
|
2022-12-14 22:52:09 +01:00
|
|
|
Q_snprintf( netpath, sizeof( netpath ), "%s%s", dirpath, name );
|
2023-01-04 20:33:21 +01:00
|
|
|
|
2023-01-04 16:10:21 +01:00
|
|
|
if( FS_SysFileExists( netpath ))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-12-19 15:22:30 +01:00
|
|
|
static searchpath_t fs_directpath;
|
|
|
|
|
|
|
|
// clear old dir cache
|
|
|
|
if( fs_directpath.pfnClose )
|
|
|
|
fs_directpath.pfnClose( &fs_directpath );
|
|
|
|
|
|
|
|
// just copy the name, we don't do case sensitivity fix there
|
|
|
|
if( fixedname )
|
|
|
|
Q_strncpy( fixedname, name, len );
|
2022-12-14 22:52:09 +01:00
|
|
|
FS_InitDirectorySearchpath( &fs_directpath, dirpath, 0 );
|
2022-12-04 04:18:34 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
if( index != NULL )
|
2022-12-19 15:22:30 +01:00
|
|
|
*index = 0;
|
2022-12-14 22:52:09 +01:00
|
|
|
return &fs_directpath;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( index != NULL )
|
|
|
|
*index = -1;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
===========
|
|
|
|
FS_OpenReadFile
|
|
|
|
|
|
|
|
Look for a file in the search paths and open it in read-only mode
|
|
|
|
===========
|
|
|
|
*/
|
|
|
|
file_t *FS_OpenReadFile( const char *filename, const char *mode, qboolean gamedironly )
|
|
|
|
{
|
2022-12-19 15:22:30 +01:00
|
|
|
searchpath_t *search;
|
|
|
|
char netpath[MAX_SYSPATH];
|
|
|
|
int pack_ind;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
search = FS_FindFile( filename, &pack_ind, netpath, sizeof( netpath ), gamedironly );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
// not found?
|
|
|
|
if( search == NULL )
|
2021-01-03 02:28:45 +01:00
|
|
|
return NULL;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
return search->pfnOpenFile( search, netpath, mode, pack_ind );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============================================================================
|
|
|
|
|
|
|
|
MAIN PUBLIC FUNCTIONS
|
|
|
|
|
|
|
|
=============================================================================
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_Open
|
|
|
|
|
|
|
|
Open a file. The syntax is the same as fopen
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
file_t *FS_Open( const char *filepath, const char *mode, qboolean gamedironly )
|
|
|
|
{
|
|
|
|
// some stupid mappers used leading '/' or '\' in path to models or sounds
|
|
|
|
if( filepath[0] == '/' || filepath[0] == '\\' )
|
|
|
|
filepath++;
|
|
|
|
|
|
|
|
if( filepath[0] == '/' || filepath[0] == '\\' )
|
|
|
|
filepath++;
|
|
|
|
|
|
|
|
if( FS_CheckNastyPath( filepath, false ))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// if the file is opened in "write", "append", or "read/write" mode
|
|
|
|
if( mode[0] == 'w' || mode[0] == 'a'|| mode[0] == 'e' || Q_strchr( mode, '+' ))
|
|
|
|
{
|
|
|
|
char real_path[MAX_SYSPATH];
|
|
|
|
|
|
|
|
// open the file on disk directly
|
2022-12-26 12:39:51 +01:00
|
|
|
if( !FS_FixFileCase( fs_writepath->dir, filepath, real_path, sizeof( real_path ), true ))
|
|
|
|
return NULL;
|
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
FS_CreatePath( real_path ); // Create directories up to the file
|
2022-12-26 12:39:51 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
return FS_SysOpen( real_path, mode );
|
|
|
|
}
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
// else, we look at the various search paths and open the file in read-only mode
|
|
|
|
return FS_OpenReadFile( filepath, mode, gamedironly );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_Close
|
|
|
|
|
|
|
|
Close a file
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
int FS_Close( file_t *file )
|
|
|
|
{
|
|
|
|
if( !file ) return 0;
|
|
|
|
|
2020-02-08 16:50:30 +01:00
|
|
|
FS_BackupFileName( file, NULL, 0 );
|
|
|
|
|
|
|
|
if( file->handle >= 0 )
|
|
|
|
if( close( file->handle ))
|
|
|
|
return EOF;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
Mem_Free( file );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_Flush
|
|
|
|
|
|
|
|
flushes written data to disk
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
int FS_Flush( file_t *file )
|
|
|
|
{
|
|
|
|
if( !file ) return 0;
|
|
|
|
|
|
|
|
// purge cached data
|
|
|
|
FS_Purge( file );
|
|
|
|
|
|
|
|
// sync
|
2022-08-01 02:12:35 +02:00
|
|
|
#if XASH_POSIX
|
2022-07-01 18:37:21 +02:00
|
|
|
if( fsync( file->handle ) < 0 )
|
|
|
|
return EOF;
|
2022-08-01 02:12:35 +02:00
|
|
|
#else
|
2022-08-06 19:19:42 +02:00
|
|
|
if( _commit( file->handle ) < 0 )
|
2022-08-01 02:12:35 +02:00
|
|
|
return EOF;
|
|
|
|
#endif
|
2022-07-01 18:37:21 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_Write
|
|
|
|
|
|
|
|
Write "datasize" bytes into a file
|
|
|
|
====================
|
|
|
|
*/
|
2018-12-05 17:57:05 +01:00
|
|
|
fs_offset_t FS_Write( file_t *file, const void *data, size_t datasize )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2018-12-05 17:57:05 +01:00
|
|
|
fs_offset_t result;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
if( !file ) return 0;
|
|
|
|
|
|
|
|
// if necessary, seek to the exact file position we're supposed to be
|
|
|
|
if( file->buff_ind != file->buff_len )
|
|
|
|
lseek( file->handle, file->buff_ind - file->buff_len, SEEK_CUR );
|
|
|
|
|
|
|
|
// purge cached data
|
|
|
|
FS_Purge( file );
|
|
|
|
|
|
|
|
// write the buffer and update the position
|
2018-12-05 17:57:05 +01:00
|
|
|
result = write( file->handle, data, (fs_offset_t)datasize );
|
2018-04-13 18:23:45 +02:00
|
|
|
file->position = lseek( file->handle, 0, SEEK_CUR );
|
|
|
|
|
|
|
|
if( file->real_length < file->position )
|
|
|
|
file->real_length = file->position;
|
|
|
|
|
|
|
|
if( result < 0 )
|
|
|
|
return 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_Read
|
|
|
|
|
|
|
|
Read up to "buffersize" bytes from a file
|
|
|
|
====================
|
|
|
|
*/
|
2018-12-05 17:57:05 +01:00
|
|
|
fs_offset_t FS_Read( file_t *file, void *buffer, size_t buffersize )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2018-12-05 17:57:05 +01:00
|
|
|
fs_offset_t count, done;
|
|
|
|
fs_offset_t nb;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
// nothing to copy
|
|
|
|
if( buffersize == 0 ) return 1;
|
|
|
|
|
|
|
|
// Get rid of the ungetc character
|
|
|
|
if( file->ungetc != EOF )
|
|
|
|
{
|
|
|
|
((char*)buffer)[0] = file->ungetc;
|
|
|
|
buffersize--;
|
|
|
|
file->ungetc = EOF;
|
|
|
|
done = 1;
|
|
|
|
}
|
|
|
|
else done = 0;
|
|
|
|
|
|
|
|
// first, we copy as many bytes as we can from "buff"
|
|
|
|
if( file->buff_ind < file->buff_len )
|
|
|
|
{
|
|
|
|
count = file->buff_len - file->buff_ind;
|
|
|
|
|
2018-12-05 17:57:05 +01:00
|
|
|
done += ((fs_offset_t)buffersize > count ) ? count : (fs_offset_t)buffersize;
|
2018-04-13 18:23:45 +02:00
|
|
|
memcpy( buffer, &file->buff[file->buff_ind], done );
|
|
|
|
file->buff_ind += done;
|
|
|
|
|
|
|
|
buffersize -= done;
|
|
|
|
if( buffersize == 0 )
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: at this point, the read buffer is always empty
|
|
|
|
|
2020-02-08 16:50:30 +01:00
|
|
|
FS_EnsureOpenFile( file );
|
2018-04-13 18:23:45 +02:00
|
|
|
// we must take care to not read after the end of the file
|
|
|
|
count = file->real_length - file->position;
|
|
|
|
|
|
|
|
// if we have a lot of data to get, put them directly into "buffer"
|
|
|
|
if( buffersize > sizeof( file->buff ) / 2 )
|
|
|
|
{
|
2018-12-05 17:57:05 +01:00
|
|
|
if( count > (fs_offset_t)buffersize )
|
|
|
|
count = (fs_offset_t)buffersize;
|
2018-04-13 18:23:45 +02:00
|
|
|
lseek( file->handle, file->offset + file->position, SEEK_SET );
|
|
|
|
nb = read (file->handle, &((byte *)buffer)[done], count );
|
|
|
|
|
|
|
|
if( nb > 0 )
|
|
|
|
{
|
|
|
|
done += nb;
|
|
|
|
file->position += nb;
|
|
|
|
// purge cached data
|
|
|
|
FS_Purge( file );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-05 17:57:05 +01:00
|
|
|
if( count > (fs_offset_t)sizeof( file->buff ))
|
|
|
|
count = (fs_offset_t)sizeof( file->buff );
|
2018-04-13 18:23:45 +02:00
|
|
|
lseek( file->handle, file->offset + file->position, SEEK_SET );
|
|
|
|
nb = read( file->handle, file->buff, count );
|
|
|
|
|
|
|
|
if( nb > 0 )
|
|
|
|
{
|
|
|
|
file->buff_len = nb;
|
|
|
|
file->position += nb;
|
|
|
|
|
|
|
|
// copy the requested data in "buffer" (as much as we can)
|
2018-12-05 17:57:05 +01:00
|
|
|
count = (fs_offset_t)buffersize > file->buff_len ? file->buff_len : (fs_offset_t)buffersize;
|
2018-04-13 18:23:45 +02:00
|
|
|
memcpy( &((byte *)buffer)[done], file->buff, count );
|
|
|
|
file->buff_ind = count;
|
|
|
|
done += count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_Print
|
|
|
|
|
|
|
|
Print a string into a file
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
int FS_Print( file_t *file, const char *msg )
|
|
|
|
{
|
|
|
|
return FS_Write( file, msg, Q_strlen( msg ));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_Printf
|
|
|
|
|
|
|
|
Print a string into a file
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
int FS_Printf( file_t *file, const char *format, ... )
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start( args, format );
|
|
|
|
result = FS_VPrintf( file, format, args );
|
|
|
|
va_end( args );
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_VPrintf
|
|
|
|
|
|
|
|
Print a string into a file
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
int FS_VPrintf( file_t *file, const char *format, va_list ap )
|
|
|
|
{
|
|
|
|
int len;
|
2018-12-05 17:57:05 +01:00
|
|
|
fs_offset_t buff_size = MAX_SYSPATH;
|
2018-04-13 18:23:45 +02:00
|
|
|
char *tempbuff;
|
|
|
|
|
|
|
|
if( !file ) return 0;
|
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
2018-06-09 00:28:35 +02:00
|
|
|
tempbuff = (char *)Mem_Malloc( fs_mempool, buff_size );
|
2018-04-13 18:23:45 +02:00
|
|
|
len = Q_vsprintf( tempbuff, format, ap );
|
|
|
|
|
|
|
|
if( len >= 0 && len < buff_size )
|
|
|
|
break;
|
|
|
|
|
|
|
|
Mem_Free( tempbuff );
|
|
|
|
buff_size *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = write( file->handle, tempbuff, len );
|
|
|
|
Mem_Free( tempbuff );
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_Getc
|
|
|
|
|
|
|
|
Get the next character of a file
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
int FS_Getc( file_t *file )
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
|
|
|
if( FS_Read( file, &c, 1 ) != 1 )
|
|
|
|
return EOF;
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_UnGetc
|
|
|
|
|
|
|
|
Put a character back into the read buffer (only supports one character!)
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
int FS_UnGetc( file_t *file, byte c )
|
|
|
|
{
|
|
|
|
// If there's already a character waiting to be read
|
|
|
|
if( file->ungetc != EOF )
|
|
|
|
return EOF;
|
|
|
|
|
|
|
|
file->ungetc = c;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_Gets
|
|
|
|
|
|
|
|
Same as fgets
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
int FS_Gets( file_t *file, byte *string, size_t bufsize )
|
|
|
|
{
|
|
|
|
int c, end = 0;
|
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
c = FS_Getc( file );
|
|
|
|
|
|
|
|
if( c == '\r' || c == '\n' || c < 0 )
|
|
|
|
break;
|
|
|
|
|
|
|
|
if( end < bufsize - 1 )
|
|
|
|
string[end++] = c;
|
|
|
|
}
|
|
|
|
string[end] = 0;
|
|
|
|
|
|
|
|
// remove \n following \r
|
|
|
|
if( c == '\r' )
|
|
|
|
{
|
|
|
|
c = FS_Getc( file );
|
|
|
|
|
|
|
|
if( c != '\n' )
|
|
|
|
FS_UnGetc( file, (byte)c );
|
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_Seek
|
|
|
|
|
|
|
|
Move the position index in a file
|
|
|
|
====================
|
|
|
|
*/
|
2018-12-05 17:57:05 +01:00
|
|
|
int FS_Seek( file_t *file, fs_offset_t offset, int whence )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
// compute the file offset
|
|
|
|
switch( whence )
|
|
|
|
{
|
|
|
|
case SEEK_CUR:
|
|
|
|
offset += file->position - file->buff_len + file->buff_ind;
|
|
|
|
break;
|
|
|
|
case SEEK_SET:
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
|
|
|
offset += file->real_length;
|
|
|
|
break;
|
2021-01-03 02:28:45 +01:00
|
|
|
default:
|
2018-04-13 18:23:45 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
if( offset < 0 || offset > file->real_length )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// if we have the data in our read buffer, we don't need to actually seek
|
|
|
|
if( file->position - file->buff_len <= offset && offset <= file->position )
|
|
|
|
{
|
|
|
|
file->buff_ind = offset + file->buff_len - file->position;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-08 16:50:30 +01:00
|
|
|
FS_EnsureOpenFile( file );
|
2018-04-13 18:23:45 +02:00
|
|
|
// Purge cached data
|
|
|
|
FS_Purge( file );
|
|
|
|
|
|
|
|
if( lseek( file->handle, file->offset + offset, SEEK_SET ) == -1 )
|
|
|
|
return -1;
|
|
|
|
file->position = offset;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_Tell
|
|
|
|
|
|
|
|
Give the current position in a file
|
|
|
|
====================
|
|
|
|
*/
|
2018-12-05 17:57:05 +01:00
|
|
|
fs_offset_t FS_Tell( file_t *file )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
if( !file ) return 0;
|
|
|
|
return file->position - file->buff_len + file->buff_ind;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_Eof
|
|
|
|
|
|
|
|
indicates at reached end of file
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
qboolean FS_Eof( file_t *file )
|
|
|
|
{
|
|
|
|
if( !file ) return true;
|
|
|
|
return (( file->position - file->buff_len + file->buff_ind ) == file->real_length ) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
FS_Purge
|
|
|
|
|
|
|
|
Erases any buffered input or output data
|
|
|
|
====================
|
|
|
|
*/
|
2022-07-01 18:37:21 +02:00
|
|
|
static void FS_Purge( file_t *file )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
file->buff_len = 0;
|
|
|
|
file->buff_ind = 0;
|
|
|
|
file->ungetc = EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
FS_LoadFile
|
|
|
|
|
|
|
|
Filename are relative to the xash directory.
|
|
|
|
Always appends a 0 byte.
|
|
|
|
============
|
|
|
|
*/
|
2018-12-05 17:57:05 +01:00
|
|
|
byte *FS_LoadFile( const char *path, fs_offset_t *filesizeptr, qboolean gamedironly )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
file_t *file;
|
|
|
|
byte *buf = NULL;
|
2018-12-05 17:57:05 +01:00
|
|
|
fs_offset_t filesize = 0;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
file = FS_Open( path, "rb", gamedironly );
|
|
|
|
|
|
|
|
if( file )
|
|
|
|
{
|
|
|
|
filesize = file->real_length;
|
2020-02-08 16:50:30 +01:00
|
|
|
|
2018-06-09 00:28:35 +02:00
|
|
|
buf = (byte *)Mem_Malloc( fs_mempool, filesize + 1 );
|
2018-04-13 18:23:45 +02:00
|
|
|
buf[filesize] = '\0';
|
|
|
|
FS_Read( file, buf, filesize );
|
|
|
|
FS_Close( file );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-01 18:37:21 +02:00
|
|
|
buf = FS_LoadWADFile( path, &filesize, gamedironly );
|
2019-05-24 18:13:03 +02:00
|
|
|
|
2019-05-25 21:38:21 +02:00
|
|
|
if( !buf )
|
2022-07-01 18:37:21 +02:00
|
|
|
buf = FS_LoadZIPFile( path, &filesize, gamedironly );
|
2019-05-28 15:51:44 +02:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( filesizeptr )
|
2019-05-27 20:02:38 +02:00
|
|
|
*filesizeptr = filesize;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2019-06-06 03:33:57 +02:00
|
|
|
qboolean CRC32_File( dword *crcvalue, const char *filename )
|
|
|
|
{
|
|
|
|
char buffer[1024];
|
|
|
|
int num_bytes;
|
|
|
|
file_t *f;
|
|
|
|
|
|
|
|
f = FS_Open( filename, "rb", false );
|
|
|
|
if( !f ) return false;
|
|
|
|
|
|
|
|
CRC32_Init( crcvalue );
|
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
num_bytes = FS_Read( f, buffer, sizeof( buffer ));
|
|
|
|
|
|
|
|
if( num_bytes > 0 )
|
|
|
|
CRC32_ProcessBuffer( crcvalue, buffer, num_bytes );
|
|
|
|
|
|
|
|
if( FS_Eof( f )) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
FS_Close( f );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
qboolean MD5_HashFile( byte digest[16], const char *pszFileName, uint seed[4] )
|
|
|
|
{
|
|
|
|
file_t *file;
|
2019-07-13 22:25:03 +02:00
|
|
|
byte buffer[1024];
|
2019-06-06 03:33:57 +02:00
|
|
|
MD5Context_t MD5_Hash;
|
|
|
|
int bytes;
|
|
|
|
|
|
|
|
if(( file = FS_Open( pszFileName, "rb", false )) == NULL )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
memset( &MD5_Hash, 0, sizeof( MD5Context_t ));
|
|
|
|
|
|
|
|
MD5Init( &MD5_Hash );
|
|
|
|
|
|
|
|
if( seed )
|
|
|
|
{
|
|
|
|
MD5Update( &MD5_Hash, (const byte *)seed, 16 );
|
|
|
|
}
|
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
bytes = FS_Read( file, buffer, sizeof( buffer ));
|
|
|
|
|
|
|
|
if( bytes > 0 )
|
|
|
|
MD5Update( &MD5_Hash, buffer, bytes );
|
|
|
|
|
|
|
|
if( FS_Eof( file ))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
FS_Close( file );
|
|
|
|
MD5Final( digest, &MD5_Hash );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-17 02:46:08 +02:00
|
|
|
/*
|
|
|
|
============
|
|
|
|
FS_LoadFile
|
|
|
|
|
|
|
|
Filename are relative to the xash directory.
|
|
|
|
Always appends a 0 byte.
|
|
|
|
============
|
|
|
|
*/
|
2018-12-05 17:57:05 +01:00
|
|
|
byte *FS_LoadDirectFile( const char *path, fs_offset_t *filesizeptr )
|
2018-04-17 02:46:08 +02:00
|
|
|
{
|
|
|
|
file_t *file;
|
|
|
|
byte *buf = NULL;
|
2018-12-05 17:57:05 +01:00
|
|
|
fs_offset_t filesize = 0;
|
2018-04-17 02:46:08 +02:00
|
|
|
|
|
|
|
file = FS_SysOpen( path, "rb" );
|
|
|
|
|
|
|
|
if( !file )
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to load
|
|
|
|
filesize = file->real_length;
|
2018-06-11 13:04:50 +02:00
|
|
|
buf = (byte *)Mem_Malloc( fs_mempool, filesize + 1 );
|
2018-04-17 02:46:08 +02:00
|
|
|
buf[filesize] = '\0';
|
|
|
|
FS_Read( file, buf, filesize );
|
|
|
|
FS_Close( file );
|
|
|
|
|
|
|
|
if( filesizeptr )
|
|
|
|
*filesizeptr = filesize;
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
============
|
|
|
|
FS_WriteFile
|
|
|
|
|
|
|
|
The filename will be prefixed by the current game directory
|
|
|
|
============
|
|
|
|
*/
|
2018-12-05 17:57:05 +01:00
|
|
|
qboolean FS_WriteFile( const char *filename, const void *data, fs_offset_t len )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
file_t *file;
|
|
|
|
|
|
|
|
file = FS_Open( filename, "wb", false );
|
|
|
|
|
|
|
|
if( !file )
|
|
|
|
{
|
2018-10-27 23:39:29 +02:00
|
|
|
Con_Reportf( S_ERROR "FS_WriteFile: failed on %s\n", filename);
|
2018-04-13 18:23:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FS_Write( file, data, len );
|
|
|
|
FS_Close( file );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=============================================================================
|
|
|
|
|
|
|
|
OTHERS PUBLIC FUNCTIONS
|
|
|
|
|
|
|
|
=============================================================================
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
FS_FileExists
|
|
|
|
|
|
|
|
Look for a file in the packages and in the filesystem
|
2022-07-01 18:37:21 +02:00
|
|
|
==================
|
|
|
|
*/
|
|
|
|
int GAME_EXPORT FS_FileExists( const char *filename, int gamedironly )
|
|
|
|
{
|
2022-12-19 15:22:30 +01:00
|
|
|
return FS_FindFile( filename, NULL, NULL, 0, gamedironly ) != NULL;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
2022-07-01 18:37:21 +02:00
|
|
|
FS_GetDiskPath
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
Build direct path for file in the filesystem
|
|
|
|
return NULL for file in pack
|
2018-04-13 18:23:45 +02:00
|
|
|
==================
|
|
|
|
*/
|
2022-07-01 18:37:21 +02:00
|
|
|
const char *FS_GetDiskPath( const char *name, qboolean gamedironly )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-12-19 15:22:30 +01:00
|
|
|
static char temp[MAX_SYSPATH];
|
2018-04-13 18:23:45 +02:00
|
|
|
searchpath_t *search;
|
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
search = FS_FindFile( name, NULL, temp, sizeof( temp ), gamedironly );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( search )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-12-12 04:54:42 +01:00
|
|
|
if( search->type != SEARCHPATH_PLAIN ) // file in pack or wad
|
2022-07-01 18:37:21 +02:00
|
|
|
return NULL;
|
2022-12-19 15:22:30 +01:00
|
|
|
return temp;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
return NULL;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
FS_FileSize
|
|
|
|
|
|
|
|
return size of file in bytes
|
|
|
|
==================
|
|
|
|
*/
|
2018-12-05 17:57:05 +01:00
|
|
|
fs_offset_t FS_FileSize( const char *filename, qboolean gamedironly )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
int length = -1; // in case file was missed
|
2021-01-03 02:28:45 +01:00
|
|
|
file_t *fp;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
fp = FS_Open( filename, "rb", gamedironly );
|
|
|
|
|
|
|
|
if( fp )
|
|
|
|
{
|
|
|
|
// it exists
|
|
|
|
FS_Seek( fp, 0, SEEK_END );
|
|
|
|
length = FS_Tell( fp );
|
|
|
|
FS_Close( fp );
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
FS_FileLength
|
|
|
|
|
|
|
|
return size of file in bytes
|
|
|
|
==================
|
|
|
|
*/
|
2018-12-05 17:57:05 +01:00
|
|
|
fs_offset_t FS_FileLength( file_t *f )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
if( !f ) return 0;
|
|
|
|
return f->real_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
FS_FileTime
|
|
|
|
|
|
|
|
return time of creation file in seconds
|
|
|
|
==================
|
|
|
|
*/
|
2018-12-05 17:57:05 +01:00
|
|
|
int FS_FileTime( const char *filename, qboolean gamedironly )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-12-19 15:22:30 +01:00
|
|
|
searchpath_t *search;
|
|
|
|
char netpath[MAX_SYSPATH];
|
|
|
|
int pack_ind;
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
search = FS_FindFile( filename, &pack_ind, netpath, sizeof( netpath ), gamedironly );
|
2018-04-13 18:23:45 +02:00
|
|
|
if( !search ) return -1; // doesn't exist
|
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
return search->pfnFileTime( search, netpath );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
FS_Rename
|
|
|
|
|
|
|
|
rename specified file from gamefolder
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
qboolean FS_Rename( const char *oldname, const char *newname )
|
|
|
|
{
|
2022-12-26 12:39:51 +01:00
|
|
|
char oldname2[MAX_SYSPATH], newname2[MAX_SYSPATH], oldpath[MAX_SYSPATH], newpath[MAX_SYSPATH];
|
|
|
|
int ret;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
if( !COM_CheckString( oldname ) || !COM_CheckString( newname ))
|
2018-04-13 18:23:45 +02:00
|
|
|
return false;
|
|
|
|
|
2022-12-19 15:22:30 +01:00
|
|
|
// no work done
|
|
|
|
if( !Q_stricmp( oldname, newname ))
|
|
|
|
return true;
|
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
// fix up slashes
|
|
|
|
Q_strncpy( oldname2, oldname, sizeof( oldname2 ));
|
|
|
|
Q_strncpy( newname2, newname, sizeof( newname2 ));
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
COM_FixSlashes( oldname2 );
|
|
|
|
COM_FixSlashes( newname2 );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
// file does not exist
|
|
|
|
if( !FS_FixFileCase( fs_writepath->dir, oldname2, oldpath, sizeof( oldpath ), false ))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// exit if overflowed
|
|
|
|
if( !FS_FixFileCase( fs_writepath->dir, newname2, newpath, sizeof( newpath ), true ))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ret = rename( oldpath, newpath );
|
|
|
|
if( ret < 0 )
|
|
|
|
{
|
|
|
|
Con_Printf( "%s: failed to rename file %s (%s) to %s (%s): %s\n",
|
|
|
|
__FUNCTION__, oldpath, oldname2, newpath, newname2, strerror( errno ));
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
return true;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
FS_Delete
|
|
|
|
|
|
|
|
delete specified file from gamefolder
|
|
|
|
==================
|
|
|
|
*/
|
2020-01-19 02:15:54 +01:00
|
|
|
qboolean GAME_EXPORT FS_Delete( const char *path )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-12-26 12:39:51 +01:00
|
|
|
char path2[MAX_SYSPATH], real_path[MAX_SYSPATH];
|
|
|
|
int ret;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
if( !COM_CheckString( path ))
|
2018-04-13 18:23:45 +02:00
|
|
|
return false;
|
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
Q_strncpy( path2, path, sizeof( path2 ));
|
|
|
|
COM_FixSlashes( path2 );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-12-26 12:39:51 +01:00
|
|
|
if( !FS_FixFileCase( fs_writepath->dir, path2, real_path, sizeof( real_path ), true ))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
ret = remove( real_path );
|
|
|
|
if( ret < 0 )
|
|
|
|
{
|
|
|
|
Con_Printf( "%s: failed to delete file %s (%s): %s\n", __FUNCTION__, real_path, path, strerror( errno ));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
FS_FileCopy
|
|
|
|
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
qboolean FS_FileCopy( file_t *pOutput, file_t *pInput, int fileSize )
|
|
|
|
{
|
2018-06-09 00:28:35 +02:00
|
|
|
char *buf = Mem_Malloc( fs_mempool, FILE_COPY_SIZE );
|
2018-04-13 18:23:45 +02:00
|
|
|
int size, readSize;
|
|
|
|
qboolean done = true;
|
|
|
|
|
|
|
|
while( fileSize > 0 )
|
|
|
|
{
|
|
|
|
if( fileSize > FILE_COPY_SIZE )
|
|
|
|
size = FILE_COPY_SIZE;
|
|
|
|
else size = fileSize;
|
|
|
|
|
|
|
|
if(( readSize = FS_Read( pInput, buf, size )) < size )
|
|
|
|
{
|
2018-10-27 23:39:29 +02:00
|
|
|
Con_Reportf( S_ERROR "FS_FileCopy: unexpected end of input file (%d < %d)\n", readSize, size );
|
2018-04-13 18:23:45 +02:00
|
|
|
fileSize = 0;
|
|
|
|
done = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
FS_Write( pOutput, buf, readSize );
|
|
|
|
fileSize -= size;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mem_Free( buf );
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
===========
|
|
|
|
FS_Search
|
|
|
|
|
|
|
|
Allocate and fill a search structure with information on matching filenames.
|
|
|
|
===========
|
|
|
|
*/
|
|
|
|
search_t *FS_Search( const char *pattern, int caseinsensitive, int gamedironly )
|
|
|
|
{
|
2022-11-18 14:35:21 +01:00
|
|
|
search_t *search = NULL;
|
|
|
|
searchpath_t *searchpath;
|
2022-11-21 13:11:55 +01:00
|
|
|
int i, numfiles, numchars;
|
2022-11-18 14:35:21 +01:00
|
|
|
stringlist_t resultlist;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
if( pattern[0] == '.' || pattern[0] == ':' || pattern[0] == '/' || pattern[0] == '\\' )
|
|
|
|
return NULL; // punctuation issues
|
|
|
|
|
|
|
|
stringlistinit( &resultlist );
|
|
|
|
|
|
|
|
// search through the path, one element at a time
|
|
|
|
for( searchpath = fs_searchpaths; searchpath; searchpath = searchpath->next )
|
|
|
|
{
|
2018-07-10 22:03:27 +02:00
|
|
|
if( gamedironly && !FBitSet( searchpath->flags, FS_GAMEDIRONLY_SEARCH_FLAGS ))
|
2018-04-13 18:23:45 +02:00
|
|
|
continue;
|
2022-12-26 12:39:51 +01:00
|
|
|
|
2022-12-14 23:06:20 +01:00
|
|
|
searchpath->pfnSearch( searchpath, &resultlist, pattern, caseinsensitive );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( resultlist.numstrings )
|
|
|
|
{
|
|
|
|
stringlistsort( &resultlist );
|
|
|
|
numfiles = resultlist.numstrings;
|
|
|
|
numchars = 0;
|
|
|
|
|
2022-11-18 14:35:21 +01:00
|
|
|
for( i = 0; i < resultlist.numstrings; i++ )
|
|
|
|
numchars += (int)Q_strlen( resultlist.strings[i]) + 1;
|
2018-06-09 00:28:35 +02:00
|
|
|
search = Mem_Calloc( fs_mempool, sizeof(search_t) + numchars + numfiles * sizeof( char* ));
|
2018-04-13 18:23:45 +02:00
|
|
|
search->filenames = (char **)((char *)search + sizeof( search_t ));
|
|
|
|
search->filenamesbuffer = (char *)((char *)search + sizeof( search_t ) + numfiles * sizeof( char* ));
|
|
|
|
search->numfilenames = (int)numfiles;
|
|
|
|
numfiles = numchars = 0;
|
|
|
|
|
2022-11-18 14:35:21 +01:00
|
|
|
for( i = 0; i < resultlist.numstrings; i++ )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
size_t textlen;
|
|
|
|
|
|
|
|
search->filenames[numfiles] = search->filenamesbuffer + numchars;
|
2022-11-18 14:35:21 +01:00
|
|
|
textlen = Q_strlen(resultlist.strings[i]) + 1;
|
|
|
|
memcpy( search->filenames[numfiles], resultlist.strings[i], textlen );
|
2018-04-13 18:23:45 +02:00
|
|
|
numfiles++;
|
|
|
|
numchars += (int)textlen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stringlistfreecontents( &resultlist );
|
|
|
|
|
|
|
|
return search;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FS_InitMemory( void )
|
|
|
|
{
|
2021-01-03 02:28:45 +01:00
|
|
|
fs_mempool = Mem_AllocPool( "FileSystem Pool" );
|
2018-04-13 18:23:45 +02:00
|
|
|
fs_searchpaths = NULL;
|
|
|
|
}
|
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
fs_interface_t g_engfuncs =
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-07-01 18:37:21 +02:00
|
|
|
_Con_Printf,
|
|
|
|
_Con_Printf,
|
|
|
|
_Con_Printf,
|
|
|
|
_Sys_Error,
|
|
|
|
_Mem_AllocPool,
|
|
|
|
_Mem_FreePool,
|
|
|
|
_Mem_Alloc,
|
|
|
|
_Mem_Realloc,
|
|
|
|
_Mem_Free
|
2018-04-13 18:23:45 +02:00
|
|
|
};
|
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
static qboolean FS_InitInterface( int version, fs_interface_t *engfuncs )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-07-01 18:37:21 +02:00
|
|
|
// to be extended in future interface revisions
|
|
|
|
if( version != FS_API_VERSION )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-07-01 18:37:21 +02:00
|
|
|
Con_Printf( S_ERROR "filesystem optional interface version mismatch: expected %d, got %d\n",
|
|
|
|
FS_API_VERSION, version );
|
|
|
|
return false;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( engfuncs->_Con_Printf )
|
|
|
|
g_engfuncs._Con_Printf = engfuncs->_Con_Printf;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( engfuncs->_Con_DPrintf )
|
|
|
|
g_engfuncs._Con_DPrintf = engfuncs->_Con_DPrintf;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( engfuncs->_Con_Reportf )
|
|
|
|
g_engfuncs._Con_Reportf = engfuncs->_Con_Reportf;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( engfuncs->_Sys_Error )
|
|
|
|
g_engfuncs._Sys_Error = engfuncs->_Sys_Error;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( engfuncs->_Mem_AllocPool && engfuncs->_Mem_FreePool )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-07-01 18:37:21 +02:00
|
|
|
g_engfuncs._Mem_AllocPool = engfuncs->_Mem_AllocPool;
|
|
|
|
g_engfuncs._Mem_FreePool = engfuncs->_Mem_FreePool;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
Con_Reportf( "filesystem_stdio: custom pool allocation functions found\n" );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
if( engfuncs->_Mem_Alloc && engfuncs->_Mem_Realloc && engfuncs->_Mem_Free )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-07-01 18:37:21 +02:00
|
|
|
g_engfuncs._Mem_Alloc = engfuncs->_Mem_Alloc;
|
|
|
|
g_engfuncs._Mem_Realloc = engfuncs->_Mem_Realloc;
|
|
|
|
g_engfuncs._Mem_Free = engfuncs->_Mem_Free;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
Con_Reportf( "filesystem_stdio: custom memory allocation functions found\n" );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
return true;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2022-08-25 19:33:16 +02:00
|
|
|
fs_api_t g_api =
|
2022-07-01 18:37:21 +02:00
|
|
|
{
|
|
|
|
FS_InitStdio,
|
|
|
|
FS_ShutdownStdio,
|
|
|
|
|
|
|
|
// search path utils
|
|
|
|
FS_Rescan,
|
|
|
|
FS_ClearSearchPath,
|
|
|
|
FS_AllowDirectPaths,
|
|
|
|
FS_AddGameDirectory,
|
|
|
|
FS_AddGameHierarchy,
|
|
|
|
FS_Search,
|
|
|
|
FS_SetCurrentDirectory,
|
|
|
|
FS_FindLibrary,
|
|
|
|
FS_Path_f,
|
|
|
|
|
|
|
|
// gameinfo utils
|
|
|
|
FS_LoadGameInfo,
|
|
|
|
|
|
|
|
// file ops
|
|
|
|
FS_Open,
|
|
|
|
FS_Write,
|
|
|
|
FS_Read,
|
|
|
|
FS_Seek,
|
|
|
|
FS_Tell,
|
|
|
|
FS_Eof,
|
|
|
|
FS_Flush,
|
|
|
|
FS_Close,
|
|
|
|
FS_Gets,
|
|
|
|
FS_UnGetc,
|
|
|
|
FS_Getc,
|
|
|
|
FS_VPrintf,
|
|
|
|
FS_Printf,
|
|
|
|
FS_Print,
|
|
|
|
FS_FileLength,
|
|
|
|
FS_FileCopy,
|
|
|
|
|
|
|
|
// file buffer ops
|
|
|
|
FS_LoadFile,
|
|
|
|
FS_LoadDirectFile,
|
|
|
|
FS_WriteFile,
|
|
|
|
|
|
|
|
// file hashing
|
|
|
|
CRC32_File,
|
|
|
|
MD5_HashFile,
|
|
|
|
|
|
|
|
// filesystem ops
|
|
|
|
FS_FileExists,
|
|
|
|
FS_FileTime,
|
|
|
|
FS_FileSize,
|
|
|
|
FS_Rename,
|
|
|
|
FS_Delete,
|
|
|
|
FS_SysFileExists,
|
|
|
|
FS_GetDiskPath,
|
|
|
|
};
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
int EXPORT GetFSAPI( int version, fs_api_t *api, fs_globals_t **globals, fs_interface_t *engfuncs )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-12-26 16:58:03 +01:00
|
|
|
if( engfuncs && !FS_InitInterface( version, engfuncs ))
|
2022-07-01 18:37:21 +02:00
|
|
|
return 0;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
memcpy( api, &g_api, sizeof( *api ));
|
|
|
|
*globals = &FI;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
return FS_API_VERSION;
|
2018-04-13 19:44:39 +02:00
|
|
|
}
|