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 (/)
\r
41 // Leonardo Zide (leo@lokigames.com)
\r
46 #if defined __linux__ || defined (__APPLE__)
\r
54 #define S_ISDIR(mode) (mode & _S_IFDIR)
\r
59 #include <sys/stat.h>
\r
65 char magic[4]; // Name of the new WAD format ("PACK")
\r
66 gint32 diroffset; // Position of WAD directory from start of file
\r
67 gint32 dirsize; // Number of entries * 0x40 (64 char)
\r
72 char filename[0x38]; // Name of the file, Unix style, with extension, 50 chars, padded with '\0'.
\r
73 gint32 offset; // Position of the entry in PACK file
\r
74 gint32 size; // Size of the entry in PACK file
\r
84 // =============================================================================
\r
87 static GSList* g_unzFiles;
\r
88 static GSList* g_pakFiles;
\r
89 static char g_strDirs[VFS_MAXDIRS][PATH_MAX];
\r
90 static int g_numDirs;
\r
91 static bool g_bUsePak = true;
\r
93 // =============================================================================
\r
96 static void vfsAddSlash (char *str)
\r
98 int n = strlen (str);
\r
101 if (str[n-1] != '\\' && str[n-1] != '/')
\r
106 static void vfsFixDOSName (char *src)
\r
119 static void vfsInitPakFile (const char *filename)
\r
121 pakheader_t header;
\r
125 f = fopen (filename, "rb");
\r
130 fread (header.magic, 1, 4, f);
\r
131 fread (&header.diroffset, 1, 4, f);
\r
132 fread (&header.dirsize, 1, 4, f);
\r
135 header.diroffset = GINT32_FROM_LE (header.diroffset);
\r
136 header.dirsize = GINT32_FROM_LE (header.dirsize);
\r
138 // check that the magic header
\r
139 if (strncmp (header.magic, "PACK", 4))
\r
145 g_FuncTable.m_pfnSysPrintf(" pak file: %s\n", filename);
\r
147 g_unzFiles = g_slist_append (g_unzFiles, f);
\r
148 fseek (f, header.diroffset, SEEK_SET);
\r
150 for (i = 0; i < (long)(header.dirsize/sizeof (pakentry_t)); i++)
\r
154 file = (VFS_PAKFILE*)g_malloc (sizeof (VFS_PAKFILE));
\r
155 g_pakFiles = g_slist_append (g_pakFiles, file);
\r
157 fread (file->entry.filename, 1, sizeof (file->entry.filename), f);
\r
158 fread (&file->entry.offset, 1, sizeof (file->entry.offset), f);
\r
159 fread (&file->entry.size, 1, sizeof (file->entry.size), f);
\r
163 file->entry.offset = GINT32_FROM_LE (file->entry.offset);
\r
164 file->entry.size = GINT32_FROM_LE (file->entry.size);
\r
167 vfsFixDOSName (file->entry.filename);
\r
168 g_strdown (file->entry.filename);
\r
169 //g_FuncTable.m_pfnSysPrintf("vfs file from pak: %s\n", file->entry.filename);
\r
173 static GSList* vfsGetListInternal (const char *dir, const char *ext, bool directories)
\r
175 GSList *lst, *lst_aux, *files = NULL;
\r
176 char dirname[NAME_MAX], extension[NAME_MAX], filename[NAME_MAX];
\r
179 //struct dirent *dirlist;
\r
188 strcat (dirname, dir);
\r
189 g_strdown (dirname);
\r
190 vfsFixDOSName (dirname);
\r
191 vfsAddSlash (dirname);
\r
192 Sys_Printf("vfs dirname_1: %s\n", dirname);
\r
195 // dirname[0] = '\0';
\r
196 dirlen = strlen (dirname);
\r
199 strcpy (extension, ext);
\r
201 extension[0] = '\0';
\r
202 g_strdown (extension);
\r
204 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
\r
206 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
\r
207 gboolean found = FALSE;
\r
208 ptr = file->entry.filename;
\r
210 // check that the file name begins with dirname
\r
211 for (i = 0; (*ptr && i < dirlen); i++, ptr++)
\r
212 if (*ptr != dirname[i])
\r
220 char *sep = strchr (ptr, '/');
\r
226 // check for duplicates
\r
227 for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))
\r
228 if (strncmp ((char*)lst_aux->data, ptr, i) == 0)
\r
236 char *name = g_strndup (ptr, i+1);
\r
238 files = g_slist_append (files, name);
\r
244 if ((ext != NULL) && (strstr (ptr, extension) == NULL))
\r
247 // check for duplicates
\r
248 for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))
\r
249 if (strcmp ((char*)lst_aux->data, ptr) == 0)
\r
256 files = g_slist_append (files, g_strdup (ptr));
\r
260 for (i = 0; i < g_numDirs; i++)
\r
262 strcpy (dirname, g_strDirs[i]);
\r
263 strcat (dirname, dir);
\r
264 g_strdown (dirname);
\r
265 vfsFixDOSName (dirname);
\r
266 vfsAddSlash (dirname);
\r
268 diskdir = g_dir_open (dirname, 0, NULL);
\r
270 if (diskdir != NULL)
\r
274 const char* name = g_dir_read_name(diskdir);
\r
278 if (directories && (name[0] == '.'))
\r
281 sprintf (filename, "%s%s", dirname, name);
\r
282 stat (filename, &st);
\r
283 Sys_Printf("vfs FileName: %s\n", filename);
\r
285 if ((S_ISDIR (st.st_mode) != 0) != directories)
\r
288 gboolean found = FALSE;
\r
290 dirlist = g_strdup(name);
\r
292 g_strdown (dirlist);
\r
294 char *ptr_ext = strrchr (dirlist, '.');
\r
296 || (ext != NULL && ptr_ext != NULL && ptr_ext[0] != '\0' && strcmp (ptr_ext+1, extension) == 0))
\r
299 // check for duplicates
\r
300 for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))
\r
301 if (strcmp ((char*)lst_aux->data, dirlist) == 0)
\r
308 files = g_slist_append (files, g_strdup (dirlist));
\r
313 g_dir_close (diskdir);
\r
321 This behaves identically to -stricmp(a,b), except that ASCII chars
\r
322 [\]^`_ come AFTER alphabet chars instead of before. This is because
\r
323 it effectively converts all alphabet chars to uppercase before comparison,
\r
324 while stricmp converts them to lowercase.
\r
326 //!\todo Analyse the code in rtcw/q3 to see how it behaves.
\r
327 static int vfsPakSort (const void *a, const void *b)
\r
339 if (c1 >= 'a' && c1 <= 'z')
\r
343 if (c2 >= 'a' && c2 <= 'z')
\r
348 if ( c1 == '\\' || c1 == ':' )
\r
352 if ( c2 == '\\' || c2 == ':' )
\r
357 // Arnout: note - sort pakfiles in reverse order. This ensures that
\r
358 // later pakfiles override earlier ones. This because the vfs module
\r
359 // returns a filehandle to the first file it can find (while it should
\r
360 // return the filehandle to the file in the most overriding pakfile, the
\r
361 // last one in the list that is).
\r
364 //return -1; // strings not equal
\r
365 return 1; // strings not equal
\r
374 return 0; // strings are equal
\r
377 // =============================================================================
\r
378 // Global functions
\r
380 void vfsInitDirectory (const char *path)
\r
382 char filename[PATH_MAX];
\r
383 //struct dirent *direntry;
\r
385 //GSList *dirlistptr;
\r
386 GSList *dirlist = NULL;
\r
388 if (g_numDirs == (VFS_MAXDIRS-1))
\r
391 strcpy (g_strDirs[g_numDirs], path);
\r
392 vfsFixDOSName (g_strDirs[g_numDirs]);
\r
393 vfsAddSlash (g_strDirs[g_numDirs]);
\r
398 dir = g_dir_open (path, 0, NULL);
\r
401 g_FuncTable.m_pfnSysPrintf("vfs directory: %s\n", path);
\r
405 const char* name = g_dir_read_name(dir);
\r
409 char *ext = strrchr (name, '.');
\r
410 if ((ext == NULL) || (strcasecmp (ext, ".pak") != 0))
\r
413 char* direntry = g_strdup(name);
\r
414 dirlist = g_slist_append (dirlist, direntry);
\r
421 dirlist = g_slist_sort (dirlist, vfsPakSort);
\r
423 // add the entries to the vfs and free the list
\r
426 GSList *cur = dirlist;
\r
427 char* name = (char*)cur->data;
\r
429 sprintf (filename, "%s/%s", path, name);
\r
430 vfsInitPakFile (filename);
\r
433 dirlist = g_slist_remove (cur, name);
\r
436 g_FuncTable.m_pfnSysFPrintf(SYS_WRN, "vfs directory not found: %s\n", path);
\r
442 // frees all memory that we allocated
\r
443 void vfsShutdown ()
\r
447 fclose ((FILE*)g_unzFiles->data);
\r
448 g_unzFiles = g_slist_remove (g_unzFiles, g_unzFiles->data);
\r
453 g_free (g_pakFiles->data);
\r
454 g_pakFiles = g_slist_remove (g_pakFiles, g_pakFiles->data);
\r
458 GSList* vfsGetFileList (const char *dir, const char *ext)
\r
460 return vfsGetListInternal (dir, ext, false);
\r
463 GSList* vfsGetDirList (const char *dir)
\r
465 return vfsGetListInternal (dir, NULL, true);
\r
468 void vfsClearFileDirList (GSList **lst)
\r
472 g_free ((*lst)->data);
\r
473 *lst = g_slist_remove (*lst, (*lst)->data);
\r
477 // return the number of files that match
\r
478 int vfsGetFileCount (const char *filename, int flag)
\r
481 char fixed[NAME_MAX], tmp[NAME_MAX];
\r
484 strcpy (fixed, filename);
\r
485 vfsFixDOSName (fixed);
\r
488 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
\r
490 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
\r
492 if (strcmp (file->entry.filename, fixed) == 0)
\r
496 for (i = 0; i < g_numDirs; i++)
\r
498 strcpy (tmp, g_strDirs[i]);
\r
499 strcat (tmp, fixed);
\r
500 if (access (tmp, R_OK) == 0)
\r
507 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
\r
508 int vfsLoadFile (const char *filename, void **bufferptr, int index)
\r
511 char tmp[NAME_MAX], fixed[NAME_MAX];
\r
515 strcpy (fixed, filename);
\r
516 vfsFixDOSName (fixed);
\r
519 for (i = 0; i < g_numDirs; i++)
\r
521 strcpy (tmp, g_strDirs[i]);
\r
522 strcat (tmp, filename);
\r
523 if (access (tmp, R_OK) == 0)
\r
525 if (count == index)
\r
530 f = fopen (tmp, "rb");
\r
534 fseek (f, 0, SEEK_END);
\r
538 *bufferptr = malloc (len+1);
\r
539 if (*bufferptr == NULL)
\r
542 fread (*bufferptr, 1, len, f);
\r
545 // we need to end the buffer with a 0
\r
546 ((char*) (*bufferptr))[len] = 0;
\r
555 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
\r
557 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
\r
559 if (strcmp (file->entry.filename, fixed) != 0)
\r
562 if (count == index)
\r
564 fseek (file->pak, file->entry.offset, SEEK_SET);
\r
566 *bufferptr = malloc (file->entry.size+1);
\r
567 // we need to end the buffer with a 0
\r
568 ((char*) (*bufferptr))[file->entry.size] = 0;
\r
570 return fread (*bufferptr, 1, file->entry.size, file->pak);
\r
579 void vfsFreeFile (void *p)
\r
584 // open a full path file
\r
585 int vfsLoadFullPathFile (const char *filename, void **bufferptr)
\r
590 f = fopen (filename, "rb");
\r
594 fseek (f, 0, SEEK_END);
\r
598 *bufferptr = g_malloc (len+1);
\r
599 if (*bufferptr == NULL)
\r
602 fread (*bufferptr, 1, len, f);
\r
605 // we need to end the buffer with a 0
\r
606 ((char*) (*bufferptr))[len] = 0;
\r
611 void vfsCleanFileName(char *in)
\r
615 int n = strlen(in);
\r
616 if (in[n-1] == '/')
\r
620 const char* vfsBasePromptPath()
\r
623 static char* path = "C:";
\r
625 static char* path = "/";
\r
631 \param shorten will try to match against the short version
\r
632 http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=144
\r
633 recent switch back to short path names in project settings has broken some stuff
\r
634 with shorten == true, we will convert in to short version before looking for root
\r
635 FIXME WAAA .. the stuff below is much more simple on linux .. add appropriate #ifdef
\r
637 char* vfsExtractRelativePath_short(const char *in, bool shorten)
\r
640 char l_in[PATH_MAX];
\r
641 char check[PATH_MAX];
\r
642 static char out[PATH_MAX];
\r
646 Sys_Printf("vfsExtractRelativePath: %s\n", in);
\r
653 if (GetShortPathName(in, l_in, PATH_MAX) == 0)
\r
656 Sys_Printf("GetShortPathName failed\n");
\r
665 vfsCleanFileName(l_in);
\r
668 vfsCleanFileName(l_in);
\r
669 #endif // ifdef WIN32
\r
673 Sys_Printf("cleaned path: %s\n", l_in);
\r
676 for (i = 0; i < g_numDirs; i++)
\r
678 strcpy(check,g_strDirs[i]);
\r
679 vfsCleanFileName(check);
\r
681 Sys_Printf("Matching against %s\n", check);
\r
684 // try to find a match
\r
685 if (strstr(l_in, check))
\r
687 strcpy(out,l_in+strlen(check)+1);
\r
695 Sys_Printf("vfsExtractRelativePath: success\n");
\r
700 Sys_Printf("vfsExtractRelativePath: failed\n");
\r
705 // HYDRA: this now searches VFS/PAK files in addition to the filesystem
\r
706 // if FLAG is unspecified then ONLY dirs are searched.
\r
707 // PAK's are searched before DIRs to mimic engine behaviour
\r
708 // index is ignored when searching PAK files.
\r
709 // see ifilesystem.h
\r
710 char* vfsGetFullPath(const char *in, int index, int flag)
\r
713 static char out[PATH_MAX];
\r
714 char tmp[NAME_MAX];
\r
717 if (flag & VFS_SEARCH_PAK)
\r
719 char fixed[NAME_MAX];
\r
722 strcpy (fixed, in);
\r
723 vfsFixDOSName (fixed);
\r
726 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
\r
728 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
\r
730 char *ptr,*lastptr;
\r
731 lastptr = file->name;
\r
733 while (ptr = strchr(lastptr,'/'))
\r
736 if (strcmp (lastptr, fixed) == 0)
\r
738 strncpy(out,file->name,PATH_MAX);
\r
745 if (!flag || (flag & VFS_SEARCH_DIR))
\r
747 for (i = 0; i < g_numDirs; i++)
\r
749 strcpy (tmp, g_strDirs[i]);
\r
751 if (access (tmp, R_OK) == 0)
\r
753 if (count == index)
\r
765 // FIXME TTimo: this and the above should be merged at some point
\r
766 char* vfsExtractRelativePath(const char *in)
\r
768 static char out[PATH_MAX];
\r
769 unsigned int i, count;
\r
770 char *chunk, *backup = NULL; // those point to out stuff
\r
771 char *ret = vfsExtractRelativePath_short(in, false);
\r
775 Sys_Printf("trying with a short version\n");
\r
777 ret = vfsExtractRelativePath_short(in, true);
\r
780 // ok, but we have a relative short version now
\r
781 // hack the long relative version out of here
\r
783 for(i=0;i<strlen(ret);i++)
\r
788 // this is the clean, not short version
\r
790 vfsCleanFileName(out);
\r
791 for(i=0;i<=count;i++)
\r
793 chunk = strrchr(out, '/');
\r