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)
45 #include "globaldefs.h"
51 #include "qerplugin.h"
52 #include "idatastream.h"
54 ArchiveModules& FileSystemQ3API_getArchiveModules();
55 #include "ifilesystem.h"
57 #include "generic/callback.h"
58 #include "string/string.h"
59 #include "stream/stringstream.h"
61 #include "moduleobservers.h"
62 #include "filematch.h"
66 const int VFS_MAXDIRS = 64;
72 #define gamemode_get GlobalRadiant().getGameMode
76 // =============================================================================
79 Archive* OpenArchive( const char* name );
81 struct archive_entry_t
91 typedef std::list<archive_entry_t> archives_t;
93 static archives_t g_archives;
94 static char g_strDirs[VFS_MAXDIRS][PATH_MAX + 1];
96 static char g_strForbiddenDirs[VFS_MAXDIRS][PATH_MAX + 1];
97 static int g_numForbiddenDirs = 0;
98 static bool g_bUsePak = true;
100 ModuleObservers g_observers;
102 // =============================================================================
105 static void AddSlash( char *str ){
106 std::size_t n = strlen( str );
108 if ( str[n - 1] != '\\' && str[n - 1] != '/' ) {
109 globalErrorStream() << "WARNING: directory path does not end with separator: " << str << "\n";
115 static void FixDOSName( char *src ){
116 if ( src == 0 || strchr( src, '\\' ) == 0 ) {
120 globalErrorStream() << "WARNING: invalid path separator '\\': " << src << "\n";
124 if ( *src == '\\' ) {
131 const _QERArchiveTable* GetArchiveTable( ArchiveModules& archiveModules, const char* ext ){
132 StringOutputStream tmp( 16 );
133 tmp << LowerCase( ext );
134 return archiveModules.findModule( tmp.c_str() );
137 static Archive* InitPakFile( ArchiveModules& archiveModules, const char *filename ){
138 const _QERArchiveTable* table = GetArchiveTable( archiveModules, path_get_extension( filename ) );
141 archive_entry_t entry;
142 entry.name = filename;
144 entry.archive = table->m_pfnOpenArchive( filename );
145 entry.is_pakfile = true;
146 g_archives.push_back( entry );
147 globalOutputStream() << "pak file: " << filename << "\n";
149 return entry.archive;
155 inline void pathlist_prepend_unique( GSList*& pathlist, char* path ){
156 if ( g_slist_find_custom( pathlist, path, (GCompareFunc)path_compare ) == 0 ) {
157 pathlist = g_slist_prepend( pathlist, path );
165 class DirectoryListVisitor : public Archive::Visitor
168 const char* m_directory;
170 DirectoryListVisitor( GSList*& matches, const char* directory )
171 : m_matches( matches ), m_directory( directory )
173 void visit( const char* name ){
174 const char* subname = path_make_relative( name, m_directory );
175 if ( subname != name ) {
176 if ( subname[0] == '/' ) {
179 char* dir = g_strdup( subname );
180 char* last_char = dir + strlen( dir );
181 if ( last_char != dir && *( --last_char ) == '/' ) {
184 pathlist_prepend_unique( m_matches, dir );
189 class FileListVisitor : public Archive::Visitor
192 const char* m_directory;
193 const char* m_extension;
195 FileListVisitor( GSList*& matches, const char* directory, const char* extension )
196 : m_matches( matches ), m_directory( directory ), m_extension( extension )
198 void visit( const char* name ){
199 const char* subname = path_make_relative( name, m_directory );
200 if ( subname != name ) {
201 if ( subname[0] == '/' ) {
204 if ( m_extension[0] == '*' || extension_equal( path_get_extension( subname ), m_extension ) ) {
205 pathlist_prepend_unique( m_matches, g_strdup( subname ) );
211 static GSList* GetListInternal( const char *refdir, const char *ext, bool directories, std::size_t depth ){
214 ASSERT_MESSAGE( refdir[strlen( refdir ) - 1] == '/', "search path does not end in '/'" );
217 for ( archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i )
219 DirectoryListVisitor visitor( files, refdir );
220 ( *i ).archive->forEachFile( Archive::VisitorFunc( visitor, Archive::eDirectories, depth ), refdir );
225 for ( archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i )
227 FileListVisitor visitor( files, refdir, ext );
228 ( *i ).archive->forEachFile( Archive::VisitorFunc( visitor, Archive::eFiles, depth ), refdir );
232 files = g_slist_reverse( files );
237 inline int ascii_to_upper( int c ){
238 if ( c >= 'a' && c <= 'z' ) {
239 return c - ( 'a' - 'A' );
245 This behaves identically to stricmp(a,b), except that ASCII chars
246 [\]^`_ come AFTER alphabet chars instead of before. This is because
247 it converts all alphabet chars to uppercase before comparison,
248 while stricmp converts them to lowercase.
250 static int string_compare_nocase_upper( const char* a, const char* b ){
253 int c1 = ascii_to_upper( *a++ );
254 int c2 = ascii_to_upper( *b++ );
268 // Arnout: note - sort pakfiles in reverse order. This ensures that
269 // later pakfiles override earlier ones. This because the vfs module
270 // returns a filehandle to the first file it can find (while it should
271 // return the filehandle to the file in the most overriding pakfile, the
272 // last one in the list that is).
274 //!\todo Analyse the code in rtcw/q3 to see which order it sorts pak files.
278 bool operator()( const CopiedString& self, const CopiedString& other ) const {
279 return string_compare_nocase_upper( self.c_str(), other.c_str() ) > 0;
283 typedef std::set<CopiedString, PakLess> Archives;
285 Archive* AddPakDir( const char* fullpath ){
286 if ( g_numDirs == VFS_MAXDIRS ) return 0;
288 globalOutputStream() << "pak directory: " << fullpath << "\n";
289 strncpy( g_strDirs[g_numDirs], fullpath, PATH_MAX );
290 g_strDirs[g_numDirs][PATH_MAX] = '\0';
294 archive_entry_t entry;
295 entry.name = fullpath;
296 entry.archive = OpenArchive( fullpath );
297 entry.is_pakfile = false;
298 g_archives.push_back( entry );
300 return entry.archive;
304 // for Daemon DPK VFS
306 Archive* AddDpkDir( const char* fullpath ){
307 return AddPakDir( fullpath );
310 struct pakfile_path_t
312 CopiedString fullpath; // full pak dir or pk3dir name
313 bool is_pakfile; // tells it is .pk3dir or .pk3 file
316 typedef std::pair<CopiedString, pakfile_path_t> PakfilePathsKV;
317 typedef std::map<CopiedString, pakfile_path_t> PakfilePaths; // key must have no extension, only name
319 static PakfilePaths g_pakfile_paths;
321 void AddDpkPak( const char* name, const char* fullpath, bool is_pakfile ){
322 pakfile_path_t pakfile_path;
323 pakfile_path.fullpath = fullpath;
324 pakfile_path.is_pakfile = is_pakfile;
325 g_pakfile_paths.insert( PakfilePathsKV( name, pakfile_path ) );
328 // takes name without ext, returns without ext
329 static const char* GetLatestDpkPakVersion( const char* name ){
330 const char* maxversion = 0;
331 const char* result = 0;
333 const char* pakversion;
334 int namelen = string_length( name );
336 for ( PakfilePaths::iterator i = g_pakfile_paths.begin(); i != g_pakfile_paths.end(); ++i )
338 pakname = i->first.c_str();
339 if ( strncmp( pakname, name, namelen ) != 0 || pakname[namelen] != '_' ) continue;
340 pakversion = pakname + (namelen + 1);
341 if ( maxversion == 0 || DpkPakVersionCmp( pakversion, maxversion ) > 0 ){
342 maxversion = pakversion;
349 // release string after using
350 static char* GetCurrentMapDpkPakName(){
356 mapname = string_clone( GlobalRadiant().getMapName() );
357 mapnamelen = string_length( mapname );
359 mapdir = strrchr( mapname, '/' );
362 if ( strncmp( mapdir, ".dpkdir/maps/", 13 ) == 0 ) {
364 mapdir = strrchr( mapname, '/' );
365 if ( mapdir ) mapdir++;
366 else mapdir = mapname;
367 result = string_clone( mapdir );
371 string_release( mapname, mapnamelen );
376 // prevent loading duplicates or circular references
377 static Archives g_loaded_dpk_paks;
379 // actual pak adding on initialise, deferred from InitDirectory
380 // Daemon DPK filesystem doesn't need load all paks it finds
381 static void LoadDpkPakWithDeps( const char* pakname ){
383 ArchiveTextFile* depsFile;
385 if (pakname == NULL) {
386 // load DEPS from game pack
387 StringOutputStream baseDirectory( 256 );
388 const char* basegame = GlobalRadiant().getRequiredGameDescriptionKeyValue( "basegame" );
389 baseDirectory << GlobalRadiant().getGameToolsPath() << basegame << '/';
390 arc = AddDpkDir( baseDirectory.c_str() );
391 depsFile = arc->openTextFile( "DEPS" );
393 const char* und = strrchr( pakname, '_' );
395 pakname = GetLatestDpkPakVersion( pakname );
397 if ( !pakname || g_loaded_dpk_paks.find( pakname ) != g_loaded_dpk_paks.end() ) {
401 PakfilePaths::iterator i = g_pakfile_paths.find( pakname );
402 if ( i == g_pakfile_paths.end() ) {
406 if ( i->second.is_pakfile ){
407 arc = InitPakFile( FileSystemQ3API_getArchiveModules(), i->second.fullpath.c_str() );
409 arc = AddDpkDir( i->second.fullpath.c_str() );
411 g_loaded_dpk_paks.insert( pakname );
413 depsFile = arc->openTextFile( "DEPS" );
421 TextLinesInputStream<TextInputStream> istream = depsFile->getInputStream();
426 while ( line = istream.readLine(), string_length( line.c_str() ) ) {
427 if ( !DpkReadDepsLine( line.c_str(), &p_name, &p_version ) ) continue;
429 const char* p_latest = GetLatestDpkPakVersion( p_name );
430 if ( p_latest ) LoadDpkPakWithDeps( p_latest );
432 int len = string_length( p_name ) + string_length( p_version ) + 1;
433 char* p_pakname = string_new( len );
434 sprintf( p_pakname, "%s_%s", p_name, p_version );
435 LoadDpkPakWithDeps( p_pakname );
436 string_release( p_pakname, len );
438 string_release( p_name, string_length( p_name ) );
439 if ( p_version ) string_release( p_version, string_length( p_version ) );
446 // end for Daemon DPK vfs
448 // =============================================================================
451 // reads all pak files from a dir
452 void InitDirectory( const char* directory, ArchiveModules& archiveModules ){
455 g_numForbiddenDirs = 0;
456 StringTokeniser st( GlobalRadiant().getGameDescriptionKeyValue( "forbidden_paths" ), " " );
458 for ( j = 0; j < VFS_MAXDIRS; ++j )
460 const char *t = st.getToken();
461 if ( string_empty( t ) ) {
464 strncpy( g_strForbiddenDirs[g_numForbiddenDirs], t, PATH_MAX );
465 g_strForbiddenDirs[g_numForbiddenDirs][PATH_MAX] = '\0';
466 ++g_numForbiddenDirs;
469 for ( j = 0; j < g_numForbiddenDirs; ++j )
471 char* dbuf = g_strdup( directory );
472 if ( *dbuf && dbuf[strlen( dbuf ) - 1] == '/' ) {
473 dbuf[strlen( dbuf ) - 1] = 0;
475 const char *p = strrchr( dbuf, '/' );
476 p = ( p ? ( p + 1 ) : dbuf );
477 if ( matchpattern( p, g_strForbiddenDirs[j], TRUE ) ) {
484 if ( j < g_numForbiddenDirs ) {
485 printf( "Directory %s matched by forbidden dirs, removed\n", directory );
489 if ( g_numDirs == VFS_MAXDIRS ) {
493 strncpy( g_strDirs[g_numDirs], directory, PATH_MAX );
494 g_strDirs[g_numDirs][PATH_MAX] = '\0';
495 FixDOSName( g_strDirs[g_numDirs] );
496 AddSlash( g_strDirs[g_numDirs] );
498 const char* path = g_strDirs[g_numDirs];
503 archive_entry_t entry;
505 entry.archive = OpenArchive( path );
506 entry.is_pakfile = false;
507 g_archives.push_back( entry );
512 GDir* dir = g_dir_open( path, 0, 0 );
515 globalOutputStream() << "vfs directory: " << path << "\n";
518 Archives archivesOverride;
519 const char* ignore_prefix = "";
520 const char* override_prefix = "";
521 bool is_wad_vfs, is_pak_vfs, is_pk3_vfs, is_pk4_vfs, is_dpk_vfs;
523 is_wad_vfs = !!GetArchiveTable( archiveModules, "wad" );
524 is_pak_vfs = !!GetArchiveTable( archiveModules, "pak" );
525 is_pk3_vfs = !!GetArchiveTable( archiveModules, "pk3" );
526 is_pk4_vfs = !!GetArchiveTable( archiveModules, "pk4" );
527 is_dpk_vfs = !!GetArchiveTable( archiveModules, "dpk" );
530 // See if we are in "sp" or "mp" mapping mode
531 const char* gamemode = gamemode_get();
533 if ( strcmp( gamemode, "sp" ) == 0 ) {
534 ignore_prefix = "mp_";
535 override_prefix = "sp_";
537 else if ( strcmp( gamemode, "mp" ) == 0 ) {
538 ignore_prefix = "sp_";
539 override_prefix = "mp_";
545 const char* name = g_dir_read_name( dir );
547 if ( name == nullptr ) {
551 for ( j = 0; j < g_numForbiddenDirs; ++j )
553 const char *p = strrchr( name, '/' );
554 p = ( p ? ( p + 1 ) : name );
555 if ( matchpattern( p, g_strForbiddenDirs[j], TRUE ) ) {
560 if ( j < g_numForbiddenDirs ) {
564 const char *ext = strrchr( name, '.' );
565 char tmppath[PATH_MAX + 1];
567 if ( ext != nullptr ) {
568 if ( is_dpk_vfs && !string_compare_nocase_upper( ext, ".dpkdir" ) ) {
569 snprintf( tmppath, PATH_MAX, "%s%s/", path, name );
570 tmppath[PATH_MAX] = '\0';
571 FixDOSName( tmppath );
573 AddDpkPak( CopiedString( StringRange( name, ext ) ).c_str(), tmppath, false );
576 else if ( ( is_wad_vfs && !string_compare_nocase_upper( ext, ".pakdir" ) )
577 || ( is_pk3_vfs && !string_compare_nocase_upper( ext, ".pk3dir" ) )
578 || ( is_pk4_vfs && !string_compare_nocase_upper( ext, ".pk4dir" ) ) ) {
579 snprintf( tmppath, PATH_MAX, "%s%s/", path, name );
580 tmppath[PATH_MAX] = '\0';
581 FixDOSName( tmppath );
583 AddPakDir( tmppath );
587 // GetArchiveTable() needs "pk3" if ext is ".pk3"
588 if ( ( ext == nullptr ) || *( ext + 1 ) == '\0' || GetArchiveTable( archiveModules, ext + 1 ) == 0 ) {
592 // using the same kludge as in engine to ensure consistency
593 if ( !string_empty( ignore_prefix ) && strncmp( name, ignore_prefix, strlen( ignore_prefix ) ) == 0 ) {
597 if ( !string_empty( override_prefix ) && strncmp( name, override_prefix, strlen( override_prefix ) ) == 0 ) {
598 if ( !string_compare_nocase_upper( ext, ".dpk" ) ) {
600 archives.insert( name );
605 archivesOverride.insert( name );
610 archives.insert( name );
615 // add the entries to the vfs
618 for ( Archives::iterator i = archives.begin(); i != archives.end(); ++i ) {
619 const char* name = i->c_str();
620 const char* ext = strrchr( name, '.' );
621 if ( !string_compare_nocase_upper( ext, ".dpk" ) )
623 CopiedString name_final = CopiedString( StringRange( name, ext ) );
624 fullpath = string_new_concat( path, name );
625 AddDpkPak( name_final.c_str(), fullpath, true );
626 string_release( fullpath, string_length( fullpath ) );
632 for ( Archives::iterator i = archivesOverride.begin(); i != archivesOverride.end(); ++i )
634 const char* name = i->c_str();
635 const char* ext = strrchr( name, '.' );
636 if ( ( is_wad_vfs && !string_compare_nocase_upper( ext, ".wad" ) )
637 || ( is_pak_vfs && !string_compare_nocase_upper( ext, ".pak" ) )
638 || ( is_pk3_vfs && !string_compare_nocase_upper( ext, ".pk3" ) )
639 || ( is_pk4_vfs && !string_compare_nocase_upper( ext, ".pk4" ) ) ) {
640 fullpath = string_new_concat( path, i->c_str() );
641 InitPakFile( archiveModules, fullpath );
642 string_release( fullpath, string_length( fullpath ) );
646 for ( Archives::iterator i = archives.begin(); i != archives.end(); ++i )
648 const char* name = i->c_str();
649 const char* ext = strrchr( name, '.' );
650 if ( ( is_wad_vfs && !string_compare_nocase_upper( ext, ".wad" ) )
651 || ( is_pak_vfs && !string_compare_nocase_upper( ext, ".pak" ) )
652 || ( is_pk3_vfs && !string_compare_nocase_upper( ext, ".pk3" ) )
653 || ( is_pk4_vfs && !string_compare_nocase_upper( ext, ".pk4" ) ) ) {
654 fullpath = string_new_concat( path, i->c_str() );
655 InitPakFile( archiveModules, fullpath );
656 string_release( fullpath, string_length( fullpath ) );
663 globalErrorStream() << "vfs directory not found: " << path << "\n";
668 // frees all memory that we allocated
669 // FIXME TTimo this should be improved so that we can shutdown and restart the VFS without exiting Radiant?
670 // (for instance when modifying the project settings)
672 for ( archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i )
674 ( *i ).archive->release();
679 g_numForbiddenDirs = 0;
681 g_pakfile_paths.clear();
682 g_loaded_dpk_paks.clear();
685 const int VFS_SEARCH_PAK = 0x1;
686 const int VFS_SEARCH_DIR = 0x2;
688 int GetFileCount( const char *filename, int flag ){
690 char fixed[PATH_MAX + 1];
692 strncpy( fixed, filename, PATH_MAX );
693 fixed[PATH_MAX] = '\0';
697 flag = VFS_SEARCH_PAK | VFS_SEARCH_DIR;
700 for ( archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i )
702 if ( (( *i ).is_pakfile && ( flag & VFS_SEARCH_PAK ) != 0)
703 || (!( *i ).is_pakfile && ( flag & VFS_SEARCH_DIR ) != 0) ) {
704 if ( ( *i ).archive->containsFile( fixed ) ) {
713 ArchiveFile* OpenFile( const char* filename ){
714 ASSERT_MESSAGE( strchr( filename, '\\' ) == 0, "path contains invalid separator '\\': \"" << filename << "\"" );
715 for ( archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i )
717 ArchiveFile* file = ( *i ).archive->openFile( filename );
726 ArchiveTextFile* OpenTextFile( const char* filename ){
727 ASSERT_MESSAGE( strchr( filename, '\\' ) == 0, "path contains invalid separator '\\': \"" << filename << "\"" );
728 for ( archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i )
730 ArchiveTextFile* file = ( *i ).archive->openTextFile( filename );
739 // NOTE: when loading a file, you have to allocate one extra byte and set it to \0
740 std::size_t LoadFile( const char *filename, void **bufferptr, int index ){
741 char fixed[PATH_MAX + 1];
743 strncpy( fixed, filename, PATH_MAX );
744 fixed[PATH_MAX] = '\0';
747 ArchiveFile* file = OpenFile( fixed );
750 *bufferptr = malloc( file->size() + 1 );
751 // we need to end the buffer with a 0
752 ( (char*) ( *bufferptr ) )[file->size()] = 0;
754 std::size_t length = file->getInputStream().read( (InputStream::byte_type*)*bufferptr, file->size() );
763 void FreeFile( void *p ){
767 GSList* GetFileList( const char *dir, const char *ext, std::size_t depth ){
768 return GetListInternal( dir, ext, false, depth );
771 GSList* GetDirList( const char *dir, std::size_t depth ){
772 return GetListInternal( dir, 0, true, depth );
775 void ClearFileDirList( GSList **lst ){
778 g_free( ( *lst )->data );
779 *lst = g_slist_remove( *lst, ( *lst )->data );
783 const char* FindFile( const char* relative ){
784 for ( archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i )
786 if ( ( *i ).archive->containsFile( relative ) ) {
787 return ( *i ).name.c_str();
794 const char* FindPath( const char* absolute ){
795 const char *best = "";
796 for ( archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i )
798 if ( string_length( ( *i ).name.c_str() ) > string_length( best ) ) {
799 if ( path_equal_n( absolute, ( *i ).name.c_str(), string_length( ( *i ).name.c_str() ) ) ) {
800 best = ( *i ).name.c_str();
809 class Quake3FileSystem : public VirtualFileSystem
812 void initDirectory( const char *path ){
813 InitDirectory( path, FileSystemQ3API_getArchiveModules() );
817 globalOutputStream() << "filesystem initialised\n";
818 g_observers.realise();
822 ArchiveModules& archiveModules = FileSystemQ3API_getArchiveModules();
823 bool is_dpk_vfs = !!GetArchiveTable( archiveModules, "dpk" );
827 g_loaded_dpk_paks.clear();
829 // Load DEPS from game pack
830 LoadDpkPakWithDeps( NULL );
832 // prevent VFS double start, for MapName="" and MapName="unnamed.map"
833 if ( string_length( GlobalRadiant().getMapName() ) ){
834 // load map's paks from DEPS
835 char* mappakname = GetCurrentMapDpkPakName();
836 if ( mappakname != NULL ) {
837 LoadDpkPakWithDeps( mappakname );
838 string_release( mappakname, string_length( mappakname ) );
842 g_pakfile_paths.clear();
843 g_loaded_dpk_paks.clear();
848 // like shutdown() but does not unrealise (keep map etc.)
853 // like initialise() but does not realise (keep map etc.)
855 globalOutputStream() << "filesystem refreshed\n";
859 g_observers.unrealise();
860 globalOutputStream() << "filesystem shutdown\n";
864 int getFileCount( const char *filename, int flags ){
865 return GetFileCount( filename, flags );
867 ArchiveFile* openFile( const char* filename ){
868 return OpenFile( filename );
870 ArchiveTextFile* openTextFile( const char* filename ){
871 return OpenTextFile( filename );
873 std::size_t loadFile( const char *filename, void **buffer ){
874 return LoadFile( filename, buffer, 0 );
876 void freeFile( void *p ){
880 void forEachDirectory( const char* basedir, const FileNameCallback& callback, std::size_t depth ){
881 GSList* list = GetDirList( basedir, depth );
883 for ( GSList* i = list; i != 0; i = g_slist_next( i ) )
885 callback( reinterpret_cast<const char*>( ( *i ).data ) );
888 ClearFileDirList( &list );
890 void forEachFile( const char* basedir, const char* extension, const FileNameCallback& callback, std::size_t depth ){
891 GSList* list = GetFileList( basedir, extension, depth );
893 for ( GSList* i = list; i != 0; i = g_slist_next( i ) )
895 const char* name = reinterpret_cast<const char*>( ( *i ).data );
896 if ( extension_equal( path_get_extension( name ), extension ) ) {
901 ClearFileDirList( &list );
903 GSList* getDirList( const char *basedir ){
904 return GetDirList( basedir, 1 );
906 GSList* getFileList( const char *basedir, const char *extension ){
907 return GetFileList( basedir, extension, 1 );
909 void clearFileDirList( GSList **lst ){
910 ClearFileDirList( lst );
913 const char* findFile( const char *name ){
914 return FindFile( name );
916 const char* findRoot( const char *name ){
917 return FindPath( name );
920 void attach( ModuleObserver& observer ){
921 g_observers.attach( observer );
923 void detach( ModuleObserver& observer ){
924 g_observers.detach( observer );
927 Archive* getArchive( const char* archiveName, bool pakonly ){
928 for ( archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i )
930 if ( pakonly && !( *i ).is_pakfile ) {
934 if ( path_equal( ( *i ).name.c_str(), archiveName ) ) {
935 return ( *i ).archive;
937 else if ( path_equal( path_get_filename_start( ( *i ).name.c_str() ), archiveName ) ) {
938 return ( *i ).archive;
943 void forEachArchive( const ArchiveNameCallback& callback, bool pakonly, bool reverse ){
945 g_archives.reverse();
948 for ( archives_t::iterator i = g_archives.begin(); i != g_archives.end(); ++i )
950 if ( pakonly && !( *i ).is_pakfile ) {
954 callback( ( *i ).name.c_str() );
958 g_archives.reverse();
964 Quake3FileSystem g_Quake3FileSystem;
966 VirtualFileSystem& GetFileSystem(){
967 return g_Quake3FileSystem;
970 void FileSystem_Init(){
973 void FileSystem_Shutdown(){