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