]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - fs.c
added COMMANDLINEOPTION comments for every commandline option, these will be listed...
[xonotic/darkplaces.git] / fs.c
diff --git a/fs.c b/fs.c
index 4c8befc5380e682d493707037d0a15cf287b21f6..089fb3dc2679ae3ac385ff366ea195b9906ea34c 100644 (file)
--- a/fs.c
+++ b/fs.c
@@ -25,8 +25,6 @@
 
 #include "quakedef.h"
 
-#include <stdlib.h>
-#include <string.h>
 #include <limits.h>
 #include <fcntl.h>
 
@@ -262,7 +260,7 @@ int fs_filesize;
 
 pack_t *packlist = NULL;
 
-searchpath_t *fs_searchpaths;
+searchpath_t *fs_searchpaths = NULL;
 
 #define MAX_FILES_IN_PACK      65536
 
@@ -317,11 +315,7 @@ Unload the Zlib DLL
 */
 void PK3_CloseLibrary (void)
 {
-       if (!zlib_dll)
-               return;
-
-       Sys_UnloadLibrary (zlib_dll);
-       zlib_dll = NULL;
+       Sys_UnloadLibrary (&zlib_dll);
 }
 
 
@@ -335,7 +329,6 @@ Try to load the Zlib DLL
 qboolean PK3_OpenLibrary (void)
 {
        const char* dllname;
-       const dllfunction_t *func;
 
        // Already loaded?
        if (zlib_dll)
@@ -347,27 +340,14 @@ qboolean PK3_OpenLibrary (void)
        dllname = "libz.so";
 #endif
 
-       // Initializations
-       for (func = zlibfuncs; func && func->name != NULL; func++)
-               *func->funcvariable = NULL;
-
        // Load the DLL
-       if (! (zlib_dll = Sys_LoadLibrary (dllname)))
+       if (! Sys_LoadLibrary (dllname, &zlib_dll, zlibfuncs))
        {
-               Con_Printf("Can't find %s. Compressed files support disabled\n", dllname);
+               Con_Printf ("Compressed files support disabled\n");
                return false;
        }
 
-       // Get the function adresses
-       for (func = zlibfuncs; func && func->name != NULL; func++)
-               if (!(*func->funcvariable = (void *) Sys_GetProcAddress (zlib_dll, func->name)))
-               {
-                       Con_Printf("missing function \"%s\" - broken Zlib library!\n", func->name);
-                       PK3_CloseLibrary ();
-                       return false;
-               }
-
-       Con_Printf("%s loaded. Compressed files support enabled\n", dllname);
+       Con_Printf ("Compressed files support enabled\n");
        return true;
 }
 
@@ -570,7 +550,7 @@ pack_t *FS_LoadPackPK3 (const char *packfile)
        strlcpy (pack->filename, packfile, sizeof (pack->filename));
        pack->handle = packhandle;
        pack->numfiles = eocd.nbentries;
-       pack->mempool = Mem_AllocPool (packfile);
+       pack->mempool = Mem_AllocPool (packfile, 0, NULL);
        pack->files = Mem_Alloc (pack->mempool, eocd.nbentries * sizeof(packfile_t));
        pack->next = packlist;
        packlist = pack;
@@ -579,7 +559,7 @@ pack_t *FS_LoadPackPK3 (const char *packfile)
        if (real_nb_files <= 0)
                Sys_Error ("%s is not a valid PK3 file", packfile);
 
-       Con_Printf ("Added packfile %s (%i files)\n", packfile, real_nb_files);
+       Con_Printf("Added packfile %s (%i files)\n", packfile, real_nb_files);
        return pack;
 }
 
@@ -710,15 +690,15 @@ void FS_Path_f (void)
 {
        searchpath_t *s;
 
-       Con_Print("Current search path:\n");
+       Con_Print("Current search path:\n");
        for (s=fs_searchpaths ; s ; s=s->next)
        {
                if (s->pack)
                {
-                       Con_Printf ("%s (%i files)\n", s->pack->filename, s->pack->numfiles);
+                       Con_Printf("%s (%i files)\n", s->pack->filename, s->pack->numfiles);
                }
                else
-                       Con_Printf ("%s\n", s->filename);
+                       Con_Printf("%s\n", s->filename);
        }
 }
 
@@ -764,7 +744,7 @@ pack_t *FS_LoadPackPAK (const char *packfile)
        strlcpy (pack->filename, packfile, sizeof (pack->filename));
        pack->handle = packhandle;
        pack->numfiles = 0;
-       pack->mempool = Mem_AllocPool(packfile);
+       pack->mempool = Mem_AllocPool(packfile, 0, NULL);
        pack->files = Mem_Alloc(pack->mempool, numpackfiles * sizeof(packfile_t));
        pack->next = packlist;
        packlist = pack;
@@ -784,7 +764,7 @@ pack_t *FS_LoadPackPAK (const char *packfile)
 
        Mem_Free(info);
 
-       Con_Printf ("Added packfile %s (%i files)\n", packfile, numpackfiles);
+       Con_Printf("Added packfile %s (%i files)\n", packfile, numpackfiles);
        return pack;
 }
 
@@ -886,7 +866,7 @@ char *FS_FileExtension (const char *in)
                separator = backslash;
        if (separator < colon)
                separator = colon;
-       if (dot < separator)
+       if (dot == NULL || dot < separator)
                return "";
        dot++;
        for (i = 0;i < 7 && dot[i];i++)
@@ -906,54 +886,39 @@ void FS_Init (void)
        int i;
        searchpath_t *search;
 
-       fs_mempool = Mem_AllocPool("file management");
-       pak_mempool = Mem_AllocPool("paks");
+       fs_mempool = Mem_AllocPool("file management", 0, NULL);
+       pak_mempool = Mem_AllocPool("paks", 0, NULL);
+
+       Cvar_RegisterVariable (&scr_screenshot_name);
 
        Cmd_AddCommand ("path", FS_Path_f);
        Cmd_AddCommand ("dir", FS_Dir_f);
        Cmd_AddCommand ("ls", FS_Ls_f);
 
        strcpy(fs_basedir, ".");
+       strcpy(fs_gamedir, ".");
 
        PK3_OpenLibrary ();
 
        // -basedir <path>
        // Overrides the system supplied base directory (under GAMENAME)
+// COMMANDLINEOPTION: -basedir <path> chooses what base directory the game data is in, inside this there should be a data directory for the game (for example id1)
        i = COM_CheckParm ("-basedir");
-       if (i && i < com_argc-1)
-               strlcpy (fs_basedir, com_argv[i+1], sizeof (fs_basedir));
-
-       i = strlen (fs_basedir);
-       if (i > 0 && (fs_basedir[i-1] == '\\' || fs_basedir[i-1] == '/'))
-               fs_basedir[i-1] = 0;
-
-       // start up with GAMENAME by default (id1)
-       strlcpy (com_modname, GAMENAME, sizeof (com_modname));
-       FS_AddGameDirectory (va("%s/"GAMENAME, fs_basedir));
-       if (gamedirname[0])
-       {
-               fs_modified = true;
-               strlcpy (com_modname, gamedirname, sizeof (com_modname));
-               FS_AddGameDirectory (va("%s/%s", fs_basedir, gamedirname));
-       }
-
-       // -game <gamedir>
-       // Adds basedir/gamedir as an override game
-       i = COM_CheckParm ("-game");
        if (i && i < com_argc-1)
        {
-               fs_modified = true;
-               strlcpy (com_modname, com_argv[i+1], sizeof (com_modname));
-               FS_AddGameDirectory (va("%s/%s", fs_basedir, com_argv[i+1]));
+               strlcpy (fs_basedir, com_argv[i+1], sizeof (fs_basedir));
+               i = strlen (fs_basedir);
+               if (i > 0 && (fs_basedir[i-1] == '\\' || fs_basedir[i-1] == '/'))
+                       fs_basedir[i-1] = 0;
        }
 
        // -path <dir or packfile> [<dir or packfile>] ...
        // Fully specifies the exact search path, overriding the generated one
+// COMMANDLINEOPTION: -path <path ..> specifies the full search path manually, overriding the generated one, example: -path c:\quake\id1 c:\quake\pak0.pak c:\quake\pak1.pak (not recommended)
        i = COM_CheckParm ("-path");
        if (i)
        {
                fs_modified = true;
-               fs_searchpaths = NULL;
                while (++i < com_argc)
                {
                        if (!com_argv[i] || com_argv[i][0] == '+' || com_argv[i][0] == '-')
@@ -977,6 +942,37 @@ void FS_Init (void)
                        search->next = fs_searchpaths;
                        fs_searchpaths = search;
                }
+               return;
+       }
+
+       // start up with GAMENAME by default (id1)
+       strlcpy (com_modname, GAMENAME, sizeof (com_modname));
+       FS_AddGameDirectory (va("%s/"GAMENAME, fs_basedir));
+       Cvar_SetQuick (&scr_screenshot_name, gamescreenshotname);
+
+       // add the game-specific path, if any
+       if (gamedirname[0])
+       {
+               fs_modified = true;
+               strlcpy (com_modname, gamedirname, sizeof (com_modname));
+               FS_AddGameDirectory (va("%s/%s", fs_basedir, gamedirname));
+       }
+
+       // -game <gamedir>
+       // Adds basedir/gamedir as an override game
+       // LordHavoc: now supports multiple -game directories
+       for (i = 1;i < com_argc;i++)
+       {
+               if (!com_argv[i])
+                       continue;
+               if (!strcmp (com_argv[i], "-game") && i < com_argc-1)
+               {
+                       i++;
+                       fs_modified = true;
+                       strlcpy (com_modname, com_argv[i], sizeof (com_modname));
+                       FS_AddGameDirectory (va("%s/%s", fs_basedir, com_argv[i]));
+                       Cvar_SetQuick (&scr_screenshot_name, com_modname);
+               }
        }
 }
 
@@ -1086,7 +1082,7 @@ static searchpath_t *FS_FindFile (const char *name, int* index, qboolean quiet)
                                if (!diff)
                                {
                                        if (!quiet)
-                                               Sys_Printf ("FS_FindFile: %s in %s\n",
+                                               Sys_Printf("FS_FindFile: %s in %s\n",
                                                                        pak->files[middle].name, pak->filename);
 
                                        if (index != NULL)
@@ -1108,7 +1104,7 @@ static searchpath_t *FS_FindFile (const char *name, int* index, qboolean quiet)
                        if (FS_SysFileExists (netpath))
                        {
                                if (!quiet)
-                                       Sys_Printf ("FS_FindFile: %s\n", netpath);
+                                       Sys_Printf("FS_FindFile: %s\n", netpath);
 
                                if (index != NULL)
                                        *index = -1;
@@ -1118,7 +1114,7 @@ static searchpath_t *FS_FindFile (const char *name, int* index, qboolean quiet)
        }
 
        if (!quiet)
-               Sys_Printf ("FS_FindFile: can't find %s\n", name);
+               Sys_Printf("FS_FindFile: can't find %s\n", name);
 
        if (index != NULL)
                *index = -1;
@@ -1170,7 +1166,7 @@ qfile_t *FS_FOpenFile (const char *filename, qboolean quiet)
        // No Zlib DLL = no compressed files
        if (!zlib_dll && (packfile->flags & FILE_FLAG_DEFLATED))
        {
-               Con_Printf ("WARNING: can't open the compressed file %s\n"
+               Con_Printf("WARNING: can't open the compressed file %s\n"
                                        "You need the Zlib DLL to use compressed files\n",
                                        filename);
                fs_filesize = -1;
@@ -1431,6 +1427,18 @@ int FS_Flush (qfile_t* file)
 }
 
 
+/*
+====================
+FS_Print
+
+Print a string into a file
+====================
+*/
+int FS_Print(qfile_t* file, const char *msg)
+{
+       return FS_Write(file, msg, strlen(msg));
+}
+
 /*
 ====================
 FS_Printf
@@ -1438,7 +1446,7 @@ FS_Printf
 Print a string into a file
 ====================
 */
-int FS_Printf (qfile_t* file, const char* format, ...)
+int FS_Printf(qfile_t* file, const char* format, ...)
 {
        int result;
        va_list args;
@@ -1451,6 +1459,19 @@ int FS_Printf (qfile_t* file, const char* format, ...)
 }
 
 
+/*
+====================
+FS_VPrintf
+
+Print a string into a file
+====================
+*/
+int FS_VPrintf(qfile_t* file, const char* format, va_list ap)
+{
+       return vfprintf (file->stream, format, ap);
+}
+
+
 /*
 ====================
 FS_Getc
@@ -1707,7 +1728,7 @@ Filename are relative to the quake directory.
 Always appends a 0 byte.
 ============
 */
-qbyte *FS_LoadFile (const char *path, qboolean quiet)
+qbyte *FS_LoadFile (const char *path, mempool_t *pool, qboolean quiet)
 {
        qfile_t *h;
        qbyte *buf;
@@ -1717,7 +1738,7 @@ qbyte *FS_LoadFile (const char *path, qboolean quiet)
        if (!h)
                return NULL;
 
-       buf = Mem_Alloc(tempmempool, fs_filesize+1);
+       buf = Mem_Alloc(pool, fs_filesize+1);
        if (!buf)
                Sys_Error ("FS_LoadFile: not enough available memory for %s (size %i)", path, fs_filesize);
 
@@ -1744,11 +1765,11 @@ qboolean FS_WriteFile (const char *filename, void *data, int len)
        handle = FS_Open (filename, "wb", false);
        if (!handle)
        {
-               Con_Printf ("FS_WriteFile: failed on %s\n", filename);
+               Con_Printf("FS_WriteFile: failed on %s\n", filename);
                return false;
        }
 
-       Con_DPrintf ("FS_WriteFile: %s\n", filename);
+       Con_DPrintf("FS_WriteFile: %s\n", filename);
        FS_Write (handle, data, len);
        FS_Close (handle);
        return true;
@@ -1906,7 +1927,7 @@ fssearch_t *FS_Search(const char *pattern, int caseinsensitive, int quiet)
        if (separator < colon)
                separator = colon;
        basepathlength = separator - pattern;
-       basepath = Z_Malloc(basepathlength + 1);
+       basepath = Mem_Alloc (tempmempool, basepathlength + 1);
        if (basepathlength)
                memcpy(basepath, pattern, basepathlength);
        basepath[basepathlength] = 0;
@@ -2010,7 +2031,7 @@ fssearch_t *FS_Search(const char *pattern, int caseinsensitive, int quiet)
                        stringlistfree(liststart);
        }
 
-       Z_Free(basepath);
+       Mem_Free(basepath);
        return search;
 }
 
@@ -2031,7 +2052,7 @@ int FS_ListDirectory(const char *pattern, int oneperline)
        const char *name;
        char linebuf[4096];
        fssearch_t *search;
-       search = FS_Search(pattern, true, false);
+       search = FS_Search(pattern, true, true);
        if (!search)
                return 0;
        numfiles = search->numfilenames;
@@ -2101,7 +2122,7 @@ static void FS_ListDirectoryCmd (const char* cmdname, int oneperline)
        else
                pattern = "*";
        if (!FS_ListDirectory(pattern, oneperline))
-               Con_Printf("No files found.\n");
+               Con_Print("No files found.\n");
 }
 
 void FS_Dir_f(void)