]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - fs.c
added LINK_TO_ZLIB define checks, to allow direct linking
[xonotic/darkplaces.git] / fs.c
diff --git a/fs.c b/fs.c
index c7721ef2efe75b8c1278ec335a5b77d0a3ece7a5..fcc4470a30fde77d1d29f849dcb19e7802f89f21 100644 (file)
--- a/fs.c
+++ b/fs.c
@@ -106,6 +106,19 @@ CONSTANTS
 #define ZIP_CDIR_CHUNK_BASE_SIZE       46
 #define ZIP_LOCAL_CHUNK_BASE_SIZE      30
 
+#ifdef LINK_TO_ZLIB
+#include <zlib.h>
+
+#define qz_inflate inflate
+#define qz_inflateEnd inflateEnd
+#define qz_inflateInit2_ inflateInit2_
+#define qz_inflateReset inflateReset
+#define qz_deflateInit2_ deflateInit2_
+#define qz_deflateEnd deflateEnd
+#define qz_deflate deflate
+#define Z_MEMLEVEL_DEFAULT 8
+#else
+
 // Zlib constants (from zlib.h)
 #define Z_SYNC_FLUSH   2
 #define MAX_WBITS              15
@@ -166,6 +179,7 @@ typedef struct
        unsigned long   adler;          ///< adler32 value of the uncompressed data
        unsigned long   reserved;       ///< reserved for future use
 } z_stream;
+#endif
 
 
 /// inside a package (PAK or PK3)
@@ -321,8 +335,9 @@ char fs_gamedirs[MAX_GAMEDIRS][MAX_QPATH];
 gamedir_t *fs_all_gamedirs = NULL;
 int fs_all_gamedirs_count = 0;
 
-cvar_t scr_screenshot_name = {0, "scr_screenshot_name","dp", "prefix name for saved screenshots (changes based on -game commandline, as well as which game mode is running; the date is encoded using strftime escapes)"};
+cvar_t scr_screenshot_name = {CVAR_NORESETTODEFAULTS, "scr_screenshot_name","dp", "prefix name for saved screenshots (changes based on -game commandline, as well as which game mode is running; the date is encoded using strftime escapes)"};
 cvar_t fs_empty_files_in_pack_mark_deletions = {0, "fs_empty_files_in_pack_mark_deletions", "0", "if enabled, empty files in a pak/pk3 count as not existing but cancel the search in further packs, effectively allowing patch pak/pk3 files to 'delete' files"};
+cvar_t cvar_fs_gamedir = {CVAR_READONLY | CVAR_NORESETTODEFAULTS, "fs_gamedir", "", "the list of currently selected gamedirs (use the 'gamedir' command to change this)"};
 
 
 /*
@@ -333,6 +348,7 @@ PRIVATE FUNCTIONS - PK3 HANDLING
 =============================================================================
 */
 
+#ifndef LINK_TO_ZLIB
 // Functions exported from zlib
 #if defined(WIN32) && defined(ZLIB_USES_WINAPI)
 # define ZEXPORT WINAPI
@@ -347,12 +363,14 @@ static int (ZEXPORT *qz_inflateReset) (z_stream* strm);
 static int (ZEXPORT *qz_deflateInit2_) (z_stream* strm, int level, int method, int windowBits, int memLevel, int strategy, const char *version, int stream_size);
 static int (ZEXPORT *qz_deflateEnd) (z_stream* strm);
 static int (ZEXPORT *qz_deflate) (z_stream* strm, int flush);
+#endif
 
 #define qz_inflateInit2(strm, windowBits) \
         qz_inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
 #define qz_deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
         qz_deflateInit2_((strm), (level), (method), (windowBits), (memLevel), (strategy), ZLIB_VERSION, sizeof(z_stream))
 
+#ifndef LINK_TO_ZLIB
 //        qz_deflateInit_((strm), (level), ZLIB_VERSION, sizeof(z_stream))
 
 static dllfunction_t zlibfuncs[] =
@@ -369,6 +387,7 @@ static dllfunction_t zlibfuncs[] =
 
 /// Handle for Zlib DLL
 static dllhandle_t zlib_dll = NULL;
+#endif
 
 #ifdef WIN32
 static HRESULT (WINAPI *qSHGetFolderPath) (HWND hwndOwner, int nFolder, HANDLE hToken, DWORD dwFlags, LPTSTR pszPath);
@@ -389,7 +408,9 @@ Unload the Zlib DLL
 */
 void PK3_CloseLibrary (void)
 {
+#ifndef LINK_TO_ZLIB
        Sys_UnloadLibrary (&zlib_dll);
+#endif
 }
 
 
@@ -402,6 +423,9 @@ Try to load the Zlib DLL
 */
 qboolean PK3_OpenLibrary (void)
 {
+#ifdef LINK_TO_ZLIB
+       return true;
+#else
        const char* dllnames [] =
        {
 #if defined(WIN64)
@@ -428,6 +452,7 @@ qboolean PK3_OpenLibrary (void)
 
        // Load the DLL
        return Sys_LoadLibrary (dllnames, &zlib_dll, zlibfuncs);
+#endif
 }
 
 /*
@@ -439,8 +464,12 @@ See if zlib is available
 */
 qboolean FS_HasZlib(void)
 {
+#ifdef LINK_TO_ZLIB
+       return true;
+#else
        PK3_OpenLibrary(); // to be safe
        return (zlib_dll != 0);
+#endif
 }
 
 /*
@@ -1037,7 +1066,7 @@ FS_AddPack
  */
 qboolean FS_AddPack(const char *pakfile, qboolean *already_loaded, qboolean keep_plain_dirs)
 {
-       char fullpath[MAX_QPATH];
+       char fullpath[MAX_OSPATH];
        int index;
        searchpath_t *search;
 
@@ -1204,6 +1233,7 @@ void FS_Rescan (void)
 {
        int i;
        qboolean fs_modified = false;
+       char gamedirbuf[MAX_INPUTLINE];
 
        FS_ClearSearchPath();
 
@@ -1225,13 +1255,19 @@ void FS_Rescan (void)
        // Adds basedir/gamedir as an override game
        // LordHavoc: now supports multiple -game directories
        // set the com_modname (reported in server info)
+       *gamedirbuf = 0;
        for (i = 0;i < fs_numgamedirs;i++)
        {
                fs_modified = true;
                FS_AddGameHierarchy (fs_gamedirs[i]);
                // update the com_modname (used server info)
                strlcpy (com_modname, fs_gamedirs[i], sizeof (com_modname));
+               if(i)
+                       strlcat(gamedirbuf, va(" %s", fs_gamedirs[i]), sizeof(gamedirbuf));
+               else
+                       strlcpy(gamedirbuf, fs_gamedirs[i], sizeof(gamedirbuf));
        }
+       Cvar_SetQuick(&cvar_fs_gamedir, gamedirbuf); // so QC or console code can query it
 
        // set the default screenshot name to either the mod name or the
        // gamemode screenshot name
@@ -1239,6 +1275,9 @@ void FS_Rescan (void)
                Cvar_SetQuick (&scr_screenshot_name, com_modname);
        else
                Cvar_SetQuick (&scr_screenshot_name, gamescreenshotname);
+       
+       if((i = COM_CheckParm("-modname")) && i < com_argc - 1)
+               strlcpy(com_modname, com_argv[i+1], sizeof(com_modname));
 
        // If "-condebug" is in the command line, remove the previous log file
        if (COM_CheckParm ("-condebug") != 0)
@@ -1445,7 +1484,7 @@ const char *FS_CheckGameDir(const char *gamedir)
        return fs_checkgamedir_missing;
 }
 
-static void FS_ListGameDirs()
+static void FS_ListGameDirs(void)
 {
        stringlist_t list, list2;
        int i, j;
@@ -1477,7 +1516,7 @@ static void FS_ListGameDirs()
        }
        stringlistfreecontents(&list);
 
-       fs_all_gamedirs = Mem_Alloc(fs_mempool, list2.numstrings * sizeof(*fs_all_gamedirs));
+       fs_all_gamedirs = (gamedir_t *)Mem_Alloc(fs_mempool, list2.numstrings * sizeof(*fs_all_gamedirs));
        for(i = 0; i < list2.numstrings; ++i)
        {
                info = FS_CheckGameDir(list2.strings[i]);
@@ -1671,6 +1710,7 @@ void FS_Init_Commands(void)
 {
        Cvar_RegisterVariable (&scr_screenshot_name);
        Cvar_RegisterVariable (&fs_empty_files_in_pack_mark_deletions);
+       Cvar_RegisterVariable (&cvar_fs_gamedir);
 
        Cmd_AddCommand ("gamedir", FS_GameDir_f, "changes active gamedir list (can take multiple arguments), not including base directory (example usage: gamedir ctf)");
        Cmd_AddCommand ("fs_rescan", FS_Rescan_f, "rescans filesystem for new pack archives and any other changes");
@@ -1796,6 +1836,7 @@ qfile_t *FS_OpenPackedFile (pack_t* pack, int pack_ind)
                if (!PK3_GetTrueFileOffset (pfile, pack))
                        return NULL;
 
+#ifndef LINK_TO_ZLIB
        // No Zlib DLL = no compressed files
        if (!zlib_dll && (pfile->flags & PACKFILE_FLAG_DEFLATED))
        {
@@ -1804,6 +1845,7 @@ qfile_t *FS_OpenPackedFile (pack_t* pack, int pack_ind)
                                        pfile->name);
                return NULL;
        }
+#endif
 
        // LordHavoc: lseek affects all duplicates of a handle so we do it before
        // the dup() call to avoid having to close the dup_handle on error here
@@ -2829,7 +2871,7 @@ Look for a file in the packages and in the filesystem
 int FS_FileType (const char *filename)
 {
        searchpath_t *search;
-       char fullpath[MAX_QPATH];
+       char fullpath[MAX_OSPATH];
 
        search = FS_FindFile (filename, NULL, true);
        if(!search)
@@ -2886,6 +2928,9 @@ int FS_SysFileType (const char *path)
        if (stat (path,&buf) == -1)
                return FS_FILETYPE_NONE;
 
+#ifndef S_ISDIR
+#define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
+#endif
        if(S_ISDIR(buf.st_mode))
                return FS_FILETYPE_DIRECTORY;
 
@@ -3325,6 +3370,12 @@ unsigned char *FS_Deflate(const unsigned char *data, size_t size, size_t *deflat
        unsigned char *out = NULL;
        unsigned char *tmp;
 
+       *deflated_size = 0;
+#ifndef LINK_TO_ZLIB
+       if(!zlib_dll)
+               return NULL;
+#endif
+
        memset(&strm, 0, sizeof(strm));
        strm.zalloc = Z_NULL;
        strm.zfree = Z_NULL;
@@ -3418,6 +3469,12 @@ unsigned char *FS_Inflate(const unsigned char *data, size_t size, size_t *inflat
        unsigned int have;
        sizebuf_t outbuf;
 
+       *inflated_size = 0;
+#ifndef LINK_TO_ZLIB
+       if(!zlib_dll)
+               return NULL;
+#endif
+
        memset(&outbuf, 0, sizeof(outbuf));
        outbuf.data = (unsigned char *) Mem_Alloc(tempmempool, sizeof(tmp));
        outbuf.maxsize = sizeof(tmp);