]> 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 d4b15178b8433a1c3c26af119cae43c1a1bbfd2e..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)
@@ -317,8 +331,13 @@ char fs_basedir[MAX_OSPATH];
 int fs_numgamedirs = 0;
 char fs_gamedirs[MAX_GAMEDIRS][MAX_QPATH];
 
-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)"};
+// list of all gamedirs with modinfo.txt
+gamedir_t *fs_all_gamedirs = NULL;
+int fs_all_gamedirs_count = 0;
+
+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)"};
 
 
 /*
@@ -329,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
@@ -343,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[] =
@@ -365,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);
@@ -385,7 +408,9 @@ Unload the Zlib DLL
 */
 void PK3_CloseLibrary (void)
 {
+#ifndef LINK_TO_ZLIB
        Sys_UnloadLibrary (&zlib_dll);
+#endif
 }
 
 
@@ -398,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)
@@ -424,6 +452,7 @@ qboolean PK3_OpenLibrary (void)
 
        // Load the DLL
        return Sys_LoadLibrary (dllnames, &zlib_dll, zlibfuncs);
+#endif
 }
 
 /*
@@ -435,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
 }
 
 /*
@@ -1033,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;
 
@@ -1200,6 +1233,7 @@ void FS_Rescan (void)
 {
        int i;
        qboolean fs_modified = false;
+       char gamedirbuf[MAX_INPUTLINE];
 
        FS_ClearSearchPath();
 
@@ -1221,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
@@ -1235,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)
@@ -1422,7 +1465,17 @@ const char *FS_CheckGameDir(const char *gamedir)
 
        ret = FS_SysCheckGameDir(va("%s%s/", fs_userdir, gamedir));
        if(ret)
+       {
+               if(!*ret)
+               {
+                       // get description from basedir
+                       ret = FS_SysCheckGameDir(va("%s%s/", fs_basedir, gamedir));
+                       if(ret)
+                               return ret;
+                       return "";
+               }
                return ret;
+       }
 
        ret = FS_SysCheckGameDir(va("%s%s/", fs_basedir, gamedir));
        if(ret)
@@ -1431,6 +1484,54 @@ const char *FS_CheckGameDir(const char *gamedir)
        return fs_checkgamedir_missing;
 }
 
+static void FS_ListGameDirs(void)
+{
+       stringlist_t list, list2;
+       int i, j;
+       const char *info;
+
+       fs_all_gamedirs_count = 0;
+       if(fs_all_gamedirs)
+               Mem_Free(fs_all_gamedirs);
+
+       stringlistinit(&list);
+       listdirectory(&list, va("%s/", fs_basedir), "");
+       listdirectory(&list, va("%s/", fs_userdir), "");
+       stringlistsort(&list);
+
+       stringlistinit(&list2);
+       for(i = 0; i < list.numstrings; ++i)
+       {
+               if(i)
+                       if(!strcmp(list.strings[i-1], list.strings[i]))
+                               continue;
+               info = FS_CheckGameDir(list.strings[i]);
+               if(!info)
+                       continue;
+               if(info == fs_checkgamedir_missing)
+                       continue;
+               if(!*info)
+                       continue;
+               stringlistappend(&list2, list.strings[i]); 
+       }
+       stringlistfreecontents(&list);
+
+       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]);
+               // all this cannot happen any more, but better be safe than sorry
+               if(!info)
+                       continue;
+               if(info == fs_checkgamedir_missing)
+                       continue;
+               if(!*info)
+                       continue;
+               strlcpy(fs_all_gamedirs[fs_all_gamedirs_count].name, list2.strings[i], sizeof(fs_all_gamedirs[j].name));
+               strlcpy(fs_all_gamedirs[fs_all_gamedirs_count].description, info, sizeof(fs_all_gamedirs[j].description));
+               ++fs_all_gamedirs_count;
+       }
+}
 
 /*
 ================
@@ -1513,9 +1614,9 @@ void FS_Init (void)
                {
 #if _MSC_VER >= 1400
                        int fd;
-                       _sopen_s(&fd, va("%s%s/config.cfg", fs_basedir, dir), O_WRONLY | O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE); // note: no O_TRUNC here!
+                       _sopen_s(&fd, va("%s%s/config.cfg", fs_basedir, gamedirname1), O_WRONLY | O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE); // note: no O_TRUNC here!
 #else
-                       int fd = open (va("%s%s/config.cfg", fs_basedir, dir), O_WRONLY | O_CREAT, 0666); // note: no O_TRUNC here!
+                       int fd = open (va("%s%s/config.cfg", fs_basedir, gamedirname1), O_WRONLY | O_CREAT, 0666); // note: no O_TRUNC here!
 #endif
                        if(fd >= 0)
                        {
@@ -1567,6 +1668,8 @@ void FS_Init (void)
        if (fs_basedir[0] && fs_basedir[strlen(fs_basedir) - 1] != '/' && fs_basedir[strlen(fs_basedir) - 1] != '\\')
                strlcat(fs_basedir, "/", sizeof(fs_basedir));
 
+       FS_ListGameDirs();
+
        p = FS_CheckGameDir(gamedirname1);
        if(!p || p == fs_checkgamedir_missing)
                Con_Printf("WARNING: base gamedir %s%s/ not found!\n", fs_basedir, gamedirname1);
@@ -1607,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");
@@ -1732,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))
        {
@@ -1740,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
@@ -2765,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)
@@ -2822,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;
 
@@ -3261,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;
@@ -3354,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);