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 (/)
41 // Leonardo Zide (leo@lokigames.com)
46 #if defined __linux__ || defined (__APPLE__)
54 #define S_ISDIR(mode) (mode & _S_IFDIR)
65 char magic[4]; // Name of the new WAD format ("PACK")
66 gint32 diroffset; // Position of WAD directory from start of file
67 gint32 dirsize; // Number of entries * 0x40 (64 char)
72 char filename[0x38]; // Name of the file, Unix style, with extension, 50 chars, padded with '\0'.
73 gint32 offset; // Position of the entry in PACK file
74 gint32 size; // Size of the entry in PACK file
84 // =============================================================================
87 static GSList* g_unzFiles;
88 static GSList* g_pakFiles;
89 static char g_strDirs[VFS_MAXDIRS][PATH_MAX];
91 static bool g_bUsePak = true;
93 // =============================================================================
96 static void vfsAddSlash (char *str)
101 if (str[n-1] != '\\' && str[n-1] != '/')
106 static void vfsFixDOSName (char *src)
119 static void vfsInitPakFile (const char *filename)
125 f = fopen (filename, "rb");
130 fread (header.magic, 1, 4, f);
131 fread (&header.diroffset, 1, 4, f);
132 fread (&header.dirsize, 1, 4, f);
135 header.diroffset = GINT32_FROM_LE (header.diroffset);
136 header.dirsize = GINT32_FROM_LE (header.dirsize);
138 // check that the magic header
139 if (strncmp (header.magic, "PACK", 4))
145 g_FuncTable.m_pfnSysPrintf(" pak file: %s\n", filename);
147 g_unzFiles = g_slist_append (g_unzFiles, f);
148 fseek (f, header.diroffset, SEEK_SET);
150 for (i = 0; i < (long)(header.dirsize/sizeof (pakentry_t)); i++)
154 file = (VFS_PAKFILE*)g_malloc (sizeof (VFS_PAKFILE));
155 g_pakFiles = g_slist_append (g_pakFiles, file);
157 fread (file->entry.filename, 1, sizeof (file->entry.filename), f);
158 fread (&file->entry.offset, 1, sizeof (file->entry.offset), f);
159 fread (&file->entry.size, 1, sizeof (file->entry.size), f);
163 file->entry.offset = GINT32_FROM_LE (file->entry.offset);
164 file->entry.size = GINT32_FROM_LE (file->entry.size);
167 vfsFixDOSName (file->entry.filename);
168 g_strdown (file->entry.filename);
169 //g_FuncTable.m_pfnSysPrintf("vfs file from pak: %s\n", file->entry.filename);
173 static GSList* vfsGetListInternal (const char *dir, const char *ext, bool directories)
175 GSList *lst, *lst_aux, *files = NULL;
176 char dirname[NAME_MAX], extension[NAME_MAX], filename[NAME_MAX];
179 //struct dirent *dirlist;
188 strcat (dirname, dir);
190 vfsFixDOSName (dirname);
191 vfsAddSlash (dirname);
192 Sys_Printf("vfs dirname_1: %s\n", dirname);
195 // dirname[0] = '\0';
196 dirlen = strlen (dirname);
199 strcpy (extension, ext);
202 g_strdown (extension);
204 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
206 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
207 gboolean found = FALSE;
208 ptr = file->entry.filename;
210 // check that the file name begins with dirname
211 for (i = 0; (*ptr && i < dirlen); i++, ptr++)
212 if (*ptr != dirname[i])
220 char *sep = strchr (ptr, '/');
226 // check for duplicates
227 for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))
228 if (strncmp ((char*)lst_aux->data, ptr, i) == 0)
236 char *name = g_strndup (ptr, i+1);
238 files = g_slist_append (files, name);
244 if ((ext != NULL) && (strstr (ptr, extension) == NULL))
247 // check for duplicates
248 for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))
249 if (strcmp ((char*)lst_aux->data, ptr) == 0)
256 files = g_slist_append (files, g_strdup (ptr));
260 for (i = 0; i < g_numDirs; i++)
262 strcpy (dirname, g_strDirs[i]);
263 strcat (dirname, dir);
265 vfsFixDOSName (dirname);
266 vfsAddSlash (dirname);
268 diskdir = g_dir_open (dirname, 0, NULL);
274 const char* name = g_dir_read_name(diskdir);
278 if (directories && (name[0] == '.'))
281 sprintf (filename, "%s%s", dirname, name);
282 stat (filename, &st);
283 Sys_Printf("vfs FileName: %s\n", filename);
285 if ((S_ISDIR (st.st_mode) != 0) != directories)
288 gboolean found = FALSE;
290 dirlist = g_strdup(name);
294 char *ptr_ext = strrchr (dirlist, '.');
296 || (ext != NULL && ptr_ext != NULL && ptr_ext[0] != '\0' && strcmp (ptr_ext+1, extension) == 0))
299 // check for duplicates
300 for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))
301 if (strcmp ((char*)lst_aux->data, dirlist) == 0)
308 files = g_slist_append (files, g_strdup (dirlist));
313 g_dir_close (diskdir);
321 This behaves identically to -stricmp(a,b), except that ASCII chars
322 [\]^`_ come AFTER alphabet chars instead of before. This is because
323 it effectively converts all alphabet chars to uppercase before comparison,
324 while stricmp converts them to lowercase.
326 //!\todo Analyse the code in rtcw/q3 to see how it behaves.
327 static int vfsPakSort (const void *a, const void *b)
339 if (c1 >= 'a' && c1 <= 'z')
343 if (c2 >= 'a' && c2 <= 'z')
348 if ( c1 == '\\' || c1 == ':' )
352 if ( c2 == '\\' || c2 == ':' )
357 // Arnout: note - sort pakfiles in reverse order. This ensures that
358 // later pakfiles override earlier ones. This because the vfs module
359 // returns a filehandle to the first file it can find (while it should
360 // return the filehandle to the file in the most overriding pakfile, the
361 // last one in the list that is).
364 //return -1; // strings not equal
365 return 1; // strings not equal
374 return 0; // strings are equal
377 // =============================================================================
380 void vfsInitDirectory (const char *path)
382 char filename[PATH_MAX];
383 //struct dirent *direntry;
385 GSList *dirlist = NULL;
387 if (g_numDirs == (VFS_MAXDIRS-1))
390 strcpy (g_strDirs[g_numDirs], path);
391 vfsFixDOSName (g_strDirs[g_numDirs]);
392 vfsAddSlash (g_strDirs[g_numDirs]);
397 dir = g_dir_open (path, 0, NULL);
400 g_FuncTable.m_pfnSysPrintf("vfs directory: %s\n", path);
404 const char* name = g_dir_read_name(dir);
408 char *ext = strrchr (name, '.');
409 if ((ext == NULL) || (strcasecmp (ext, ".pak") != 0))
412 char* direntry = g_strdup(name);
413 dirlist = g_slist_append (dirlist, direntry);
420 dirlist = g_slist_sort (dirlist, vfsPakSort);
422 // add the entries to the vfs and free the list
425 GSList *cur = dirlist;
426 char* name = (char*)cur->data;
428 sprintf (filename, "%s/%s", path, name);
429 vfsInitPakFile (filename);
432 dirlist = g_slist_remove (cur, name);
435 g_FuncTable.m_pfnSysFPrintf(SYS_WRN, "vfs directory not found: %s\n", path);
441 // frees all memory that we allocated
446 fclose ((FILE*)g_unzFiles->data);
447 g_unzFiles = g_slist_remove (g_unzFiles, g_unzFiles->data);
452 g_free (g_pakFiles->data);
453 g_pakFiles = g_slist_remove (g_pakFiles, g_pakFiles->data);
457 GSList* vfsGetFileList (const char *dir, const char *ext)
459 return vfsGetListInternal (dir, ext, false);
462 GSList* vfsGetDirList (const char *dir)
464 return vfsGetListInternal (dir, NULL, true);
467 void vfsClearFileDirList (GSList **lst)
471 g_free ((*lst)->data);
472 *lst = g_slist_remove (*lst, (*lst)->data);
476 // return the number of files that match
477 int vfsGetFileCount (const char *filename, int flag)
480 char fixed[NAME_MAX], tmp[NAME_MAX];
483 strcpy (fixed, filename);
484 vfsFixDOSName (fixed);
487 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
489 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
491 if (strcmp (file->entry.filename, fixed) == 0)
495 for (i = 0; i < g_numDirs; i++)
497 strcpy (tmp, g_strDirs[i]);
499 if (access (tmp, R_OK) == 0)
506 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
507 int vfsLoadFile (const char *filename, void **bufferptr, int index)
510 char tmp[NAME_MAX], fixed[NAME_MAX];
514 strcpy (fixed, filename);
515 vfsFixDOSName (fixed);
518 for (i = 0; i < g_numDirs; i++)
520 strcpy (tmp, g_strDirs[i]);
521 strcat (tmp, filename);
522 if (access (tmp, R_OK) == 0)
529 f = fopen (tmp, "rb");
533 fseek (f, 0, SEEK_END);
537 *bufferptr = malloc (len+1);
538 if (*bufferptr == NULL)
541 fread (*bufferptr, 1, len, f);
544 // we need to end the buffer with a 0
545 ((char*) (*bufferptr))[len] = 0;
554 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
556 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
558 if (strcmp (file->entry.filename, fixed) != 0)
563 fseek (file->pak, file->entry.offset, SEEK_SET);
565 *bufferptr = malloc (file->entry.size+1);
566 // we need to end the buffer with a 0
567 ((char*) (*bufferptr))[file->entry.size] = 0;
569 return fread (*bufferptr, 1, file->entry.size, file->pak);
578 void vfsFreeFile (void *p)
583 // open a full path file
584 int vfsLoadFullPathFile (const char *filename, void **bufferptr)
589 f = fopen (filename, "rb");
593 fseek (f, 0, SEEK_END);
597 *bufferptr = g_malloc (len+1);
598 if (*bufferptr == NULL)
601 fread (*bufferptr, 1, len, f);
604 // we need to end the buffer with a 0
605 ((char*) (*bufferptr))[len] = 0;
610 void vfsCleanFileName(char *in)
619 const char* vfsBasePromptPath()
622 static const char* path = "C:";
624 static const char* path = "/";
630 \param shorten will try to match against the short version
631 http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=144
632 recent switch back to short path names in project settings has broken some stuff
633 with shorten == true, we will convert in to short version before looking for root
634 FIXME WAAA .. the stuff below is much more simple on linux .. add appropriate #ifdef
636 char* vfsExtractRelativePath_short(const char *in, bool shorten)
640 char check[PATH_MAX];
641 static char out[PATH_MAX];
645 Sys_Printf("vfsExtractRelativePath: %s\n", in);
652 if (GetShortPathName(in, l_in, PATH_MAX) == 0)
655 Sys_Printf("GetShortPathName failed\n");
664 vfsCleanFileName(l_in);
667 vfsCleanFileName(l_in);
668 #endif // ifdef WIN32
672 Sys_Printf("cleaned path: %s\n", l_in);
675 for (i = 0; i < g_numDirs; i++)
677 strcpy(check,g_strDirs[i]);
678 vfsCleanFileName(check);
680 Sys_Printf("Matching against %s\n", check);
683 // try to find a match
684 if (strstr(l_in, check))
686 strcpy(out,l_in+strlen(check)+1);
694 Sys_Printf("vfsExtractRelativePath: success\n");
699 Sys_Printf("vfsExtractRelativePath: failed\n");
704 // HYDRA: this now searches VFS/PAK files in addition to the filesystem
705 // if FLAG is unspecified then ONLY dirs are searched.
706 // PAK's are searched before DIRs to mimic engine behaviour
707 // index is ignored when searching PAK files.
709 char* vfsGetFullPath(const char *in, int index, int flag)
712 static char out[PATH_MAX];
716 if (flag & VFS_SEARCH_PAK)
718 char fixed[NAME_MAX];
722 vfsFixDOSName (fixed);
725 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
727 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
730 lastptr = file->name;
732 while ((ptr = strchr(lastptr,'/')) != NULL)
735 if (strcmp (lastptr, fixed) == 0)
737 strncpy(out,file->name,PATH_MAX);
744 if (!flag || (flag & VFS_SEARCH_DIR))
746 for (i = 0; i < g_numDirs; i++)
748 strcpy (tmp, g_strDirs[i]);
750 if (access (tmp, R_OK) == 0)
764 // FIXME TTimo: this and the above should be merged at some point
765 char* vfsExtractRelativePath(const char *in)
767 static char out[PATH_MAX];
768 unsigned int i, count;
769 char *chunk, *backup = NULL; // those point to out stuff
770 char *ret = vfsExtractRelativePath_short(in, false);
774 Sys_Printf("trying with a short version\n");
776 ret = vfsExtractRelativePath_short(in, true);
779 // ok, but we have a relative short version now
780 // hack the long relative version out of here
782 for(i=0;i<strlen(ret);i++)
787 // this is the clean, not short version
789 vfsCleanFileName(out);
790 for(i=0;i<=count;i++)
792 chunk = strrchr(out, '/');