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
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
73 unsigned long filenumber;
\r
77 // =============================================================================
\r
80 static GSList* g_wadFiles;
\r
81 static GSList* g_pakFiles;
\r
82 static char g_strDirs[VFS_MAXDIRS][PATH_MAX];
\r
83 static int g_numDirs;
\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 //FIXME: STUPID short filenames.. get RID of it asap
\r
112 // copied verbatim from qe3.cpp
\r
113 int vfsBuildShortPathName(const char* pPath, char* pBuffer, int nBufferLen)
\r
116 char *pFile = NULL;
\r
117 int nResult = GetFullPathName(pPath, nBufferLen, pBuffer, &pFile);
\r
118 nResult = GetShortPathName(pPath, pBuffer, nBufferLen);
\r
120 strcpy(pBuffer, pPath); // Use long filename
\r
124 #if defined (__linux__) || defined (__APPLE__)
\r
126 // remove /../ from directories
\r
127 const char *scr = pPath; char *dst = pBuffer;
\r
128 for (int i = 0; (i < nBufferLen) && (*scr != 0); i++)
\r
130 if (*scr == '/' && *(scr+1) == '.' && *(scr+2) == '.')
\r
133 while (dst != pBuffer && *(--dst) != '/')
\r
145 return strlen (pBuffer);
\r
149 static void vfsInitPakFile (const char *filename)
\r
155 char wadname[NAME_MAX];
\r
157 wf = wadOpen (filename);
\r
160 g_FuncTable.m_pfnSysFPrintf(SYS_WRN, " failed to init wad file %s\n", filename);
\r
163 g_FuncTable.m_pfnSysPrintf(" wad file: %s\n", filename);
\r
165 for (i = strlen(filename)-1 ; i >= 0 && filename[i] != '\\' && filename[i] != '/' ; i --)
\r
166 wadnameptr = (char *)filename + i;
\r
168 strcpy(wadname,wadnameptr);
\r
169 wadname[strlen(wadname)-4] = 0; // ditch the .wad so everthing looks nice!
\r
171 g_wadFiles = g_slist_append (g_wadFiles, wf); // store the wadfile handle
\r
173 wadGoToFirstFile(wf);
\r
175 for (i = 0; i < wf->lpHeader->numlumps; i++)
\r
177 char filename_inwad[NAME_MAX];
\r
178 char filename_inwadfixed[NAME_MAX];
\r
179 unsigned long filesize;
\r
182 err = wadGetCurrentFileInfo (wf, filename_inwad, sizeof(filename_inwad) - 5, &filesize); // -5 for extension + null terminator
\r
186 file = (VFS_PAKFILE*)g_malloc (sizeof (VFS_PAKFILE));
\r
187 g_pakFiles = g_slist_append (g_pakFiles, file);
\r
189 vfsFixDOSName (filename_inwad);
\r
190 g_strdown (filename_inwad);
\r
192 // texturenames in wad files don't have an extensions or paths, so we must add them!
\r
193 if (wf->lpLump->type == WAD2_TYPE_MIP)
\r
195 sprintf(filename_inwadfixed,"textures/%s/%s.mip",wadname,filename_inwad);
\r
197 sprintf(filename_inwadfixed,"textures/%s/%s.hlw",wadname,filename_inwad);
\r
200 //g_FuncTable.m_pfnSysFPrintf(SYS_WRN, " scanned %s\\%s\n", filename,filename_inwad);
\r
202 file->name = g_strdup (filename_inwadfixed);
\r
203 file->size = filesize;
\r
204 file->filenumber = wf->currentfile;
\r
205 file->wadfile = wf;
\r
206 memcpy(&file->wadlump, wf->lpLump, sizeof(WAD3_LUMP));
\r
208 err = wadGoToNextFile(wf);
\r
214 static GSList* vfsGetListInternal (const char *refdir, const char *ext, bool directories)
\r
216 GSList *lst, *lst_aux, *files = NULL;
\r
217 char dirname[NAME_MAX], extension[NAME_MAX], filename[NAME_MAX];
\r
218 char basedir[NAME_MAX];
\r
224 if (refdir != NULL)
\r
226 strcpy (dirname, refdir);
\r
227 g_strdown (dirname);
\r
228 vfsFixDOSName (dirname);
\r
229 vfsAddSlash (dirname);
\r
232 dirlen = strlen (dirname);
\r
235 strcpy (extension, ext);
\r
237 extension[0] = '\0';
\r
238 g_strdown (extension);
\r
240 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
\r
242 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
\r
243 gboolean found = FALSE;
\r
246 // check that the file name begins with dirname
\r
247 for (i = 0; (*ptr && i < dirlen); i++, ptr++)
\r
248 if (*ptr != dirname[i])
\r
256 char *sep = strchr (ptr, '/');
\r
262 // check for duplicates
\r
263 for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))
\r
264 if (strncmp ((char*)lst_aux->data, ptr, i) == 0)
\r
272 char *name = g_strndup (ptr, i+1);
\r
274 files = g_slist_append (files, name);
\r
279 char *ptr_ext = strrchr (ptr, '.');
\r
280 if ((ext != NULL) && ((ptr_ext == NULL) || (strcmp (ptr_ext+1, extension) != 0)))
\r
283 // check for duplicates
\r
284 for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))
\r
285 if (strcmp ((char*)lst_aux->data, ptr) == 0)
\r
292 files = g_slist_append (files, g_strdup (ptr));
\r
296 for (i = 0; i < g_numDirs; i++)
\r
298 strcpy (basedir, g_strDirs[i]);
\r
299 strcat (basedir, dirname);
\r
301 GDir* dir = g_dir_open (basedir, 0, NULL);
\r
307 const char* name = g_dir_read_name(dir);
\r
311 if (directories && (name[0] == '.'))
\r
314 sprintf (filename, "%s%s", basedir, name);
\r
315 stat (filename, &st);
\r
317 if ((S_ISDIR (st.st_mode) != 0) != directories)
\r
320 gboolean found = FALSE;
\r
322 char* direntry = g_strdup(name);
\r
324 g_strdown (direntry);
\r
326 char *ptr_ext = strrchr (direntry, '.');
\r
329 || (ext != NULL && ptr_ext != NULL && ptr_ext[0] != '\0' && strcmp (ptr_ext+1, extension) == 0))
\r
332 // check for duplicates
\r
333 for (lst_aux = files; lst_aux; lst_aux = g_slist_next (lst_aux))
\r
334 if (strcmp ((char*)lst_aux->data, direntry) == 0)
\r
341 files = g_slist_append (files, g_strdup (direntry));
\r
353 // =============================================================================
\r
354 // Global functions
\r
356 // reads all pak files from a dir
\r
357 void vfsInitDirectory (const char *path)
\r
359 char filename[PATH_MAX];
\r
361 if (g_numDirs == (VFS_MAXDIRS-1))
\r
364 strcpy (g_strDirs[g_numDirs], path);
\r
365 vfsFixDOSName (g_strDirs[g_numDirs]);
\r
366 vfsAddSlash (g_strDirs[g_numDirs]);
\r
369 // if (g_PrefsDlg.m_bPAK)
\r
370 // TODO: can't read prefs from a module, bah..
\r
373 GDir* dir = g_dir_open (path, 0, NULL);
\r
376 g_FuncTable.m_pfnSysPrintf("vfs directory: %s\n", path);
\r
379 const char* name = g_dir_read_name(dir);
\r
383 char *ext = strrchr (name, '.');
\r
384 if ((ext == NULL) || (strcmp (ext, ".wad") != 0))
\r
387 sprintf (filename, "%s/%s", path, name);
\r
388 vfsInitPakFile (filename);
\r
392 g_FuncTable.m_pfnSysFPrintf(SYS_WRN, "vfs directory not found: %s\n", path);
\r
396 // frees all memory that we allocated
\r
397 // FIXME TTimo this should be improved so that we can shutdown and restart the VFS without exiting Radiant?
\r
398 // (for instance when modifying the project settings)
\r
399 void vfsShutdown ()
\r
405 wadCleanup((wadFile_t *)g_wadFiles->data);
\r
406 g_wadFiles = g_slist_remove (g_wadFiles, g_wadFiles->data);
\r
409 // avoid dangling pointer operation (makes BC hangry)
\r
410 GSList *cur = g_pakFiles;
\r
411 GSList *next = cur;
\r
415 VFS_PAKFILE* file = (VFS_PAKFILE*)cur->data;
\r
416 g_free (file->name);
\r
418 next = g_slist_remove (cur, file);
\r
423 void vfsFreeFile (void *p)
\r
428 GSList* vfsGetFileList (const char *dir, const char *ext)
\r
430 return vfsGetListInternal (dir, ext, false);
\r
433 GSList* vfsGetDirList (const char *dir)
\r
435 return vfsGetListInternal (dir, NULL, true);
\r
438 void vfsClearFileDirList (GSList **lst)
\r
442 g_free ((*lst)->data);
\r
443 *lst = g_slist_remove (*lst, (*lst)->data);
\r
447 // return the number of files that match
\r
448 int vfsGetFileCount (const char *filename, int flag)
\r
451 char fixed[NAME_MAX], tmp[NAME_MAX];
\r
454 strcpy (fixed, filename);
\r
455 vfsFixDOSName (fixed);
\r
458 if (!flag || (flag & VFS_SEARCH_PAK))
\r
460 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
\r
462 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
\r
464 if (strcmp (file->name, fixed) == 0)
\r
469 if (!flag || (flag & VFS_SEARCH_DIR))
\r
471 for (i = 0; i < g_numDirs; i++)
\r
473 strcpy (tmp, g_strDirs[i]);
\r
474 strcat (tmp, fixed);
\r
475 if (access (tmp, R_OK) == 0)
\r
483 // open a full path file
\r
484 int vfsLoadFullPathFile (const char *filename, void **bufferptr)
\r
489 f = fopen (filename, "rb");
\r
493 fseek (f, 0, SEEK_END);
\r
497 *bufferptr = g_malloc (len+1);
\r
498 if (*bufferptr == NULL)
\r
501 fread (*bufferptr, 1, len, f);
\r
504 // we need to end the buffer with a 0
\r
505 ((char*) (*bufferptr))[len] = 0;
\r
510 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
\r
511 int vfsLoadFile (const char *filename, void **bufferptr, int index)
\r
514 char tmp[NAME_MAX], fixed[NAME_MAX];
\r
518 strcpy (fixed, filename);
\r
519 vfsFixDOSName (fixed);
\r
522 for (i = 0; i < g_numDirs; i++)
\r
524 strcpy (tmp, g_strDirs[i]);
\r
525 strcat (tmp, filename);
\r
526 if (access (tmp, R_OK) == 0)
\r
528 if (count == index)
\r
530 return vfsLoadFullPathFile(tmp,bufferptr);
\r
535 f = fopen (tmp, "rb");
\r
539 fseek (f, 0, SEEK_END);
\r
543 *bufferptr = g_malloc (len+1);
\r
544 if (*bufferptr == NULL)
\r
547 fread (*bufferptr, 1, len, f);
\r
550 // we need to end the buffer with a 0
\r
551 ((char*) (*bufferptr))[len] = 0;
\r
562 // Textures in HalfLife wads don't have paths, but in the list of files
\r
563 // we store the actual full paths of the files and what WAD they're in.
\r
564 // so what we have to do is strip the paths and just compare filenames.
\r
566 // Hydra: well, we did do this, but now we don't, as the map loader now
\r
567 // fills in the correct paths for each texture.
\r
575 for (i = strlen(fixed)-1 ; i >= 0 && fixed[i] != '\\' && fixed[i] != '/' ; i --)
\r
576 fixedptr = (char *)fixed + i;
\r
578 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
\r
580 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
\r
584 searchname = file->name;
\r
585 for (i = strlen(file->name)-1 ; i >= 0 && file->name[i] != '\\' && file->name[i] != '/' ; i --)
\r
586 searchname = (char *)file->name + i;
\r
587 if (strcmp (searchname, fixedptr) != 0)
\r
591 if (strcmp (file->name, fixed) != 0)
\r
594 if (count == index)
\r
596 // Useful for debugging
\r
597 //Sys_Printf("VFSWAD: reading from %s\n",file->wadfile->wadfilename);
\r
599 if (wadOpenCurrentFileByNum (file->wadfile, file->filenumber) != 1)
\r
602 *bufferptr = g_malloc (file->size+1);
\r
603 // we need to end the buffer with a 0
\r
604 ((char*) (*bufferptr))[file->size] = 0;
\r
606 i = wadReadCurrentFile (file->wadfile , (char *)*bufferptr, file->size);
\r
607 wadCloseCurrentFile (file->wadfile);
\r
622 #define DBG_RLTPATH
\r
625 char* vfsExtractRelativePath(const char *in)
\r
628 char l_in[PATH_MAX];
\r
629 char check[PATH_MAX];
\r
630 static char out[PATH_MAX];
\r
634 Sys_Printf("vfsExtractRelativePath: %s\n", in);
\r
638 vfsCleanFileName(l_in);
\r
641 Sys_Printf("cleaned path: %s\n", l_in);
\r
644 for (i = 0; i < g_numDirs; i++)
\r
646 strcpy(check,g_strDirs[i]);
\r
647 vfsCleanFileName(check);
\r
649 Sys_Printf("Matching against %s\n", check);
\r
652 // try to find a match
\r
653 if (strstr(l_in, check))
\r
655 strcpy(out,l_in+strlen(check)+1);
\r
662 Sys_Printf("vfsExtractRelativePath: success\n");
\r
667 Sys_Printf("vfsExtractRelativePath: failed\n");
\r
672 // removed CString usage
\r
673 void vfsCleanFileName(char *in)
\r
675 char str[PATH_MAX];
\r
676 vfsBuildShortPathName (in, str, PATH_MAX);
\r
678 vfsFixDOSName(str);
\r
679 int n = strlen(str);
\r
680 if (str[n-1] == '/')
\r
685 // HYDRA: this now searches VFS/PAK files in addition to the filesystem
\r
686 // if FLAG is unspecified then ONLY dirs are searched.
\r
687 // PAK's are searched before DIRs to mimic engine behaviour
\r
688 // index is ignored when searching PAK files.
\r
689 // see ifilesystem.h
\r
690 char* vfsGetFullPath(const char *in, int index, int flag)
\r
693 static char out[PATH_MAX];
\r
694 char tmp[NAME_MAX];
\r
697 if (flag & VFS_SEARCH_PAK)
\r
699 char fixed[NAME_MAX];
\r
702 strcpy (fixed, in);
\r
703 vfsFixDOSName (fixed);
\r
706 for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
\r
708 VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
\r
710 char *ptr,*lastptr;
\r
711 lastptr = file->name;
\r
713 while (ptr = strchr(lastptr,'/'))
\r
716 if (strcmp (lastptr, fixed) == 0)
\r
718 strncpy(out,file->name,PATH_MAX);
\r
725 if (!flag || (flag & VFS_SEARCH_DIR))
\r
727 for (i = 0; i < g_numDirs; i++)
\r
729 strcpy (tmp, g_strDirs[i]);
\r
731 if (access (tmp, R_OK) == 0)
\r
733 if (count == index)
\r
745 // TODO TTimo on linux the base prompt is ~/.q3a/<fs_game>
\r
746 // given the file dialog, we could push the strFSBasePath and ~/.q3a into the directory shortcuts
\r
747 // FIXME TTimo is this really a VFS functionality?
\r
748 // actually .. this should be the decision of the core isn't it?
\r
749 // or .. add an API so that the base prompt can be set during VFS init
\r
750 const char* vfsBasePromptPath()
\r
753 static char* path = "C:";
\r
755 static char* path = "/";
\r