]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/vfspk3/vfs.cpp
transfer from internal tree r5311 branches/1.4-gpl
[xonotic/netradiant.git] / plugins / vfspk3 / vfs.cpp
1 /*\r
2 Copyright (c) 2001, Loki software, inc.\r
3 All rights reserved.\r
4 \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
7 \r
8 Redistributions of source code must retain the above copyright notice, this list \r
9 of conditions and the following disclaimer.\r
10 \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
14 \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
18 \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
29 */\r
30 \r
31 //\r
32 // Rules:\r
33 //\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
36 //\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
40 //\r
41 // Leonardo Zide (leo@lokigames.com)\r
42 //\r
43 \r
44 #include <glib.h>\r
45 #include <stdio.h>\r
46 \r
47 #if defined (__linux__) || defined (__APPLE__)\r
48   #include <dirent.h>\r
49   #include <unistd.h>\r
50 #else\r
51   #include <wtypes.h>\r
52   #include <io.h>\r
53   #define R_OK 04\r
54   #define S_ISDIR(mode) (mode & _S_IFDIR)\r
55 #endif\r
56 \r
57 // TTimo: String functions\r
58 //   see http://www.qeradiant.com/faq/index.cgi?file=175\r
59 #include "str.h"\r
60 \r
61 #include <stdlib.h>\r
62 #include <sys/stat.h>\r
63 \r
64 #include "vfspk3.h"\r
65 #include "vfs.h"\r
66 #include "unzip-vfspk3.h"\r
67 \r
68 typedef struct\r
69 {\r
70   char*   name;\r
71   unz_s   zipinfo;\r
72   unzFile zipfile;\r
73   guint32   size;\r
74 } VFS_PAKFILE;\r
75 \r
76 // =============================================================================\r
77 // Global variables\r
78 \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
84 \r
85 // =============================================================================\r
86 // Static functions\r
87 \r
88 static void vfsAddSlash (char *str)\r
89 {\r
90   int n = strlen (str);\r
91   if (n > 0)\r
92   {\r
93     if (str[n-1] != '\\' && str[n-1] != '/')\r
94       strcat (str, "/");\r
95   }\r
96 }\r
97 \r
98 static void vfsFixDOSName (char *src)\r
99 {\r
100   if (src == NULL)\r
101     return;\r
102 \r
103   while (*src)\r
104   {\r
105     if (*src == '\\')\r
106       *src = '/';\r
107     src++;\r
108   }\r
109 }\r
110 \r
111 static void vfsInitPakFile (const char *filename)\r
112 {\r
113   unz_global_info gi;\r
114   unzFile uf;\r
115   guint32 i;\r
116   int err;\r
117 \r
118   uf = unzOpen (filename);\r
119   if (uf == NULL)\r
120   {\r
121     g_FuncTable.m_pfnSysFPrintf(SYS_WRN, "  failed to init pak file %s\n", filename);\r
122     return;\r
123   }\r
124   g_FuncTable.m_pfnSysPrintf("  pak file: %s\n", filename);\r
125 \r
126   g_unzFiles = g_slist_append (g_unzFiles, uf);\r
127 \r
128   err = unzGetGlobalInfo (uf,&gi);\r
129   if (err != UNZ_OK)\r
130     return;\r
131   unzGoToFirstFile(uf);\r
132 \r
133   for (i = 0; i < gi.number_entry; i++)\r
134   {\r
135     char filename_inzip[NAME_MAX];\r
136     unz_file_info file_info;\r
137     VFS_PAKFILE* file;\r
138 \r
139     err = unzGetCurrentFileInfo (uf, &file_info, filename_inzip, sizeof(filename_inzip), NULL, 0, NULL, 0);\r
140     if (err != UNZ_OK)\r
141       break;\r
142 \r
143     file = (VFS_PAKFILE*)g_malloc (sizeof (VFS_PAKFILE));\r
144     g_pakFiles = g_slist_append (g_pakFiles, file);\r
145 \r
146     vfsFixDOSName (filename_inzip);\r
147     g_strdown (filename_inzip);\r
148 \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
153 \r
154     if ((i+1) < gi.number_entry)\r
155     {\r
156       err = unzGoToNextFile(uf);\r
157       if (err!=UNZ_OK)\r
158         break;\r
159     }\r
160   }\r
161 }\r
162 \r
163 static GSList* vfsGetListInternal (const char *refdir, const char *ext, bool directories)\r
164 {\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
168   int dirlen;\r
169   char *ptr;\r
170   char *dirlist;\r
171   struct stat st;\r
172   GDir *diskdir;\r
173   int i;\r
174 \r
175   if (refdir != NULL)\r
176   {\r
177     strcpy (dirname, refdir);\r
178     g_strdown (dirname);\r
179     vfsFixDOSName (dirname);\r
180     vfsAddSlash (dirname);\r
181   } else\r
182     dirname[0] = '\0';\r
183   dirlen = strlen (dirname);\r
184 \r
185   if (ext != NULL)\r
186     strcpy (extension, ext);\r
187   else\r
188     extension[0] = '\0';\r
189   g_strdown (extension);\r
190 \r
191   for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))\r
192   {\r
193     VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;\r
194     gboolean found = FALSE;\r
195     ptr = file->name;\r
196 \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
200         break;\r
201 \r
202     if (i != dirlen)\r
203       continue;\r
204 \r
205     if (directories)\r
206     {\r
207       char *sep = strchr (ptr, '/');\r
208       if (sep == NULL)\r
209         continue;\r
210 \r
211       i = sep-ptr;\r
212 \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
216         {\r
217           found = TRUE;\r
218           break;\r
219         }\r
220 \r
221       if (!found)\r
222       {\r
223         char *name = g_strndup (ptr, i+1);\r
224         name[i] = '\0';\r
225         files = g_slist_append (files, name);\r
226       }\r
227     } else\r
228     {\r
229       // check extension\r
230       char *ptr_ext = strrchr (ptr, '.');\r
231       if ((ext != NULL) && ((ptr_ext == NULL) || (strcmp (ptr_ext+1, extension) != 0)))\r
232         continue;\r
233 \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
237         {\r
238           found = TRUE;\r
239           break;\r
240         }\r
241 \r
242       if (!found)\r
243         files = g_slist_append (files, g_strdup (ptr));\r
244     }\r
245   }\r
246 \r
247   for (i = 0; i < g_numDirs; i++)\r
248   {\r
249     strcpy (basedir, g_strDirs[i]);\r
250     strcat (basedir, dirname);\r
251 \r
252     diskdir = g_dir_open (basedir, 0, NULL);\r
253 \r
254     if (diskdir != NULL)\r
255     {\r
256       while (1)\r
257       {\r
258         const char* name = g_dir_read_name(diskdir);\r
259         if(name == NULL)\r
260           break;\r
261 \r
262         if (directories && (name[0] == '.'))\r
263           continue;\r
264 \r
265         sprintf (filename, "%s%s", basedir, name);\r
266         stat (filename, &st);\r
267 \r
268         if ((S_ISDIR (st.st_mode) != 0) != directories)\r
269           continue;\r
270 \r
271         gboolean found = FALSE;\r
272 \r
273         dirlist = g_strdup(name);\r
274 \r
275         g_strdown (dirlist);\r
276 \r
277         char *ptr_ext = strrchr (dirlist, '.');\r
278         if(ext == NULL\r
279           || (ext != NULL && ptr_ext != NULL && ptr_ext[0] != '\0' && strcmp (ptr_ext+1, extension) == 0))\r
280         {\r
281 \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
285             {\r
286               found = TRUE;\r
287               break;\r
288             }\r
289 \r
290           if (!found)\r
291             files = g_slist_append (files, g_strdup (dirlist));\r
292         }\r
293 \r
294         g_free(dirlist);\r
295       }\r
296       g_dir_close (diskdir);\r
297     }\r
298   }\r
299 \r
300   return files;\r
301 }\r
302 \r
303 /*!\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
308 */\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
311 {\r
312         char    *s1, *s2;\r
313         int             c1, c2;\r
314 \r
315         s1 = (char*)a;\r
316         s2 = (char*)b;\r
317 \r
318         do {\r
319                 c1 = *s1++;\r
320                 c2 = *s2++;\r
321 \r
322                 if (c1 >= 'a' && c1 <= 'z')\r
323                 {\r
324                         c1 -= ('a' - 'A');\r
325                 }\r
326                 if (c2 >= 'a' && c2 <= 'z')\r
327                 {\r
328                         c2 -= ('a' - 'A');\r
329                 }\r
330 \r
331                 if ( c1 == '\\' || c1 == ':' )\r
332                 {\r
333                         c1 = '/';\r
334                 }\r
335                 if ( c2 == '\\' || c2 == ':' )\r
336                 {\r
337                         c2 = '/';\r
338                 }\r
339                 \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
345                 if (c1 < c2)\r
346                 {\r
347                         //return -1;            // strings not equal\r
348                         return 1;               // strings not equal\r
349                 }\r
350                 if (c1 > c2)\r
351                 {\r
352                         //return 1;\r
353                         return -1;\r
354                 }\r
355         } while (c1);\r
356         \r
357         return 0;               // strings are equal\r
358 }\r
359 \r
360 // =============================================================================\r
361 // Global functions\r
362 \r
363 // reads all pak files from a dir\r
364 /*!\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
368 */\r
369 void vfsInitDirectory (const char *path)\r
370 {\r
371   char filename[PATH_MAX];\r
372   GDir *dir;\r
373         GSList *dirlistptr, *dirlist = NULL;\r
374         int iGameMode; // 0: no filtering 1: SP 2: MP\r
375 \r
376   if (g_numDirs == (VFS_MAXDIRS-1))\r
377     return;\r
378 \r
379   // See if we are in "sp" or "mp" mapping mode\r
380   const char* gamemode = g_FuncTable.m_pfnReadProjectKey("gamemode");\r
381 \r
382         if (gamemode)\r
383         {\r
384                 if (strcmp (gamemode, "sp") == 0)\r
385                         iGameMode = 1;\r
386                 else if (strcmp (gamemode, "mp") == 0)\r
387                         iGameMode = 2;\r
388                 else\r
389                         iGameMode = 0;                  \r
390         } else\r
391                 iGameMode = 0;\r
392 \r
393   strcpy (g_strDirs[g_numDirs], path);\r
394   vfsFixDOSName (g_strDirs[g_numDirs]);\r
395   vfsAddSlash (g_strDirs[g_numDirs]);\r
396   g_numDirs++;\r
397 \r
398   if (g_bUsePak)\r
399   {\r
400     dir = g_dir_open (path, 0, NULL);\r
401 \r
402     if (dir != NULL)\r
403     {\r
404       g_FuncTable.m_pfnSysPrintf("vfs directory: %s\n", path);\r
405 \r
406       for(;;)\r
407       {\r
408         const char* name = g_dir_read_name(dir);\r
409         if(name == NULL)\r
410           break;\r
411 \r
412         char *ext = (char*)strrchr(name, '.');\r
413         if ((ext == NULL) || (strcasecmp (ext, ".pk3") != 0))\r
414           continue;\r
415 \r
416         char* direntry = g_strdup(name);\r
417 \r
418                                 // using the same kludge as in engine to ensure consistency\r
419                                 switch (iGameMode)\r
420                                 {\r
421                                 case 1: // SP\r
422                                         if (strncmp(direntry,"sp_",3) == 0)\r
423                                                 memcpy(direntry,"zz",2);\r
424                                         break;\r
425                                 case 2: // MP\r
426                                         if (strncmp(direntry,"mp_",3) == 0)\r
427                                                 memcpy(direntry,"zz",2);\r
428                                         break;\r
429                                 }\r
430 \r
431                                 dirlist = g_slist_append (dirlist, direntry);\r
432       }\r
433 \r
434       g_dir_close (dir);\r
435 \r
436                         // sort them\r
437                         dirlist = g_slist_sort (dirlist, vfsPakSort);\r
438 \r
439                         // add the entries to the vfs and free the list\r
440                         while (dirlist)\r
441                         {\r
442                                 GSList *cur = dirlist;\r
443                                 char* name = (char*)cur->data;\r
444 \r
445                                 switch (iGameMode)\r
446                                 {\r
447                                 case 1: // SP\r
448                                         if (strncmp(name,"mp_",3) == 0)\r
449                                         {\r
450                                                 g_free (name);\r
451                                                 dirlist = g_slist_remove (cur, name);\r
452                                                 continue;\r
453                                         } else if (strncmp(name,"zz_",3) == 0)\r
454                                                 memcpy(name,"sp",2);\r
455                                         break;\r
456                                 case 2: // MP\r
457                                         if (strncmp(name,"sp_",3) == 0)\r
458                                         {\r
459                                                 g_free (name);\r
460                                                 dirlist = g_slist_remove (cur, name);\r
461                                                 continue;\r
462                                         } else if (strncmp(name,"zz_",3) == 0)\r
463                                                 memcpy(name,"mp",2);\r
464                                         break;\r
465                                 }\r
466 \r
467                                 sprintf (filename, "%s/%s", path, name);\r
468         vfsInitPakFile (filename);\r
469 \r
470                                 g_free (name);\r
471                                 dirlist = g_slist_remove (cur, name);\r
472                         }\r
473     }\r
474     else\r
475       g_FuncTable.m_pfnSysFPrintf(SYS_WRN, "vfs directory not found: %s\n", path);\r
476   }\r
477 }\r
478 \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
483 {\r
484   while (g_unzFiles)\r
485   {\r
486     unzClose ((unzFile)g_unzFiles->data);\r
487     g_unzFiles = g_slist_remove (g_unzFiles, g_unzFiles->data);\r
488   }\r
489 \r
490   // avoid dangling pointer operation (makes BC hangry)\r
491   GSList *cur = g_pakFiles;\r
492   GSList *next = cur;\r
493   while (next)\r
494   {\r
495     cur = next;\r
496     VFS_PAKFILE* file = (VFS_PAKFILE*)cur->data;\r
497     g_free (file->name);\r
498     g_free (file);\r
499     next = g_slist_remove (cur, file);\r
500   }\r
501   g_pakFiles = NULL;\r
502 }\r
503 \r
504 void vfsFreeFile (void *p)\r
505 {\r
506   g_free(p);\r
507 }\r
508 \r
509 GSList* vfsGetFileList (const char *dir, const char *ext)\r
510 {\r
511   return vfsGetListInternal (dir, ext, false);\r
512 }\r
513 \r
514 GSList* vfsGetDirList (const char *dir)\r
515 {\r
516   return vfsGetListInternal (dir, NULL, true);\r
517 }\r
518 \r
519 void vfsClearFileDirList (GSList **lst)\r
520 {\r
521   while (*lst)\r
522   {\r
523     g_free ((*lst)->data);\r
524     *lst = g_slist_remove (*lst, (*lst)->data);\r
525   }\r
526 }\r
527 \r
528 int vfsGetFileCount (const char *filename, int flag)\r
529 {\r
530   int i, count = 0;\r
531   char fixed[NAME_MAX], tmp[NAME_MAX];\r
532   GSList *lst;\r
533 \r
534   strcpy (fixed, filename);\r
535   vfsFixDOSName (fixed);\r
536   g_strdown (fixed);\r
537 \r
538   if (!flag || (flag & VFS_SEARCH_PAK))\r
539   {\r
540     for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))\r
541     {\r
542       VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;\r
543       \r
544       if (strcmp (file->name, fixed) == 0)\r
545         count++;\r
546     }\r
547   }\r
548 \r
549   if (!flag || (flag & VFS_SEARCH_DIR))\r
550   {\r
551     for (i = 0; i < g_numDirs; i++)\r
552     {\r
553       strcpy (tmp, g_strDirs[i]);\r
554       strcat (tmp, fixed);\r
555       if (access (tmp, R_OK) == 0)\r
556         count++;\r
557     }\r
558   }\r
559 \r
560   return count;\r
561 }\r
562 \r
563 // open a full path file\r
564 int vfsLoadFullPathFile (const char *filename, void **bufferptr)\r
565 {\r
566   FILE *f;\r
567   long len;\r
568 \r
569   f = fopen (filename, "rb");\r
570   if (f == NULL)\r
571     return -1;\r
572 \r
573   fseek (f, 0, SEEK_END);\r
574   len = ftell (f);\r
575   rewind (f);\r
576 \r
577   *bufferptr = g_malloc (len+1);\r
578   if (*bufferptr == NULL)\r
579     return -1;\r
580 \r
581   fread (*bufferptr, 1, len, f);\r
582   fclose (f);\r
583 \r
584   // we need to end the buffer with a 0\r
585   ((char*) (*bufferptr))[len] = 0;\r
586 \r
587   return len;\r
588 }\r
589 \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
592 {\r
593   int i, count = 0;\r
594   char tmp[NAME_MAX], fixed[NAME_MAX];\r
595   GSList *lst;\r
596 \r
597   *bufferptr = NULL;\r
598   strcpy (fixed, filename);\r
599   vfsFixDOSName (fixed);\r
600   g_strdown (fixed);\r
601 \r
602   for (i = 0; i < g_numDirs; i++)\r
603   {\r
604     strcpy (tmp, g_strDirs[i]);\r
605     strcat (tmp, filename);\r
606     if (access (tmp, R_OK) == 0)\r
607     {\r
608       if (count == index)\r
609       {\r
610         return vfsLoadFullPathFile(tmp,bufferptr);\r
611       }\r
612 \r
613       count++;\r
614     }\r
615   }\r
616 \r
617   for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))\r
618   {\r
619     VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;\r
620 \r
621     if (strcmp (file->name, fixed) != 0)\r
622       continue;\r
623 \r
624     if (count == index)\r
625     {\r
626       memcpy (file->zipfile, &file->zipinfo, sizeof (unz_s));\r
627 \r
628       if (unzOpenCurrentFile (file->zipfile) != UNZ_OK)\r
629         return -1;\r
630 \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
634 \r
635       i = unzReadCurrentFile (file->zipfile , *bufferptr, file->size);\r
636       unzCloseCurrentFile (file->zipfile); \r
637       if (i > 0)\r
638         return file->size;\r
639       else\r
640         return -1;\r
641     }\r
642 \r
643     count++;\r
644   }\r
645 \r
646   return -1;\r
647 }\r
648 \r
649 //#ifdef _DEBUG\r
650 #if 1\r
651   #define DBG_RLTPATH\r
652 #endif\r
653 \r
654 /*!\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
660 */\r
661 char* vfsExtractRelativePath_short(const char *in, bool shorten)\r
662 {\r
663   int i;\r
664   char l_in[PATH_MAX];\r
665   char check[PATH_MAX];\r
666   static char out[PATH_MAX];\r
667   out[0] = 0;\r
668 \r
669 #ifdef DBG_RLTPATH\r
670   Sys_Printf("vfsExtractRelativePath: %s\n", in);\r
671 #endif\r
672 \r
673 #ifdef _WIN32  \r
674   if (shorten)\r
675   {\r
676     // make it short\r
677     if (GetShortPathName(in, l_in, PATH_MAX) == 0)\r
678     {\r
679 #ifdef DBG_RLTPATH\r
680       Sys_Printf("GetShortPathName failed\n");\r
681 #endif\r
682       return NULL;\r
683     }\r
684   }\r
685   else\r
686   {\r
687     strcpy(l_in,in);\r
688   }\r
689   vfsCleanFileName(l_in);\r
690 #else\r
691   strcpy(l_in, in);\r
692   vfsCleanFileName(l_in);\r
693 #endif // ifdef WIN32  \r
694 \r
695 \r
696 #ifdef DBG_RLTPATH\r
697   Sys_Printf("cleaned path: %s\n", l_in);\r
698 #endif\r
699 \r
700   for (i = 0; i < g_numDirs; i++)\r
701   {\r
702     strcpy(check,g_strDirs[i]);\r
703     vfsCleanFileName(check);\r
704 #ifdef DBG_RLTPATH\r
705     Sys_Printf("Matching against %s\n", check);\r
706 #endif\r
707 \r
708     // try to find a match\r
709     if (strstr(l_in, check))\r
710     {\r
711       strcpy(out,l_in+strlen(check)+1);\r
712       break;\r
713     }\r
714 \r
715   }\r
716   if (out[0]!=0)\r
717   {\r
718 #ifdef DBG_RLTPATH\r
719     Sys_Printf("vfsExtractRelativePath: success\n");\r
720 #endif\r
721     return out;\r
722   }\r
723 #ifdef DBG_RLTPATH\r
724   Sys_Printf("vfsExtractRelativePath: failed\n");\r
725 #endif\r
726   return NULL;\r
727 }\r
728 \r
729 \r
730 // FIXME TTimo: this and the above should be merged at some point\r
731 char* vfsExtractRelativePath(const char *in)\r
732 {\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
737   if (!ret)\r
738   {\r
739 #ifdef DBG_RLTPATH\r
740     Sys_Printf("trying with a short version\n");\r
741 #endif\r
742     ret = vfsExtractRelativePath_short(in, true);\r
743     if (ret)\r
744     {\r
745       // ok, but we have a relative short version now\r
746       // hack the long relative version out of here\r
747       count = 0;\r
748       for(i=0;i<strlen(ret);i++)\r
749       {\r
750         if (ret[i]=='/')\r
751           count++;\r
752       }\r
753       // this is the clean, not short version\r
754       strcpy(out, in);\r
755       vfsCleanFileName(out);\r
756       for(i=0;i<=count;i++)\r
757       {\r
758         chunk = strrchr(out, '/');\r
759         if (backup)\r
760           backup[0] = '/';\r
761         chunk[0] = '\0';\r
762         backup = chunk;\r
763       }\r
764       return chunk+1;\r
765     }\r
766   }\r
767   return ret;\r
768 }\r
769 \r
770 void vfsCleanFileName(char *in)\r
771 {\r
772   strlwr(in);\r
773   vfsFixDOSName(in);\r
774   int n = strlen(in);\r
775   if (in[n-1] == '/')\r
776     in[n-1] = '\0';\r
777 }\r
778 \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
785 {\r
786   int count = 0;\r
787   static char out[PATH_MAX];\r
788   char tmp[NAME_MAX];\r
789   int i;\r
790 \r
791   if (flag & VFS_SEARCH_PAK)\r
792   {\r
793     char fixed[NAME_MAX];\r
794     GSList *lst;\r
795 \r
796     strcpy (fixed, in);\r
797     vfsFixDOSName (fixed);\r
798     g_strdown (fixed);\r
799 \r
800     for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))\r
801     {\r
802       VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;\r
803 \r
804       char *ptr,*lastptr;\r
805       lastptr = file->name;\r
806 \r
807       while (ptr = strchr(lastptr,'/'))\r
808         lastptr = ptr+1;\r
809 \r
810       if (strcmp (lastptr, fixed) == 0)\r
811       {\r
812         strncpy(out,file->name,PATH_MAX);\r
813         return out;\r
814       }\r
815     }\r
816 \r
817   }\r
818 \r
819   if (!flag || (flag & VFS_SEARCH_DIR))\r
820   {\r
821   for (i = 0; i < g_numDirs; i++)\r
822   {\r
823     strcpy (tmp, g_strDirs[i]);\r
824     strcat (tmp, in);\r
825     if (access (tmp, R_OK) == 0)\r
826     {\r
827       if (count == index)\r
828       {\r
829         strcpy (out, tmp);\r
830         return out;\r
831       }\r
832       count++;\r
833     }\r
834   }\r
835   }\r
836   return NULL;\r
837 }\r
838 \r
839 \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
846 {\r
847 #ifdef _WIN32  \r
848   static char* path = "C:";\r
849 #else\r
850   static char* path = "/";\r
851 #endif\r
852   return path;\r
853 }\r
854 \r