DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

FileSystem.h (10333B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
     10 it under the terms of the GNU General Public License as published by
     11 the Free Software Foundation, either version 3 of the License, or
     12 (at your option) any later version.
     13 
     14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
     15 but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 GNU General Public License for more details.
     18 
     19 You should have received a copy of the GNU General Public License
     20 along with Doom 3 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code.  If not, please request a copy in writing from id Software at the address below.
     23 
     24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
     25 
     26 ===========================================================================
     27 */
     28 
     29 #ifndef __FILESYSTEM_H__
     30 #define __FILESYSTEM_H__
     31 
     32 /*
     33 ===============================================================================
     34 
     35 	File System
     36 
     37 	No stdio calls should be used by any part of the game, because of all sorts
     38 	of directory and separator char issues. Throughout the game a forward slash
     39 	should be used as a separator. The file system takes care of the conversion
     40 	to an OS specific separator. The file system treats all file and directory
     41 	names as case insensitive.
     42 
     43 	The following cvars store paths used by the file system:
     44 
     45 	"fs_basepath"		path to local install
     46 	"fs_savepath"		path to config, save game, etc. files, read & write
     47 
     48 	The base path for file saving can be set to "fs_savepath" or "fs_basepath".
     49 
     50 ===============================================================================
     51 */
     52 
     53 static const ID_TIME_T	FILE_NOT_FOUND_TIMESTAMP	= (ID_TIME_T)-1;
     54 static const int		MAX_OSPATH					= 256;
     55 
     56 // modes for OpenFileByMode
     57 typedef enum {
     58 	FS_READ			= 0,
     59 	FS_WRITE		= 1,
     60 	FS_APPEND	= 2
     61 } fsMode_t;
     62 
     63 typedef enum {
     64 	FIND_NO,
     65 	FIND_YES
     66 } findFile_t;
     67 
     68 // file list for directory listings
     69 class idFileList {
     70 	friend class idFileSystemLocal;
     71 public:
     72 	const char *			GetBasePath() const { return basePath; }
     73 	int						GetNumFiles() const { return list.Num(); }
     74 	const char *			GetFile( int index ) const { return list[index]; }
     75 	const idStrList &		GetList() const { return list; }
     76 
     77 private:
     78 	idStr					basePath;
     79 	idStrList				list;
     80 };
     81 
     82 class idFileSystem {
     83 public:
     84 	virtual					~idFileSystem() {}
     85 							// Initializes the file system.
     86 	virtual void			Init() = 0;
     87 							// Restarts the file system.
     88 	virtual void			Restart() = 0;
     89 							// Shutdown the file system.
     90 	virtual void			Shutdown( bool reloading ) = 0;
     91 							// Returns true if the file system is initialized.
     92 	virtual bool			IsInitialized() const = 0;
     93 							// Lists files with the given extension in the given directory.
     94 							// Directory should not have either a leading or trailing '/'
     95 							// The returned files will not include any directories or '/' unless fullRelativePath is set.
     96 							// The extension must include a leading dot and may not contain wildcards.
     97 							// If extension is "/", only subdirectories will be returned.
     98 	virtual idFileList *	ListFiles( const char *relativePath, const char *extension, bool sort = false, bool fullRelativePath = false, const char* gamedir = NULL ) = 0;
     99 							// Lists files in the given directory and all subdirectories with the given extension.
    100 							// Directory should not have either a leading or trailing '/'
    101 							// The returned files include a full relative path.
    102 							// The extension must include a leading dot and may not contain wildcards.
    103 	virtual idFileList *	ListFilesTree( const char *relativePath, const char *extension, bool sort = false, const char* gamedir = NULL ) = 0;
    104 							// Frees the given file list.
    105 	virtual void			FreeFileList( idFileList *fileList ) = 0;
    106 							// Converts a relative path to a full OS path.
    107 	virtual const char *	OSPathToRelativePath( const char *OSPath ) = 0;
    108 							// Converts a full OS path to a relative path.
    109 	virtual const char *	RelativePathToOSPath( const char *relativePath, const char *basePath = "fs_basepath" ) = 0;
    110 							// Builds a full OS path from the given components.
    111 	virtual const char *	BuildOSPath( const char *base, const char *game, const char *relativePath ) = 0;
    112 	virtual const char *	BuildOSPath( const char *base, const char *relativePath ) = 0;
    113 							// Creates the given OS path for as far as it doesn't exist already.
    114 	virtual void			CreateOSPath( const char *OSPath ) = 0;
    115 							// Reads a complete file.
    116 							// Returns the length of the file, or -1 on failure.
    117 							// A null buffer will just return the file length without loading.
    118 							// A null timestamp will be ignored.
    119 							// As a quick check for existance. -1 length == not present.
    120 							// A 0 byte will always be appended at the end, so string ops are safe.
    121 							// The buffer should be considered read-only, because it may be cached for other uses.
    122 	virtual int				ReadFile( const char *relativePath, void **buffer, ID_TIME_T *timestamp = NULL ) = 0;
    123 							// Frees the memory allocated by ReadFile.
    124 	virtual void			FreeFile( void *buffer ) = 0;
    125 							// Writes a complete file, will create any needed subdirectories.
    126 							// Returns the length of the file, or -1 on failure.
    127 	virtual int				WriteFile( const char *relativePath, const void *buffer, int size, const char *basePath = "fs_savepath" ) = 0;
    128 							// Removes the given file.
    129 	virtual void			RemoveFile( const char *relativePath ) = 0;
    130 							// Removes the specified directory.
    131 	virtual	bool			RemoveDir( const char * relativePath ) = 0;
    132 							// Renames a file, taken from idTech5 (minus the fsPath_t)
    133 	virtual bool			RenameFile( const char * relativePath, const char * newName, const char * basePath = "fs_savepath" ) = 0;
    134 							// Opens a file for reading.
    135 	virtual idFile *		OpenFileRead( const char *relativePath, bool allowCopyFiles = true, const char* gamedir = NULL ) = 0;
    136 							// Opens a file for reading, reads the file completely in memory and returns an idFile_Memory obj.
    137 	virtual idFile *		OpenFileReadMemory( const char *relativePath, bool allowCopyFiles = true, const char* gamedir = NULL ) = 0;
    138 							// Opens a file for writing, will create any needed subdirectories.
    139 	virtual idFile *		OpenFileWrite( const char *relativePath, const char *basePath = "fs_savepath" ) = 0;
    140 							// Opens a file for writing at the end.
    141 	virtual idFile *		OpenFileAppend( const char *filename, bool sync = false, const char *basePath = "fs_basepath" ) = 0;
    142 							// Opens a file for reading, writing, or appending depending on the value of mode.
    143 	virtual idFile *		OpenFileByMode( const char *relativePath, fsMode_t mode ) = 0;
    144 							// Opens a file for reading from a full OS path.
    145 	virtual idFile *		OpenExplicitFileRead( const char *OSPath ) = 0;
    146 							// Opens a file for writing to a full OS path.
    147 	virtual idFile *		OpenExplicitFileWrite( const char *OSPath ) = 0;
    148 							// opens a zip container
    149 	virtual idFile_Cached *		OpenExplicitPakFile( const char *OSPath ) = 0;
    150 							// Closes a file.
    151 	virtual void			CloseFile( idFile *f ) = 0;
    152 							// look for a dynamic module
    153 	virtual void			FindDLL( const char *basename, char dllPath[ MAX_OSPATH ] ) = 0;
    154 
    155 							// don't use for large copies - allocates a single memory block for the copy
    156 	virtual void			CopyFile( const char *fromOSPath, const char *toOSPath ) = 0;
    157 
    158 							// look for a file in the loaded paks or the addon paks
    159 							// if the file is found in addons, FS's internal structures are ready for a reloadEngine
    160 	virtual findFile_t		FindFile( const char *path ) = 0;
    161 
    162 							// ignore case and seperator char distinctions
    163 	virtual bool			FilenameCompare( const char *s1, const char *s2 ) const = 0;
    164 
    165 	// This is just handy
    166 	ID_TIME_T				GetTimestamp( const char * relativePath ) {
    167 		ID_TIME_T timestamp = FILE_NOT_FOUND_TIMESTAMP;
    168 		if ( relativePath == NULL || relativePath[ 0 ] == '\0' ) {
    169 			return timestamp;
    170 		}
    171 		ReadFile( relativePath, NULL, &timestamp );
    172 		return timestamp;
    173 	}
    174 	
    175 	// Returns length of file, -1 if no file exists
    176 	virtual int				GetFileLength( const char * relativePath ) = 0;
    177 
    178 	virtual sysFolder_t		IsFolder( const char * relativePath, const char *basePath = "fs_basepath" ) = 0;
    179 
    180 	// resource tracking and related things
    181 	virtual void			EnableBackgroundCache( bool enable ) = 0;
    182 	virtual void			BeginLevelLoad( const char *name, char *_blockBuffer, int _blockBufferSize  ) = 0;
    183 	virtual void			EndLevelLoad() = 0;
    184 	virtual bool			InProductionMode() = 0;
    185 	virtual bool			UsingResourceFiles() = 0;
    186 	virtual void			UnloadMapResources( const char *name ) = 0;
    187 	virtual void			UnloadResourceContainer( const char *name ) = 0;
    188 	virtual void			StartPreload( const idStrList &_preload ) = 0;
    189 	virtual void			StopPreload() = 0;
    190 	virtual int				ReadFromBGL( idFile *_resourceFile, void * _buffer, int _offset, int _len ) = 0;
    191 	virtual bool			IsBinaryModel( const idStr & resName ) const = 0;
    192 	virtual bool			IsSoundSample( const idStr & resName ) const = 0;
    193 	virtual bool			GetResourceCacheEntry( const char *fileName, idResourceCacheEntry &rc ) = 0;
    194 	virtual void			FreeResourceBuffer() = 0;
    195 	virtual void			AddImagePreload( const char *resName, int filter, int repeat, int usage, int cube ) = 0;
    196 	virtual void			AddSamplePreload( const char *resName ) = 0;
    197 	virtual void			AddModelPreload( const char *resName ) = 0;
    198 	virtual void			AddAnimPreload( const char *resName ) = 0;
    199 	virtual void			AddParticlePreload( const char *resName ) = 0;
    200 	virtual void			AddCollisionPreload( const char *resName ) = 0;
    201 
    202 };
    203 
    204 extern idFileSystem *		fileSystem;
    205 
    206 #endif /* !__FILESYSTEM_H__ */