]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/vfswad/vfs.cpp
more eol-style
[xonotic/netradiant.git] / plugins / vfswad / vfs.cpp
1 /*
2 Copyright (c) 2001, Loki software, inc.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list
9 of conditions and the following disclaimer.
10
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.
14
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
17 written permission.
18
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.
29 */
30
31 //
32 // Rules:
33 //
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).
36 //
37 // - Pak files are searched first inside the directories.
38 // - Case insensitive.
39 // - Unix-style slashes (/) (windows is backwards .. everyone knows that)
40 //
41 // Leonardo Zide (leo@lokigames.com)
42 //
43
44 #include <glib.h>
45 #include <stdio.h>
46
47 #if defined (__linux__) || defined (__APPLE__)
48   #include <dirent.h>
49   #include <unistd.h>
50 #else
51   #include <wtypes.h>
52   #include <io.h>
53   #define R_OK 04
54   #define S_ISDIR(mode) (mode & _S_IFDIR)
55 #endif
56
57 // TTimo: String functions
58 //   see http://www.qeradiant.com/faq/index.cgi?file=175
59 #include "str.h"
60
61 #include <stdlib.h>
62 #include <sys/stat.h>
63
64 #include "vfswad.h"
65 #include "vfs.h"
66 #include "unwad.h"
67
68 typedef struct
69 {
70   char*         name;
71   WAD3_LUMP     wadlump;
72   wadFile_t     *wadfile;
73   unsigned long filenumber;
74   unsigned long size;
75 } VFS_PAKFILE;
76
77 // =============================================================================
78 // Global variables
79
80 static GSList* g_wadFiles;
81 static GSList* g_pakFiles;
82 static char    g_strDirs[VFS_MAXDIRS][PATH_MAX];
83 static int     g_numDirs;
84
85 // =============================================================================
86 // Static functions
87
88 static void vfsAddSlash (char *str)
89 {
90   int n = strlen (str);
91   if (n > 0)
92   {
93     if (str[n-1] != '\\' && str[n-1] != '/')
94       strcat (str, "/");
95   }
96 }
97
98 static void vfsFixDOSName (char *src)
99 {
100   if (src == NULL)
101     return;
102
103   while (*src)
104   {
105     if (*src == '\\')
106       *src = '/';
107     src++;
108   }
109 }
110
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)
114 {
115 #ifdef _WIN32
116   char *pFile = NULL;
117   int nResult = GetFullPathName(pPath, nBufferLen, pBuffer, &pFile);
118   nResult = GetShortPathName(pPath, pBuffer, nBufferLen);
119   if (nResult == 0)
120     strcpy(pBuffer, pPath);                     // Use long filename
121   return nResult;
122 #endif
123
124 #if defined (__linux__) || defined (__APPLE__)
125
126   // remove /../ from directories
127   const char *scr = pPath; char *dst = pBuffer;
128   for (int i = 0; (i < nBufferLen) && (*scr != 0); i++)
129   {
130     if (*scr == '/' && *(scr+1) == '.' && *(scr+2) == '.')
131     {
132       scr += 3;
133       while (dst != pBuffer && *(--dst) != '/')
134       {
135         i--;
136       }
137     }
138
139     *dst = *scr;
140
141     scr++; dst++;
142   }
143   *dst = 0;
144
145   return strlen (pBuffer);
146 #endif
147 }
148
149 static void vfsInitPakFile (const char *filename)
150 {
151   wadFile_t *wf;
152   unsigned int i;
153   int err;
154   char *wadnameptr;
155   char wadname[NAME_MAX];
156
157   wf = wadOpen (filename);
158   if (wf == NULL)
159   {
160     g_FuncTable.m_pfnSysFPrintf(SYS_WRN, "  failed to init wad file %s\n", filename);
161     return;
162   }
163   g_FuncTable.m_pfnSysPrintf("  wad file: %s\n", filename);
164
165   for (i = strlen(filename)-1 ; i >= 0 && filename[i] != '\\' && filename[i] != '/' ; i --)
166     wadnameptr = (char *)filename + i;
167
168   strcpy(wadname,wadnameptr);
169   wadname[strlen(wadname)-4] = 0; // ditch the .wad so everthing looks nice!
170
171   g_wadFiles = g_slist_append (g_wadFiles, wf); // store the wadfile handle
172
173   wadGoToFirstFile(wf);
174
175   for (i = 0; i < wf->lpHeader->numlumps; i++)
176   {
177     char filename_inwad[NAME_MAX];
178     char filename_inwadfixed[NAME_MAX];
179     unsigned long filesize;
180     VFS_PAKFILE* file;
181
182     err = wadGetCurrentFileInfo (wf, filename_inwad, sizeof(filename_inwad) - 5, &filesize); // -5 for extension + null terminator
183     if (err != 1)
184       break;
185
186     file = (VFS_PAKFILE*)g_malloc (sizeof (VFS_PAKFILE));
187     g_pakFiles = g_slist_append (g_pakFiles, file);
188
189     vfsFixDOSName (filename_inwad);
190     g_strdown (filename_inwad);
191
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)
194     {
195       sprintf(filename_inwadfixed,"textures/%s/%s.mip",wadname,filename_inwad);
196     }else {
197       sprintf(filename_inwadfixed,"textures/%s/%s.hlw",wadname,filename_inwad);
198     }
199
200     //g_FuncTable.m_pfnSysFPrintf(SYS_WRN, "  scanned %s\\%s\n", filename,filename_inwad);
201
202     file->name = g_strdup (filename_inwadfixed);
203     file->size = filesize;
204     file->filenumber = wf->currentfile;
205     file->wadfile = wf;
206     memcpy(&file->wadlump, wf->lpLump, sizeof(WAD3_LUMP));
207
208     err = wadGoToNextFile(wf);
209     if (err != 1)
210       break;
211   }
212 }
213
214 static GSList* vfsGetListInternal (const char *refdir, const char *ext, bool directories)
215 {
216   GSList *lst, *lst_aux, *files = NULL;
217   char dirname[NAME_MAX], extension[NAME_MAX], filename[NAME_MAX];
218   char basedir[NAME_MAX];
219   int dirlen;
220   char *ptr;
221   struct stat st;
222   int i;
223
224   if (refdir != NULL)
225   {
226     strcpy (dirname, refdir);
227     g_strdown (dirname);
228     vfsFixDOSName (dirname);
229     vfsAddSlash (dirname);
230   } else
231     dirname[0] = '\0';
232   dirlen = strlen (dirname);
233
234   if (ext != NULL)
235     strcpy (extension, ext);
236   else
237     extension[0] = '\0';
238   g_strdown (extension);
239
240   for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
241   {
242     VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
243     gboolean found = FALSE;
244     ptr = file->name;
245
246     // check that the file name begins with dirname
247     for (i = 0; (*ptr && i < dirlen); i++, ptr++)
248       if (*ptr != dirname[i])
249         break;
250
251     if (i != dirlen)
252       continue;
253
254     if (directories)
255     {
256       char *sep = strchr (ptr, '/');
257       if (sep == NULL)
258         continue;
259
260       i = sep-ptr;
261
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)
265         {
266           found = TRUE;
267           break;
268         }
269
270       if (!found)
271       {
272         char *name = g_strndup (ptr, i+1);
273         name[i] = '\0';
274         files = g_slist_append (files, name);
275       }
276     } else
277     {
278       // check extension
279       char *ptr_ext = strrchr (ptr, '.');
280       if ((ext != NULL) && ((ptr_ext == NULL) || (strcmp (ptr_ext+1, extension) != 0)))
281         continue;
282
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)
286         {
287           found = TRUE;
288           break;
289         }
290
291       if (!found)
292         files = g_slist_append (files, g_strdup (ptr));
293     }
294   }
295
296   for (i = 0; i < g_numDirs; i++)
297   {
298     strcpy (basedir, g_strDirs[i]);
299     strcat (basedir, dirname);
300
301     GDir* dir = g_dir_open (basedir, 0, NULL);
302
303     if (dir != NULL)
304     {
305       for(;;)
306       {
307         const char* name = g_dir_read_name(dir);
308         if(name == NULL)
309           break;
310
311         if (directories && (name[0] == '.'))
312           continue;
313
314         sprintf (filename, "%s%s", basedir, name);
315         stat (filename, &st);
316
317         if ((S_ISDIR (st.st_mode) != 0) != directories)
318           continue;
319
320         gboolean found = FALSE;
321
322         char* direntry = g_strdup(name);
323
324         g_strdown (direntry);
325
326         char *ptr_ext = strrchr (direntry, '.');
327
328         if(ext == NULL
329           || (ext != NULL && ptr_ext != NULL && ptr_ext[0] != '\0' && strcmp (ptr_ext+1, extension) == 0))
330         {
331
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)
335             {
336               found = TRUE;
337               break;
338             }
339
340           if (!found)
341             files = g_slist_append (files, g_strdup (direntry));
342         }
343
344         g_free(direntry);
345       }
346       g_dir_close(dir);
347     }
348   }
349
350   return files;
351 }
352
353 // =============================================================================
354 // Global functions
355
356 // reads all pak files from a dir
357 void vfsInitDirectory (const char *path)
358 {
359   char filename[PATH_MAX];
360
361   if (g_numDirs == (VFS_MAXDIRS-1))
362     return;
363
364   strcpy (g_strDirs[g_numDirs], path);
365   vfsFixDOSName (g_strDirs[g_numDirs]);
366   vfsAddSlash (g_strDirs[g_numDirs]);
367   g_numDirs++;
368
369 //  if (g_PrefsDlg.m_bPAK)
370   // TODO: can't read prefs from a module, bah..
371   if (1)
372   {
373     GDir* dir = g_dir_open (path, 0, NULL);
374     if (dir != NULL)
375     {
376       g_FuncTable.m_pfnSysPrintf("vfs directory: %s\n", path);
377       while (1)
378       {
379         const char* name = g_dir_read_name(dir);
380         if(name == NULL)
381           break;
382
383         char *ext = strrchr (name, '.');
384         if ((ext == NULL) || (strcmp (ext, ".wad") != 0))
385           continue;
386
387         sprintf (filename, "%s/%s", path, name);
388         vfsInitPakFile (filename);
389       }
390       g_dir_close (dir);
391     } else
392       g_FuncTable.m_pfnSysFPrintf(SYS_WRN, "vfs directory not found: %s\n", path);
393   }
394 }
395
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)
399 void vfsShutdown ()
400 {
401   wadFile_t *tmpptr;
402
403   while (g_wadFiles)
404   {
405     wadCleanup((wadFile_t *)g_wadFiles->data);
406     g_wadFiles = g_slist_remove (g_wadFiles, g_wadFiles->data);
407   }
408
409   // avoid dangling pointer operation (makes BC hangry)
410   GSList *cur = g_pakFiles;
411   GSList *next = cur;
412   while (next)
413   {
414     cur = next;
415     VFS_PAKFILE* file = (VFS_PAKFILE*)cur->data;
416     g_free (file->name);
417     g_free (file);
418     next = g_slist_remove (cur, file);
419   }
420   g_pakFiles = NULL;
421 }
422
423 void vfsFreeFile (void *p)
424 {
425   g_free(p);
426 }
427
428 GSList* vfsGetFileList (const char *dir, const char *ext)
429 {
430   return vfsGetListInternal (dir, ext, false);
431 }
432
433 GSList* vfsGetDirList (const char *dir)
434 {
435   return vfsGetListInternal (dir, NULL, true);
436 }
437
438 void vfsClearFileDirList (GSList **lst)
439 {
440   while (*lst)
441   {
442     g_free ((*lst)->data);
443     *lst = g_slist_remove (*lst, (*lst)->data);
444   }
445 }
446
447 // return the number of files that match
448 int vfsGetFileCount (const char *filename, int flag)
449 {
450   int i, count = 0;
451   char fixed[NAME_MAX], tmp[NAME_MAX];
452   GSList *lst;
453
454   strcpy (fixed, filename);
455   vfsFixDOSName (fixed);
456   g_strdown (fixed);
457
458   if (!flag || (flag & VFS_SEARCH_PAK))
459   {
460     for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
461     {
462       VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
463
464       if (strcmp (file->name, fixed) == 0)
465         count++;
466     }
467   }
468
469   if (!flag || (flag & VFS_SEARCH_DIR))
470   {
471     for (i = 0; i < g_numDirs; i++)
472     {
473       strcpy (tmp, g_strDirs[i]);
474       strcat (tmp, fixed);
475       if (access (tmp, R_OK) == 0)
476         count++;
477     }
478   }
479
480   return count;
481 }
482
483 // open a full path file
484 int vfsLoadFullPathFile (const char *filename, void **bufferptr)
485 {
486   FILE *f;
487   long len;
488
489   f = fopen (filename, "rb");
490   if (f == NULL)
491     return -1;
492
493   fseek (f, 0, SEEK_END);
494   len = ftell (f);
495   rewind (f);
496
497   *bufferptr = g_malloc (len+1);
498   if (*bufferptr == NULL)
499     return -1;
500
501   fread (*bufferptr, 1, len, f);
502   fclose (f);
503
504   // we need to end the buffer with a 0
505   ((char*) (*bufferptr))[len] = 0;
506
507   return len;
508 }
509
510 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
511 int vfsLoadFile (const char *filename, void **bufferptr, int index)
512 {
513   int i, count = 0;
514   char tmp[NAME_MAX], fixed[NAME_MAX];
515   GSList *lst;
516
517   *bufferptr = NULL;
518   strcpy (fixed, filename);
519   vfsFixDOSName (fixed);
520   g_strdown (fixed);
521
522   for (i = 0; i < g_numDirs; i++)
523   {
524     strcpy (tmp, g_strDirs[i]);
525     strcat (tmp, filename);
526     if (access (tmp, R_OK) == 0)
527     {
528       if (count == index)
529       {
530         return vfsLoadFullPathFile(tmp,bufferptr);
531         /*
532   long len;
533   FILE *f;
534
535   f = fopen (tmp, "rb");
536   if (f == NULL)
537     return -1;
538
539   fseek (f, 0, SEEK_END);
540   len = ftell (f);
541   rewind (f);
542
543   *bufferptr = g_malloc (len+1);
544   if (*bufferptr == NULL)
545     return -1;
546
547   fread (*bufferptr, 1, len, f);
548   fclose (f);
549
550         // we need to end the buffer with a 0
551         ((char*) (*bufferptr))[len] = 0;
552
553   return len;
554   */
555       }
556
557       count++;
558     }
559   }
560
561
562   // Textures in HalfLife wads don't have paths, but in the list of files
563   // we store the actual full paths of the files and what WAD they're in.
564   // so what we have to do is strip the paths and just compare filenames.
565
566   // Hydra: well, we did do this, but now we don't, as the map loader now
567   // fills in the correct paths for each texture.
568
569   /*
570   char *searchname;
571   char *fixedptr;
572
573   fixedptr = fixed;
574
575   for (i = strlen(fixed)-1 ; i >= 0 && fixed[i] != '\\' && fixed[i] != '/' ; i --)
576     fixedptr = (char *)fixed + i;
577   */
578   for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
579   {
580     VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
581
582
583     /*
584     searchname = file->name;
585     for (i = strlen(file->name)-1 ; i >= 0 && file->name[i] != '\\' && file->name[i] != '/' ; i --)
586       searchname = (char *)file->name + i;
587     if (strcmp (searchname, fixedptr) != 0)
588       continue;
589     */
590
591     if (strcmp (file->name, fixed) != 0)
592       continue;
593
594     if (count == index)
595     {
596       // Useful for debugging
597       //Sys_Printf("VFSWAD: reading from %s\n",file->wadfile->wadfilename);
598
599       if (wadOpenCurrentFileByNum (file->wadfile, file->filenumber) != 1)
600         return -1;
601
602       *bufferptr = g_malloc (file->size+1);
603       // we need to end the buffer with a 0
604       ((char*) (*bufferptr))[file->size] = 0;
605
606       i = wadReadCurrentFile (file->wadfile , (char *)*bufferptr, file->size);
607       wadCloseCurrentFile (file->wadfile);
608       if (i > 0)
609         return file->size;
610       else
611         return -1;
612     }
613
614     count++;
615   }
616
617   return -1;
618 }
619
620 //#ifdef _DEBUG
621 #if 0
622   #define DBG_RLTPATH
623 #endif
624
625 char* vfsExtractRelativePath(const char *in)
626 {
627   int i;
628   char l_in[PATH_MAX];
629   char check[PATH_MAX];
630   static char out[PATH_MAX];
631   out[0] = 0;
632
633 #ifdef DBG_RLTPATH
634   Sys_Printf("vfsExtractRelativePath: %s\n", in);
635 #endif
636
637   strcpy(l_in,in);
638   vfsCleanFileName(l_in);
639
640 #ifdef DBG_RLTPATH
641   Sys_Printf("cleaned path: %s\n", l_in);
642 #endif
643
644   for (i = 0; i < g_numDirs; i++)
645   {
646     strcpy(check,g_strDirs[i]);
647     vfsCleanFileName(check);
648 #ifdef DBG_RLTPATH
649     Sys_Printf("Matching against %s\n", check);
650 #endif
651
652     // try to find a match
653     if (strstr(l_in, check))
654     {
655       strcpy(out,l_in+strlen(check)+1);
656       break;
657     }
658   }
659   if (out[0]!=0)
660   {
661 #ifdef DBG_RLTPATH
662     Sys_Printf("vfsExtractRelativePath: success\n");
663 #endif
664     return out;
665   }
666 #ifdef DBG_RLTPATH
667   Sys_Printf("vfsExtractRelativePath: failed\n");
668 #endif
669   return NULL;
670 }
671
672 // removed CString usage
673 void vfsCleanFileName(char *in)
674 {
675   char str[PATH_MAX];
676   vfsBuildShortPathName (in, str, PATH_MAX);
677   strlwr(str);
678   vfsFixDOSName(str);
679   int n = strlen(str);
680   if (str[n-1] == '/')
681     str[n-1] = '\0';
682   strcpy (in, str);
683 }
684
685 // HYDRA: this now searches VFS/PAK files in addition to the filesystem
686 // if FLAG is unspecified then ONLY dirs are searched.
687 // PAK's are searched before DIRs to mimic engine behaviour
688 // index is ignored when searching PAK files.
689 // see ifilesystem.h
690 char* vfsGetFullPath(const char *in, int index, int flag)
691 {
692   int count = 0;
693   static char out[PATH_MAX];
694   char tmp[NAME_MAX];
695   int i;
696
697   if (flag & VFS_SEARCH_PAK)
698   {
699     char fixed[NAME_MAX];
700     GSList *lst;
701
702     strcpy (fixed, in);
703     vfsFixDOSName (fixed);
704     g_strdown (fixed);
705
706     for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
707     {
708       VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
709
710       char *ptr,*lastptr;
711       lastptr = file->name;
712
713       while (ptr = strchr(lastptr,'/'))
714         lastptr = ptr+1;
715
716       if (strcmp (lastptr, fixed) == 0)
717       {
718         strncpy(out,file->name,PATH_MAX);
719         return out;
720       }
721     }
722
723   }
724
725   if (!flag || (flag & VFS_SEARCH_DIR))
726   {
727   for (i = 0; i < g_numDirs; i++)
728   {
729     strcpy (tmp, g_strDirs[i]);
730     strcat (tmp, in);
731     if (access (tmp, R_OK) == 0)
732     {
733       if (count == index)
734       {
735         strcpy (out, tmp);
736         return out;
737       }
738       count++;
739     }
740   }
741   }
742   return NULL;
743 }
744
745 // TODO TTimo on linux the base prompt is ~/.q3a/<fs_game>
746 // given the file dialog, we could push the strFSBasePath and ~/.q3a into the directory shortcuts
747 // FIXME TTimo is this really a VFS functionality?
748 //   actually .. this should be the decision of the core isn't it?
749 //   or .. add an API so that the base prompt can be set during VFS init
750 const char* vfsBasePromptPath()
751 {
752 #ifdef _WIN32
753   static char* path = "C:";
754 #else
755   static char* path = "/";
756 #endif
757   return path;
758 }
759