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
73 unsigned long filenumber;
77 // =============================================================================
80 static GSList* g_wadFiles;
81 static GSList* g_pakFiles;
82 static char g_strDirs[VFS_MAXDIRS][PATH_MAX];
85 // =============================================================================
88 static void vfsAddSlash (char *str)
93 if (str[n-1] != '\\' && str[n-1] != '/')
98 static void vfsFixDOSName (char *src)
111 //FIXME: STUPID short filenames.. get RID of it asap
112 // copied verbatim from qe3.cpp
113 int vfsBuildShortPathName(const char* pPath, char* pBuffer, int nBufferLen)
117 int nResult = GetFullPathName(pPath, nBufferLen, pBuffer, &pFile);
118 nResult = GetShortPathName(pPath, pBuffer, nBufferLen);
120 strcpy(pBuffer, pPath); // Use long filename
124 #if defined (__linux__) || defined (__APPLE__)
126 // remove /../ from directories
127 const char *scr = pPath; char *dst = pBuffer;
128 for (int i = 0; (i < nBufferLen) && (*scr != 0); i++)
130 if (*scr == '/' && *(scr+1) == '.' && *(scr+2) == '.')
133 while (dst != pBuffer && *(--dst) != '/')
145 return strlen (pBuffer);
149 static void vfsInitPakFile (const char *filename)
155 char wadname[NAME_MAX];
157 wf = wadOpen (filename);
160 g_FuncTable.m_pfnSysFPrintf(SYS_WRN, " failed to init wad file %s\n", filename);
163 g_FuncTable.m_pfnSysPrintf(" wad file: %s\n", filename);
165 for (i = strlen(filename)-1 ; i >= 0 && filename[i] != '\\' && filename[i] != '/' ; i --)
166 wadnameptr = (char *)filename + i;
168 strcpy(wadname,wadnameptr);
169 wadname[strlen(wadname)-4] = 0; // ditch the .wad so everthing looks nice!
171 g_wadFiles = g_slist_append (g_wadFiles, wf); // store the wadfile handle
173 wadGoToFirstFile(wf);
175 for (i = 0; i < wf->lpHeader->numlumps; i++)
177 char filename_inwad[NAME_MAX];
178 char filename_inwadfixed[NAME_MAX];
179 unsigned long filesize;
182 err = wadGetCurrentFileInfo (wf, filename_inwad, sizeof(filename_inwad) - 5, &filesize); // -5 for extension + null terminator
186 file = (VFS_PAKFILE*)g_malloc (sizeof (VFS_PAKFILE));
187 g_pakFiles = g_slist_append (g_pakFiles, file);
189 vfsFixDOSName (filename_inwad);
190 g_strdown (filename_inwad);
192 // texturenames in wad files don't have an extensions or paths, so we must add them!
193 if (wf->lpLump->type == WAD2_TYPE_MIP)
195 sprintf(filename_inwadfixed,"textures/%s/%s.mip",wadname,filename_inwad);
197 sprintf(filename_inwadfixed,"textures/%s/%s.hlw",wadname,filename_inwad);
200 //g_FuncTable.m_pfnSysFPrintf(SYS_WRN, " scanned %s\\%s\n", filename,filename_inwad);
202 file->name = g_strdup (filename_inwadfixed);
203 file->size = filesize;
204 file->filenumber = wf->currentfile;
206 memcpy(&file->wadlump, wf->lpLump, sizeof(WAD3_LUMP));
208 err = wadGoToNextFile(wf);
214 static GSList* vfsGetListInternal (const char *refdir, const char *ext, bool directories)
216 GSList *lst, *lst_aux, *files = NULL;
217 char dirname[NAME_MAX], extension[NAME_MAX], filename[NAME_MAX];
218 char basedir[NAME_MAX];
226 strcpy (dirname, refdir);
228 vfsFixDOSName (dirname);
229 vfsAddSlash (dirname);
232 dirlen = strlen (dirname);
235 strcpy (extension, ext);
238 g_strdown (extension);
240 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
242 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
243 gboolean found = FALSE;
246 // check that the file name begins with dirname
247 for (i = 0; (*ptr && i < dirlen); i++, ptr++)
248 if (*ptr != dirname[i])
256 char *sep = strchr (ptr, '/');
262 // check for duplicates
263 for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))
264 if (strncmp ((char*)lst_aux->data, ptr, i) == 0)
272 char *name = g_strndup (ptr, i+1);
274 files = g_slist_append (files, name);
279 char *ptr_ext = strrchr (ptr, '.');
280 if ((ext != NULL) && ((ptr_ext == NULL) || (strcmp (ptr_ext+1, extension) != 0)))
283 // check for duplicates
284 for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))
285 if (strcmp ((char*)lst_aux->data, ptr) == 0)
292 files = g_slist_append (files, g_strdup (ptr));
296 for (i = 0; i < g_numDirs; i++)
298 strcpy (basedir, g_strDirs[i]);
299 strcat (basedir, dirname);
301 GDir* dir = g_dir_open (basedir, 0, NULL);
307 const char* name = g_dir_read_name(dir);
311 if (directories && (name[0] == '.'))
314 sprintf (filename, "%s%s", basedir, name);
315 stat (filename, &st);
317 if ((S_ISDIR (st.st_mode) != 0) != directories)
320 gboolean found = FALSE;
322 char* direntry = g_strdup(name);
324 g_strdown (direntry);
326 char *ptr_ext = strrchr (direntry, '.');
329 || (ext != NULL && ptr_ext != NULL && ptr_ext[0] != '\0' && strcmp (ptr_ext+1, extension) == 0))
332 // check for duplicates
333 for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))
334 if (strcmp ((char*)lst_aux->data, direntry) == 0)
341 files = g_slist_append (files, g_strdup (direntry));
353 // =============================================================================
356 // reads all pak files from a dir
357 void vfsInitDirectory (const char *path)
359 char filename[PATH_MAX];
361 if (g_numDirs == (VFS_MAXDIRS-1))
364 strcpy (g_strDirs[g_numDirs], path);
365 vfsFixDOSName (g_strDirs[g_numDirs]);
366 vfsAddSlash (g_strDirs[g_numDirs]);
369 // if (g_PrefsDlg.m_bPAK)
370 // TODO: can't read prefs from a module, bah..
373 GDir* dir = g_dir_open (path, 0, NULL);
376 g_FuncTable.m_pfnSysPrintf("vfs directory: %s\n", path);
379 const char* name = g_dir_read_name(dir);
383 const char *ext = strrchr (name, '.');
384 if ((ext == NULL) || (strcmp (ext, ".wad") != 0))
387 sprintf (filename, "%s/%s", path, name);
388 vfsInitPakFile (filename);
392 g_FuncTable.m_pfnSysFPrintf(SYS_WRN, "vfs directory not found: %s\n", path);
396 // frees all memory that we allocated
397 // FIXME TTimo this should be improved so that we can shutdown and restart the VFS without exiting Radiant?
398 // (for instance when modifying the project settings)
403 wadCleanup((wadFile_t *)g_wadFiles->data);
404 g_wadFiles = g_slist_remove (g_wadFiles, g_wadFiles->data);
407 // avoid dangling pointer operation (makes BC hangry)
408 GSList *cur = g_pakFiles;
413 VFS_PAKFILE* file = (VFS_PAKFILE*)cur->data;
416 next = g_slist_remove (cur, file);
421 void vfsFreeFile (void *p)
426 GSList* vfsGetFileList (const char *dir, const char *ext)
428 return vfsGetListInternal (dir, ext, false);
431 GSList* vfsGetDirList (const char *dir)
433 return vfsGetListInternal (dir, NULL, true);
436 void vfsClearFileDirList (GSList **lst)
440 g_free ((*lst)->data);
441 *lst = g_slist_remove (*lst, (*lst)->data);
445 // return the number of files that match
446 int vfsGetFileCount (const char *filename, int flag)
449 char fixed[NAME_MAX], tmp[NAME_MAX];
452 strcpy (fixed, filename);
453 vfsFixDOSName (fixed);
456 if (!flag || (flag & VFS_SEARCH_PAK))
458 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
460 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
462 if (strcmp (file->name, fixed) == 0)
467 if (!flag || (flag & VFS_SEARCH_DIR))
469 for (i = 0; i < g_numDirs; i++)
471 strcpy (tmp, g_strDirs[i]);
473 if (access (tmp, R_OK) == 0)
481 // open a full path file
482 int vfsLoadFullPathFile (const char *filename, void **bufferptr)
487 f = fopen (filename, "rb");
491 fseek (f, 0, SEEK_END);
495 *bufferptr = g_malloc (len+1);
496 if (*bufferptr == NULL)
499 fread (*bufferptr, 1, len, f);
502 // we need to end the buffer with a 0
503 ((char*) (*bufferptr))[len] = 0;
508 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
509 int vfsLoadFile (const char *filename, void **bufferptr, int index)
512 char tmp[NAME_MAX], fixed[NAME_MAX];
516 strcpy (fixed, filename);
517 vfsFixDOSName (fixed);
520 for (i = 0; i < g_numDirs; i++)
522 strcpy (tmp, g_strDirs[i]);
523 strcat (tmp, filename);
524 if (access (tmp, R_OK) == 0)
528 return vfsLoadFullPathFile(tmp,bufferptr);
533 f = fopen (tmp, "rb");
537 fseek (f, 0, SEEK_END);
541 *bufferptr = g_malloc (len+1);
542 if (*bufferptr == NULL)
545 fread (*bufferptr, 1, len, f);
548 // we need to end the buffer with a 0
549 ((char*) (*bufferptr))[len] = 0;
560 // Textures in HalfLife wads don't have paths, but in the list of files
561 // we store the actual full paths of the files and what WAD they're in.
562 // so what we have to do is strip the paths and just compare filenames.
564 // Hydra: well, we did do this, but now we don't, as the map loader now
565 // fills in the correct paths for each texture.
573 for (i = strlen(fixed)-1 ; i >= 0 && fixed[i] != '\\' && fixed[i] != '/' ; i --)
574 fixedptr = (char *)fixed + i;
576 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
578 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
582 searchname = file->name;
583 for (i = strlen(file->name)-1 ; i >= 0 && file->name[i] != '\\' && file->name[i] != '/' ; i --)
584 searchname = (char *)file->name + i;
585 if (strcmp (searchname, fixedptr) != 0)
589 if (strcmp (file->name, fixed) != 0)
594 // Useful for debugging
595 //Sys_Printf("VFSWAD: reading from %s\n",file->wadfile->wadfilename);
597 if (wadOpenCurrentFileByNum (file->wadfile, file->filenumber) != 1)
600 *bufferptr = g_malloc (file->size+1);
601 // we need to end the buffer with a 0
602 ((char*) (*bufferptr))[file->size] = 0;
604 i = wadReadCurrentFile (file->wadfile , (char *)*bufferptr, file->size);
605 wadCloseCurrentFile (file->wadfile);
623 char* vfsExtractRelativePath(const char *in)
627 char check[PATH_MAX];
628 static char out[PATH_MAX];
632 Sys_Printf("vfsExtractRelativePath: %s\n", in);
636 vfsCleanFileName(l_in);
639 Sys_Printf("cleaned path: %s\n", l_in);
642 for (i = 0; i < g_numDirs; i++)
644 strcpy(check,g_strDirs[i]);
645 vfsCleanFileName(check);
647 Sys_Printf("Matching against %s\n", check);
650 // try to find a match
651 if (strstr(l_in, check))
653 strcpy(out,l_in+strlen(check)+1);
660 Sys_Printf("vfsExtractRelativePath: success\n");
665 Sys_Printf("vfsExtractRelativePath: failed\n");
670 // removed CString usage
671 void vfsCleanFileName(char *in)
674 vfsBuildShortPathName (in, str, PATH_MAX);
683 // HYDRA: this now searches VFS/PAK files in addition to the filesystem
684 // if FLAG is unspecified then ONLY dirs are searched.
685 // PAK's are searched before DIRs to mimic engine behaviour
686 // index is ignored when searching PAK files.
688 char* vfsGetFullPath(const char *in, int index, int flag)
691 static char out[PATH_MAX];
695 if (flag & VFS_SEARCH_PAK)
697 char fixed[NAME_MAX];
701 vfsFixDOSName (fixed);
704 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
706 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
709 lastptr = file->name;
711 while ((ptr = strchr(lastptr,'/')) != NULL)
714 if (strcmp (lastptr, fixed) == 0)
716 strncpy(out,file->name,PATH_MAX);
723 if (!flag || (flag & VFS_SEARCH_DIR))
725 for (i = 0; i < g_numDirs; i++)
727 strcpy (tmp, g_strDirs[i]);
729 if (access (tmp, R_OK) == 0)
743 // TODO TTimo on linux the base prompt is ~/.q3a/<fs_game>
744 // given the file dialog, we could push the strFSBasePath and ~/.q3a into the directory shortcuts
745 // FIXME TTimo is this really a VFS functionality?
746 // actually .. this should be the decision of the core isn't it?
747 // or .. add an API so that the base prompt can be set during VFS init
748 const char* vfsBasePromptPath()
751 static const char* path = "C:";
753 static const char* path = "/";