]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/vfspk3/vfs.cpp
- Updated UFA:Plugin (mattn2)
[xonotic/netradiant.git] / plugins / vfspk3 / 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 "vfs.h"
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <glib/gslist.h>
49 #include <glib/gdir.h>
50 #include <glib/gstrfuncs.h>
51
52 #include "qerplugin.h"
53 #include "idatastream.h"
54 #include "iarchive.h"
55 ArchiveModules& FileSystemQ3API_getArchiveModules();
56 #include "ifilesystem.h"
57
58 #include "generic/callback.h"
59 #include "string/string.h"
60 #include "stream/stringstream.h"
61 #include "os/path.h"
62 #include "moduleobservers.h"
63
64
65 #define VFS_MAXDIRS 8
66
67 #if defined(WIN32)
68 #define PATH_MAX 260
69 #endif
70
71 #define gamemode_get GlobalRadiant().getGameMode
72
73
74
75 // =============================================================================
76 // Global variables
77
78 Archive* OpenArchive(const char* name);
79
80 struct archive_entry_t
81 {
82   CopiedString name;
83   Archive* archive;
84   bool is_pakfile;
85 };
86
87 #include <list>
88
89 typedef std::list<archive_entry_t> archives_t;
90
91 static archives_t g_archives;
92 static char    g_strDirs[VFS_MAXDIRS][PATH_MAX+1];
93 static int     g_numDirs;
94 static bool    g_bUsePak = true;
95
96 ModuleObservers g_observers;
97
98 // =============================================================================
99 // Static functions
100
101 static void AddSlash (char *str)
102 {
103   std::size_t n = strlen (str);
104   if (n > 0)
105   {
106     if (str[n-1] != '\\' && str[n-1] != '/')
107     {
108       globalErrorStream() << "WARNING: directory path does not end with separator: " << str << "\n";
109       strcat (str, "/");
110     }
111   }
112 }
113
114 static void FixDOSName (char *src)
115 {
116   if (src == 0 || strchr(src, '\\') == 0)
117     return;
118
119   globalErrorStream() << "WARNING: invalid path separator '\\': " << src << "\n";
120
121   while (*src)
122   {
123     if (*src == '\\')
124       *src = '/';
125     src++;
126   }
127 }
128
129
130
131 const _QERArchiveTable* GetArchiveTable(ArchiveModules& archiveModules, const char* ext)
132 {
133   StringOutputStream tmp(16);
134   tmp << LowerCase(ext);
135   return archiveModules.findModule(tmp.c_str());
136 }
137 static void InitPakFile (ArchiveModules& archiveModules, const char *filename)
138 {
139   const _QERArchiveTable* table = GetArchiveTable(archiveModules, path_get_extension(filename));
140
141   if(table != 0)
142   {
143     archive_entry_t entry;
144     entry.name = filename;
145
146     entry.archive = table->m_pfnOpenArchive(filename);
147     entry.is_pakfile = true;
148     g_archives.push_back(entry);
149     globalOutputStream() << "  pak file: " << filename << "\n";
150   }
151 }
152
153 inline void pathlist_prepend_unique(GSList*& pathlist, char* path)
154 {
155   if(g_slist_find_custom(pathlist, path, (GCompareFunc)path_compare) == 0)
156   {
157     pathlist = g_slist_prepend(pathlist, path);
158   }
159   else
160   {
161     g_free(path);
162   }
163 }
164
165 class DirectoryListVisitor : public Archive::Visitor
166 {
167   GSList*& m_matches;
168   const char* m_directory;
169 public:
170   DirectoryListVisitor(GSList*& matches, const char* directory)
171     : m_matches(matches), m_directory(directory)
172   {}
173   void visit(const char* name)
174   {
175     const char* subname = path_make_relative(name, m_directory);
176     if(subname != name)
177     {
178       if(subname[0] == '/')
179         ++subname;
180       char* dir = g_strdup(subname);
181       char* last_char = dir + strlen(dir);
182       if(last_char != dir && *(--last_char) == '/')
183         *last_char = '\0';
184       pathlist_prepend_unique(m_matches, dir);
185     }
186   }
187 };
188
189 class FileListVisitor : public Archive::Visitor
190 {
191   GSList*& m_matches;
192   const char* m_directory;
193   const char* m_extension;
194 public:
195   FileListVisitor(GSList*& matches, const char* directory, const char* extension)
196     : m_matches(matches), m_directory(directory), m_extension(extension)
197   {}
198   void visit(const char* name)
199   {
200     const char* subname = path_make_relative(name, m_directory);
201     if(subname != name)
202     {
203       if(subname[0] == '/')
204         ++subname;
205       if(m_extension[0] == '*' || extension_equal(path_get_extension(subname), m_extension))
206         pathlist_prepend_unique(m_matches, g_strdup (subname));
207     }
208   }
209 };
210     
211 static GSList* GetListInternal (const char *refdir, const char *ext, bool directories, std::size_t depth)
212 {
213   GSList* files = 0;
214
215   ASSERT_MESSAGE(refdir[strlen(refdir) - 1] == '/', "search path does not end in '/'");
216
217   if(directories)
218   {
219     for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
220     {
221       DirectoryListVisitor visitor(files, refdir);
222       (*i).archive->forEachFile(Archive::VisitorFunc(visitor, Archive::eDirectories, depth), refdir);
223     }
224   }
225   else
226   {
227     for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
228     {
229       FileListVisitor visitor(files, refdir, ext);
230       (*i).archive->forEachFile(Archive::VisitorFunc(visitor, Archive::eFiles, depth), refdir);
231     }
232   }
233
234   files = g_slist_reverse(files);
235
236   return files;
237 }
238
239 inline int ascii_to_upper(int c)
240 {
241   if (c >= 'a' && c <= 'z')
242         {
243                 return c - ('a' - 'A');
244         }
245   return c;
246 }
247
248 /*!
249 This behaves identically to stricmp(a,b), except that ASCII chars
250 [\]^`_ come AFTER alphabet chars instead of before. This is because
251 it converts all alphabet chars to uppercase before comparison,
252 while stricmp converts them to lowercase.
253 */
254 static int string_compare_nocase_upper(const char* a, const char* b)
255 {
256         for(;;)
257   {
258                 int c1 = ascii_to_upper(*a++);
259                 int c2 = ascii_to_upper(*b++);
260
261                 if (c1 < c2)
262                 {
263                         return -1; // a < b
264                 }
265                 if (c1 > c2)
266                 {
267                         return 1; // a > b
268                 }
269     if(c1 == 0)
270     {
271       return 0; // a == b
272     }
273         }       
274 }
275
276 // Arnout: note - sort pakfiles in reverse order. This ensures that
277 // later pakfiles override earlier ones. This because the vfs module
278 // returns a filehandle to the first file it can find (while it should
279 // return the filehandle to the file in the most overriding pakfile, the
280 // last one in the list that is).
281
282 //!\todo Analyse the code in rtcw/q3 to see which order it sorts pak files.
283 class PakLess
284 {
285 public:
286   bool operator()(const CopiedString& self, const CopiedString& other) const
287   {
288     return string_compare_nocase_upper(self.c_str(), other.c_str()) > 0;
289   }
290 };
291
292 typedef std::set<CopiedString, PakLess> Archives;
293
294 // =============================================================================
295 // Global functions
296
297 // reads all pak files from a dir
298 void InitDirectory(const char* directory, ArchiveModules& archiveModules)
299 {
300   if (g_numDirs == (VFS_MAXDIRS-1))
301     return;
302
303   strncpy(g_strDirs[g_numDirs], directory, PATH_MAX);
304   g_strDirs[g_numDirs][PATH_MAX] = '\0';
305   FixDOSName (g_strDirs[g_numDirs]);
306   AddSlash (g_strDirs[g_numDirs]);
307
308   const char* path = g_strDirs[g_numDirs];
309   
310   g_numDirs++;
311
312   {
313     archive_entry_t entry;
314     entry.name = path;
315     entry.archive = OpenArchive(path);
316     entry.is_pakfile = false;
317     g_archives.push_back(entry);
318   }
319
320   if (g_bUsePak)
321   {
322     GDir* dir = g_dir_open (path, 0, 0);
323
324     if (dir != 0)
325     {
326                         globalOutputStream() << "vfs directory: " << path << "\n";
327
328       const char* ignore_prefix = "";
329       const char* override_prefix = "";
330
331       {
332         // See if we are in "sp" or "mp" mapping mode
333         const char* gamemode = gamemode_get();
334
335                     if (strcmp (gamemode, "sp") == 0)
336         {
337                                   ignore_prefix = "mp_";
338           override_prefix = "sp_";
339         }
340                     else if (strcmp (gamemode, "mp") == 0)
341         {
342                                   ignore_prefix = "sp_";
343           override_prefix = "mp_";
344         }
345       }
346
347       Archives archives;
348       Archives archivesOverride;
349       for(;;)
350       {
351         const char* name = g_dir_read_name(dir);
352         if(name == 0)
353           break;
354
355         const char *ext = strrchr (name, '.');
356         if ((ext == 0) || *(++ext) == '\0' || GetArchiveTable(archiveModules, ext) == 0)
357           continue;
358
359         // using the same kludge as in engine to ensure consistency
360                                 if(!string_empty(ignore_prefix) && strncmp(name, ignore_prefix, strlen(ignore_prefix)) == 0)
361                                 {
362                                         continue;
363                                 }
364                                 if(!string_empty(override_prefix) && strncmp(name, override_prefix, strlen(override_prefix)) == 0)
365         {
366           archivesOverride.insert(name);
367                                         continue;
368         }
369
370         archives.insert(name);
371       }
372
373       g_dir_close (dir);
374
375                         // add the entries to the vfs
376       for(Archives::iterator i = archivesOverride.begin(); i != archivesOverride.end(); ++i)
377                         {
378         char filename[PATH_MAX];
379         strcpy(filename, path);
380         strcat(filename, (*i).c_str());
381         InitPakFile(archiveModules, filename);
382                         }
383       for(Archives::iterator i = archives.begin(); i != archives.end(); ++i)
384                         {
385         char filename[PATH_MAX];
386         strcpy(filename, path);
387         strcat(filename, (*i).c_str());
388         InitPakFile(archiveModules, filename);
389                         }
390     }
391     else
392     {
393       globalErrorStream() << "vfs directory not found: " << path << "\n";
394     }
395   }
396 }
397
398 // frees all memory that we allocated
399 // FIXME TTimo this should be improved so that we can shutdown and restart the VFS without exiting Radiant?
400 //   (for instance when modifying the project settings)
401 void Shutdown()
402 {
403   for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
404   {
405     (*i).archive->release();
406   }
407   g_archives.clear();
408
409   g_numDirs = 0;
410 }
411
412 #define VFS_SEARCH_PAK 0x1
413 #define VFS_SEARCH_DIR 0x2
414
415 int GetFileCount (const char *filename, int flag)
416 {
417   int count = 0;
418   char fixed[PATH_MAX+1];
419
420   strncpy(fixed, filename, PATH_MAX);
421   fixed[PATH_MAX] = '\0';
422   FixDOSName (fixed);
423
424   if(!flag)
425     flag = VFS_SEARCH_PAK | VFS_SEARCH_DIR;
426
427   for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
428   {
429     if((*i).is_pakfile && (flag & VFS_SEARCH_PAK) != 0
430       || !(*i).is_pakfile && (flag & VFS_SEARCH_DIR) != 0)
431     {
432       if((*i).archive->containsFile(fixed))
433         ++count;
434     }
435   }
436
437   return count;
438 }
439
440 ArchiveFile* OpenFile(const char* filename)
441 {
442   ASSERT_MESSAGE(strchr(filename, '\\') == 0, "path contains invalid separator '\\': \"" << filename << "\""); 
443   for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
444   {
445     ArchiveFile* file = (*i).archive->openFile(filename);
446     if(file != 0)
447     {
448       return file;
449     }
450   }
451
452   return 0;
453 }
454
455 ArchiveTextFile* OpenTextFile(const char* filename)
456 {
457   ASSERT_MESSAGE(strchr(filename, '\\') == 0, "path contains invalid separator '\\': \"" << filename << "\""); 
458   for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
459   {
460     ArchiveTextFile* file = (*i).archive->openTextFile(filename);
461     if(file != 0)
462     {
463       return file;
464     }
465   }
466
467   return 0;
468 }
469
470 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
471 std::size_t LoadFile (const char *filename, void **bufferptr, int index)
472 {
473   char fixed[PATH_MAX+1];
474
475   strncpy (fixed, filename, PATH_MAX);
476   fixed[PATH_MAX] = '\0';
477   FixDOSName (fixed);
478
479   ArchiveFile* file = OpenFile(fixed);
480   
481   if(file != 0)
482   {
483     *bufferptr = malloc (file->size()+1);
484     // we need to end the buffer with a 0
485     ((char*) (*bufferptr))[file->size()] = 0;
486
487     std::size_t length = file->getInputStream().read((InputStream::byte_type*)*bufferptr, file->size());
488     file->release();
489     return length;
490   }
491
492   *bufferptr = 0;
493   return 0;
494 }
495
496 void FreeFile (void *p)
497 {
498   free(p);
499 }
500
501 GSList* GetFileList (const char *dir, const char *ext, std::size_t depth)
502 {
503   return GetListInternal (dir, ext, false, depth);
504 }
505
506 GSList* GetDirList (const char *dir, std::size_t depth)
507 {
508   return GetListInternal (dir, 0, true, depth);
509 }
510
511 void ClearFileDirList (GSList **lst)
512 {
513   while (*lst)
514   {
515     g_free ((*lst)->data);
516     *lst = g_slist_remove (*lst, (*lst)->data);
517   }
518 }
519     
520 const char* FindFile(const char* relative)
521 {
522   for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
523   {
524     if((*i).archive->containsFile(relative))
525     {
526       return (*i).name.c_str();
527     }
528   }
529
530   return "";
531 }
532
533 const char* FindPath(const char* absolute)
534 {
535   for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
536   {
537     if(path_equal_n(absolute, (*i).name.c_str(), string_length((*i).name.c_str())))
538     {
539       return (*i).name.c_str();
540     }
541   }
542
543   return "";
544 }
545
546
547 class Quake3FileSystem : public VirtualFileSystem
548 {
549 public:
550   void initDirectory(const char *path)
551   {
552     InitDirectory(path, FileSystemQ3API_getArchiveModules());
553   }
554   void initialise()
555   {
556     globalOutputStream() << "filesystem initialised\n";
557     g_observers.realise();
558   }
559   void shutdown()
560   {
561     g_observers.unrealise();
562     globalOutputStream() << "filesystem shutdown\n";
563     Shutdown();
564   }
565
566   int getFileCount(const char *filename, int flags)
567   {
568     return GetFileCount(filename, flags);
569   }
570   ArchiveFile* openFile(const char* filename)
571   {
572     return OpenFile(filename);
573   }
574   ArchiveTextFile* openTextFile(const char* filename)
575   {
576     return OpenTextFile(filename);
577   }
578   std::size_t loadFile(const char *filename, void **buffer)
579   {
580     return LoadFile(filename, buffer, 0);
581   }
582   void freeFile(void *p)
583   {
584     FreeFile(p);
585   }
586
587   void forEachDirectory(const char* basedir, const FileNameCallback& callback, std::size_t depth)
588   {
589     GSList* list = GetDirList(basedir, depth);
590
591     for(GSList* i = list; i != 0; i = g_slist_next(i))
592     {
593       callback(reinterpret_cast<const char*>((*i).data));
594     }
595
596     ClearFileDirList(&list);
597   }
598   void forEachFile(const char* basedir, const char* extension, const FileNameCallback& callback, std::size_t depth)
599   {
600     GSList* list = GetFileList(basedir, extension, depth);
601
602     for(GSList* i = list; i != 0; i = g_slist_next(i))
603     {
604       const char* name = reinterpret_cast<const char*>((*i).data);
605       if(extension_equal(path_get_extension(name), extension))
606       {
607         callback(name);
608       }
609     }
610
611     ClearFileDirList(&list);
612   }
613   GSList* getDirList(const char *basedir)
614   {
615     return GetDirList(basedir, 1);
616   }
617   GSList* getFileList(const char *basedir, const char *extension)
618   {
619     return GetFileList(basedir, extension, 1);
620   }
621   void clearFileDirList(GSList **lst)
622   {
623     ClearFileDirList(lst);
624   }
625
626   const char* findFile(const char *name)
627   {
628     return FindFile(name);
629   }
630   const char* findRoot(const char *name)
631   {
632     return FindPath(name);
633   }
634
635   void attach(ModuleObserver& observer)
636   {
637     g_observers.attach(observer);
638   }
639   void detach(ModuleObserver& observer)
640   {
641     g_observers.detach(observer);
642   }
643
644   Archive* getArchive(const char* archiveName)
645   {
646     for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
647     {
648       if((*i).is_pakfile)
649       {
650         if(path_equal((*i).name.c_str(), archiveName))
651         {
652           return (*i).archive;
653         }
654       }
655     }
656     return 0;
657   }
658   void forEachArchive(const ArchiveNameCallback& callback)
659   {
660     for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
661     {
662       if((*i).is_pakfile)
663       {
664         callback((*i).name.c_str());
665       }
666     }
667   }
668 };
669
670 Quake3FileSystem g_Quake3FileSystem;
671
672 void FileSystem_Init()
673 {
674 }
675
676 void FileSystem_Shutdown()
677 {
678 }
679
680 VirtualFileSystem& GetFileSystem()
681 {
682   return g_Quake3FileSystem;
683 }