2 Copyright (c) 2001, Loki software, inc.
\r
5 Redistribution and use in source and binary forms, with or without modification,
\r
6 are permitted provided that the following conditions are met:
\r
8 Redistributions of source code must retain the above copyright notice, this list
\r
9 of conditions and the following disclaimer.
\r
11 Redistributions in binary form must reproduce the above copyright notice, this
\r
12 list of conditions and the following disclaimer in the documentation and/or
\r
13 other materials provided with the distribution.
\r
15 Neither the name of Loki software nor the names of its contributors may be used
\r
16 to endorse or promote products derived from this software without specific prior
\r
17 written permission.
\r
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
\r
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
\r
22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
\r
23 DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
\r
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
\r
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
\r
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
\r
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
34 // - Directories should be searched in the following order: ~/.q3a/baseq3,
\r
35 // install dir (/usr/local/games/quake3/baseq3) and cd_path (/mnt/cdrom/baseq3).
\r
37 // - Pak files are searched first inside the directories.
\r
38 // - Case insensitive.
\r
39 // - Unix-style slashes (/) (windows is backwards .. everyone knows that)
\r
41 // Leonardo Zide (leo@lokigames.com)
\r
46 #if defined (__linux__) || defined (__APPLE__)
\r
53 #define S_ISDIR(mode) (mode & _S_IFDIR)
\r
54 #define PATH_MAX 260
\r
59 #include <sys/stat.h>
\r
62 #include "mathlib.h"
\r
76 // =============================================================================
\r
79 static GSList* g_unzFiles;
\r
80 static GSList* g_pakFiles;
\r
81 static char g_strDirs[VFS_MAXDIRS][PATH_MAX];
\r
82 static int g_numDirs;
\r
83 static gboolean g_bUsePak = TRUE;
\r
85 // =============================================================================
\r
88 static void vfsAddSlash (char *str)
\r
90 int n = strlen (str);
\r
93 if (str[n-1] != '\\' && str[n-1] != '/')
\r
98 static void vfsFixDOSName (char *src)
\r
111 //!\todo Define globally or use heap-allocated string.
\r
112 #define NAME_MAX 255
\r
114 static void vfsInitPakFile (const char *filename)
\r
116 unz_global_info gi;
\r
121 uf = unzOpen (filename);
\r
125 g_unzFiles = g_slist_append (g_unzFiles, uf);
\r
127 err = unzGetGlobalInfo (uf,&gi);
\r
130 unzGoToFirstFile(uf);
\r
132 for (i = 0; i < gi.number_entry; i++)
\r
134 char filename_inzip[NAME_MAX];
\r
135 unz_file_info file_info;
\r
138 err = unzGetCurrentFileInfo (uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0);
\r
142 file = (VFS_PAKFILE*)safe_malloc (sizeof (VFS_PAKFILE));
\r
143 g_pakFiles = g_slist_append (g_pakFiles, file);
\r
145 vfsFixDOSName (filename_inzip);
\r
146 g_strdown (filename_inzip);
\r
148 file->name = strdup (filename_inzip);
\r
149 file->size = file_info.uncompressed_size;
\r
150 file->zipfile = uf;
\r
151 memcpy (&file->zipinfo, uf, sizeof (unz_s));
\r
153 if ((i+1) < gi.number_entry)
\r
155 err = unzGoToNextFile(uf);
\r
162 // =============================================================================
\r
163 // Global functions
\r
165 // reads all pak files from a dir
\r
166 void vfsInitDirectory (const char *path)
\r
168 char filename[PATH_MAX];
\r
172 if (g_numDirs == (VFS_MAXDIRS-1))
\r
175 Sys_Printf ("VFS Init: %s\n", path);
\r
177 strcpy (g_strDirs[g_numDirs], path);
\r
178 vfsFixDOSName (g_strDirs[g_numDirs]);
\r
179 vfsAddSlash (g_strDirs[g_numDirs]);
\r
184 dir = g_dir_open (path, 0, NULL);
\r
190 const char* name = g_dir_read_name(dir);
\r
194 dirlist = g_strdup(name);
\r
197 char *ext = strrchr (dirlist, '.');
\r
198 if ((ext == NULL) || (Q_stricmp (ext, ".pk3") != 0))
\r
202 sprintf (filename, "%s/%s", path, dirlist);
\r
203 vfsInitPakFile (filename);
\r
212 // frees all memory that we allocated
\r
213 void vfsShutdown ()
\r
217 unzClose ((unzFile)g_unzFiles->data);
\r
218 g_unzFiles = g_slist_remove (g_unzFiles, g_unzFiles->data);
\r
223 VFS_PAKFILE* file = (VFS_PAKFILE*)g_pakFiles->data;
\r
226 g_pakFiles = g_slist_remove (g_pakFiles, file);
\r
230 // return the number of files that match
\r
231 int vfsGetFileCount (const char *filename)
\r
234 char fixed[NAME_MAX], tmp[NAME_MAX];
\r
237 strcpy (fixed, filename);
\r
238 vfsFixDOSName (fixed);
\r
241 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
\r
243 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
\r
245 if (strcmp (file->name, fixed) == 0)
\r
249 for (i = 0; i < g_numDirs; i++)
\r
251 strcpy (tmp, g_strDirs[i]);
\r
252 strcat (tmp, fixed);
\r
253 if (access (tmp, R_OK) == 0)
\r
260 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
\r
261 int vfsLoadFile (const char *filename, void **bufferptr, int index)
\r
264 char tmp[NAME_MAX], fixed[NAME_MAX];
\r
267 // filename is a full path
\r
273 f = fopen (filename, "rb");
\r
277 fseek (f, 0, SEEK_END);
\r
281 *bufferptr = safe_malloc (len+1);
\r
282 if (*bufferptr == NULL)
\r
285 fread (*bufferptr, 1, len, f);
\r
288 // we need to end the buffer with a 0
\r
289 ((char*) (*bufferptr))[len] = 0;
\r
295 strcpy (fixed, filename);
\r
296 vfsFixDOSName (fixed);
\r
299 for (i = 0; i < g_numDirs; i++)
\r
301 strcpy (tmp, g_strDirs[i]);
\r
302 strcat (tmp, filename);
\r
303 if (access (tmp, R_OK) == 0)
\r
305 if (count == index)
\r
310 f = fopen (tmp, "rb");
\r
314 fseek (f, 0, SEEK_END);
\r
318 *bufferptr = safe_malloc (len+1);
\r
319 if (*bufferptr == NULL)
\r
322 fread (*bufferptr, 1, len, f);
\r
325 // we need to end the buffer with a 0
\r
326 ((char*) (*bufferptr))[len] = 0;
\r
335 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
\r
337 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
\r
339 if (strcmp (file->name, fixed) != 0)
\r
342 if (count == index)
\r
344 memcpy (file->zipfile, &file->zipinfo, sizeof (unz_s));
\r
346 if (unzOpenCurrentFile (file->zipfile) != UNZ_OK)
\r
349 *bufferptr = safe_malloc (file->size+1);
\r
350 // we need to end the buffer with a 0
\r
351 ((char*) (*bufferptr))[file->size] = 0;
\r
353 i = unzReadCurrentFile (file->zipfile , *bufferptr, file->size);
\r
354 unzCloseCurrentFile (file->zipfile);
\r