FileSystem.h 10 KB

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