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)
47 #if defined (__linux__) || defined (__APPLE__)
54 #define S_ISDIR(mode) (mode & _S_IFDIR)
57 // TTimo: String functions
58 // see http://www.qeradiant.com/faq/index.cgi?file=175
66 #include "unzip-vfspk3.h"
76 // =============================================================================
79 static GSList* g_unzFiles;
80 static GSList* g_pakFiles;
81 static char g_strDirs[VFS_MAXDIRS][PATH_MAX];
83 static bool 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 static void vfsInitPakFile (const char *filename)
118 uf = unzOpen (filename);
121 g_FuncTable.m_pfnSysFPrintf(SYS_WRN, " failed to init pak file %s\n", filename);
124 g_FuncTable.m_pfnSysPrintf(" pak file: %s\n", filename);
126 g_unzFiles = g_slist_append (g_unzFiles, uf);
128 err = unzGetGlobalInfo (uf,&gi);
131 unzGoToFirstFile(uf);
133 for (i = 0; i < gi.number_entry; i++)
135 char filename_inzip[NAME_MAX];
136 unz_file_info file_info;
139 err = unzGetCurrentFileInfo (uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0);
143 file = (VFS_PAKFILE*)g_malloc (sizeof (VFS_PAKFILE));
144 g_pakFiles = g_slist_append (g_pakFiles, file);
146 vfsFixDOSName (filename_inzip);
147 g_strdown (filename_inzip);
149 file->name = g_strdup (filename_inzip);
150 file->size = file_info.uncompressed_size;
152 memcpy (&file->zipinfo, uf, sizeof (unz_s));
154 if ((i+1) < gi.number_entry)
156 err = unzGoToNextFile(uf);
163 static GSList* vfsGetListInternal (const char *refdir, const char *ext, bool directories)
165 GSList *lst, *lst_aux, *files = NULL;
166 char dirname[NAME_MAX], extension[NAME_MAX], filename[NAME_MAX];
167 char basedir[NAME_MAX];
177 strcpy (dirname, refdir);
179 vfsFixDOSName (dirname);
180 vfsAddSlash (dirname);
183 dirlen = strlen (dirname);
186 strcpy (extension, ext);
189 g_strdown (extension);
191 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
193 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
194 gboolean found = FALSE;
197 // check that the file name begins with dirname
198 for (i = 0; (*ptr && i < dirlen); i++, ptr++)
199 if (*ptr != dirname[i])
207 char *sep = strchr (ptr, '/');
213 // check for duplicates
214 for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))
215 if (strncmp ((char*)lst_aux->data, ptr, i) == 0)
223 char *name = g_strndup (ptr, i+1);
225 files = g_slist_append (files, name);
230 char *ptr_ext = strrchr (ptr, '.');
231 if ((ext != NULL) && ((ptr_ext == NULL) || (strcmp (ptr_ext+1, extension) != 0)))
234 // check for duplicates
235 for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))
236 if (strcmp ((char*)lst_aux->data, ptr) == 0)
243 files = g_slist_append (files, g_strdup (ptr));
247 for (i = 0; i < g_numDirs; i++)
249 strcpy (basedir, g_strDirs[i]);
250 strcat (basedir, dirname);
252 diskdir = g_dir_open (basedir, 0, NULL);
258 const char* name = g_dir_read_name(diskdir);
262 if (directories && (name[0] == '.'))
265 sprintf (filename, "%s%s", basedir, name);
266 stat (filename, &st);
268 if ((S_ISDIR (st.st_mode) != 0) != directories)
271 gboolean found = FALSE;
273 dirlist = g_strdup(name);
277 char *ptr_ext = strrchr (dirlist, '.');
279 || (ext != NULL && ptr_ext != NULL && ptr_ext[0] != '\0' && strcmp (ptr_ext+1, extension) == 0))
282 // check for duplicates
283 for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))
284 if (strcmp ((char*)lst_aux->data, dirlist) == 0)
291 files = g_slist_append (files, g_strdup (dirlist));
296 g_dir_close (diskdir);
304 This behaves identically to -stricmp(a,b), except that ASCII chars
305 [\]^`_ come AFTER alphabet chars instead of before. This is because
306 it effectively converts all alphabet chars to uppercase before comparison,
307 while stricmp converts them to lowercase.
309 //!\todo Analyse the code in rtcw/q3 to see how it behaves.
310 static int vfsPakSort (const void *a, const void *b)
322 if (c1 >= 'a' && c1 <= 'z')
326 if (c2 >= 'a' && c2 <= 'z')
331 if ( c1 == '\\' || c1 == ':' )
335 if ( c2 == '\\' || c2 == ':' )
340 // Arnout: note - sort pakfiles in reverse order. This ensures that
341 // later pakfiles override earlier ones. This because the vfs module
342 // returns a filehandle to the first file it can find (while it should
343 // return the filehandle to the file in the most overriding pakfile, the
344 // last one in the list that is).
347 //return -1; // strings not equal
348 return 1; // strings not equal
357 return 0; // strings are equal
360 // =============================================================================
363 // reads all pak files from a dir
365 The gamemode hacks in here will do undefined things with files called zz_*.
366 This is simple to fix by cleaning up the hacks, but may be better left alone
367 if the engine code does the same thing.
369 void vfsInitDirectory (const char *path)
371 char filename[PATH_MAX];
373 GSList *dirlistptr, *dirlist = NULL;
374 int iGameMode; // 0: no filtering 1: SP 2: MP
376 if (g_numDirs == (VFS_MAXDIRS-1))
379 // See if we are in "sp" or "mp" mapping mode
380 const char* gamemode = g_FuncTable.m_pfnReadProjectKey("gamemode");
384 if (strcmp (gamemode, "sp") == 0)
386 else if (strcmp (gamemode, "mp") == 0)
393 strcpy (g_strDirs[g_numDirs], path);
394 vfsFixDOSName (g_strDirs[g_numDirs]);
395 vfsAddSlash (g_strDirs[g_numDirs]);
400 dir = g_dir_open (path, 0, NULL);
404 g_FuncTable.m_pfnSysPrintf("vfs directory: %s\n", path);
408 const char* name = g_dir_read_name(dir);
412 char *ext = (char*)strrchr(name, '.');
413 if ((ext == NULL) || (strcasecmp (ext, ".pk3") != 0))
416 char* direntry = g_strdup(name);
418 // using the same kludge as in engine to ensure consistency
422 if (strncmp(direntry,"sp_",3) == 0)
423 memcpy(direntry,"zz",2);
426 if (strncmp(direntry,"mp_",3) == 0)
427 memcpy(direntry,"zz",2);
431 dirlist = g_slist_append (dirlist, direntry);
437 dirlist = g_slist_sort (dirlist, vfsPakSort);
439 // add the entries to the vfs and free the list
442 GSList *cur = dirlist;
443 char* name = (char*)cur->data;
448 if (strncmp(name,"mp_",3) == 0)
451 dirlist = g_slist_remove (cur, name);
453 } else if (strncmp(name,"zz_",3) == 0)
457 if (strncmp(name,"sp_",3) == 0)
460 dirlist = g_slist_remove (cur, name);
462 } else if (strncmp(name,"zz_",3) == 0)
467 sprintf (filename, "%s/%s", path, name);
468 vfsInitPakFile (filename);
471 dirlist = g_slist_remove (cur, name);
475 g_FuncTable.m_pfnSysFPrintf(SYS_WRN, "vfs directory not found: %s\n", path);
479 // frees all memory that we allocated
480 // FIXME TTimo this should be improved so that we can shutdown and restart the VFS without exiting Radiant?
481 // (for instance when modifying the project settings)
486 unzClose ((unzFile)g_unzFiles->data);
487 g_unzFiles = g_slist_remove (g_unzFiles, g_unzFiles->data);
490 // avoid dangling pointer operation (makes BC hangry)
491 GSList *cur = g_pakFiles;
496 VFS_PAKFILE* file = (VFS_PAKFILE*)cur->data;
499 next = g_slist_remove (cur, file);
504 void vfsFreeFile (void *p)
509 GSList* vfsGetFileList (const char *dir, const char *ext)
511 return vfsGetListInternal (dir, ext, false);
514 GSList* vfsGetDirList (const char *dir)
516 return vfsGetListInternal (dir, NULL, true);
519 void vfsClearFileDirList (GSList **lst)
523 g_free ((*lst)->data);
524 *lst = g_slist_remove (*lst, (*lst)->data);
528 int vfsGetFileCount (const char *filename, int flag)
531 char fixed[NAME_MAX], tmp[NAME_MAX];
534 strcpy (fixed, filename);
535 vfsFixDOSName (fixed);
538 if (!flag || (flag & VFS_SEARCH_PAK))
540 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
542 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
544 if (strcmp (file->name, fixed) == 0)
549 if (!flag || (flag & VFS_SEARCH_DIR))
551 for (i = 0; i < g_numDirs; i++)
553 strcpy (tmp, g_strDirs[i]);
555 if (access (tmp, R_OK) == 0)
563 // open a full path file
564 int vfsLoadFullPathFile (const char *filename, void **bufferptr)
569 f = fopen (filename, "rb");
573 fseek (f, 0, SEEK_END);
577 *bufferptr = g_malloc (len+1);
578 if (*bufferptr == NULL)
581 fread (*bufferptr, 1, len, f);
584 // we need to end the buffer with a 0
585 ((char*) (*bufferptr))[len] = 0;
590 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
591 int vfsLoadFile (const char *filename, void **bufferptr, int index)
594 char tmp[NAME_MAX], fixed[NAME_MAX];
598 strcpy (fixed, filename);
599 vfsFixDOSName (fixed);
602 for (i = 0; i < g_numDirs; i++)
604 strcpy (tmp, g_strDirs[i]);
605 strcat (tmp, filename);
606 if (access (tmp, R_OK) == 0)
610 return vfsLoadFullPathFile(tmp,bufferptr);
617 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
619 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
621 if (strcmp (file->name, fixed) != 0)
626 memcpy (file->zipfile, &file->zipinfo, sizeof (unz_s));
628 if (unzOpenCurrentFile (file->zipfile) != UNZ_OK)
631 *bufferptr = g_malloc (file->size+1);
632 // we need to end the buffer with a 0
633 ((char*) (*bufferptr))[file->size] = 0;
635 i = unzReadCurrentFile (file->zipfile , *bufferptr, file->size);
636 unzCloseCurrentFile (file->zipfile);
655 \param shorten will try to match against the short version
656 http://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=144
657 recent switch back to short path names in project settings has broken some stuff
658 with shorten == true, we will convert in to short version before looking for root
659 FIXME WAAA .. the stuff below is much more simple on linux .. add appropriate #ifdef
661 char* vfsExtractRelativePath_short(const char *in, bool shorten)
665 char check[PATH_MAX];
666 static char out[PATH_MAX];
670 Sys_Printf("vfsExtractRelativePath: %s\n", in);
677 if (GetShortPathName(in, l_in, PATH_MAX) == 0)
680 Sys_Printf("GetShortPathName failed\n");
689 vfsCleanFileName(l_in);
692 vfsCleanFileName(l_in);
693 #endif // ifdef WIN32
697 Sys_Printf("cleaned path: %s\n", l_in);
700 for (i = 0; i < g_numDirs; i++)
702 strcpy(check,g_strDirs[i]);
703 vfsCleanFileName(check);
705 Sys_Printf("Matching against %s\n", check);
708 // try to find a match
709 if (strstr(l_in, check))
711 strcpy(out,l_in+strlen(check)+1);
719 Sys_Printf("vfsExtractRelativePath: success\n");
724 Sys_Printf("vfsExtractRelativePath: failed\n");
730 // FIXME TTimo: this and the above should be merged at some point
731 char* vfsExtractRelativePath(const char *in)
733 static char out[PATH_MAX];
734 unsigned int i, count;
735 char *chunk, *backup = NULL; // those point to out stuff
736 char *ret = vfsExtractRelativePath_short(in, false);
740 Sys_Printf("trying with a short version\n");
742 ret = vfsExtractRelativePath_short(in, true);
745 // ok, but we have a relative short version now
746 // hack the long relative version out of here
748 for(i=0;i<strlen(ret);i++)
753 // this is the clean, not short version
755 vfsCleanFileName(out);
756 for(i=0;i<=count;i++)
758 chunk = strrchr(out, '/');
770 void vfsCleanFileName(char *in)
779 // HYDRA: this now searches VFS/PAK files in addition to the filesystem
780 // if FLAG is unspecified then ONLY dirs are searched.
781 // PAK's are searched before DIRs to mimic engine behaviour
782 // index is ignored when searching PAK files.
784 char* vfsGetFullPath(const char *in, int index, int flag)
787 static char out[PATH_MAX];
791 if (flag & VFS_SEARCH_PAK)
793 char fixed[NAME_MAX];
797 vfsFixDOSName (fixed);
800 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
802 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
805 lastptr = file->name;
807 while (ptr = strchr(lastptr,'/'))
810 if (strcmp (lastptr, fixed) == 0)
812 strncpy(out,file->name,PATH_MAX);
819 if (!flag || (flag & VFS_SEARCH_DIR))
821 for (i = 0; i < g_numDirs; i++)
823 strcpy (tmp, g_strDirs[i]);
825 if (access (tmp, R_OK) == 0)
840 // TODO TTimo on linux the base prompt is ~/.q3a/<fs_game>
841 // given the file dialog, we could push the strFSBasePath and ~/.q3a into the directory shortcuts
842 // FIXME TTimo is this really a VFS functionality?
843 // actually .. this should be the decision of the core isn't it?
844 // or .. add an API so that the base prompt can be set during VFS init
845 const char* vfsBasePromptPath()
848 static char* path = "C:";
850 static char* path = "/";