2 Copyright (c) 2001, Loki software, inc.
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
8 Redistributions of source code must retain the above copyright notice, this list
9 of conditions and the following disclaimer.
11 Redistributions in binary form must reproduce the above copyright notice, this
12 list of conditions and the following disclaimer in the documentation and/or
13 other materials provided with the distribution.
15 Neither the name of Loki software nor the names of its contributors may be used
16 to endorse or promote products derived from this software without specific prior
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
23 DIRECT,INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // - Directories should be searched in the following order: ~/.q3a/baseq3,
35 // install dir (/usr/local/games/quake3/baseq3) and cd_path (/mnt/cdrom/baseq3).
37 // - Pak files are searched first inside the directories.
38 // - Case insensitive.
39 // - Unix-style slashes (/) (windows is backwards .. everyone knows that)
41 // Leonardo Zide (leo@lokigames.com)
63 // =============================================================================
66 static GSList* g_unzFiles;
67 static GSList* g_pakFiles;
68 static char g_strDirs[VFS_MAXDIRS][PATH_MAX+1];
70 char g_strForbiddenDirs[VFS_MAXDIRS][PATH_MAX+1];
71 int g_numForbiddenDirs = 0;
72 static gboolean g_bUsePak = TRUE;
74 // =============================================================================
77 static void vfsAddSlash (char *str)
82 if (str[n-1] != '\\' && str[n-1] != '/')
87 static void vfsFixDOSName (char *src)
100 //!\todo Define globally or use heap-allocated string.
103 static void vfsInitPakFile (const char *filename)
110 uf = unzOpen (filename);
114 g_unzFiles = g_slist_append (g_unzFiles, uf);
116 err = unzGetGlobalInfo (uf,&gi);
119 unzGoToFirstFile(uf);
121 for (i = 0; i < gi.number_entry; i++)
123 char filename_inzip[NAME_MAX];
124 unz_file_info file_info;
127 err = unzGetCurrentFileInfo (uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0);
131 file = (VFS_PAKFILE*)safe_malloc (sizeof (VFS_PAKFILE));
132 g_pakFiles = g_slist_append (g_pakFiles, file);
134 vfsFixDOSName (filename_inzip);
135 g_strdown (filename_inzip);
137 file->name = strdup (filename_inzip);
138 file->size = file_info.uncompressed_size;
140 memcpy (&file->zipinfo, uf, sizeof (unz_s));
142 if ((i+1) < gi.number_entry)
144 err = unzGoToNextFile(uf);
151 // =============================================================================
154 // reads all pak files from a dir
155 void vfsInitDirectory (const char *path)
157 char filename[PATH_MAX];
162 for(j = 0; j < g_numForbiddenDirs; ++j)
164 if(!Q_stricmp(path, g_strForbiddenDirs[j])
165 || (strlen(path) > strlen(g_strForbiddenDirs[j]) && path[strlen(path) - strlen(g_strForbiddenDirs[j]) - 1] == '/' && !Q_stricmp(path + strlen(path) - strlen(g_strForbiddenDirs[j]), g_strForbiddenDirs[j])))
168 if(j < g_numForbiddenDirs)
171 if (g_numDirs == VFS_MAXDIRS)
174 Sys_Printf ("VFS Init: %s\n", path);
176 strncpy (g_strDirs[g_numDirs], path, PATH_MAX);
177 g_strDirs[g_numDirs][PATH_MAX] = 0;
178 vfsFixDOSName (g_strDirs[g_numDirs]);
179 vfsAddSlash (g_strDirs[g_numDirs]);
184 dir = g_dir_open (path, 0, NULL);
190 const char* name = g_dir_read_name(dir);
194 for(j = 0; j < g_numForbiddenDirs; ++j)
195 if(!Q_stricmp(name, g_strForbiddenDirs[j]))
197 if(j < g_numForbiddenDirs)
200 dirlist = g_strdup(name);
203 char *ext = strrchr (dirlist, '.');
205 if(ext && !Q_stricmp(ext, ".pk3dir"))
207 if (g_numDirs == VFS_MAXDIRS)
209 snprintf(g_strDirs[g_numDirs], PATH_MAX, "%s/%s", path, name);
210 g_strDirs[g_numDirs][PATH_MAX] = '\0';
211 vfsFixDOSName (g_strDirs[g_numDirs]);
212 vfsAddSlash (g_strDirs[g_numDirs]);
216 if ((ext == NULL) || (Q_stricmp (ext, ".pk3") != 0))
220 sprintf (filename, "%s/%s", path, dirlist);
221 vfsInitPakFile (filename);
230 // frees all memory that we allocated
235 unzClose ((unzFile)g_unzFiles->data);
236 g_unzFiles = g_slist_remove (g_unzFiles, g_unzFiles->data);
241 VFS_PAKFILE* file = (VFS_PAKFILE*)g_pakFiles->data;
244 g_pakFiles = g_slist_remove (g_pakFiles, file);
248 // return the number of files that match
249 int vfsGetFileCount (const char *filename)
252 char fixed[NAME_MAX], tmp[NAME_MAX];
255 strcpy (fixed, filename);
256 vfsFixDOSName (fixed);
259 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
261 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
263 if (strcmp (file->name, fixed) == 0)
267 for (i = 0; i < g_numDirs; i++)
269 strcpy (tmp, g_strDirs[i]);
271 if (access (tmp, R_OK) == 0)
278 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
279 int vfsLoadFile (const char *filename, void **bufferptr, int index)
282 char tmp[NAME_MAX], fixed[NAME_MAX];
285 // filename is a full path
291 f = fopen (filename, "rb");
295 fseek (f, 0, SEEK_END);
299 *bufferptr = safe_malloc (len+1);
300 if (*bufferptr == NULL)
303 fread (*bufferptr, 1, len, f);
306 // we need to end the buffer with a 0
307 ((char*) (*bufferptr))[len] = 0;
313 strcpy (fixed, filename);
314 vfsFixDOSName (fixed);
317 for (i = 0; i < g_numDirs; i++)
319 strcpy (tmp, g_strDirs[i]);
320 strcat (tmp, filename);
321 if (access (tmp, R_OK) == 0)
328 f = fopen (tmp, "rb");
332 fseek (f, 0, SEEK_END);
336 *bufferptr = safe_malloc (len+1);
337 if (*bufferptr == NULL)
340 fread (*bufferptr, 1, len, f);
343 // we need to end the buffer with a 0
344 ((char*) (*bufferptr))[len] = 0;
353 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
355 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
357 if (strcmp (file->name, fixed) != 0)
362 memcpy (file->zipfile, &file->zipinfo, sizeof (unz_s));
364 if (unzOpenCurrentFile (file->zipfile) != UNZ_OK)
367 *bufferptr = safe_malloc (file->size+1);
368 // we need to end the buffer with a 0
369 ((char*) (*bufferptr))[file->size] = 0;
371 i = unzReadCurrentFile (file->zipfile , *bufferptr, file->size);
372 unzCloseCurrentFile (file->zipfile);