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)
48 #include <glib/gslist.h>
49 #include <glib/gdir.h>
50 #include <glib/gstrfuncs.h>
52 #include "qerplugin.h"
53 #include "idatastream.h"
55 ArchiveModules& FileSystemQ3API_getArchiveModules();
56 #include "ifilesystem.h"
58 #include "generic/callback.h"
59 #include "string/string.h"
60 #include "stream/stringstream.h"
62 #include "moduleobservers.h"
65 #define VFS_MAXDIRS 64
71 #define gamemode_get GlobalRadiant().getGameMode
75 // =============================================================================
78 Archive* OpenArchive(const char* name);
80 struct archive_entry_t
89 typedef std::list<archive_entry_t> archives_t;
91 static archives_t g_archives;
92 static char g_strDirs[VFS_MAXDIRS][PATH_MAX+1];
94 static char g_strForbiddenDirs[VFS_MAXDIRS][PATH_MAX+1];
95 static int g_numForbiddenDirs = 0;
96 static bool g_bUsePak = true;
98 ModuleObservers g_observers;
100 // =============================================================================
103 static void AddSlash (char *str)
105 std::size_t n = strlen (str);
108 if (str[n-1] != '\\' && str[n-1] != '/')
110 globalErrorStream() << "WARNING: directory path does not end with separator: " << str << "\n";
116 static void FixDOSName (char *src)
118 if (src == 0 || strchr(src, '\\') == 0)
121 globalErrorStream() << "WARNING: invalid path separator '\\': " << src << "\n";
133 const _QERArchiveTable* GetArchiveTable(ArchiveModules& archiveModules, const char* ext)
135 StringOutputStream tmp(16);
136 tmp << LowerCase(ext);
137 return archiveModules.findModule(tmp.c_str());
139 static void InitPakFile (ArchiveModules& archiveModules, const char *filename)
141 const _QERArchiveTable* table = GetArchiveTable(archiveModules, path_get_extension(filename));
145 archive_entry_t entry;
146 entry.name = filename;
148 entry.archive = table->m_pfnOpenArchive(filename);
149 entry.is_pakfile = true;
150 g_archives.push_back(entry);
151 globalOutputStream() << " pak file: " << filename << "\n";
155 inline void pathlist_prepend_unique(GSList*& pathlist, char* path)
157 if(g_slist_find_custom(pathlist, path, (GCompareFunc)path_compare) == 0)
159 pathlist = g_slist_prepend(pathlist, path);
167 class DirectoryListVisitor : public Archive::Visitor
170 const char* m_directory;
172 DirectoryListVisitor(GSList*& matches, const char* directory)
173 : m_matches(matches), m_directory(directory)
175 void visit(const char* name)
177 const char* subname = path_make_relative(name, m_directory);
180 if(subname[0] == '/')
182 char* dir = g_strdup(subname);
183 char* last_char = dir + strlen(dir);
184 if(last_char != dir && *(--last_char) == '/')
186 pathlist_prepend_unique(m_matches, dir);
191 class FileListVisitor : public Archive::Visitor
194 const char* m_directory;
195 const char* m_extension;
197 FileListVisitor(GSList*& matches, const char* directory, const char* extension)
198 : m_matches(matches), m_directory(directory), m_extension(extension)
200 void visit(const char* name)
202 const char* subname = path_make_relative(name, m_directory);
205 if(subname[0] == '/')
207 if(m_extension[0] == '*' || extension_equal(path_get_extension(subname), m_extension))
208 pathlist_prepend_unique(m_matches, g_strdup (subname));
213 static GSList* GetListInternal (const char *refdir, const char *ext, bool directories, std::size_t depth)
217 ASSERT_MESSAGE(refdir[strlen(refdir) - 1] == '/', "search path does not end in '/'");
221 for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
223 DirectoryListVisitor visitor(files, refdir);
224 (*i).archive->forEachFile(Archive::VisitorFunc(visitor, Archive::eDirectories, depth), refdir);
229 for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
231 FileListVisitor visitor(files, refdir, ext);
232 (*i).archive->forEachFile(Archive::VisitorFunc(visitor, Archive::eFiles, depth), refdir);
236 files = g_slist_reverse(files);
241 inline int ascii_to_upper(int c)
243 if (c >= 'a' && c <= 'z')
245 return c - ('a' - 'A');
251 This behaves identically to stricmp(a,b), except that ASCII chars
252 [\]^`_ come AFTER alphabet chars instead of before. This is because
253 it converts all alphabet chars to uppercase before comparison,
254 while stricmp converts them to lowercase.
256 static int string_compare_nocase_upper(const char* a, const char* b)
260 int c1 = ascii_to_upper(*a++);
261 int c2 = ascii_to_upper(*b++);
278 // Arnout: note - sort pakfiles in reverse order. This ensures that
279 // later pakfiles override earlier ones. This because the vfs module
280 // returns a filehandle to the first file it can find (while it should
281 // return the filehandle to the file in the most overriding pakfile, the
282 // last one in the list that is).
284 //!\todo Analyse the code in rtcw/q3 to see which order it sorts pak files.
288 bool operator()(const CopiedString& self, const CopiedString& other) const
290 return string_compare_nocase_upper(self.c_str(), other.c_str()) > 0;
294 typedef std::set<CopiedString, PakLess> Archives;
296 // =============================================================================
299 // reads all pak files from a dir
300 void InitDirectory(const char* directory, ArchiveModules& archiveModules)
304 g_numForbiddenDirs = 0;
305 StringTokeniser st(GlobalRadiant().getGameDescriptionKeyValue("forbidden_paths"), " ");
306 for(j = 0; j < VFS_MAXDIRS; ++j)
308 const char *t = st.getToken();
311 strncpy(g_strForbiddenDirs[g_numForbiddenDirs], t, PATH_MAX);
312 g_strForbiddenDirs[g_numForbiddenDirs][PATH_MAX] = '\0';
313 ++g_numForbiddenDirs;
316 for(j = 0; j < g_numForbiddenDirs; ++j)
318 if(!string_compare_nocase_upper(directory, g_strForbiddenDirs[j])
319 || (string_length(directory) > string_length(g_strForbiddenDirs[j]) && directory[string_length(directory) - string_length(g_strForbiddenDirs[j]) - 1] == '/' && !string_compare_nocase_upper(directory + string_length(directory) - string_length(g_strForbiddenDirs[j]), g_strForbiddenDirs[j])))
322 if(j < g_numForbiddenDirs)
324 printf("Directory %s matched by forbidden dirs, removed\n", directory);
328 if (g_numDirs == VFS_MAXDIRS)
331 strncpy(g_strDirs[g_numDirs], directory, PATH_MAX);
332 g_strDirs[g_numDirs][PATH_MAX] = '\0';
333 FixDOSName (g_strDirs[g_numDirs]);
334 AddSlash (g_strDirs[g_numDirs]);
336 const char* path = g_strDirs[g_numDirs];
341 archive_entry_t entry;
343 entry.archive = OpenArchive(path);
344 entry.is_pakfile = false;
345 g_archives.push_back(entry);
350 GDir* dir = g_dir_open (path, 0, 0);
354 globalOutputStream() << "vfs directory: " << path << "\n";
356 const char* ignore_prefix = "";
357 const char* override_prefix = "";
360 // See if we are in "sp" or "mp" mapping mode
361 const char* gamemode = gamemode_get();
363 if (strcmp (gamemode, "sp") == 0)
365 ignore_prefix = "mp_";
366 override_prefix = "sp_";
368 else if (strcmp (gamemode, "mp") == 0)
370 ignore_prefix = "sp_";
371 override_prefix = "mp_";
376 Archives archivesOverride;
379 const char* name = g_dir_read_name(dir);
383 for(j = 0; j < g_numForbiddenDirs; ++j)
384 if(!string_compare_nocase_upper(name, g_strForbiddenDirs[j]))
386 if(j < g_numForbiddenDirs)
389 const char *ext = strrchr (name, '.');
391 if(ext && !string_compare_nocase_upper(ext, ".pk3dir"))
393 if (g_numDirs == VFS_MAXDIRS)
395 snprintf(g_strDirs[g_numDirs], PATH_MAX, "%s%s/", path, name);
396 g_strDirs[g_numDirs][PATH_MAX] = '\0';
397 FixDOSName (g_strDirs[g_numDirs]);
398 AddSlash (g_strDirs[g_numDirs]);
402 archive_entry_t entry;
403 entry.name = g_strDirs[g_numDirs-1];
404 entry.archive = OpenArchive(g_strDirs[g_numDirs-1]);
405 entry.is_pakfile = false;
406 g_archives.push_back(entry);
410 if ((ext == 0) || *(++ext) == '\0' || GetArchiveTable(archiveModules, ext) == 0)
413 // using the same kludge as in engine to ensure consistency
414 if(!string_empty(ignore_prefix) && strncmp(name, ignore_prefix, strlen(ignore_prefix)) == 0)
418 if(!string_empty(override_prefix) && strncmp(name, override_prefix, strlen(override_prefix)) == 0)
420 archivesOverride.insert(name);
424 archives.insert(name);
429 // add the entries to the vfs
430 for(Archives::iterator i = archivesOverride.begin(); i != archivesOverride.end(); ++i)
432 char filename[PATH_MAX];
433 strcpy(filename, path);
434 strcat(filename, (*i).c_str());
435 InitPakFile(archiveModules, filename);
437 for(Archives::iterator i = archives.begin(); i != archives.end(); ++i)
439 char filename[PATH_MAX];
440 strcpy(filename, path);
441 strcat(filename, (*i).c_str());
442 InitPakFile(archiveModules, filename);
447 globalErrorStream() << "vfs directory not found: " << path << "\n";
452 // frees all memory that we allocated
453 // FIXME TTimo this should be improved so that we can shutdown and restart the VFS without exiting Radiant?
454 // (for instance when modifying the project settings)
457 for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
459 (*i).archive->release();
464 g_numForbiddenDirs = 0;
467 #define VFS_SEARCH_PAK 0x1
468 #define VFS_SEARCH_DIR 0x2
470 int GetFileCount (const char *filename, int flag)
473 char fixed[PATH_MAX+1];
475 strncpy(fixed, filename, PATH_MAX);
476 fixed[PATH_MAX] = '\0';
480 flag = VFS_SEARCH_PAK | VFS_SEARCH_DIR;
482 for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
484 if((*i).is_pakfile && (flag & VFS_SEARCH_PAK) != 0
485 || !(*i).is_pakfile && (flag & VFS_SEARCH_DIR) != 0)
487 if((*i).archive->containsFile(fixed))
495 ArchiveFile* OpenFile(const char* filename)
497 ASSERT_MESSAGE(strchr(filename, '\\') == 0, "path contains invalid separator '\\': \"" << filename << "\"");
498 for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
500 ArchiveFile* file = (*i).archive->openFile(filename);
510 ArchiveTextFile* OpenTextFile(const char* filename)
512 ASSERT_MESSAGE(strchr(filename, '\\') == 0, "path contains invalid separator '\\': \"" << filename << "\"");
513 for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
515 ArchiveTextFile* file = (*i).archive->openTextFile(filename);
525 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
526 std::size_t LoadFile (const char *filename, void **bufferptr, int index)
528 char fixed[PATH_MAX+1];
530 strncpy (fixed, filename, PATH_MAX);
531 fixed[PATH_MAX] = '\0';
534 ArchiveFile* file = OpenFile(fixed);
538 *bufferptr = malloc (file->size()+1);
539 // we need to end the buffer with a 0
540 ((char*) (*bufferptr))[file->size()] = 0;
542 std::size_t length = file->getInputStream().read((InputStream::byte_type*)*bufferptr, file->size());
551 void FreeFile (void *p)
556 GSList* GetFileList (const char *dir, const char *ext, std::size_t depth)
558 return GetListInternal (dir, ext, false, depth);
561 GSList* GetDirList (const char *dir, std::size_t depth)
563 return GetListInternal (dir, 0, true, depth);
566 void ClearFileDirList (GSList **lst)
570 g_free ((*lst)->data);
571 *lst = g_slist_remove (*lst, (*lst)->data);
575 const char* FindFile(const char* relative)
577 for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
579 if((*i).archive->containsFile(relative))
581 return (*i).name.c_str();
588 const char* FindPath(const char* absolute)
590 const char *best = "";
591 for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
593 if(string_length((*i).name.c_str()) > string_length(best))
594 if(path_equal_n(absolute, (*i).name.c_str(), string_length((*i).name.c_str())))
595 best = (*i).name.c_str();
602 class Quake3FileSystem : public VirtualFileSystem
605 void initDirectory(const char *path)
607 InitDirectory(path, FileSystemQ3API_getArchiveModules());
611 globalOutputStream() << "filesystem initialised\n";
612 g_observers.realise();
616 g_observers.unrealise();
617 globalOutputStream() << "filesystem shutdown\n";
621 int getFileCount(const char *filename, int flags)
623 return GetFileCount(filename, flags);
625 ArchiveFile* openFile(const char* filename)
627 return OpenFile(filename);
629 ArchiveTextFile* openTextFile(const char* filename)
631 return OpenTextFile(filename);
633 std::size_t loadFile(const char *filename, void **buffer)
635 return LoadFile(filename, buffer, 0);
637 void freeFile(void *p)
642 void forEachDirectory(const char* basedir, const FileNameCallback& callback, std::size_t depth)
644 GSList* list = GetDirList(basedir, depth);
646 for(GSList* i = list; i != 0; i = g_slist_next(i))
648 callback(reinterpret_cast<const char*>((*i).data));
651 ClearFileDirList(&list);
653 void forEachFile(const char* basedir, const char* extension, const FileNameCallback& callback, std::size_t depth)
655 GSList* list = GetFileList(basedir, extension, depth);
657 for(GSList* i = list; i != 0; i = g_slist_next(i))
659 const char* name = reinterpret_cast<const char*>((*i).data);
660 if(extension_equal(path_get_extension(name), extension))
666 ClearFileDirList(&list);
668 GSList* getDirList(const char *basedir)
670 return GetDirList(basedir, 1);
672 GSList* getFileList(const char *basedir, const char *extension)
674 return GetFileList(basedir, extension, 1);
676 void clearFileDirList(GSList **lst)
678 ClearFileDirList(lst);
681 const char* findFile(const char *name)
683 return FindFile(name);
685 const char* findRoot(const char *name)
687 return FindPath(name);
690 void attach(ModuleObserver& observer)
692 g_observers.attach(observer);
694 void detach(ModuleObserver& observer)
696 g_observers.detach(observer);
699 Archive* getArchive(const char* archiveName, bool pakonly)
701 for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
703 if(pakonly && !(*i).is_pakfile)
706 if(path_equal((*i).name.c_str(), archiveName))
711 void forEachArchive(const ArchiveNameCallback& callback, bool pakonly, bool reverse)
714 g_archives.reverse();
716 for(archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i)
718 if(pakonly && !(*i).is_pakfile)
721 callback((*i).name.c_str());
725 g_archives.reverse();
729 Quake3FileSystem g_Quake3FileSystem;
731 void FileSystem_Init()
735 void FileSystem_Shutdown()
739 VirtualFileSystem& GetFileSystem()
741 return g_Quake3FileSystem;