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)
46 #if defined (__linux__) || defined (__APPLE__)
53 #define S_ISDIR(mode) (mode & _S_IFDIR)
76 // =============================================================================
79 static GSList* g_unzFiles;
80 static GSList* g_pakFiles;
81 static char g_strDirs[VFS_MAXDIRS][PATH_MAX];
83 static gboolean g_bUsePak = TRUE;
85 // =============================================================================
88 static void vfsAddSlash (char *str)
93 if (str[n-1] != '\\' && str[n-1] != '/')
98 static void vfsFixDOSName (char *src)
111 //!\todo Define globally or use heap-allocated string.
114 static void vfsInitPakFile (const char *filename)
121 uf = unzOpen (filename);
125 g_unzFiles = g_slist_append (g_unzFiles, uf);
127 err = unzGetGlobalInfo (uf,&gi);
130 unzGoToFirstFile(uf);
132 for (i = 0; i < gi.number_entry; i++)
134 char filename_inzip[NAME_MAX];
135 unz_file_info file_info;
138 err = unzGetCurrentFileInfo (uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0);
142 file = (VFS_PAKFILE*)safe_malloc (sizeof (VFS_PAKFILE));
143 g_pakFiles = g_slist_append (g_pakFiles, file);
145 vfsFixDOSName (filename_inzip);
146 g_strdown (filename_inzip);
148 file->name = strdup (filename_inzip);
149 file->size = file_info.uncompressed_size;
151 memcpy (&file->zipinfo, uf, sizeof (unz_s));
153 if ((i+1) < gi.number_entry)
155 err = unzGoToNextFile(uf);
162 // =============================================================================
165 // reads all pak files from a dir
166 void vfsInitDirectory (const char *path)
168 char filename[PATH_MAX];
172 if (g_numDirs == VFS_MAXDIRS)
175 Sys_Printf ("VFS Init: %s\n", path);
177 strcpy (g_strDirs[g_numDirs], path);
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 dirlist = g_strdup(name);
197 char *ext = strrchr (dirlist, '.');
199 if(ext && !Q_stricmp(ext, ".pk3dir"))
201 if (g_numDirs == VFS_MAXDIRS)
203 snprintf(g_strDirs[g_numDirs], PATH_MAX, "%s/%s", path, name);
204 g_strDirs[g_numDirs][PATH_MAX] = '\0';
205 vfsFixDOSName (g_strDirs[g_numDirs]);
206 vfsAddSlash (g_strDirs[g_numDirs]);
210 if ((ext == NULL) || (Q_stricmp (ext, ".pk3") != 0))
214 sprintf (filename, "%s/%s", path, dirlist);
215 vfsInitPakFile (filename);
224 // frees all memory that we allocated
229 unzClose ((unzFile)g_unzFiles->data);
230 g_unzFiles = g_slist_remove (g_unzFiles, g_unzFiles->data);
235 VFS_PAKFILE* file = (VFS_PAKFILE*)g_pakFiles->data;
238 g_pakFiles = g_slist_remove (g_pakFiles, file);
242 // return the number of files that match
243 int vfsGetFileCount (const char *filename)
246 char fixed[NAME_MAX], tmp[NAME_MAX];
249 strcpy (fixed, filename);
250 vfsFixDOSName (fixed);
253 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
255 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
257 if (strcmp (file->name, fixed) == 0)
261 for (i = 0; i < g_numDirs; i++)
263 strcpy (tmp, g_strDirs[i]);
265 if (access (tmp, R_OK) == 0)
272 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
273 int vfsLoadFile (const char *filename, void **bufferptr, int index)
276 char tmp[NAME_MAX], fixed[NAME_MAX];
279 // filename is a full path
285 f = fopen (filename, "rb");
289 fseek (f, 0, SEEK_END);
293 *bufferptr = safe_malloc (len+1);
294 if (*bufferptr == NULL)
297 fread (*bufferptr, 1, len, f);
300 // we need to end the buffer with a 0
301 ((char*) (*bufferptr))[len] = 0;
307 strcpy (fixed, filename);
308 vfsFixDOSName (fixed);
311 for (i = 0; i < g_numDirs; i++)
313 strcpy (tmp, g_strDirs[i]);
314 strcat (tmp, filename);
315 if (access (tmp, R_OK) == 0)
322 f = fopen (tmp, "rb");
326 fseek (f, 0, SEEK_END);
330 *bufferptr = safe_malloc (len+1);
331 if (*bufferptr == NULL)
334 fread (*bufferptr, 1, len, f);
337 // we need to end the buffer with a 0
338 ((char*) (*bufferptr))[len] = 0;
347 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
349 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
351 if (strcmp (file->name, fixed) != 0)
356 memcpy (file->zipfile, &file->zipinfo, sizeof (unz_s));
358 if (unzOpenCurrentFile (file->zipfile) != UNZ_OK)
361 *bufferptr = safe_malloc (file->size+1);
362 // we need to end the buffer with a 0
363 ((char*) (*bufferptr))[file->size] = 0;
365 i = unzReadCurrentFile (file->zipfile , *bufferptr, file->size);
366 unzCloseCurrentFile (file->zipfile);