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 *dirlistptr;
386 GSList *dirlist = NULL;
388 if (g_numDirs == (VFS_MAXDIRS-1))
391 strcpy (g_strDirs[g_numDirs], path);
392 vfsFixDOSName (g_strDirs[g_numDirs]);
393 vfsAddSlash (g_strDirs[g_numDirs]);
398 dir = g_dir_open (path, 0, NULL);
401 g_FuncTable.m_pfnSysPrintf("vfs directory: %s\n", path);
405 const char* name = g_dir_read_name(dir);
409 char *ext = strrchr (name, '.');
410 if ((ext == NULL) || (strcasecmp (ext, ".pak") != 0))
413 char* direntry = g_strdup(name);
414 dirlist = g_slist_append (dirlist, direntry);
421 dirlist = g_slist_sort (dirlist, vfsPakSort);
423 // add the entries to the vfs and free the list
426 GSList *cur = dirlist;
427 char* name = (char*)cur->data;
429 sprintf (filename, "%s/%s", path, name);
430 vfsInitPakFile (filename);
433 dirlist = g_slist_remove (cur, name);
436 g_FuncTable.m_pfnSysFPrintf(SYS_WRN, "vfs directory not found: %s\n", path);
442 // frees all memory that we allocated
447 fclose ((FILE*)g_unzFiles->data);
448 g_unzFiles = g_slist_remove (g_unzFiles, g_unzFiles->data);
453 g_free (g_pakFiles->data);
454 g_pakFiles = g_slist_remove (g_pakFiles, g_pakFiles->data);
458 GSList* vfsGetFileList (const char *dir, const char *ext)
460 return vfsGetListInternal (dir, ext, false);
463 GSList* vfsGetDirList (const char *dir)
465 return vfsGetListInternal (dir, NULL, true);
468 void vfsClearFileDirList (GSList **lst)
472 g_free ((*lst)->data);
473 *lst = g_slist_remove (*lst, (*lst)->data);
477 // return the number of files that match
478 int vfsGetFileCount (const char *filename, int flag)
481 char fixed[NAME_MAX], tmp[NAME_MAX];
484 strcpy (fixed, filename);
485 vfsFixDOSName (fixed);
488 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
490 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
492 if (strcmp (file->entry.filename, fixed) == 0)
496 for (i = 0; i < g_numDirs; i++)
498 strcpy (tmp, g_strDirs[i]);
500 if (access (tmp, R_OK) == 0)
507 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
508 int vfsLoadFile (const char *filename, void **bufferptr, int index)
511 char tmp[NAME_MAX], fixed[NAME_MAX];
515 strcpy (fixed, filename);
516 vfsFixDOSName (fixed);
519 for (i = 0; i < g_numDirs; i++)
521 strcpy (tmp, g_strDirs[i]);
522 strcat (tmp, filename);
523 if (access (tmp, R_OK) == 0)
530 f = fopen (tmp, "rb");
534 fseek (f, 0, SEEK_END);
538 *bufferptr = malloc (len+1);
539 if (*bufferptr == NULL)
542 fread (*bufferptr, 1, len, f);
545 // we need to end the buffer with a 0
546 ((char*) (*bufferptr))[len] = 0;
555 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
557 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
559 if (strcmp (file->entry.filename, fixed) != 0)
564 fseek (file->pak, file->entry.offset, SEEK_SET);
566 *bufferptr = malloc (file->entry.size+1);
567 // we need to end the buffer with a 0
568 ((char*) (*bufferptr))[file->entry.size] = 0;
570 return fread (*bufferptr, 1, file->entry.size, file->pak);
579 void vfsFreeFile (void *p)
584 // open a full path file
585 int vfsLoadFullPathFile (const char *filename, void **bufferptr)
590 f = fopen (filename, "rb");
594 fseek (f, 0, SEEK_END);
598 *bufferptr = g_malloc (len+1);
599 if (*bufferptr == NULL)
602 fread (*bufferptr, 1, len, f);
605 // we need to end the buffer with a 0
606 ((char*) (*bufferptr))[len] = 0;
611 void vfsCleanFileName(char *in)
620 const char* vfsBasePromptPath()
623 static char* path = "C:";
625 static char* path = "/";
631 \param shorten will try to match against the short version
632 http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=144
633 recent switch back to short path names in project settings has broken some stuff
634 with shorten == true, we will convert in to short version before looking for root
635 FIXME WAAA .. the stuff below is much more simple on linux .. add appropriate #ifdef
637 char* vfsExtractRelativePath_short(const char *in, bool shorten)
641 char check[PATH_MAX];
642 static char out[PATH_MAX];
646 Sys_Printf("vfsExtractRelativePath: %s\n", in);
653 if (GetShortPathName(in, l_in, PATH_MAX) == 0)
656 Sys_Printf("GetShortPathName failed\n");
665 vfsCleanFileName(l_in);
668 vfsCleanFileName(l_in);
669 #endif // ifdef WIN32
673 Sys_Printf("cleaned path: %s\n", l_in);
676 for (i = 0; i < g_numDirs; i++)
678 strcpy(check,g_strDirs[i]);
679 vfsCleanFileName(check);
681 Sys_Printf("Matching against %s\n", check);
684 // try to find a match
685 if (strstr(l_in, check))
687 strcpy(out,l_in+strlen(check)+1);
695 Sys_Printf("vfsExtractRelativePath: success\n");
700 Sys_Printf("vfsExtractRelativePath: failed\n");
705 // HYDRA: this now searches VFS/PAK files in addition to the filesystem
706 // if FLAG is unspecified then ONLY dirs are searched.
707 // PAK's are searched before DIRs to mimic engine behaviour
708 // index is ignored when searching PAK files.
710 char* vfsGetFullPath(const char *in, int index, int flag)
713 static char out[PATH_MAX];
717 if (flag & VFS_SEARCH_PAK)
719 char fixed[NAME_MAX];
723 vfsFixDOSName (fixed);
726 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
728 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
731 lastptr = file->name;
733 while (ptr = strchr(lastptr,'/'))
736 if (strcmp (lastptr, fixed) == 0)
738 strncpy(out,file->name,PATH_MAX);
745 if (!flag || (flag & VFS_SEARCH_DIR))
747 for (i = 0; i < g_numDirs; i++)
749 strcpy (tmp, g_strDirs[i]);
751 if (access (tmp, R_OK) == 0)
765 // FIXME TTimo: this and the above should be merged at some point
766 char* vfsExtractRelativePath(const char *in)
768 static char out[PATH_MAX];
769 unsigned int i, count;
770 char *chunk, *backup = NULL; // those point to out stuff
771 char *ret = vfsExtractRelativePath_short(in, false);
775 Sys_Printf("trying with a short version\n");
777 ret = vfsExtractRelativePath_short(in, true);
780 // ok, but we have a relative short version now
781 // hack the long relative version out of here
783 for(i=0;i<strlen(ret);i++)
788 // this is the clean, not short version
790 vfsCleanFileName(out);
791 for(i=0;i<=count;i++)
793 chunk = strrchr(out, '/');